/ Check-in [2cc414cd]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge the latest trunk changes into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 2cc414cd144cd84aad6420058c6e674c2920ba72
User & Date: drh 2012-01-14 14:13:31
Context
2012-02-10
18:18
Pull the latest trunk changes into the apple-osx branch. check-in: e2485986 user: drh tags: apple-osx
2012-01-14
14:13
Merge the latest trunk changes into the apple-osx branch. check-in: 2cc414cd user: drh tags: apple-osx
03:34
Make sure the EXTERN macro is properly defined when building targets that require it (e.g. on MinGW). check-in: 88ad2f23 user: mistachkin tags: trunk
2012-01-03
21:54
Pull all the latest trunk changes over into the apple-osx branch. check-in: 8a048423 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

    27     27   # same unless your are cross-compiling.)
    28     28   #
    29     29   TCC = @CC@ @CPPFLAGS@ @CFLAGS@ -I. -I${TOP}/src -I${TOP}/ext/rtree
    30     30   
    31     31   # Define this for the autoconf-based build, so that the code knows it can
    32     32   # include the generated config.h
    33     33   # 
    34         -TCC += -D_HAVE_SQLITE_CONFIG_H
           34  +TCC += -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite
    35     35   
    36     36   # Define -DNDEBUG to compile without debugging (i.e., for production usage)
    37     37   # Omitting the define will cause extra debugging code to be inserted and
    38     38   # includes extra comments when "EXPLAIN stmt" is used.
    39     39   #
    40     40   TCC += @TARGET_DEBUG@ @XTHREADCONNECT@
    41     41   

Changes to ext/fts3/fts3_porter.c.

    36     36   ** Class derived from sqlite3_tokenizer
    37     37   */
    38     38   typedef struct porter_tokenizer {
    39     39     sqlite3_tokenizer base;      /* Base class */
    40     40   } porter_tokenizer;
    41     41   
    42     42   /*
    43         -** Class derived from sqlit3_tokenizer_cursor
           43  +** Class derived from sqlite3_tokenizer_cursor
    44     44   */
    45     45   typedef struct porter_tokenizer_cursor {
    46     46     sqlite3_tokenizer_cursor base;
    47     47     const char *zInput;          /* input we are tokenizing */
    48     48     int nInput;                  /* size of the input */
    49     49     int iOffset;                 /* current position in zInput */
    50     50     int iToken;                  /* index of next token to be returned */

Changes to main.mk.

   329    329      $(TOP)/src/sqliteLimit.h \
   330    330      $(TOP)/src/vdbe.h \
   331    331      $(TOP)/src/vdbeInt.h
   332    332   
   333    333   # Header files used by extensions
   334    334   #
   335    335   EXTHDR += \
   336         -  $(TOP)/ext/fts1/fts1.h \
   337         -  $(TOP)/ext/fts1/fts1_hash.h \
   338         -  $(TOP)/ext/fts1/fts1_tokenizer.h
   339         -EXTHDR += \
   340         -  $(TOP)/ext/fts2/fts2.h \
   341         -  $(TOP)/ext/fts2/fts2_hash.h \
   342         -  $(TOP)/ext/fts2/fts2_tokenizer.h
   343         -EXTHDR += \
   344         -  $(TOP)/ext/fts3/fts3.h \
   345         -  $(TOP)/ext/fts3/fts3Int.h \
   346         -  $(TOP)/ext/fts3/fts3_hash.h \
   347         -  $(TOP)/ext/fts3/fts3_tokenizer.h
   348         -EXTHDR += \
   349         -  $(TOP)/ext/rtree/rtree.h
   350         -EXTHDR += \
   351         -  $(TOP)/ext/icu/sqliteicu.h
   352         -EXTHDR += \
   353    336     $(TOP)/ext/sqlrr/sqlrr.h
   354    337   
   355    338   # This is the default Makefile target.  The objects listed here
   356    339   # are what get build when you type just "make" with no arguments.
   357    340   #
   358    341   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
   359    342   

Changes to src/backup.c.

   674    674     sqlite3BtreeEnter(pTo);
   675    675     sqlite3BtreeEnter(pFrom);
   676    676   
   677    677     assert( sqlite3BtreeIsInTrans(pTo) );
   678    678     pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
   679    679     if( pFd->pMethods ){
   680    680       i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
   681         -    sqlite3BeginBenignMalloc();
   682         -    sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
   683         -    sqlite3EndBenignMalloc();
          681  +    rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
          682  +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
          683  +    if( rc ) goto copy_finished;
   684    684     }
   685    685   
   686    686     /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
   687    687     ** to 0. This is used by the implementations of sqlite3_backup_step()
   688    688     ** and sqlite3_backup_finish() to detect that they are being called
   689    689     ** from this function, not directly by the user.
   690    690     */
................................................................................
   701    701     ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
   702    702     ** or an error code.
   703    703     */
   704    704     sqlite3_backup_step(&b, 0x7FFFFFFF);
   705    705     assert( b.rc!=SQLITE_OK );
   706    706     rc = sqlite3_backup_finish(&b);
   707    707     if( rc==SQLITE_OK ){
   708         -    pTo->pBt->pageSizeFixed = 0;
          708  +    pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
   709    709     }else{
   710    710       sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
   711    711     }
   712    712   
   713    713     assert( sqlite3BtreeIsInTrans(pTo)==0 );
          714  +copy_finished:
   714    715     sqlite3BtreeLeave(pFrom);
   715    716     sqlite3BtreeLeave(pTo);
   716    717     return rc;
   717    718   }
   718    719   #endif /* SQLITE_OMIT_VACUUM */

Changes to src/btree.c.

   245    245     if( !p->sharable ){
   246    246       return SQLITE_OK;
   247    247     }
   248    248   
   249    249     /* If some other connection is holding an exclusive lock, the
   250    250     ** requested lock may not be obtained.
   251    251     */
   252         -  if( pBt->pWriter!=p && pBt->isExclusive ){
          252  +  if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
   253    253       sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
   254    254       return SQLITE_LOCKED_SHAREDCACHE;
   255    255     }
   256    256   
   257    257     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
   258    258       /* The condition (pIter->eLock!=eLock) in the following if(...) 
   259    259       ** statement is a simplification of:
................................................................................
   266    266       */
   267    267       assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
   268    268       assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
   269    269       if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
   270    270         sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
   271    271         if( eLock==WRITE_LOCK ){
   272    272           assert( p==pBt->pWriter );
   273         -        pBt->isPending = 1;
          273  +        pBt->btsFlags |= BTS_PENDING;
   274    274         }
   275    275         return SQLITE_LOCKED_SHAREDCACHE;
   276    276       }
   277    277     }
   278    278     return SQLITE_OK;
   279    279   }
   280    280   #endif /* !SQLITE_OMIT_SHARED_CACHE */
................................................................................
   354    354   
   355    355   #ifndef SQLITE_OMIT_SHARED_CACHE
   356    356   /*
   357    357   ** Release all the table locks (locks obtained via calls to
   358    358   ** the setSharedCacheTableLock() procedure) held by Btree object p.
   359    359   **
   360    360   ** This function assumes that Btree p has an open read or write 
   361         -** transaction. If it does not, then the BtShared.isPending variable
          361  +** transaction. If it does not, then the BTS_PENDING flag
   362    362   ** may be incorrectly cleared.
   363    363   */
   364    364   static void clearAllSharedCacheTableLocks(Btree *p){
   365    365     BtShared *pBt = p->pBt;
   366    366     BtLock **ppIter = &pBt->pLock;
   367    367   
   368    368     assert( sqlite3BtreeHoldsMutex(p) );
   369    369     assert( p->sharable || 0==*ppIter );
   370    370     assert( p->inTrans>0 );
   371    371   
   372    372     while( *ppIter ){
   373    373       BtLock *pLock = *ppIter;
   374         -    assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
          374  +    assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
   375    375       assert( pLock->pBtree->inTrans>=pLock->eLock );
   376    376       if( pLock->pBtree==p ){
   377    377         *ppIter = pLock->pNext;
   378    378         assert( pLock->iTable!=1 || pLock==&p->lock );
   379    379         if( pLock->iTable!=1 ){
   380    380           sqlite3_free(pLock);
   381    381         }
   382    382       }else{
   383    383         ppIter = &pLock->pNext;
   384    384       }
   385    385     }
   386    386   
   387         -  assert( pBt->isPending==0 || pBt->pWriter );
          387  +  assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
   388    388     if( pBt->pWriter==p ){
   389    389       pBt->pWriter = 0;
   390         -    pBt->isExclusive = 0;
   391         -    pBt->isPending = 0;
          390  +    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
   392    391     }else if( pBt->nTransaction==2 ){
   393    392       /* This function is called when Btree p is concluding its 
   394    393       ** transaction. If there currently exists a writer, and p is not
   395    394       ** that writer, then the number of locks held by connections other
   396    395       ** than the writer must be about to drop to zero. In this case
   397         -    ** set the isPending flag to 0.
          396  +    ** set the BTS_PENDING flag to 0.
   398    397       **
   399         -    ** If there is not currently a writer, then BtShared.isPending must
          398  +    ** If there is not currently a writer, then BTS_PENDING must
   400    399       ** be zero already. So this next line is harmless in that case.
   401    400       */
   402         -    pBt->isPending = 0;
          401  +    pBt->btsFlags &= ~BTS_PENDING;
   403    402     }
   404    403   }
   405    404   
   406    405   /*
   407    406   ** This function changes all write-locks held by Btree p into read-locks.
   408    407   */
   409    408   static void downgradeAllSharedCacheTableLocks(Btree *p){
   410    409     BtShared *pBt = p->pBt;
   411    410     if( pBt->pWriter==p ){
   412    411       BtLock *pLock;
   413    412       pBt->pWriter = 0;
   414         -    pBt->isExclusive = 0;
   415         -    pBt->isPending = 0;
          413  +    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
   416    414       for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
   417    415         assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
   418    416         pLock->eLock = READ_LOCK;
   419    417       }
   420    418     }
   421    419   }
   422    420   
................................................................................
  1266   1264     assert( pPage->pBt!=0 );
  1267   1265     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1268   1266     assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
  1269   1267     assert( (start + size) <= (int)pPage->pBt->usableSize );
  1270   1268     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1271   1269     assert( size>=0 );   /* Minimum cell size is 4 */
  1272   1270   
  1273         -  if( pPage->pBt->secureDelete ){
         1271  +  if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
  1274   1272       /* Overwrite deleted information with zeros when the secure_delete
  1275   1273       ** option is enabled */
  1276   1274       memset(&data[start], 0, size);
  1277   1275     }
  1278   1276   
  1279   1277     /* Add the space back into the linked list of freeblocks.  Note that
  1280   1278     ** even though the freeblock list was checked by btreeInitPage(),
................................................................................
  1369   1367       pPage->intKey = 0;
  1370   1368       pPage->hasData = 0;
  1371   1369       pPage->maxLocal = pBt->maxLocal;
  1372   1370       pPage->minLocal = pBt->minLocal;
  1373   1371     }else{
  1374   1372       return SQLITE_CORRUPT_BKPT;
  1375   1373     }
         1374  +  pPage->max1bytePayload = pBt->max1bytePayload;
  1376   1375     return SQLITE_OK;
  1377   1376   }
  1378   1377   
  1379   1378   /*
  1380   1379   ** Initialize the auxiliary information for a disk block.
  1381   1380   **
  1382   1381   ** Return SQLITE_OK on success.  If we see that the page does
................................................................................
  1504   1503     u16 first;
  1505   1504   
  1506   1505     assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
  1507   1506     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  1508   1507     assert( sqlite3PagerGetData(pPage->pDbPage) == data );
  1509   1508     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1510   1509     assert( sqlite3_mutex_held(pBt->mutex) );
  1511         -  if( pBt->secureDelete ){
         1510  +  if( pBt->btsFlags & BTS_SECURE_DELETE ){
  1512   1511       memset(&data[hdr], 0, pBt->usableSize - hdr);
  1513   1512     }
  1514   1513     data[hdr] = (char)flags;
  1515   1514     first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
  1516   1515     memset(&data[hdr+1], 0, 4);
  1517   1516     data[hdr+7] = 0;
  1518   1517     put2byte(&data[hdr+5], pBt->usableSize);
................................................................................
  1857   1856       pBt->openFlags = (u8)flags;
  1858   1857       pBt->db = db;
  1859   1858       sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
  1860   1859       p->pBt = pBt;
  1861   1860     
  1862   1861       pBt->pCursor = 0;
  1863   1862       pBt->pPage1 = 0;
  1864         -    pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
         1863  +    if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
  1865   1864   #ifdef SQLITE_SECURE_DELETE
  1866         -    pBt->secureDelete = 1;
         1865  +    pBt->btsFlags |= BTS_SECURE_DELETE;
  1867   1866   #endif
  1868   1867       pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
  1869   1868       if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
  1870   1869            || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
  1871   1870         pBt->pageSize = 0;
  1872   1871   #ifndef SQLITE_OMIT_AUTOVACUUM
  1873   1872         /* If the magic name ":memory:" will create an in-memory database, then
................................................................................
  1880   1879           pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
  1881   1880           pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
  1882   1881         }
  1883   1882   #endif
  1884   1883         nReserve = 0;
  1885   1884       }else{
  1886   1885         nReserve = zDbHeader[20];
  1887         -      pBt->pageSizeFixed = 1;
         1886  +      pBt->btsFlags |= BTS_PAGESIZE_FIXED;
  1888   1887   #ifndef SQLITE_OMIT_AUTOVACUUM
  1889   1888         pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
  1890   1889         pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
  1891   1890   #endif
  1892   1891       }
  1893   1892       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
  1894   1893       if( rc ) goto btree_open_out;
................................................................................
  2168   2167   ** of the database file used for locking (beginning at PENDING_BYTE,
  2169   2168   ** the first byte past the 1GB boundary, 0x40000000) needs to occur
  2170   2169   ** at the beginning of a page.
  2171   2170   **
  2172   2171   ** If parameter nReserve is less than zero, then the number of reserved
  2173   2172   ** bytes per page is left unchanged.
  2174   2173   **
  2175         -** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
         2174  +** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
  2176   2175   ** and autovacuum mode can no longer be changed.
  2177   2176   */
  2178   2177   int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
  2179   2178     int rc = SQLITE_OK;
  2180   2179     BtShared *pBt = p->pBt;
  2181   2180     assert( nReserve>=-1 && nReserve<=255 );
  2182   2181     sqlite3BtreeEnter(p);
  2183         -  if( pBt->pageSizeFixed ){
         2182  +  if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
  2184   2183       sqlite3BtreeLeave(p);
  2185   2184       return SQLITE_READONLY;
  2186   2185     }
  2187   2186     if( nReserve<0 ){
  2188   2187       nReserve = pBt->pageSize - pBt->usableSize;
  2189   2188     }
  2190   2189     assert( nReserve>=0 && nReserve<=255 );
................................................................................
  2193   2192       assert( (pageSize & 7)==0 );
  2194   2193       assert( !pBt->pPage1 && !pBt->pCursor );
  2195   2194       pBt->pageSize = (u32)pageSize;
  2196   2195       freeTempSpace(pBt);
  2197   2196     }
  2198   2197     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
  2199   2198     pBt->usableSize = pBt->pageSize - (u16)nReserve;
  2200         -  if( iFix ) pBt->pageSizeFixed = 1;
         2199  +  if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
  2201   2200     sqlite3BtreeLeave(p);
  2202   2201     return rc;
  2203   2202   }
  2204   2203   
  2205   2204   /*
  2206   2205   ** Return the currently defined page size
  2207   2206   */
................................................................................
  2233   2232     sqlite3BtreeEnter(p);
  2234   2233     n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
  2235   2234     sqlite3BtreeLeave(p);
  2236   2235     return n;
  2237   2236   }
  2238   2237   
  2239   2238   /*
  2240         -** Set the secureDelete flag if newFlag is 0 or 1.  If newFlag is -1,
  2241         -** then make no changes.  Always return the value of the secureDelete
         2239  +** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
         2240  +** then make no changes.  Always return the value of the BTS_SECURE_DELETE
  2242   2241   ** setting after the change.
  2243   2242   */
  2244   2243   int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
  2245   2244     int b;
  2246   2245     if( p==0 ) return 0;
  2247   2246     sqlite3BtreeEnter(p);
  2248   2247     if( newFlag>=0 ){
  2249         -    p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
         2248  +    p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
         2249  +    if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
  2250   2250     } 
  2251         -  b = p->pBt->secureDelete;
         2251  +  b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
  2252   2252     sqlite3BtreeLeave(p);
  2253   2253     return b;
  2254   2254   }
  2255   2255   #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
  2256   2256   
  2257   2257   /*
  2258   2258   ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
................................................................................
  2265   2265     return SQLITE_READONLY;
  2266   2266   #else
  2267   2267     BtShared *pBt = p->pBt;
  2268   2268     int rc = SQLITE_OK;
  2269   2269     u8 av = (u8)autoVacuum;
  2270   2270   
  2271   2271     sqlite3BtreeEnter(p);
  2272         -  if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
         2272  +  if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
  2273   2273       rc = SQLITE_READONLY;
  2274   2274     }else{
  2275   2275       pBt->autoVacuum = av ?1:0;
  2276   2276       pBt->incrVacuum = av==2 ?1:0;
  2277   2277     }
  2278   2278     sqlite3BtreeLeave(p);
  2279   2279     return rc;
................................................................................
  2339   2339       rc = SQLITE_NOTADB;
  2340   2340       if( memcmp(page1, zMagicHeader, 16)!=0 ){
  2341   2341         goto page1_init_failed;
  2342   2342       }
  2343   2343   
  2344   2344   #ifdef SQLITE_OMIT_WAL
  2345   2345       if( page1[18]>1 ){
  2346         -      pBt->readOnly = 1;
         2346  +      pBt->btsFlags |= BTS_READ_ONLY;
  2347   2347       }
  2348   2348       if( page1[19]>1 ){
  2349   2349         goto page1_init_failed;
  2350   2350       }
  2351   2351   #else
  2352   2352       if( page1[18]>2 ){
  2353         -      pBt->readOnly = 1;
         2353  +      pBt->btsFlags |= BTS_READ_ONLY;
  2354   2354       }
  2355   2355       if( page1[19]>2 ){
  2356   2356         goto page1_init_failed;
  2357   2357       }
  2358   2358   
  2359   2359       /* If the write version is set to 2, this database should be accessed
  2360   2360       ** in WAL mode. If the log is not already open, open it now. Then 
  2361   2361       ** return SQLITE_OK and return without populating BtShared.pPage1.
  2362   2362       ** The caller detects this and calls this function again. This is
  2363   2363       ** required as the version of page 1 currently in the page1 buffer
  2364   2364       ** may not be the latest version - there may be a newer one in the log
  2365   2365       ** file.
  2366   2366       */
  2367         -    if( page1[19]==2 && pBt->doNotUseWAL==0 ){
         2367  +    if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
  2368   2368         int isOpen = 0;
  2369   2369         rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
  2370   2370         if( rc!=SQLITE_OK ){
  2371   2371           goto page1_init_failed;
  2372   2372         }else if( isOpen==0 ){
  2373   2373   #ifdef SQLITE_DEFAULT_WAL_SAFETYLEVEL
  2374   2374           /* Default to specified safety_level for WAL mode */
................................................................................
  2458   2458     ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
  2459   2459     ** page pointer.
  2460   2460     */
  2461   2461     pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
  2462   2462     pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
  2463   2463     pBt->maxLeaf = (u16)(pBt->usableSize - 35);
  2464   2464     pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
         2465  +  if( pBt->maxLocal>127 ){
         2466  +    pBt->max1bytePayload = 127;
         2467  +  }else{
         2468  +    pBt->max1bytePayload = (u8)pBt->maxLocal;
         2469  +  }
  2465   2470     assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
  2466   2471     pBt->pPage1 = pPage1;
  2467   2472     pBt->nPage = nPage;
  2468   2473     return SQLITE_OK;
  2469   2474   
  2470   2475   page1_init_failed:
  2471   2476     releasePage(pPage1);
................................................................................
  2521   2526     assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
  2522   2527     data[20] = (u8)(pBt->pageSize - pBt->usableSize);
  2523   2528     data[21] = 64;
  2524   2529     data[22] = 32;
  2525   2530     data[23] = 32;
  2526   2531     memset(&data[24], 0, 100-24);
  2527   2532     zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
  2528         -  pBt->pageSizeFixed = 1;
         2533  +  pBt->btsFlags |= BTS_PAGESIZE_FIXED;
  2529   2534   #ifndef SQLITE_OMIT_AUTOVACUUM
  2530   2535     assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
  2531   2536     assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
  2532   2537     put4byte(&data[36 + 4*4], pBt->autoVacuum);
  2533   2538     put4byte(&data[36 + 7*4], pBt->incrVacuum);
  2534   2539   #endif
  2535   2540     pBt->nPage = 1;
................................................................................
  2585   2590     ** is requested, this is a no-op.
  2586   2591     */
  2587   2592     if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
  2588   2593       goto trans_begun;
  2589   2594     }
  2590   2595   
  2591   2596     /* Write transactions are not possible on a read-only database */
  2592         -  if( pBt->readOnly && wrflag ){
         2597  +  if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
  2593   2598       rc = SQLITE_READONLY;
  2594   2599       goto trans_begun;
  2595   2600     }
  2596   2601   
  2597   2602   #ifndef SQLITE_OMIT_SHARED_CACHE
  2598   2603     /* If another database handle has already opened a write transaction 
  2599   2604     ** on this shared-btree structure and a second write transaction is
  2600   2605     ** requested, return SQLITE_LOCKED.
  2601   2606     */
  2602         -  if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
         2607  +  if( (wrflag && pBt->inTransaction==TRANS_WRITE)
         2608  +   || (pBt->btsFlags & BTS_PENDING)!=0
         2609  +  ){
  2603   2610       pBlock = pBt->pWriter->db;
  2604   2611     }else if( wrflag>1 ){
  2605   2612       BtLock *pIter;
  2606   2613       for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
  2607   2614         if( pIter->pBtree!=p ){
  2608   2615           pBlock = pIter->pBtree->db;
  2609   2616           break;
................................................................................
  2619   2626   
  2620   2627     /* Any read-only or read-write transaction implies a read-lock on 
  2621   2628     ** page 1. So if some other shared-cache client already has a write-lock 
  2622   2629     ** on page 1, the transaction cannot be opened. */
  2623   2630     rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
  2624   2631     if( SQLITE_OK!=rc ) goto trans_begun;
  2625   2632   
  2626         -  pBt->initiallyEmpty = (u8)(pBt->nPage==0);
         2633  +  pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
         2634  +  if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
  2627   2635     do {
  2628   2636       /* Call lockBtree() until either pBt->pPage1 is populated or
  2629   2637       ** lockBtree() returns something other than SQLITE_OK. lockBtree()
  2630   2638       ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
  2631   2639       ** reading page 1 it discovers that the page-size of the database 
  2632   2640       ** file is not pBt->pageSize. In this case lockBtree() will update
  2633   2641       ** pBt->pageSize to the page-size of the file on disk.
  2634   2642       */
  2635   2643       while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
  2636   2644   
  2637   2645       if( rc==SQLITE_OK && wrflag ){
  2638         -      if( pBt->readOnly ){
         2646  +      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
  2639   2647           rc = SQLITE_READONLY;
  2640   2648         }else{
  2641   2649           rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
  2642   2650           if( rc==SQLITE_OK ){
  2643   2651             rc = newDatabase(pBt);
  2644   2652           }
  2645   2653         }
................................................................................
  2668   2676         pBt->inTransaction = p->inTrans;
  2669   2677       }
  2670   2678       if( wrflag ){
  2671   2679         MemPage *pPage1 = pBt->pPage1;
  2672   2680   #ifndef SQLITE_OMIT_SHARED_CACHE
  2673   2681         assert( !pBt->pWriter );
  2674   2682         pBt->pWriter = p;
  2675         -      pBt->isExclusive = (u8)(wrflag>1);
         2683  +      pBt->btsFlags &= ~BTS_EXCLUSIVE;
         2684  +      if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
  2676   2685   #endif
  2677   2686   
  2678   2687         /* If the db-size header field is incorrect (as it may be if an old
  2679   2688         ** client has been writing the database file), update it now. Doing
  2680   2689         ** this sooner rather than later means the database size can safely 
  2681   2690         ** re-read the database size from page 1 if a savepoint or transaction
  2682   2691         ** rollback occurs within the transaction.
................................................................................
  3397   3406   ** using the sqlite3BtreeSavepoint() function.
  3398   3407   */
  3399   3408   int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
  3400   3409     int rc;
  3401   3410     BtShared *pBt = p->pBt;
  3402   3411     sqlite3BtreeEnter(p);
  3403   3412     assert( p->inTrans==TRANS_WRITE );
  3404         -  assert( pBt->readOnly==0 );
         3413  +  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  3405   3414     assert( iStatement>0 );
  3406   3415     assert( iStatement>p->db->nSavepoint );
  3407   3416     assert( pBt->inTransaction==TRANS_WRITE );
  3408   3417     /* At the pager level, a statement transaction is a savepoint with
  3409   3418     ** an index greater than all savepoints created explicitly using
  3410   3419     ** SQL statements. It is illegal to open, release or rollback any
  3411   3420     ** such savepoints while the statement transaction savepoint is active.
................................................................................
  3432   3441     if( p && p->inTrans==TRANS_WRITE ){
  3433   3442       BtShared *pBt = p->pBt;
  3434   3443       assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
  3435   3444       assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
  3436   3445       sqlite3BtreeEnter(p);
  3437   3446       rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
  3438   3447       if( rc==SQLITE_OK ){
  3439         -      if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
         3448  +      if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
         3449  +        pBt->nPage = 0;
         3450  +      }
  3440   3451         rc = newDatabase(pBt);
  3441   3452         pBt->nPage = get4byte(28 + pBt->pPage1->aData);
  3442   3453   
  3443   3454         /* The database size was written into the offset 28 of the header
  3444   3455         ** when the transaction started, so we know that the value at offset
  3445   3456         ** 28 is nonzero. */
  3446   3457         assert( pBt->nPage>0 );
................................................................................
  3502   3513     assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
  3503   3514   
  3504   3515     /* Assert that the caller has opened the required transaction. */
  3505   3516     assert( p->inTrans>TRANS_NONE );
  3506   3517     assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
  3507   3518     assert( pBt->pPage1 && pBt->pPage1->aData );
  3508   3519   
  3509         -  if( NEVER(wrFlag && pBt->readOnly) ){
         3520  +  if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
  3510   3521       return SQLITE_READONLY;
  3511   3522     }
  3512   3523     if( iTable==1 && btreePagecount(pBt)==0 ){
  3513   3524       assert( wrFlag==0 );
  3514   3525       iTable = 0;
  3515   3526     }
  3516   3527   
................................................................................
  4206   4217     pCur->validNKey = 0;
  4207   4218     if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
  4208   4219       return SQLITE_CORRUPT_BKPT;
  4209   4220     }
  4210   4221     return SQLITE_OK;
  4211   4222   }
  4212   4223   
  4213         -#ifndef NDEBUG
         4224  +#if 0
  4214   4225   /*
  4215   4226   ** Page pParent is an internal (non-leaf) tree page. This function 
  4216   4227   ** asserts that page number iChild is the left-child if the iIdx'th
  4217   4228   ** cell in page pParent. Or, if iIdx is equal to the total number of
  4218   4229   ** cells in pParent, that page number iChild is the right-child of
  4219   4230   ** the page.
  4220   4231   */
................................................................................
  4239   4250   ** the largest cell index.
  4240   4251   */
  4241   4252   static void moveToParent(BtCursor *pCur){
  4242   4253     assert( cursorHoldsMutex(pCur) );
  4243   4254     assert( pCur->eState==CURSOR_VALID );
  4244   4255     assert( pCur->iPage>0 );
  4245   4256     assert( pCur->apPage[pCur->iPage] );
         4257  +
         4258  +  /* UPDATE: It is actually possible for the condition tested by the assert
         4259  +  ** below to be untrue if the database file is corrupt. This can occur if
         4260  +  ** one cursor has modified page pParent while a reference to it is held 
         4261  +  ** by a second cursor. Which can only happen if a single page is linked
         4262  +  ** into more than one b-tree structure in a corrupt database.  */
         4263  +#if 0
  4246   4264     assertParentIndex(
  4247   4265       pCur->apPage[pCur->iPage-1], 
  4248   4266       pCur->aiIdx[pCur->iPage-1], 
  4249   4267       pCur->apPage[pCur->iPage]->pgno
  4250   4268     );
         4269  +#endif
         4270  +  testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
         4271  +
  4251   4272     releasePage(pCur->apPage[pCur->iPage]);
  4252   4273     pCur->iPage--;
  4253   4274     pCur->info.nSize = 0;
  4254   4275     pCur->validNKey = 0;
  4255   4276   }
  4256   4277   
  4257   4278   /*
................................................................................
  4582   4603           ** page is less than 16384 bytes and may be stored as a 2-byte
  4583   4604           ** varint. This information is used to attempt to avoid parsing 
  4584   4605           ** the entire cell by checking for the cases where the record is 
  4585   4606           ** stored entirely within the b-tree page by inspecting the first 
  4586   4607           ** 2 bytes of the cell.
  4587   4608           */
  4588   4609           int nCell = pCell[0];
  4589         -        if( !(nCell & 0x80)
  4590         -         && nCell<=pPage->maxLocal
  4591         -         && (pCell+nCell+1)<=pPage->aDataEnd
         4610  +        if( nCell<=pPage->max1bytePayload
         4611  +         /* && (pCell+nCell)<pPage->aDataEnd */
  4592   4612           ){
  4593   4613             /* This branch runs if the record-size field of the cell is a
  4594   4614             ** single byte varint and the record fits entirely on the main
  4595   4615             ** b-tree page.  */
  4596   4616             testcase( pCell+nCell+1==pPage->aDataEnd );
  4597   4617             c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
  4598   4618           }else if( !(pCell[1] & 0x80) 
  4599   4619             && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
  4600         -          && (pCell+nCell+2)<=pPage->aDataEnd
         4620  +          /* && (pCell+nCell+2)<=pPage->aDataEnd */
  4601   4621           ){
  4602   4622             /* The record-size field is a 2 byte varint and the record 
  4603   4623             ** fits entirely on the main b-tree page.  */
  4604   4624             testcase( pCell+nCell+2==pPage->aDataEnd );
  4605   4625             c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
  4606   4626           }else{
  4607   4627             /* The record flows over onto one or more overflow pages. In
................................................................................
  4714   4734       return SQLITE_OK;
  4715   4735     }
  4716   4736     pCur->skipNext = 0;
  4717   4737   
  4718   4738     pPage = pCur->apPage[pCur->iPage];
  4719   4739     idx = ++pCur->aiIdx[pCur->iPage];
  4720   4740     assert( pPage->isInit );
  4721         -  assert( idx<=pPage->nCell );
         4741  +
         4742  +  /* If the database file is corrupt, it is possible for the value of idx 
         4743  +  ** to be invalid here. This can only occur if a second cursor modifies
         4744  +  ** the page while cursor pCur is holding a reference to it. Which can
         4745  +  ** only happen if the database is corrupt in such a way as to link the
         4746  +  ** page into more than one b-tree structure. */
         4747  +  testcase( idx>pPage->nCell );
  4722   4748   
  4723   4749     pCur->info.nSize = 0;
  4724   4750     pCur->validNKey = 0;
  4725   4751     if( idx>=pPage->nCell ){
  4726   4752       if( !pPage->leaf ){
  4727   4753         rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
  4728   4754         if( rc ) return rc;
................................................................................
  5139   5165   
  5140   5166     /* Increment the free page count on pPage1 */
  5141   5167     rc = sqlite3PagerWrite(pPage1->pDbPage);
  5142   5168     if( rc ) goto freepage_out;
  5143   5169     nFree = get4byte(&pPage1->aData[36]);
  5144   5170     put4byte(&pPage1->aData[36], nFree+1);
  5145   5171   
  5146         -  if( pBt->secureDelete ){
         5172  +  if( pBt->btsFlags & BTS_SECURE_DELETE ){
  5147   5173       /* If the secure_delete option is enabled, then
  5148   5174       ** always fully overwrite deleted information with zeros.
  5149   5175       */
  5150   5176       if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
  5151   5177        ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
  5152   5178       ){
  5153   5179         goto freepage_out;
................................................................................
  5200   5226         ** to 3.6.0 or later) we should consider fixing the conditional above
  5201   5227         ** to read "usableSize/4-2" instead of "usableSize/4-8".
  5202   5228         */
  5203   5229         rc = sqlite3PagerWrite(pTrunk->pDbPage);
  5204   5230         if( rc==SQLITE_OK ){
  5205   5231           put4byte(&pTrunk->aData[4], nLeaf+1);
  5206   5232           put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
  5207         -        if( pPage && !pBt->secureDelete ){
         5233  +        if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
  5208   5234             sqlite3PagerDontWrite(pPage->pDbPage);
  5209   5235           }
  5210   5236           rc = btreeSetHasContent(pBt, iPage);
  5211   5237         }
  5212   5238         TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
  5213   5239         goto freepage_out;
  5214   5240       }
................................................................................
  6041   6067         ** later on.  
  6042   6068         **
  6043   6069         ** But not if we are in secure-delete mode. In secure-delete mode,
  6044   6070         ** the dropCell() routine will overwrite the entire cell with zeroes.
  6045   6071         ** In this case, temporarily copy the cell into the aOvflSpace[]
  6046   6072         ** buffer. It will be copied out again as soon as the aSpace[] buffer
  6047   6073         ** is allocated.  */
  6048         -      if( pBt->secureDelete ){
         6074  +      if( pBt->btsFlags & BTS_SECURE_DELETE ){
  6049   6075           int iOff;
  6050   6076   
  6051   6077           iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
  6052   6078           if( (iOff+szNew[i])>(int)pBt->usableSize ){
  6053   6079             rc = SQLITE_CORRUPT_BKPT;
  6054   6080             memset(apOld, 0, (i+1)*sizeof(MemPage*));
  6055   6081             goto balance_cleanup;
................................................................................
  6783   6809   
  6784   6810     if( pCur->eState==CURSOR_FAULT ){
  6785   6811       assert( pCur->skipNext!=SQLITE_OK );
  6786   6812       return pCur->skipNext;
  6787   6813     }
  6788   6814   
  6789   6815     assert( cursorHoldsMutex(pCur) );
  6790         -  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
         6816  +  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
         6817  +              && (pBt->btsFlags & BTS_READ_ONLY)==0 );
  6791   6818     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  6792   6819   
  6793   6820     /* Assert that the caller has been consistent. If this cursor was opened
  6794   6821     ** expecting an index b-tree, then the caller should be inserting blob
  6795   6822     ** keys with no associated data. If the cursor was opened expecting an
  6796   6823     ** intkey table, the caller should be inserting integer keys with a
  6797   6824     ** blob of associated data.  */
................................................................................
  6912   6939     MemPage *pPage;                      /* Page to delete cell from */
  6913   6940     unsigned char *pCell;                /* Pointer to cell to delete */
  6914   6941     int iCellIdx;                        /* Index of cell to delete */
  6915   6942     int iCellDepth;                      /* Depth of node containing pCell */ 
  6916   6943   
  6917   6944     assert( cursorHoldsMutex(pCur) );
  6918   6945     assert( pBt->inTransaction==TRANS_WRITE );
  6919         -  assert( !pBt->readOnly );
         6946  +  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  6920   6947     assert( pCur->wrFlag );
  6921   6948     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  6922   6949     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
  6923   6950   
  6924   6951     if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
  6925   6952      || NEVER(pCur->eState!=CURSOR_VALID)
  6926   6953     ){
................................................................................
  7033   7060     MemPage *pRoot;
  7034   7061     Pgno pgnoRoot;
  7035   7062     int rc;
  7036   7063     int ptfFlags;          /* Page-type flage for the root page of new table */
  7037   7064   
  7038   7065     assert( sqlite3BtreeHoldsMutex(p) );
  7039   7066     assert( pBt->inTransaction==TRANS_WRITE );
  7040         -  assert( !pBt->readOnly );
         7067  +  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  7041   7068   
  7042   7069   #ifdef SQLITE_OMIT_AUTOVACUUM
  7043   7070     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
  7044   7071     if( rc ){
  7045   7072       return rc;
  7046   7073     }
  7047   7074   #else
................................................................................
  7407   7434     assert( idx>=0 && idx<=15 );
  7408   7435   
  7409   7436     *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
  7410   7437   
  7411   7438     /* If auto-vacuum is disabled in this build and this is an auto-vacuum
  7412   7439     ** database, mark the database as read-only.  */
  7413   7440   #ifdef SQLITE_OMIT_AUTOVACUUM
  7414         -  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
         7441  +  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
         7442  +    pBt->btsFlags |= BTS_READ_ONLY;
         7443  +  }
  7415   7444   #endif
  7416   7445   
  7417   7446     sqlite3BtreeLeave(p);
  7418   7447   }
  7419   7448   
  7420   7449   /*
  7421   7450   ** Write meta-information back into the database.  Meta[0] is
................................................................................
  8207   8236     **   (c) the connection holds a write-lock on the table (if required),
  8208   8237     **   (d) there are no conflicting read-locks, and
  8209   8238     **   (e) the cursor points at a valid row of an intKey table.
  8210   8239     */
  8211   8240     if( !pCsr->wrFlag ){
  8212   8241       return SQLITE_READONLY;
  8213   8242     }
  8214         -  assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
         8243  +  assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
         8244  +              && pCsr->pBt->inTransaction==TRANS_WRITE );
  8215   8245     assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
  8216   8246     assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
  8217   8247     assert( pCsr->apPage[pCsr->iPage]->intKey );
  8218   8248   
  8219   8249     return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
  8220   8250   }
  8221   8251   
................................................................................
  8247   8277     int rc;                         /* Return code */
  8248   8278    
  8249   8279     assert( iVersion==1 || iVersion==2 );
  8250   8280   
  8251   8281     /* If setting the version fields to 1, do not automatically open the
  8252   8282     ** WAL connection, even if the version fields are currently set to 2.
  8253   8283     */
  8254         -  pBt->doNotUseWAL = (u8)(iVersion==1);
         8284  +  pBt->btsFlags &= ~BTS_NO_WAL;
         8285  +  if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
  8255   8286   
  8256   8287     rc = sqlite3BtreeBeginTrans(pBtree, 0);
  8257   8288     if( rc==SQLITE_OK ){
  8258   8289       u8 *aData = pBt->pPage1->aData;
  8259   8290       if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
  8260   8291         rc = sqlite3BtreeBeginTrans(pBtree, 2);
  8261   8292         if( rc==SQLITE_OK ){
................................................................................
  8264   8295             aData[18] = (u8)iVersion;
  8265   8296             aData[19] = (u8)iVersion;
  8266   8297           }
  8267   8298         }
  8268   8299       }
  8269   8300     }
  8270   8301   
  8271         -  pBt->doNotUseWAL = 0;
         8302  +  pBt->btsFlags &= ~BTS_NO_WAL;
  8272   8303     return rc;
  8273   8304   }

Changes to src/btreeInt.h.

   273    273     u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
   274    274     u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
   275    275     u8 intKey;           /* True if intkey flag is set */
   276    276     u8 leaf;             /* True if leaf flag is set */
   277    277     u8 hasData;          /* True if this page stores data */
   278    278     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   279    279     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
          280  +  u8 max1bytePayload;  /* min(maxLocal,127) */
   280    281     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
   281    282     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   282    283     u16 cellOffset;      /* Index in aData of first cell pointer */
   283    284     u16 nFree;           /* Number of free bytes on the page */
   284    285     u16 nCell;           /* Number of cells on this page, local and ovfl */
   285    286     u16 maskPage;        /* Mask for page offset */
   286    287     struct _OvflCell {   /* Cells that will not fit on aData[] */
................................................................................
   403    404   **   This feature is included to help prevent writer-starvation.
   404    405   */
   405    406   struct BtShared {
   406    407     Pager *pPager;        /* The page cache */
   407    408     sqlite3 *db;          /* Database connection currently using this Btree */
   408    409     BtCursor *pCursor;    /* A list of all open cursors */
   409    410     MemPage *pPage1;      /* First page of the database */
   410         -  u8 readOnly;          /* True if the underlying file is readonly */
   411         -  u8 pageSizeFixed;     /* True if the page size can no longer be changed */
   412         -  u8 secureDelete;      /* True if secure_delete is enabled */
   413         -  u8 initiallyEmpty;    /* Database is empty at start of transaction */
   414    411     u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
   415    412   #ifndef SQLITE_OMIT_AUTOVACUUM
   416    413     u8 autoVacuum;        /* True if auto-vacuum is enabled */
   417    414     u8 incrVacuum;        /* True if incr-vacuum is enabled */
   418    415   #endif
   419    416     u8 inTransaction;     /* Transaction state */
   420         -  u8 doNotUseWAL;       /* If true, do not open write-ahead-log file */
          417  +  u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
          418  +  u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
   421    419     u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
   422    420     u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
   423    421     u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
   424    422     u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
   425    423     u32 pageSize;         /* Total number of bytes on a page */
   426    424     u32 usableSize;       /* Number of usable bytes on each page */
   427    425     int nTransaction;     /* Number of open transactions (read + write) */
................................................................................
   431    429     sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
   432    430     Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
   433    431   #ifndef SQLITE_OMIT_SHARED_CACHE
   434    432     int nRef;             /* Number of references to this structure */
   435    433     BtShared *pNext;      /* Next on a list of sharable BtShared structs */
   436    434     BtLock *pLock;        /* List of locks held on this shared-btree struct */
   437    435     Btree *pWriter;       /* Btree with currently open write transaction */
   438         -  u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
   439         -  u8 isPending;         /* If waiting for read-locks to clear */
   440    436   #endif
   441    437     u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
   442    438   };
   443    439   
          440  +/*
          441  +** Allowed values for BtShared.btsFlags
          442  +*/
          443  +#define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
          444  +#define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
          445  +#define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
          446  +#define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
          447  +#define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
          448  +#define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
          449  +#define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
          450  +
   444    451   /*
   445    452   ** An instance of the following structure is used to hold information
   446    453   ** about a cell.  The parseCellPtr() function fills in this structure
   447    454   ** based on information extract from the raw disk page.
   448    455   */
   449    456   typedef struct CellInfo CellInfo;
   450    457   struct CellInfo {

Changes to src/build.c.

  2657   2657   
  2658   2658     /* 
  2659   2659     ** Allocate the index structure. 
  2660   2660     */
  2661   2661     nName = sqlite3Strlen30(zName);
  2662   2662     nCol = pList->nExpr;
  2663   2663     pIndex = sqlite3DbMallocZero(db, 
  2664         -      sizeof(Index) +                      /* Index structure  */
         2664  +      ROUND8(sizeof(Index)) +              /* Index structure  */
  2665   2665         ROUND8(sizeof(tRowcnt)*(nCol+1)) +   /* Index.aiRowEst   */
  2666   2666         sizeof(char *)*nCol +                /* Index.azColl     */
  2667   2667         sizeof(int)*nCol +                   /* Index.aiColumn   */
  2668   2668         sizeof(u8)*nCol +                    /* Index.aSortOrder */
  2669   2669         nName + 1 +                          /* Index.zName      */
  2670   2670         nExtra                               /* Collation sequence names */
  2671   2671     );
  2672   2672     if( db->mallocFailed ){
  2673   2673       goto exit_create_index;
  2674   2674     }
  2675         -  pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
         2675  +  zExtra = (char*)pIndex;
         2676  +  pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
  2676   2677     pIndex->azColl = (char**)
  2677   2678        ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
  2678   2679     assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
  2679   2680     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
  2680   2681     pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
  2681   2682     pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
  2682   2683     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);

Changes to src/insert.c.

  1103   1103   **                                return code of SQLITE_CONSTRAINT.
  1104   1104   **
  1105   1105   **  any              ABORT        Back out changes from the current command
  1106   1106   **                                only (do not do a complete rollback) then
  1107   1107   **                                cause sqlite3_exec() to return immediately
  1108   1108   **                                with SQLITE_CONSTRAINT.
  1109   1109   **
  1110         -**  any              FAIL         Sqlite_exec() returns immediately with a
         1110  +**  any              FAIL         Sqlite3_exec() returns immediately with a
  1111   1111   **                                return code of SQLITE_CONSTRAINT.  The
  1112   1112   **                                transaction is not rolled back and any
  1113   1113   **                                prior changes are retained.
  1114   1114   **
  1115   1115   **  any              IGNORE       The record number and data is popped from
  1116   1116   **                                the stack and there is an immediate jump
  1117   1117   **                                to label ignoreDest.

Changes to src/mutex.c.

   146    146     return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
   147    147   }
   148    148   int sqlite3_mutex_notheld(sqlite3_mutex *p){
   149    149     return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
   150    150   }
   151    151   #endif
   152    152   
   153         -#endif /* SQLITE_MUTEX_OMIT */
          153  +#endif /* !defined(SQLITE_MUTEX_OMIT) */

Changes to src/mutex_noop.c.

   198    198   ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
   199    199   ** is used regardless of the run-time threadsafety setting.
   200    200   */
   201    201   #ifdef SQLITE_MUTEX_NOOP
   202    202   sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   203    203     return sqlite3NoopMutex();
   204    204   }
   205         -#endif /* SQLITE_MUTEX_NOOP */
   206         -#endif /* SQLITE_MUTEX_OMIT */
          205  +#endif /* defined(SQLITE_MUTEX_NOOP) */
          206  +#endif /* !defined(SQLITE_MUTEX_OMIT) */

Changes to src/mutex_unix.c.

   344    344       0
   345    345   #endif
   346    346     };
   347    347   
   348    348     return &sMutex;
   349    349   }
   350    350   
   351         -#endif /* SQLITE_MUTEX_PTHREAD */
          351  +#endif /* SQLITE_MUTEX_PTHREADS */

Changes to src/os.c.

    93     93   int sqlite3OsUnlock(sqlite3_file *id, int lockType){
    94     94     return id->pMethods->xUnlock(id, lockType);
    95     95   }
    96     96   int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
    97     97     DO_OS_MALLOC_TEST(id);
    98     98     return id->pMethods->xCheckReservedLock(id, pResOut);
    99     99   }
          100  +
          101  +/*
          102  +** Use sqlite3OsFileControl() when we are doing something that might fail
          103  +** and we need to know about the failures.  Use sqlite3OsFileControlHint()
          104  +** when simply tossing information over the wall to the VFS and we do not
          105  +** really care if the VFS receives and understands the information since it
          106  +** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
          107  +** routine has no return value since the return value would be meaningless.
          108  +*/
   100    109   int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
   101    110     DO_OS_MALLOC_TEST(id);
   102    111     return id->pMethods->xFileControl(id, op, pArg);
   103    112   }
          113  +void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
          114  +  (void)id->pMethods->xFileControl(id, op, pArg);
          115  +}
          116  +
   104    117   int sqlite3OsSectorSize(sqlite3_file *id){
   105    118     int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
   106    119     return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
   107    120   }
   108    121   int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
   109    122     return id->pMethods->xDeviceCharacteristics(id);
   110    123   }
................................................................................
   153    166   #endif
   154    167     rc = pVfs->xOpen(pVfs, zPath, pFile, openFlags, pFlagsOut);
   155    168     assert( rc==SQLITE_OK || pFile->pMethods==0 );
   156    169     return rc;
   157    170   }
   158    171   int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
   159    172     DO_OS_MALLOC_TEST(0);
          173  +  assert( dirSync==0 || dirSync==1 );
   160    174     return pVfs->xDelete(pVfs, zPath, dirSync);
   161    175   }
   162    176   int sqlite3OsAccess(
   163    177     sqlite3_vfs *pVfs, 
   164    178     const char *zPath, 
   165    179     int flags, 
   166    180     int *pResOut

Changes to src/os.h.

   248    248   int sqlite3OsTruncate(sqlite3_file*, i64 size);
   249    249   int sqlite3OsSync(sqlite3_file*, int);
   250    250   int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
   251    251   int sqlite3OsLock(sqlite3_file*, int);
   252    252   int sqlite3OsUnlock(sqlite3_file*, int);
   253    253   int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
   254    254   int sqlite3OsFileControl(sqlite3_file*,int,void*);
          255  +void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
   255    256   #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
   256    257   int sqlite3OsSectorSize(sqlite3_file *id);
   257    258   int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
   258    259   int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
   259    260   int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
   260    261   void sqlite3OsShmBarrier(sqlite3_file *id);
   261    262   int sqlite3OsShmUnmap(sqlite3_file *id, int);
          263  +
   262    264   
   263    265   /* 
   264    266   ** Functions for accessing sqlite3_vfs methods 
   265    267   */
   266    268   int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
   267    269   int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
   268    270   int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);

Changes to src/os_unix.c.

   232    232   #if SQLITE_ENABLE_DATA_PROTECTION
   233    233     int protFlags;                      /* Data protection flags from unixOpen */
   234    234   #endif
   235    235   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
   236    236     unsigned fsFlags;                   /* cached details from statfs() */
   237    237   #endif
   238    238   #if OS_VXWORKS
   239         -  int isDelete;                       /* Delete on close if true */
   240    239     struct vxworksFileId *pId;          /* Unique file ID */
   241    240   #endif
   242    241   #ifndef NDEBUG
   243    242     /* The next group of variables are used to track whether or not the
   244    243     ** transaction counter in bytes 24-27 of database files are updated
   245    244     ** whenever any part of the database changes.  An assertion fault will
   246    245     ** occur if a file is updated without also updating the transaction
................................................................................
   267    266   #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
   268    267   #ifndef SQLITE_DISABLE_DIRSYNC
   269    268   # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
   270    269   #else
   271    270   # define UNIXFILE_DIRSYNC    0x00
   272    271   #endif
   273    272   #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
          273  +#define UNIXFILE_DELETE      0x20     /* Delete on close */
          274  +#define UNIXFILE_URI         0x40     /* Filename might have query parameters */
          275  +#define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
   274    276   
   275    277   /*
   276    278   ** Include code that is common to all os_*.c files
   277    279   */
   278    280   #include "os_common.h"
   279    281   
   280    282   /*
................................................................................
  2122   2124     if( pFile->h>=0 ){
  2123   2125       robust_close(pFile, pFile->h, __LINE__);
  2124   2126       pFile->h = -1;
  2125   2127     }
  2126   2128   #endif
  2127   2129   #if OS_VXWORKS
  2128   2130     if( pFile->pId ){
  2129         -    if( pFile->isDelete ){
         2131  +    if( pFile->ctrlFlags & UNIXFILE_DELETE ){
  2130   2132         osUnlink(pFile->pId->zCanonicalName);
  2131   2133       }
  2132   2134       vxworksReleaseFileId(pFile->pId);
  2133   2135       pFile->pId = 0;
  2134   2136     }
  2135   2137   #endif
  2136   2138     OSTRACE(("CLOSE   %-3d\n", pFile->h));
................................................................................
  4372   4374       */
  4373   4375       case SQLITE_FCNTL_DB_UNCHANGED: {
  4374   4376         ((unixFile*)id)->dbUpdate = 0;
  4375   4377         return SQLITE_OK;
  4376   4378       }
  4377   4379   #endif
  4378   4380   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
  4379         -    case SQLITE_FCNTL_SET_LOCKPROXYFILE:
  4380         -    case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
         4381  +    case SQLITE_SET_LOCKPROXYFILE:
         4382  +    case SQLITE_GET_LOCKPROXYFILE: {
  4381   4383         return proxyFileControl(id,op,pArg);
  4382   4384       }
  4383   4385   #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
  4384   4386   #if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
  4385   4387       case SQLITE_FCNTL_TRUNCATE_DATABASE: {
  4386   4388         return unixTruncateDatabase(pFile, (pArg ? (*(int *)pArg) : 0));
  4387   4389       }
................................................................................
  4397   4399         }
  4398   4400         pLockstate = (LockstatePID *)pArg;
  4399   4401         rc = unixLockstatePid(pFile, pLockstate->pid, &(pLockstate->state));
  4400   4402         return rc;
  4401   4403       }
  4402   4404         
  4403   4405   #endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */
  4404         -    case SQLITE_FCNTL_SYNC_OMITTED: {
  4405         -      return SQLITE_OK;  /* A no-op */
  4406         -    }
  4407   4406     }
  4408   4407     return SQLITE_NOTFOUND;
  4409   4408   }
  4410   4409   
  4411   4410   /*
  4412   4411   ** Return the sector size in bytes of the underlying block device for
  4413   4412   ** the specified file. This is almost always 512 bytes, but may be
................................................................................
  4718   4717       nShmFilename = 6 + (int)strlen(zBasePath);
  4719   4718   #endif
  4720   4719       pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
  4721   4720       if( pShmNode==0 ){
  4722   4721         rc = SQLITE_NOMEM;
  4723   4722         goto shm_open_err;
  4724   4723       }
  4725         -    memset(pShmNode, 0, sizeof(*pShmNode));
         4724  +    memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
  4726   4725       zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
  4727   4726   #ifdef SQLITE_SHM_DIRECTORY
  4728   4727       sqlite3_snprintf(nShmFilename, zShmFilename, 
  4729   4728                        SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
  4730   4729                        (u32)sStat.st_ino, (u32)sStat.st_dev);
  4731   4730   #else
  4732   4731       sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
................................................................................
  5407   5406   
  5408   5407   /*
  5409   5408   ** Initialize the contents of the unixFile structure pointed to by pId.
  5410   5409   */
  5411   5410   static int fillInUnixFile(
  5412   5411     sqlite3_vfs *pVfs,      /* Pointer to vfs object */
  5413   5412     int h,                  /* Open file descriptor of file being opened */
  5414         -  int syncDir,            /* True to sync directory on first sync */
  5415   5413     sqlite3_file *pId,      /* Write to the unixFile structure here */
  5416   5414     const char *zFilename,  /* Name of the file being opened */
  5417         -  int noLock,             /* Omit locking if true */
  5418         -  int isDelete,           /* Delete on close if true */
  5419         -  int isReadOnly          /* True if the file is opened read-only */
         5415  +  int ctrlFlags           /* Zero or more UNIXFILE_* values */
  5420   5416   ){
  5421   5417     const sqlite3_io_methods *pLockingStyle;
  5422   5418     unixFile *pNew = (unixFile *)pId;
  5423   5419     int rc = SQLITE_OK;
  5424   5420   
  5425   5421     assert( pNew->pInode==NULL );
  5426   5422   
  5427         -  /* Parameter isDelete is only used on vxworks. Express this explicitly 
  5428         -  ** here to prevent compiler warnings about unused parameters.
  5429         -  */
  5430         -  UNUSED_PARAMETER(isDelete);
  5431         -
  5432   5423     /* Usually the path zFilename should not be a relative pathname. The
  5433   5424     ** exception is when opening the proxy "conch" file in builds that
  5434   5425     ** include the special Apple locking styles.
  5435   5426     */
  5436   5427   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  5437   5428     assert( zFilename==0 || zFilename[0]=='/' 
  5438   5429       || pVfs->pAppData==(void*)&autolockIoFinder );
  5439   5430   #else
  5440   5431     assert( zFilename==0 || zFilename[0]=='/' );
  5441   5432   #endif
  5442   5433   
  5443   5434     /* No locking occurs in temporary files */
  5444         -  assert( zFilename!=0 || noLock );
         5435  +  assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
  5445   5436   
  5446   5437     OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
  5447   5438     pNew->h = h;
  5448   5439     pNew->pVfs = pVfs;
  5449   5440     pNew->zPath = zFilename;
  5450         -  pNew->ctrlFlags = 0;
  5451         -  if( sqlite3_uri_boolean(zFilename, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
         5441  +  pNew->ctrlFlags = (u8)ctrlFlags;
         5442  +  if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
         5443  +                           "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  5452   5444       pNew->ctrlFlags |= UNIXFILE_PSOW;
  5453   5445     }
  5454   5446     if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
  5455   5447       pNew->ctrlFlags |= UNIXFILE_EXCL;
  5456   5448     }
  5457         -  if( isReadOnly ){
  5458         -    pNew->ctrlFlags |= UNIXFILE_RDONLY;
  5459         -  }
  5460         -  if( syncDir ){
  5461         -    pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
  5462         -  }
  5463   5449   
  5464   5450   #if OS_VXWORKS
  5465   5451     pNew->pId = vxworksFindFileId(zFilename);
  5466   5452     if( pNew->pId==0 ){
  5467         -    noLock = 1;
         5453  +    ctrlFlags |= UNIXFILE_NOLOCK;
  5468   5454       rc = SQLITE_NOMEM;
  5469   5455     }
  5470   5456   #endif
  5471   5457   
  5472         -  if( noLock ){
         5458  +  if( ctrlFlags & UNIXFILE_NOLOCK ){
  5473   5459       pLockingStyle = &nolockIoMethods;
  5474   5460     }else{
  5475   5461       pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
  5476   5462   #if SQLITE_ENABLE_LOCKING_STYLE
  5477   5463       /* Cache zFilename in the locking context (AFP and dotlock override) for
  5478   5464       ** proxyLock activation is possible (remote proxy is based on db name)
  5479   5465       ** zFilename remains valid until file is closed, to support */
................................................................................
  5586   5572   #if OS_VXWORKS
  5587   5573     if( rc!=SQLITE_OK ){
  5588   5574       if( h>=0 ) robust_close(pNew, h, __LINE__);
  5589   5575       h = -1;
  5590   5576       osUnlink(zFilename);
  5591   5577       isDelete = 0;
  5592   5578     }
  5593         -  pNew->isDelete = isDelete;
         5579  +  if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
  5594   5580   #endif
  5595   5581     if( rc!=SQLITE_OK ){
  5596   5582       if( h>=0 ) robust_close(pNew, h, __LINE__);
  5597   5583     }else{
  5598   5584       pNew->pMethod = pLockingStyle;
  5599   5585       OpenCounter(+1);
  5600   5586     }
................................................................................
  5651   5637   
  5652   5638     zDir = unixTempFileDir();
  5653   5639     if( zDir==0 ) zDir = ".";
  5654   5640   
  5655   5641     /* Check that the output buffer is large enough for the temporary file 
  5656   5642     ** name. If it is not, return SQLITE_ERROR.
  5657   5643     */
  5658         -  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
         5644  +  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
  5659   5645       return SQLITE_ERROR;
  5660   5646     }
  5661   5647   
  5662   5648     do{
  5663         -    sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
         5649  +    sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
  5664   5650       j = (int)strlen(zBuf);
  5665   5651       sqlite3_randomness(15, &zBuf[j]);
  5666   5652       for(i=0; i<15; i++, j++){
  5667   5653         zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  5668   5654       }
  5669   5655       zBuf[j] = 0;
         5656  +    zBuf[j+1] = 0;
  5670   5657     }while( osAccess(zBuf,0)==0 );
  5671   5658     return SQLITE_OK;
  5672   5659   }
  5673   5660   
  5674   5661   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
  5675   5662   /*
  5676   5663   ** Routine to transform a unixFile into a proxy-locking unixFile.
................................................................................
  5849   5836   #if SQLITE_ENABLE_DATA_PROTECTION
  5850   5837     int eType = flags&0xFF0FFF00;  /* Type of file to open */
  5851   5838   #else
  5852   5839     int eType = flags&0xFFFFFF00;  /* Type of file to open */
  5853   5840   #endif
  5854   5841     int noLock;                    /* True to omit locking primitives */
  5855   5842     int rc = SQLITE_OK;            /* Function Return Code */
         5843  +  int ctrlFlags = 0;             /* UNIXFILE_* flags */
  5856   5844   
  5857   5845     int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  5858   5846     int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  5859   5847     int isCreate     = (flags & SQLITE_OPEN_CREATE);
  5860   5848     int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  5861   5849     int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  5862   5850   #if SQLITE_ENABLE_LOCKING_STYLE
................................................................................
  5875   5863        || eType==SQLITE_OPEN_MAIN_JOURNAL 
  5876   5864        || eType==SQLITE_OPEN_WAL
  5877   5865     ));
  5878   5866   
  5879   5867     /* If argument zPath is a NULL pointer, this function is required to open
  5880   5868     ** a temporary file. Use this buffer to store the file name in.
  5881   5869     */
  5882         -  char zTmpname[MAX_PATHNAME+1];
         5870  +  char zTmpname[MAX_PATHNAME+2];
  5883   5871     const char *zName = zPath;
  5884   5872   
  5885   5873     /* Check the following statements are true: 
  5886   5874     **
  5887   5875     **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
  5888   5876     **   (b) if CREATE is set, then READWRITE must also be set, and
  5889   5877     **   (c) if EXCLUSIVE is set, then CREATE must also be set.
................................................................................
  5918   5906       }else{
  5919   5907         pUnused = sqlite3_malloc(sizeof(*pUnused));
  5920   5908         if( !pUnused ){
  5921   5909           return SQLITE_NOMEM;
  5922   5910         }
  5923   5911       }
  5924   5912       p->pUnused = pUnused;
         5913  +
         5914  +    /* Database filenames are double-zero terminated if they are not
         5915  +    ** URIs with parameters.  Hence, they can always be passed into
         5916  +    ** sqlite3_uri_parameter(). */
         5917  +    assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
         5918  +
  5925   5919     }else if( !zName ){
  5926   5920       /* If zName is NULL, the upper layer is requesting a temp file. */
  5927   5921       assert(isDelete && !syncDir);
  5928         -    rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
         5922  +    rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
  5929   5923       if( rc!=SQLITE_OK ){
  5930   5924         return rc;
  5931   5925       }
  5932   5926       zName = zTmpname;
         5927  +
         5928  +    /* Generated temporary filenames are always double-zero terminated
         5929  +    ** for use by sqlite3_uri_parameter(). */
         5930  +    assert( zName[strlen(zName)+1]==0 );
  5933   5931     }
  5934   5932   
  5935   5933     /* Determine the value of the flags parameter passed to POSIX function
  5936   5934     ** open(). These must be calculated even if open() is not called, as
  5937   5935     ** they may be stored as part of the file handle and used by the 
  5938   5936     ** 'conch file' locking functions later on.  */
  5939   5937     if( isReadonly )  openFlags |= O_RDONLY;
................................................................................
  6021   6019     if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
  6022   6020       ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  6023   6021     }
  6024   6022     if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
  6025   6023       ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  6026   6024     }
  6027   6025   #endif
  6028         -  
         6026  +
         6027  +  /* Set up appropriate ctrlFlags */
         6028  +  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
         6029  +  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
         6030  +  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
         6031  +  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
         6032  +  if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
         6033  +
  6029   6034   #if SQLITE_ENABLE_LOCKING_STYLE
  6030   6035   #if SQLITE_PREFER_PROXY_LOCKING
  6031   6036     isAutoProxy = 1;
  6032   6037   #endif
  6033   6038     if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
  6034   6039       char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
  6035   6040       int useProxy = 0;
................................................................................
  6051   6056           robust_close(p, fd, __LINE__);
  6052   6057           rc = SQLITE_IOERR_ACCESS;
  6053   6058           goto open_finished;
  6054   6059         }
  6055   6060         useProxy = !(fsInfo.f_flags&MNT_LOCAL);
  6056   6061       }
  6057   6062       if( useProxy ){
  6058         -      rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
  6059         -                          isDelete, isReadonly);
         6063  +      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
  6060   6064         if( rc==SQLITE_OK ){
  6061   6065           /* cache the pMethod in case the transform fails */
  6062   6066           const struct sqlite3_io_methods *pMethod = pFile->pMethods;
  6063   6067           rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
  6064   6068           if( rc!=SQLITE_OK ){
  6065   6069             /* Use unixClose to clean up the resources added in fillInUnixFile 
  6066   6070             ** and clear all the structure's references.  Specifically, 
................................................................................
  6075   6079           }
  6076   6080         }
  6077   6081         goto open_finished;
  6078   6082       }
  6079   6083     }
  6080   6084   #endif
  6081   6085     
  6082         -  rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
  6083         -                      isDelete, isReadonly);
         6086  +  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
         6087  +
  6084   6088   open_finished:
  6085   6089     if( rc!=SQLITE_OK ){
  6086   6090       sqlite3_free(p->pUnused);
  6087   6091     }
  6088   6092     return rc;
  6089   6093   }
  6090   6094   
................................................................................
  6101   6105     int rc = SQLITE_OK;
  6102   6106     UNUSED_PARAMETER(NotUsed);
  6103   6107     SimulateIOError(return SQLITE_IOERR_DELETE);
  6104   6108     if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
  6105   6109       return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  6106   6110     }
  6107   6111   #ifndef SQLITE_DISABLE_DIRSYNC
  6108         -  if( dirSync ){
         6112  +  if( (dirSync & 1)!=0 ){
  6109   6113       int fd;
  6110   6114       rc = osOpenDirectory(zPath, &fd);
  6111   6115       if( rc==SQLITE_OK ){
  6112   6116   #if OS_VXWORKS
  6113   6117         if( fsync(fd)==-1 )
  6114   6118   #else
  6115   6119         if( fsync(fd) )
................................................................................
  6777   6781     memset(&dummyVfs, 0, sizeof(dummyVfs));
  6778   6782     dummyVfs.pAppData = (void*)&autolockIoFinder;
  6779   6783     dummyVfs.zName = "dummy";
  6780   6784     pUnused->fd = fd;
  6781   6785     pUnused->flags = openFlags;
  6782   6786     pNew->pUnused = pUnused;
  6783   6787     
  6784         -  rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
         6788  +  rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
  6785   6789     if( rc==SQLITE_OK ){
  6786   6790       *ppFile = pNew;
  6787   6791       return SQLITE_OK;
  6788   6792     }
  6789   6793   end_create_proxy:    
  6790   6794     robust_close(pNew, fd, __LINE__);
  6791   6795     sqlite3_free(pNew);

Changes to src/os_win.c.

  2189   2189         winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
  2190   2190         return SQLITE_OK;
  2191   2191       }
  2192   2192       case SQLITE_FCNTL_VFSNAME: {
  2193   2193         *(char**)pArg = sqlite3_mprintf("win32");
  2194   2194         return SQLITE_OK;
  2195   2195       }
  2196         -    case SQLITE_FCNTL_SYNC_OMITTED: {
  2197         -      return SQLITE_OK;
  2198         -    }
  2199   2196       case SQLITE_FCNTL_WIN32_AV_RETRY: {
  2200   2197         int *a = (int*)pArg;
  2201   2198         if( a[0]>0 ){
  2202   2199           win32IoerrRetry = a[0];
  2203   2200         }else{
  2204   2201           a[0] = win32IoerrRetry;
  2205   2202         }
................................................................................
  2436   2433         if( p->hFile.h != INVALID_HANDLE_VALUE ){
  2437   2434           SimulateIOErrorBenign(1);
  2438   2435           winClose((sqlite3_file *)&p->hFile);
  2439   2436           SimulateIOErrorBenign(0);
  2440   2437         }
  2441   2438         if( deleteFlag ){
  2442   2439           SimulateIOErrorBenign(1);
         2440  +        sqlite3BeginBenignMalloc();
  2443   2441           winDelete(pVfs, p->zFilename, 0);
         2442  +        sqlite3EndBenignMalloc();
  2444   2443           SimulateIOErrorBenign(0);
  2445   2444         }
  2446   2445         *pp = p->pNext;
  2447   2446         sqlite3_free(p->aRegion);
  2448   2447         sqlite3_free(p);
  2449   2448       }else{
  2450   2449         pp = &p->pNext;
................................................................................
  2471   2470     /* Allocate space for the new sqlite3_shm object.  Also speculatively
  2472   2471     ** allocate space for a new winShmNode and filename.
  2473   2472     */
  2474   2473     p = sqlite3_malloc( sizeof(*p) );
  2475   2474     if( p==0 ) return SQLITE_IOERR_NOMEM;
  2476   2475     memset(p, 0, sizeof(*p));
  2477   2476     nName = sqlite3Strlen30(pDbFd->zPath);
  2478         -  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 16 );
         2477  +  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
  2479   2478     if( pNew==0 ){
  2480   2479       sqlite3_free(p);
  2481   2480       return SQLITE_IOERR_NOMEM;
  2482   2481     }
  2483         -  memset(pNew, 0, sizeof(*pNew));
         2482  +  memset(pNew, 0, sizeof(*pNew) + nName + 17);
  2484   2483     pNew->zFilename = (char*)&pNew[1];
  2485   2484     sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  2486   2485     sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  2487   2486   
  2488   2487     /* Look to see if there is an existing winShmNode that can be used.
  2489   2488     ** If no matching winShmNode currently exists, create a new one.
  2490   2489     */
................................................................................
  2512   2511   
  2513   2512       rc = winOpen(pDbFd->pVfs,
  2514   2513                    pShmNode->zFilename,             /* Name of the file (UTF-8) */
  2515   2514                    (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
  2516   2515                    SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
  2517   2516                    0);
  2518   2517       if( SQLITE_OK!=rc ){
  2519         -      rc = SQLITE_CANTOPEN_BKPT;
  2520   2518         goto shm_open_err;
  2521   2519       }
  2522   2520   
  2523   2521       /* Check to see if another process is holding the dead-man switch.
  2524   2522       ** If not, truncate the file to zero length. 
  2525   2523       */
  2526   2524       if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
................................................................................
  2935   2933   */
  2936   2934   static int getTempname(int nBuf, char *zBuf){
  2937   2935     static char zChars[] =
  2938   2936       "abcdefghijklmnopqrstuvwxyz"
  2939   2937       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2940   2938       "0123456789";
  2941   2939     size_t i, j;
  2942         -  char zTempPath[MAX_PATH+1];
         2940  +  char zTempPath[MAX_PATH+2];
  2943   2941   
  2944   2942     /* It's odd to simulate an io-error here, but really this is just
  2945   2943     ** using the io-error infrastructure to test that SQLite handles this
  2946   2944     ** function failing. 
  2947   2945     */
  2948   2946     SimulateIOError( return SQLITE_IOERR );
  2949   2947   
................................................................................
  2978   2976       }
  2979   2977   #endif
  2980   2978     }
  2981   2979   
  2982   2980     /* Check that the output buffer is large enough for the temporary file 
  2983   2981     ** name. If it is not, return SQLITE_ERROR.
  2984   2982     */
  2985         -  if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
         2983  +  if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
  2986   2984       return SQLITE_ERROR;
  2987   2985     }
  2988   2986   
  2989   2987     for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
  2990   2988     zTempPath[i] = 0;
  2991   2989   
  2992         -  sqlite3_snprintf(nBuf-17, zBuf,
         2990  +  sqlite3_snprintf(nBuf-18, zBuf,
  2993   2991                      "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
  2994   2992     j = sqlite3Strlen30(zBuf);
  2995   2993     sqlite3_randomness(15, &zBuf[j]);
  2996   2994     for(i=0; i<15; i++, j++){
  2997   2995       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  2998   2996     }
  2999   2997     zBuf[j] = 0;
         2998  +  zBuf[j+1] = 0;
  3000   2999   
  3001   3000     OSTRACE(("TEMP FILENAME: %s\n", zBuf));
  3002   3001     return SQLITE_OK; 
  3003   3002   }
  3004   3003   
  3005   3004   /*
  3006   3005   ** Open a file.
................................................................................
  3025   3024     void *zConverted;              /* Filename in OS encoding */
  3026   3025     const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
  3027   3026     int cnt = 0;
  3028   3027   
  3029   3028     /* If argument zPath is a NULL pointer, this function is required to open
  3030   3029     ** a temporary file. Use this buffer to store the file name in.
  3031   3030     */
  3032         -  char zTmpname[MAX_PATH+1];     /* Buffer used to create temp filename */
         3031  +  char zTmpname[MAX_PATH+2];     /* Buffer used to create temp filename */
  3033   3032   
  3034   3033     int rc = SQLITE_OK;            /* Function Return Code */
  3035   3034   #if !defined(NDEBUG) || SQLITE_OS_WINCE
  3036   3035     int eType = flags&0xFFFFFF00;  /* Type of file to open */
  3037   3036   #endif
  3038   3037   
  3039   3038     int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
................................................................................
  3084   3083     pFile->h = INVALID_HANDLE_VALUE;
  3085   3084   
  3086   3085     /* If the second argument to this function is NULL, generate a 
  3087   3086     ** temporary file name to use 
  3088   3087     */
  3089   3088     if( !zUtf8Name ){
  3090   3089       assert(isDelete && !isOpenJournal);
  3091         -    rc = getTempname(MAX_PATH+1, zTmpname);
         3090  +    rc = getTempname(MAX_PATH+2, zTmpname);
  3092   3091       if( rc!=SQLITE_OK ){
  3093   3092         return rc;
  3094   3093       }
  3095   3094       zUtf8Name = zTmpname;
  3096   3095     }
         3096  +
         3097  +  /* Database filenames are double-zero terminated if they are not
         3098  +  ** URIs with parameters.  Hence, they can always be passed into
         3099  +  ** sqlite3_uri_parameter().
         3100  +  */
         3101  +  assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
         3102  +        zUtf8Name[strlen(zUtf8Name)+1]==0 );
  3097   3103   
  3098   3104     /* Convert the filename to the system encoding. */
  3099   3105     zConverted = convertUtf8Filename(zUtf8Name);
  3100   3106     if( zConverted==0 ){
  3101   3107       return SQLITE_IOERR_NOMEM;
  3102   3108     }
  3103   3109   

Changes to src/pager.c.

  2490   2490       assert( pPager->eLock==EXCLUSIVE_LOCK );
  2491   2491       /* TODO: Is it safe to use Pager.dbFileSize here? */
  2492   2492       rc = sqlite3OsFileSize(pPager->fd, &currentSize);
  2493   2493       newSize = szPage*(i64)nPage;
  2494   2494       if( rc==SQLITE_OK && currentSize!=newSize ){
  2495   2495         if( currentSize>newSize ){
  2496   2496           rc = sqlite3OsTruncate(pPager->fd, newSize);
  2497         -      }else{
         2497  +      }else if( (currentSize+szPage)<=newSize ){
  2498   2498           char *pTmp = pPager->pTmpSpace;
  2499   2499           memset(pTmp, 0, szPage);
  2500         -        testcase( (newSize-szPage) <  currentSize );
  2501   2500           testcase( (newSize-szPage) == currentSize );
  2502   2501           testcase( (newSize-szPage) >  currentSize );
  2503   2502           rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
  2504   2503         }
  2505   2504         if( rc==SQLITE_OK ){
  2506   2505           pPager->dbFileSize = nPage;
  2507   2506         }
................................................................................
  2751   2750   
  2752   2751   end_playback:
  2753   2752     /* Following a rollback, the database file should be back in its original
  2754   2753     ** state prior to the start of the transaction, so invoke the
  2755   2754     ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
  2756   2755     ** assertion that the transaction counter was modified.
  2757   2756     */
  2758         -  assert(
  2759         -    pPager->fd->pMethods==0 ||
  2760         -    sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
  2761         -  );
         2757  +#ifdef SQLITE_DEBUG
         2758  +  if( pPager->fd->pMethods ){
         2759  +    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
         2760  +  }
         2761  +#endif
  2762   2762   
  2763   2763     /* If this playback is happening automatically as a result of an IO or 
  2764   2764     ** malloc error that occurred after the change-counter was updated but 
  2765   2765     ** before the transaction was committed, then the change-counter 
  2766   2766     ** modification may just have been reverted. If this happens in exclusive 
  2767   2767     ** mode, then subsequent transactions performed by the connection will not
  2768   2768     ** update the change-counter at all. This may lead to cache inconsistency
................................................................................
  3091   3091       assert( isOpen(pPager->fd) || pPager->tempFile );
  3092   3092       if( isOpen(pPager->fd) ){
  3093   3093         int rc = sqlite3OsFileSize(pPager->fd, &n);
  3094   3094         if( rc!=SQLITE_OK ){
  3095   3095           return rc;
  3096   3096         }
  3097   3097       }
  3098         -    nPage = (Pgno)(n / pPager->pageSize);
  3099         -    if( nPage==0 && n>0 ){
  3100         -      nPage = 1;
  3101         -    }
         3098  +    nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
  3102   3099     }
  3103   3100   
  3104   3101     /* If the current number of pages in the file is greater than the
  3105   3102     ** configured maximum pager number, increase the allowed limit so
  3106   3103     ** that the file can be read.
  3107   3104     */
  3108   3105     if( nPage>pPager->mxPgno ){
................................................................................
  3529   3526       if( rc==SQLITE_OK ){
  3530   3527         pNew = (char *)sqlite3PageMalloc(pageSize);
  3531   3528         if( !pNew ) rc = SQLITE_NOMEM;
  3532   3529       }
  3533   3530   
  3534   3531       if( rc==SQLITE_OK ){
  3535   3532         pager_reset(pPager);
  3536         -      pPager->dbSize = (Pgno)(nByte/pageSize);
         3533  +      pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
  3537   3534         pPager->pageSize = pageSize;
  3538   3535         sqlite3PageFree(pPager->pTmpSpace);
  3539   3536         pPager->pTmpSpace = pNew;
  3540   3537         sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
  3541   3538       }
  3542   3539     }
  3543   3540   
................................................................................
  4037   4034   
  4038   4035     /* Before the first write, give the VFS a hint of what the final
  4039   4036     ** file size will be.
  4040   4037     */
  4041   4038     assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
  4042   4039     if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
  4043   4040       sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
  4044         -    sqlite3BeginBenignMalloc();
  4045         -    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
  4046         -    sqlite3EndBenignMalloc();
         4041  +    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
  4047   4042       pPager->dbHintSize = pPager->dbSize;
  4048   4043     }
  4049   4044   
  4050   4045     while( rc==SQLITE_OK && pList ){
  4051   4046       Pgno pgno = pList->pgno;
  4052   4047   
  4053   4048       /* If there are dirty pages in the page cache with page numbers greater
................................................................................
  6077   6072         return rc;
  6078   6073       }
  6079   6074     }else{
  6080   6075       rc = pager_playback(pPager, 0);
  6081   6076     }
  6082   6077   
  6083   6078     assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
  6084         -  assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
         6079  +  assert( rc==SQLITE_OK || rc==SQLITE_FULL
         6080  +          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
  6085   6081   
  6086   6082     /* If an error occurs during a ROLLBACK, we can no longer trust the pager
  6087   6083     ** cache. So call pager_error() on the way out to make any error persistent.
  6088   6084     */
  6089   6085     return pager_error(pPager, rc);
  6090   6086   }
  6091   6087   

Changes to src/pcache1.c.

    36     36   **
    37     37   **   (2)  There is a single global PGroup that all PCaches are a member
    38     38   **        of.
    39     39   **
    40     40   ** Mode 1 uses more memory (since PCache instances are not able to rob
    41     41   ** unused pages from other PCaches) but it also operates without a mutex,
    42     42   ** and is therefore often faster.  Mode 2 requires a mutex in order to be
    43         -** threadsafe, but is able recycle pages more efficient.
           43  +** threadsafe, but recycles pages more efficiently.
    44     44   **
    45     45   ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
    46     46   ** PGroup which is the pcache1.grp global variable and its mutex is
    47     47   ** SQLITE_MUTEX_STATIC_LRU.
    48     48   */
    49     49   struct PGroup {
    50     50     sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
................................................................................
    62     62   **
    63     63   ** Pointers to structures of this type are cast and returned as 
    64     64   ** opaque sqlite3_pcache* handles.
    65     65   */
    66     66   struct PCache1 {
    67     67     /* Cache configuration parameters. Page size (szPage) and the purgeable
    68     68     ** flag (bPurgeable) are set when the cache is created. nMax may be 
    69         -  ** modified at any time by a call to the pcache1CacheSize() method.
           69  +  ** modified at any time by a call to the pcache1Cachesize() method.
    70     70     ** The PGroup mutex must be held when accessing nMax.
    71     71     */
    72     72     PGroup *pGroup;                     /* PGroup this cache belongs to */
    73     73     int szPage;                         /* Size of allocated pages in bytes */
    74     74     int szExtra;                        /* Size of extra space in bytes */
    75     75     int bPurgeable;                     /* True if cache is purgeable */
    76     76     unsigned int nMin;                  /* Minimum number of pages reserved */
................................................................................
   353    353   ** If memory was allocated specifically to the page cache using
   354    354   ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
   355    355   ** it is desirable to avoid allocating a new page cache entry because
   356    356   ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
   357    357   ** for all page cache needs and we should not need to spill the
   358    358   ** allocation onto the heap.
   359    359   **
   360         -** Or, the heap is used for all page cache memory put the heap is
          360  +** Or, the heap is used for all page cache memory but the heap is
   361    361   ** under memory pressure, then again it is desirable to avoid
   362    362   ** allocating a new page cache entry in order to avoid stressing
   363    363   ** the heap even further.
   364    364   */
   365    365   static int pcache1UnderMemoryPressure(PCache1 *pCache){
   366    366     if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
   367    367       return pcache1.bUnderPressure;
................................................................................
   664    664   ** the value of the createFlag argument.  0 means do not allocate a new
   665    665   ** page.  1 means allocate a new page if space is easily available.  2 
   666    666   ** means to try really hard to allocate a new page.
   667    667   **
   668    668   ** For a non-purgeable cache (a cache used as the storage for an in-memory
   669    669   ** database) there is really no difference between createFlag 1 and 2.  So
   670    670   ** the calling function (pcache.c) will never have a createFlag of 1 on
   671         -** a non-purgable cache.
          671  +** a non-purgeable cache.
   672    672   **
   673    673   ** There are three different approaches to obtaining space for a page,
   674    674   ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
   675    675   **
   676    676   **   1. Regardless of the value of createFlag, the cache is searched for a 
   677    677   **      copy of the requested page. If one is found, it is returned.
   678    678   **

Changes to src/pragma.c.

   808    808      **
   809    809      */
   810    810     if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
   811    811       if( !zRight ){
   812    812         Pager *pPager = sqlite3BtreePager(pDb->pBt);
   813    813         char *proxy_file_path = NULL;
   814    814         sqlite3_file *pFile = sqlite3PagerFile(pPager);
   815         -      sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE, 
          815  +      sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 
   816    816                              &proxy_file_path);
   817    817         
   818    818         if( proxy_file_path ){
   819    819           sqlite3VdbeSetNumCols(v, 1);
   820    820           sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
   821    821                                 "lock_proxy_file", SQLITE_STATIC);
   822    822           sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);

Changes to src/sqlite.h.in.

  2624   2624   /*
  2625   2625   ** CAPI3REF: Obtain Values For URI Parameters
  2626   2626   **
  2627   2627   ** These are utility routines, useful to VFS implementations, that check
  2628   2628   ** to see if a database file was a URI that contained a specific query 
  2629   2629   ** parameter, and if so obtains the value of that query parameter.
  2630   2630   **
  2631         -** If F is the filename pointer passed into the xOpen() method of a VFS
  2632         -** implementation and P is the name of the query parameter, then
         2631  +** If F is the database filename pointer passed into the xOpen() method of 
         2632  +** a VFS implementation when the flags parameter to xOpen() has one or 
         2633  +** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
         2634  +** P is the name of the query parameter, then
  2633   2635   ** sqlite3_uri_parameter(F,P) returns the value of the P
  2634   2636   ** parameter if it exists or a NULL pointer if P does not appear as a 
  2635   2637   ** query parameter on F.  If P is a query parameter of F
  2636   2638   ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
  2637   2639   ** a pointer to an empty string.
  2638   2640   **
  2639   2641   ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
................................................................................
  2645   2647   ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
  2646   2648   ** 64-bit signed integer and returns that integer, or D if P does not
  2647   2649   ** exist.  If the value of P is something other than an integer, then
  2648   2650   ** zero is returned.
  2649   2651   ** 
  2650   2652   ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
  2651   2653   ** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
  2652         -** is not a pathname pointer that SQLite passed into the xOpen VFS method,
  2653         -** then the behavior of this routine is undefined and probably undesirable.
         2654  +** is not a database file pathname pointer that SQLite passed into the xOpen
         2655  +** VFS method, then the behavior of this routine is undefined and probably
         2656  +** undesirable.
  2654   2657   */
  2655   2658   const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  2656   2659   int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  2657   2660   sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  2658   2661   
  2659   2662   
  2660   2663   /*
................................................................................
  5400   5403   ** The SQLite source code contains multiple implementations
  5401   5404   ** of these mutex routines.  An appropriate implementation
  5402   5405   ** is selected automatically at compile-time.  ^(The following
  5403   5406   ** implementations are available in the SQLite core:
  5404   5407   **
  5405   5408   ** <ul>
  5406   5409   ** <li>   SQLITE_MUTEX_OS2
  5407         -** <li>   SQLITE_MUTEX_PTHREAD
         5410  +** <li>   SQLITE_MUTEX_PTHREADS
  5408   5411   ** <li>   SQLITE_MUTEX_W32
  5409   5412   ** <li>   SQLITE_MUTEX_NOOP
  5410   5413   ** </ul>)^
  5411   5414   **
  5412   5415   ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  5413   5416   ** that does no real locking and is appropriate for use in
  5414   5417   ** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
  5415         -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
         5418  +** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
  5416   5419   ** are appropriate for use on OS/2, Unix, and Windows.
  5417   5420   **
  5418   5421   ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  5419   5422   ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  5420   5423   ** implementation is included with the library. In this case the
  5421   5424   ** application must supply a custom mutex implementation using the
  5422   5425   ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
................................................................................
  6207   6210   ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
  6208   6211   ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
  6209   6212   ** functions.
  6210   6213   **
  6211   6214   ** [[the xShrink() page cache method]]
  6212   6215   ** ^SQLite invokes the xShrink() method when it wants the page cache to
  6213   6216   ** free up as much of heap memory as possible.  The page cache implementation
  6214         -** is not obligated to free any memory, but well-behaved implementions should
         6217  +** is not obligated to free any memory, but well-behaved implementations should
  6215   6218   ** do their best.
  6216   6219   */
  6217   6220   typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
  6218   6221   struct sqlite3_pcache_methods2 {
  6219   6222     int iVersion;
  6220   6223     void *pArg;
  6221   6224     int (*xInit)(void*);

Changes to src/tclsqlite.c.

  3013   3013         Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0);
  3014   3014         return TCL_ERROR;
  3015   3015       }
  3016   3016     }
  3017   3017     if( objc<3 || (objc&1)!=1 ){
  3018   3018       Tcl_WrongNumArgs(interp, 1, objv, 
  3019   3019         "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
  3020         -      " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?"
         3020  +      " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
  3021   3021   #ifdef SQLITE_HAS_CODEC
  3022   3022         " ?-key CODECKEY?"
  3023   3023   #endif
  3024   3024       );
  3025   3025       return TCL_ERROR;
  3026   3026     }
  3027   3027     zErrMsg = 0;

Changes to src/test2.c.

   533    533     const char **argv      /* Text of each argument */
   534    534   ){
   535    535     sqlite3_vfs *pVfs;
   536    536     sqlite3_file *fd = 0;
   537    537     int rc;
   538    538     int n;
   539    539     i64 offset;
          540  +  char *zFile;
          541  +  int nFile;
   540    542     if( argc!=3 ){
   541    543       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   542    544          " N-MEGABYTES FILE\"", 0);
   543    545       return TCL_ERROR;
   544    546     }
   545    547     if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
   546    548   
   547    549     pVfs = sqlite3_vfs_find(0);
   548         -  rc = sqlite3OsOpenMalloc(pVfs, argv[2], &fd, 
          550  +  nFile = strlen(argv[2]);
          551  +  zFile = sqlite3_malloc( nFile+2 );
          552  +  if( zFile==0 ) return TCL_ERROR;
          553  +  memcpy(zFile, argv[2], nFile+1);
          554  +  zFile[nFile+1] = 0;
          555  +  rc = sqlite3OsOpenMalloc(pVfs, zFile, &fd, 
   549    556         (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0
   550    557     );
   551    558     if( rc ){
   552    559       Tcl_AppendResult(interp, "open failed: ", errorName(rc), 0);
          560  +    sqlite3_free(zFile);
   553    561       return TCL_ERROR;
   554    562     }
   555    563     offset = n;
   556    564     offset *= 1024*1024;
   557    565     rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset);
   558    566     sqlite3OsCloseFree(fd);
          567  +  sqlite3_free(zFile);
   559    568     if( rc ){
   560    569       Tcl_AppendResult(interp, "write failed: ", errorName(rc), 0);
   561    570       return TCL_ERROR;
   562    571     }
   563    572     return TCL_OK;
   564    573   }
   565    574   #endif

Changes to src/test3.c.

    62     62     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
    63     63     int argc,              /* Number of arguments */
    64     64     const char **argv      /* Text of each argument */
    65     65   ){
    66     66     Btree *pBt;
    67     67     int rc, nCache;
    68     68     char zBuf[100];
           69  +  int n;
           70  +  char *zFilename;
    69     71     if( argc!=3 ){
    70     72       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
    71     73          " FILENAME NCACHE FLAGS\"", 0);
    72     74       return TCL_ERROR;
    73     75     }
    74     76     if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
    75     77     nRefSqlite3++;
    76     78     if( nRefSqlite3==1 ){
    77     79       sDb.pVfs = sqlite3_vfs_find(0);
    78     80       sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
    79     81       sqlite3_mutex_enter(sDb.mutex);
    80     82     }
    81         -  rc = sqlite3BtreeOpen(sDb.pVfs, argv[1], &sDb, &pBt, 0, 
           83  +  n = strlen(argv[1]);
           84  +  zFilename = sqlite3_malloc( n+2 );
           85  +  if( zFilename==0 ) return TCL_ERROR;
           86  +  memcpy(zFilename, argv[1], n+1);
           87  +  zFilename[n+1] = 0;
           88  +  rc = sqlite3BtreeOpen(sDb.pVfs, zFilename, &sDb, &pBt, 0, 
    82     89        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
           90  +  sqlite3_free(zFilename);
    83     91     if( rc!=SQLITE_OK ){
    84     92       Tcl_AppendResult(interp, errorName(rc), 0);
    85     93       return TCL_ERROR;
    86     94     }
    87     95     sqlite3BtreeSetCacheSize(pBt, nCache);
    88     96     sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pBt);
    89     97     Tcl_AppendResult(interp, zBuf, 0);

Changes to src/test_func.c.


Changes to src/test_journal.c.

   405    405       if( rc==SQLITE_OK ){
   406    406         int ii;
   407    407         for(ii=0; rc==SQLITE_OK && ii<pMain->nPage; ii++){
   408    408           i64 iOff = (i64)(pMain->nPagesize) * (i64)ii;
   409    409           if( iOff==PENDING_BYTE ) continue;
   410    410           rc = sqlite3OsRead(pMain->pReal, aData, pMain->nPagesize, iOff);
   411    411           pMain->aCksum[ii] = genCksum(aData, pMain->nPagesize);
          412  +        if( ii+1==pMain->nPage && rc==SQLITE_IOERR_SHORT_READ ) rc = SQLITE_OK;
   412    413         }
   413    414       }
   414    415   
   415    416       start_ioerr_simulation(iSave, iSave2);
   416    417     }
   417    418   
   418    419     sqlite3_free(aData);
................................................................................
   658    659   }
   659    660   
   660    661   /*
   661    662   ** File control method. For custom operations on an jt-file.
   662    663   */
   663    664   static int jtFileControl(sqlite3_file *pFile, int op, void *pArg){
   664    665     jt_file *p = (jt_file *)pFile;
   665         -  return sqlite3OsFileControl(p->pReal, op, pArg);
          666  +  return p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
   666    667   }
   667    668   
   668    669   /*
   669    670   ** Return the sector-size in bytes for an jt-file.
   670    671   */
   671    672   static int jtSectorSize(sqlite3_file *pFile){
   672    673     jt_file *p = (jt_file *)pFile;

Changes to src/test_multiplex.c.

   216    216     while( *z2 ){ z2++; }
   217    217     return 0x3fffffff & (int)(z2 - z);
   218    218   }
   219    219   
   220    220   /*
   221    221   ** Generate the file-name for chunk iChunk of the group with base name
   222    222   ** zBase. The file-name is written to buffer zOut before returning. Buffer
   223         -** zOut must be allocated by the caller so that it is at least (nBase+4)
          223  +** zOut must be allocated by the caller so that it is at least (nBase+5)
   224    224   ** bytes in size, where nBase is the length of zBase, not including the
   225    225   ** nul-terminator.
          226  +**
          227  +** If iChunk is 0 (or 400 - the number for the first journal file chunk),
          228  +** the output is a copy of the input string. Otherwise, if 
          229  +** SQLITE_ENABLE_8_3_NAMES is not defined or the input buffer does not contain
          230  +** a "." character, then the output is a copy of the input string with the 
          231  +** three-digit zero-padded decimal representation if iChunk appended to it. 
          232  +** For example:
          233  +**
          234  +**   zBase="test.db", iChunk=4  ->  zOut="test.db004"
          235  +**
          236  +** Or, if SQLITE_ENABLE_8_3_NAMES is defined and the input buffer contains
          237  +** a "." character, then everything after the "." is replaced by the 
          238  +** three-digit representation of iChunk.
          239  +**
          240  +**   zBase="test.db", iChunk=4  ->  zOut="test.004"
          241  +**
          242  +** The output buffer string is terminated by 2 0x00 bytes. This makes it safe
          243  +** to pass to sqlite3_uri_parameter() and similar.
   226    244   */
   227    245   static void multiplexFilename(
   228    246     const char *zBase,              /* Filename for chunk 0 */
   229    247     int nBase,                      /* Size of zBase in bytes (without \0) */
   230    248     int flags,                      /* Flags used to open file */
   231    249     int iChunk,                     /* Chunk to generate filename for */
   232    250     char *zOut                      /* Buffer to write generated name to */
   233    251   ){
   234         -  memcpy(zOut, zBase, nBase+1);
          252  +  int n = nBase;
          253  +  memcpy(zOut, zBase, n+1);
   235    254     if( iChunk!=0 && iChunk!=SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET ){
   236         -    int n = nBase;
   237    255   #ifdef SQLITE_ENABLE_8_3_NAMES
   238    256       int i;
   239    257       for(i=n-1; i>0 && i>=n-4 && zOut[i]!='.'; i--){}
   240    258       if( i>=n-4 ) n = i+1;
   241    259       if( flags & SQLITE_OPEN_MAIN_JOURNAL ){
   242    260         /* The extensions on overflow files for main databases are 001, 002,
   243    261          ** 003 and so forth.  To avoid name collisions, add 400 to the 
   244    262          ** extensions of journal files so that they are 401, 402, 403, ....
   245    263          */
   246    264         iChunk += SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET;
   247    265       }
   248    266   #endif
   249    267       sqlite3_snprintf(4,&zOut[n],"%03d",iChunk);
          268  +    n += 3;
   250    269     }
          270  +
          271  +  assert( zOut[n]=='\0' );
          272  +  zOut[n+1] = '\0';
   251    273   }
   252    274   
   253    275   /* Compute the filename for the iChunk-th chunk
   254    276   */
   255    277   static int multiplexSubFilename(multiplexGroup *pGroup, int iChunk){
   256    278     if( iChunk>=pGroup->nReal ){
   257    279       struct multiplexReal *p;
................................................................................
   262    284       memset(&p[pGroup->nReal], 0, sizeof(p[0])*(iChunk+1-pGroup->nReal));
   263    285       pGroup->aReal = p;
   264    286       pGroup->nReal = iChunk+1;
   265    287     }
   266    288     if( pGroup->zName && pGroup->aReal[iChunk].z==0 ){
   267    289       char *z;
   268    290       int n = pGroup->nName;
   269         -    pGroup->aReal[iChunk].z = z = sqlite3_malloc( n+4 );
          291  +    pGroup->aReal[iChunk].z = z = sqlite3_malloc( n+5 );
   270    292       if( z==0 ){
   271    293         return SQLITE_NOMEM;
   272    294       }
   273    295       multiplexFilename(pGroup->zName, pGroup->nName, pGroup->flags, iChunk, z);
   274    296     }
   275    297     return SQLITE_OK;
   276    298   }
................................................................................
   302    324       return 0;
   303    325     }
   304    326   #endif
   305    327   
   306    328     *rc = multiplexSubFilename(pGroup, iChunk);
   307    329     if( (*rc)==SQLITE_OK && (pSubOpen = pGroup->aReal[iChunk].p)==0 ){
   308    330       int flags, bExists;
   309         -    createFlag = (pGroup->flags & SQLITE_OPEN_CREATE)!=0;
   310    331       flags = pGroup->flags;
   311    332       if( createFlag ){
   312    333         flags |= SQLITE_OPEN_CREATE;
   313    334       }else if( iChunk==0 ){
   314    335         /* Fall through */
   315    336       }else if( pGroup->aReal[iChunk].z==0 ){
   316    337         return 0;
................................................................................
   346    367     multiplexGroup *pGroup,    /* The multiplexor group */
   347    368     int iChunk,                /* Which chunk to open.  0==original file */
   348    369     int *rc                    /* Result code in and out */
   349    370   ){
   350    371     sqlite3_file *pSub;
   351    372     sqlite3_int64 sz = 0;
   352    373   
          374  +  if( *rc ) return 0;
   353    375     pSub = multiplexSubOpen(pGroup, iChunk, rc, NULL, 0);
   354    376     if( pSub==0 ) return 0;
   355    377     *rc = pSub->pMethods->xFileSize(pSub, &sz);
   356    378     return sz;
   357    379   }    
   358    380   
   359    381   /*
................................................................................
   487    509       pGroup = sqlite3_malloc( sz );
   488    510       if( pGroup==0 ){
   489    511         rc = SQLITE_NOMEM;
   490    512       }
   491    513     }
   492    514   
   493    515     if( rc==SQLITE_OK ){
          516  +    const char *zUri = (flags & SQLITE_OPEN_URI) ? zName : 0;
   494    517       /* assign pointers to extra space allocated */
   495    518       memset(pGroup, 0, sz);
   496    519       pMultiplexOpen->pGroup = pGroup;
   497    520       pGroup->bEnabled = -1;
   498         -    pGroup->bTruncate = sqlite3_uri_boolean(zName, "truncate", 
   499         -                                 (flags & SQLITE_OPEN_MAIN_DB)==0);
   500         -    pGroup->szChunk = sqlite3_uri_int64(zName, "chunksize",
          521  +    pGroup->bTruncate = sqlite3_uri_boolean(zUri, "truncate", 
          522  +                                   (flags & SQLITE_OPEN_MAIN_DB)==0);
          523  +    pGroup->szChunk = sqlite3_uri_int64(zUri, "chunksize",
   501    524                                           SQLITE_MULTIPLEX_CHUNK_SIZE);
   502    525       pGroup->szChunk = (pGroup->szChunk+0xffff)&~0xffff;
   503    526       if( zName ){
   504    527         char *p = (char *)&pGroup[1];
   505    528         pGroup->zName = p;
   506    529         memcpy(pGroup->zName, zName, nName+1);
   507    530         pGroup->nName = nName;
................................................................................
   607    630     rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
   608    631     if( rc==SQLITE_OK ){
   609    632       /* If the main chunk was deleted successfully, also delete any subsequent
   610    633       ** chunks - starting with the last (highest numbered). 
   611    634       */
   612    635       int nName = strlen(zName);
   613    636       char *z;
   614         -    z = sqlite3_malloc(nName + 4);
          637  +    z = sqlite3_malloc(nName + 5);
   615    638       if( z==0 ){
   616    639         rc = SQLITE_IOERR_NOMEM;
   617    640       }else{
   618    641         int iChunk = 0;
   619    642         int bExists;
   620    643         do{
   621    644           multiplexFilename(zName, nName, SQLITE_OPEN_MAIN_JOURNAL, ++iChunk, z);

Changes to src/test_quota.c.

   933    933   */
   934    934   int sqlite3_quota_file(const char *zFilename){
   935    935     char *zFull;
   936    936     sqlite3_file *fd;
   937    937     int rc;
   938    938     int outFlags = 0;
   939    939     sqlite3_int64 iSize;
   940         -  fd = (sqlite3_file*)sqlite3_malloc(gQuota.sThisVfs.szOsFile +
   941         -                                     gQuota.sThisVfs.mxPathname+1);
   942         -  if( fd==0 ) return SQLITE_NOMEM;
   943         -  zFull = gQuota.sThisVfs.szOsFile + (char*)fd;
   944         -  rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
   945         -                                      gQuota.sThisVfs.mxPathname+1, zFull);
          940  +  int nAlloc = gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+2;
          941  +
          942  +  /* Allocate space for a file-handle and the full path for file zFilename */
          943  +  fd = (sqlite3_file *)sqlite3_malloc(nAlloc);
          944  +  if( fd==0 ){
          945  +    rc = SQLITE_NOMEM;
          946  +  }else{
          947  +    zFull = &((char *)fd)[gQuota.sThisVfs.szOsFile];
          948  +    rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
          949  +        gQuota.sThisVfs.mxPathname+1, zFull);
          950  +  }
          951  +
   946    952     if( rc==SQLITE_OK ){
          953  +    zFull[strlen(zFull)+1] = '\0';
   947    954       rc = quotaOpen(&gQuota.sThisVfs, zFull, fd, 
   948    955                      SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags);
   949         -  }
   950         -  if( rc==SQLITE_OK ){
   951         -    fd->pMethods->xFileSize(fd, &iSize);
   952         -    fd->pMethods->xClose(fd);
   953         -  }else if( rc==SQLITE_CANTOPEN ){
   954         -    quotaGroup *pGroup;
   955         -    quotaFile *pFile;
   956         -    quotaEnter();
   957         -    pGroup = quotaGroupFind(zFull);
   958         -    if( pGroup ){
   959         -      pFile = quotaFindFile(pGroup, zFull, 0);
   960         -      if( pFile ) quotaRemoveFile(pFile);
          956  +    if( rc==SQLITE_OK ){
          957  +      fd->pMethods->xFileSize(fd, &iSize);
          958  +      fd->pMethods->xClose(fd);
          959  +    }else if( rc==SQLITE_CANTOPEN ){
          960  +      quotaGroup *pGroup;
          961  +      quotaFile *pFile;
          962  +      quotaEnter();
          963  +      pGroup = quotaGroupFind(zFull);
          964  +      if( pGroup ){
          965  +        pFile = quotaFindFile(pGroup, zFull, 0);
          966  +        if( pFile ) quotaRemoveFile(pFile);
          967  +      }
          968  +      quotaLeave();
   961    969       }
   962         -    quotaLeave();
   963    970     }
          971  +
   964    972     sqlite3_free(fd);
   965    973     return rc;
   966    974   }
   967    975   
   968    976   /*
   969    977   ** Open a potentially quotaed file for I/O.
   970    978   */

Changes to src/util.c.

  1160   1160   ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
  1161   1161   ** three characters, then shorten the suffix on z[] to be the last three
  1162   1162   ** characters of the original suffix.
  1163   1163   **
  1164   1164   ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
  1165   1165   ** do the suffix shortening regardless of URI parameter.
  1166   1166   **
  1167         -** Assume that zBaseFilename contains two \000 terminator bytes (so that
  1168         -** it can be harmlessly passed into sqlite3_uri_parameter()) and copy both
  1169         -** zero terminator bytes into the end of the revised name.
  1170         -**
  1171   1167   ** Examples:
  1172   1168   **
  1173   1169   **     test.db-journal    =>   test.nal
  1174   1170   **     test.db-wal        =>   test.wal
  1175   1171   **     test.db-shm        =>   test.shm
  1176   1172   **     test.db-mj7f3319fa =>   test.9fa
  1177   1173   */
  1178   1174   void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
  1179         -  assert( zBaseFilename[strlen(zBaseFilename)+1]==0 );
  1180   1175   #if SQLITE_ENABLE_8_3_NAMES<2
  1181   1176     if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
  1182   1177   #endif
  1183   1178     {
  1184   1179       int i, sz;
  1185   1180       sz = sqlite3Strlen30(z);
  1186   1181       for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
  1187         -    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 5);
         1182  +    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
  1188   1183     }
  1189   1184   }
  1190   1185   #endif

Changes to src/vdbe.c.

    48     48   
    49     49   /*
    50     50   ** Invoke this macro on memory cells just prior to changing the
    51     51   ** value of the cell.  This macro verifies that shallow copies are
    52     52   ** not misused.
    53     53   */
    54     54   #ifdef SQLITE_DEBUG
    55         -# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
           55  +# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
    56     56   #else
    57     57   # define memAboutToChange(P,M)
    58     58   #endif
    59     59   
    60     60   /*
    61     61   ** The following global variable is incremented every time a cursor
    62     62   ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
................................................................................
    66     66   */
    67     67   #ifdef SQLITE_TEST
    68     68   int sqlite3_search_count = 0;
    69     69   #endif
    70     70   
    71     71   /*
    72     72   ** When this global variable is positive, it gets decremented once before
    73         -** each instruction in the VDBE.  When reaches zero, the u1.isInterrupted
    74         -** field of the sqlite3 structure is set in order to simulate and interrupt.
           73  +** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
           74  +** field of the sqlite3 structure is set in order to simulate an interrupt.
    75     75   **
    76     76   ** This facility is used for testing purposes only.  It does not function
    77     77   ** in an ordinary build.
    78     78   */
    79     79   #ifdef SQLITE_TEST
    80     80   int sqlite3_interrupt_count = 0;
    81     81   #endif
................................................................................
   147    147   ** string that the register itself controls.  In other words, it
   148    148   ** converts an MEM_Ephem string into an MEM_Dyn string.
   149    149   */
   150    150   #define Deephemeralize(P) \
   151    151      if( ((P)->flags&MEM_Ephem)!=0 \
   152    152          && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
   153    153   
   154         -/*
   155         -** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
   156         -** P if required.
   157         -*/
   158         -#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
   159         -
   160    154   /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
   161    155   #ifdef SQLITE_OMIT_MERGE_SORT
   162    156   # define isSorter(x) 0
   163    157   #else
   164    158   # define isSorter(x) ((x)->pSorter!=0)
   165    159   #endif
   166    160   
................................................................................
   192    186   ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
   193    187   ** if we run out of memory.
   194    188   */
   195    189   static VdbeCursor *allocateCursor(
   196    190     Vdbe *p,              /* The virtual machine */
   197    191     int iCur,             /* Index of the new VdbeCursor */
   198    192     int nField,           /* Number of fields in the table or index */
   199         -  int iDb,              /* When database the cursor belongs to, or -1 */
          193  +  int iDb,              /* Database the cursor belongs to, or -1 */
   200    194     int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
   201    195   ){
   202    196     /* Find the memory cell that will be used to store the blob of memory
   203    197     ** required for this VdbeCursor structure. It is convenient to use a 
   204    198     ** vdbe memory cell to manage the memory allocation required for a
   205    199     ** VdbeCursor structure for the following reasons:
   206    200     **
................................................................................
   474    468   /*
   475    469   ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
   476    470   ** sqlite3_interrupt() routine has been called.  If it has been, then
   477    471   ** processing of the VDBE program is interrupted.
   478    472   **
   479    473   ** This macro added to every instruction that does a jump in order to
   480    474   ** implement a loop.  This test used to be on every single instruction,
   481         -** but that meant we more testing that we needed.  By only testing the
          475  +** but that meant we more testing than we needed.  By only testing the
   482    476   ** flag on jump instructions, we get a (small) speed improvement.
   483    477   */
   484    478   #define CHECK_FOR_INTERRUPT \
   485    479      if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
   486    480   
   487    481   
   488    482   #ifndef NDEBUG
................................................................................
   669    663       */
   670    664       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   671    665       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   672    666         assert( pOp->p2>0 );
   673    667         assert( pOp->p2<=p->nMem );
   674    668         pOut = &aMem[pOp->p2];
   675    669         memAboutToChange(p, pOut);
   676         -      MemReleaseExt(pOut);
          670  +      VdbeMemRelease(pOut);
   677    671         pOut->flags = MEM_Int;
   678    672       }
   679    673   
   680    674       /* Sanity checking on other operands */
   681    675   #ifdef SQLITE_DEBUG
   682    676       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   683    677         assert( pOp->p1>0 );
................................................................................
   973    967     int cnt;
   974    968     cnt = pOp->p3-pOp->p2;
   975    969     assert( pOp->p3<=p->nMem );
   976    970     pOut->flags = MEM_Null;
   977    971     while( cnt>0 ){
   978    972       pOut++;
   979    973       memAboutToChange(p, pOut);
   980         -    MemReleaseExt(pOut);
          974  +    VdbeMemRelease(pOut);
   981    975       pOut->flags = MEM_Null;
   982    976       cnt--;
   983    977     }
   984    978     break;
   985    979   }
   986    980   
   987    981   
................................................................................
  2371   2365     ** then there are not enough fields in the record to satisfy the
  2372   2366     ** request.  In this case, set the value NULL or to P4 if P4 is
  2373   2367     ** a pointer to a Mem object.
  2374   2368     */
  2375   2369     if( aOffset[p2] ){
  2376   2370       assert( rc==SQLITE_OK );
  2377   2371       if( zRec ){
  2378         -      MemReleaseExt(pDest);
         2372  +      VdbeMemRelease(pDest);
  2379   2373         sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
  2380   2374       }else{
  2381   2375         len = sqlite3VdbeSerialTypeLen(aType[p2]);
  2382   2376         sqlite3VdbeMemMove(&sMem, pDest);
  2383   2377         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
  2384   2378         if( rc!=SQLITE_OK ){
  2385   2379           goto op_column_out;

Changes to src/vdbeInt.h.

   404    404   double sqlite3VdbeRealValue(Mem*);
   405    405   void sqlite3VdbeIntegerAffinity(Mem*);
   406    406   int sqlite3VdbeMemRealify(Mem*);
   407    407   int sqlite3VdbeMemNumerify(Mem*);
   408    408   int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   409    409   void sqlite3VdbeMemRelease(Mem *p);
   410    410   void sqlite3VdbeMemReleaseExternal(Mem *p);
   411         -#define MemReleaseExt(X)  \
          411  +#define VdbeMemRelease(X)  \
   412    412     if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
   413    413       sqlite3VdbeMemReleaseExternal(X);
   414    414   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   415    415   const char *sqlite3OpcodeName(int);
   416    416   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   417    417   int sqlite3VdbeCloseStatement(Vdbe *, int);
   418    418   void sqlite3VdbeFrameDelete(VdbeFrame*);
................................................................................
   443    443     void sqlite3VdbeLeave(Vdbe*);
   444    444   #else
   445    445   # define sqlite3VdbeEnter(X)
   446    446   # define sqlite3VdbeLeave(X)
   447    447   #endif
   448    448   
   449    449   #ifdef SQLITE_DEBUG
   450         -void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
          450  +void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
   451    451   #endif
   452    452   
   453    453   #ifndef SQLITE_OMIT_FOREIGN_KEY
   454    454   int sqlite3VdbeCheckFk(Vdbe *, int);
   455    455   #else
   456    456   # define sqlite3VdbeCheckFk(p,i) 0
   457    457   #endif
................................................................................
   461    461     void sqlite3VdbePrintSql(Vdbe*);
   462    462     void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
   463    463   #endif
   464    464   int sqlite3VdbeMemHandleBom(Mem *pMem);
   465    465   
   466    466   #ifndef SQLITE_OMIT_INCRBLOB
   467    467     int sqlite3VdbeMemExpandBlob(Mem *);
          468  +  #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
   468    469   #else
   469    470     #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
          471  +  #define ExpandBlob(P) SQLITE_OK
   470    472   #endif
   471    473   
   472    474   #endif /* !defined(_VDBEINT_H_) */

Changes to src/vdbeaux.c.

   192    192     int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
   193    193     sqlite3VdbeChangeP4(p, addr, zP4, p4type);
   194    194     return addr;
   195    195   }
   196    196   
   197    197   /*
   198    198   ** Add an OP_ParseSchema opcode.  This routine is broken out from
   199         -** sqlite3VdbeAddOp4() since it needs to also local all btrees.
          199  +** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
          200  +** as having been used.
   200    201   **
   201    202   ** The zWhere string must have been obtained from sqlite3_malloc().
   202    203   ** This routine will take ownership of the allocated memory.
   203    204   */
   204    205   void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
   205    206     int j;
   206    207     int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
................................................................................
   959    960   }
   960    961   #endif
   961    962   
   962    963   /*
   963    964   ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
   964    965   **
   965    966   ** The prepared statements need to know in advance the complete set of
   966         -** attached databases that they will be using.  A mask of these databases
   967         -** is maintained in p->btreeMask and is used for locking and other purposes.
          967  +** attached databases that will be use.  A mask of these databases
          968  +** is maintained in p->btreeMask.  The p->lockMask value is the subset of
          969  +** p->btreeMask of databases that will require a lock.
   968    970   */
   969    971   void sqlite3VdbeUsesBtree(Vdbe *p, int i){
   970    972     assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
   971    973     assert( i<(int)sizeof(p->btreeMask)*8 );
   972    974     p->btreeMask |= ((yDbMask)1)<<i;
   973    975     if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
   974    976       p->lockMask |= ((yDbMask)1)<<i;

Changes to src/vdbemem.c.

    14     14   ** stores a single value in the VDBE.  Mem is an opaque structure visible
    15     15   ** only within the VDBE.  Interface routines refer to a Mem using the
    16     16   ** name sqlite_value
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "vdbeInt.h"
    20     20   
    21         -/*
    22         -** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
    23         -** P if required.
    24         -*/
    25         -#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
    26         -
    27     21   /*
    28     22   ** If pMem is an object with a valid string representation, this routine
    29     23   ** ensures the internal encoding for the string representation is
    30     24   ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
    31     25   **
    32     26   ** If pMem is not a string object, or the encoding of the string
    33     27   ** representation is already stored using the requested encoding, then this
................................................................................
   119    113   **
   120    114   ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
   121    115   */
   122    116   int sqlite3VdbeMemMakeWriteable(Mem *pMem){
   123    117     int f;
   124    118     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   125    119     assert( (pMem->flags&MEM_RowSet)==0 );
   126         -  expandBlob(pMem);
          120  +  ExpandBlob(pMem);
   127    121     f = pMem->flags;
   128    122     if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
   129    123       if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
   130    124         return SQLITE_NOMEM;
   131    125       }
   132    126       pMem->z[pMem->n] = 0;
   133    127       pMem->z[pMem->n+1] = 0;
................................................................................
   288    282   
   289    283   /*
   290    284   ** Release any memory held by the Mem. This may leave the Mem in an
   291    285   ** inconsistent state, for example with (Mem.z==0) and
   292    286   ** (Mem.type==SQLITE_TEXT).
   293    287   */
   294    288   void sqlite3VdbeMemRelease(Mem *p){
   295         -  MemReleaseExt(p);
          289  +  VdbeMemRelease(p);
   296    290     sqlite3DbFree(p->db, p->zMalloc);
   297    291     p->z = 0;
   298    292     p->zMalloc = 0;
   299    293     p->xDel = 0;
   300    294   }
   301    295   
   302    296   /*
................................................................................
   584    578   ** This routine prepares a memory cell for modication by breaking
   585    579   ** its link to a shallow copy and by marking any current shallow
   586    580   ** copies of this cell as invalid.
   587    581   **
   588    582   ** This is used for testing and debugging only - to make sure shallow
   589    583   ** copies are not misused.
   590    584   */
   591         -void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
          585  +void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
   592    586     int i;
   593    587     Mem *pX;
   594    588     for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
   595    589       if( pX->pScopyFrom==pMem ){
   596    590         pX->flags |= MEM_Invalid;
   597    591         pX->pScopyFrom = 0;
   598    592       }
................................................................................
   610    604   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   611    605   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   612    606   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   613    607   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   614    608   */
   615    609   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   616    610     assert( (pFrom->flags & MEM_RowSet)==0 );
   617         -  MemReleaseExt(pTo);
          611  +  VdbeMemRelease(pTo);
   618    612     memcpy(pTo, pFrom, MEMCELLSIZE);
   619    613     pTo->xDel = 0;
   620    614     if( (pFrom->flags&MEM_Static)==0 ){
   621    615       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   622    616       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   623    617       pTo->flags |= srcType;
   624    618     }
................................................................................
   628    622   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   629    623   ** freed before the copy is made.
   630    624   */
   631    625   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   632    626     int rc = SQLITE_OK;
   633    627   
   634    628     assert( (pFrom->flags & MEM_RowSet)==0 );
   635         -  MemReleaseExt(pTo);
          629  +  VdbeMemRelease(pTo);
   636    630     memcpy(pTo, pFrom, MEMCELLSIZE);
   637    631     pTo->flags &= ~MEM_Dyn;
   638    632   
   639    633     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   640    634       if( 0==(pFrom->flags&MEM_Static) ){
   641    635         pTo->flags |= MEM_Ephem;
   642    636         rc = sqlite3VdbeMemMakeWriteable(pTo);
................................................................................
   956    950     assert( (pVal->flags & MEM_RowSet)==0 );
   957    951   
   958    952     if( pVal->flags&MEM_Null ){
   959    953       return 0;
   960    954     }
   961    955     assert( (MEM_Blob>>3) == MEM_Str );
   962    956     pVal->flags |= (pVal->flags & MEM_Blob)>>3;
   963         -  expandBlob(pVal);
          957  +  ExpandBlob(pVal);
   964    958     if( pVal->flags&MEM_Str ){
   965    959       sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
   966    960       if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
   967    961         assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
   968    962         if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
   969    963           return 0;
   970    964         }

Changes to src/wal.c.

  1728   1728       /* If the database file may grow as a result of this checkpoint, hint
  1729   1729       ** about the eventual size of the db file to the VFS layer. 
  1730   1730       */
  1731   1731       if( rc==SQLITE_OK ){
  1732   1732         i64 nReq = ((i64)mxPage * szPage);
  1733   1733         rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
  1734   1734         if( rc==SQLITE_OK && nSize<nReq ){
  1735         -        sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
         1735  +        sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
  1736   1736         }
  1737   1737       }
  1738   1738   
  1739   1739       /* Iterate through the contents of the WAL, copying data to the db file. */
  1740   1740       while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
  1741   1741         i64 iOffset;
  1742   1742         assert( walFramePgno(pWal, iFrame)==iDbpage );
................................................................................
  1844   1844           pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
  1845   1845         }
  1846   1846         rc = sqlite3WalCheckpoint(
  1847   1847             pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
  1848   1848         );
  1849   1849         if( rc==SQLITE_OK ){
  1850   1850           int bPersist = -1;
  1851         -        sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist);
         1851  +        sqlite3OsFileControlHint(
         1852  +            pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
         1853  +        );
  1852   1854           if( bPersist!=1 ){
  1853   1855             /* Try to delete the WAL file if the checkpoint completed and
  1854   1856             ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
  1855   1857             ** mode (!bPersist) */
  1856   1858             isDelete = 1;
  1857   1859           }else if( pWal->mxWalSize>=0 ){
  1858   1860             /* Try to truncate the WAL file to zero bytes if the checkpoint
................................................................................
  1865   1867           }
  1866   1868         }
  1867   1869       }
  1868   1870   
  1869   1871       walIndexClose(pWal, isDelete);
  1870   1872       sqlite3OsClose(pWal->pWalFd);
  1871   1873       if( isDelete ){
         1874  +      sqlite3BeginBenignMalloc();
  1872   1875         sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
         1876  +      sqlite3EndBenignMalloc();
  1873   1877       }
  1874   1878       WALTRACE(("WAL%p: closed\n", pWal));
  1875   1879       sqlite3_free((void *)pWal->apWiData);
  1876   1880       sqlite3_free(pWal);
  1877   1881     }
  1878   1882     return rc;
  1879   1883   }

Changes to test/backup2.test.

   138    138     set rc [catch {db backup} res]
   139    139     lappend rc $res
   140    140   } {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}}
   141    141   
   142    142   # Try to restore from an unreadable file.
   143    143   #
   144    144   if {$tcl_platform(platform)=="windows"} {
   145         -  do_test backup2-10 {
   146         -    forcedelete bu3.db
   147         -    file mkdir bu3.db
   148         -    set rc [catch {db restore temp bu3.db} res]
   149         -    lappend rc $res
   150         -  } {1 {cannot open source database: unable to open database file}}
          145  +  set msg {cannot open source database: unable to open database file}
          146  +} elseif {$tcl_platform(os)=="OpenBSD"} {
          147  +  set msg {restore failed: file is encrypted or is not a database}
          148  +} else {
          149  +  set msg {cannot open source database: disk I/O error}
   151    150   }
   152         -if {$tcl_platform(platform)!="windows"} {
   153         -  do_test backup2-10 {
   154         -    forcedelete bu3.db
   155         -    file mkdir bu3.db
   156         -    set rc [catch {db restore temp bu3.db} res]
   157         -    lappend rc $res
   158         -  } {1 {cannot open source database: disk I/O error}}
   159         -}
          151  +do_test backup2-10 {
          152  +  forcedelete bu3.db
          153  +  file mkdir bu3.db
          154  +  set rc [catch {db restore temp bu3.db} res]
          155  +  lappend rc $res
          156  +} [list 1 $msg]
   160    157   
   161    158   # Try to restore from something that is not a database file.
   162    159   #
   163    160   do_test backup2-11 {
   164    161     set rc [catch {db restore temp bu2.db} res]
   165    162     lappend rc $res
   166    163   } {1 {restore failed: file is encrypted or is not a database}}

Added test/corruptF.test.

            1  +# 2012 January 12
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +set testprefix corruptF
           16  +
           17  +# Do not use a codec for tests in this file, as the database file is
           18  +# manipulated directly using tcl scripts (using the [hexio_write] command).
           19  +#
           20  +do_not_use_codec
           21  +
           22  +proc str {i} { format %08d $i }
           23  +
           24  +# Create a 6 page database containing a single table - t1. Table t1 
           25  +# consists of page 2 (the root page) and pages 5 and 6 (leaf pages). 
           26  +# Database pages 3 and 4 are on the free list.
           27  +#
           28  +proc create_test_db {} {
           29  +  catch { db close }
           30  +  forcedelete test.db
           31  +  sqlite3 db test.db
           32  +  db func str str
           33  +  execsql {
           34  +    PRAGMA auto_vacuum = 0;
           35  +    PRAGMA page_size = 1024;
           36  +    CREATE TABLE t1(x);         /* root page = 2 */
           37  +    CREATE TABLE t2(x);         /* root page = 3 */
           38  +    CREATE TABLE t3(x);         /* root page = 4 */
           39  +
           40  +    INSERT INTO t1 VALUES(str(1));
           41  +    INSERT INTO t1 SELECT str(rowid+1) FROM t1;
           42  +    INSERT INTO t1 SELECT str(rowid+2) FROM t1;
           43  +    INSERT INTO t1 SELECT str(rowid+4) FROM t1;
           44  +    INSERT INTO t1 SELECT str(rowid+8) FROM t1;
           45  +    INSERT INTO t1 SELECT str(rowid+16) FROM t1;
           46  +    INSERT INTO t1 SELECT str(rowid+32) FROM t1;
           47  +    INSERT INTO t1 SELECT str(rowid+64) FROM t1;
           48  +    DROP TABLE t2;
           49  +    DROP TABLE t3;
           50  +  }
           51  +  db close
           52  +}
           53  +
           54  +do_test 1.1 { create_test_db } {}
           55  +
           56  +# Check the db is as we expect. 6 pages in total, with 3 and 4 on the free
           57  +# list. Page 3 is the free list trunk and page 4 is a leaf.
           58  +#
           59  +do_test 1.2 { file size test.db } [expr 6*1024]
           60  +do_test 1.3 { hexio_read test.db 32 4 } 00000003
           61  +do_test 1.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004
           62  +
           63  +# Change the free-list entry to page 6 and reopen the db file.
           64  +do_test 1.5 { 
           65  +  hexio_write test.db [expr 2*1024 + 8] 00000006 
           66  +  sqlite3 db test.db
           67  +} {}
           68  +
           69  +# Now create a new table in the database file. The root of the new table
           70  +# is page 6, which is also the right-most leaf page in table t1.
           71  +#
           72  +do_execsql_test 1.6 { 
           73  +  CREATE TABLE t4(x);
           74  +  SELECT * FROM sqlite_master;
           75  +} {
           76  +  table t1 t1 2 {CREATE TABLE t1(x)} 
           77  +  table t4 t4 6 {CREATE TABLE t4(x)}
           78  +}
           79  +
           80  +# At one point this was causing an assert to fail.
           81  +#
           82  +# This statement opens a cursor on table t1 and does a full table scan. As
           83  +# each row is visited, it is copied into table t4. There is no temporary
           84  +# table.
           85  +#
           86  +# When the t1 cursor reaches page 6 (which is both the right-most leaf of
           87  +# t1 and the root of t4), it continues to iterate through the keys within
           88  +# it (which at this point are keys that have been inserted into t4). And
           89  +# for each row visited, another row is inserted into page 6 - it being the
           90  +# root page of t4. Eventually, page 6 becomes full and the height of the
           91  +# b-tree for table t4 increased. From the point of view of the t1 cursor,
           92  +# this unexpectedly reduces the number of keys on page 6 in the middle of
           93  +# its iteration, which causes an assert() to fail.
           94  +#
           95  +db_save_and_close
           96  +if 1 {
           97  +for {set i 0} {$i < 128} {incr i} {
           98  +  db_restore_and_reopen
           99  +  do_test 1.7.$i { 
          100  +    set res [
          101  +      catchsql { INSERT INTO t4 SELECT x FROM t1 WHERE rowid>$i }
          102  +    ]
          103  +    if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} {
          104  +      set res ""
          105  +    }
          106  +    set res
          107  +  } {}
          108  +}
          109  +}
          110  +
          111  +do_test 2.1 { create_test_db } {}
          112  +do_test 2.2 { file size test.db } [expr 6*1024]
          113  +do_test 2.3 { hexio_read test.db 32 4 } 00000003
          114  +do_test 2.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004
          115  +
          116  +# Change the free-list entry to page 5 and reopen the db file.
          117  +do_test 2.5 { 
          118  +  hexio_write test.db [expr 2*1024 + 8] 00000005 
          119  +  sqlite3 db test.db
          120  +} {}
          121  +
          122  +# Now create a new table in the database file. The root of the new table
          123  +# is page 5, which is also the right-most leaf page in table t1.
          124  +#
          125  +do_execsql_test 2.6 { 
          126  +  CREATE TABLE t4(x);
          127  +  SELECT * FROM sqlite_master;
          128  +} {
          129  +  table t1 t1 2 {CREATE TABLE t1(x)} 
          130  +  table t4 t4 5 {CREATE TABLE t4(x)}
          131  +}
          132  +
          133  +db_save_and_close
          134  +for {set i 127} {$i >= 0} {incr i -1} {
          135  +  db_restore_and_reopen
          136  +  do_test 2.7.$i { 
          137  +    set res [
          138  +      catchsql { 
          139  +        INSERT INTO t4 SELECT x FROM t1 WHERE rowid<$i ORDER BY rowid DESC 
          140  +      }
          141  +    ]
          142  +    if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} {
          143  +      set res ""
          144  +    }
          145  +    set res
          146  +  } {}
          147  +}
          148  +
          149  +finish_test
          150  +

Changes to test/malloc5.test.

   348    348     } db2
   349    349     execsql { SELECT * FROM abc } db
   350    350     expr [nPage db] + [nPage db2]
   351    351   } {20}
   352    352   do_test malloc5-6.3.2 {
   353    353     # Try to release 7700 bytes. This should release all the 
   354    354     # non-dirty pages held by db2.
   355         -  sqlite3_release_memory [expr 7*1100]
          355  +  sqlite3_release_memory [expr 7*1132]
   356    356     list [nPage db] [nPage db2]
   357    357   } {10 3}
   358    358   do_test malloc5-6.3.3 {
   359    359     # Try to release another 1000 bytes. This should come fromt the db
   360    360     # cache, since all three pages held by db2 are either in-use or diry.
   361    361     sqlite3_release_memory 1000
   362    362     list [nPage db] [nPage db2]
   363    363   } {9 3}
   364    364   do_test malloc5-6.3.4 {
   365    365     # Now release 9900 more (about 9 pages worth). This should expunge
   366    366     # the rest of the db cache. But the db2 cache remains intact, because
   367    367     # SQLite tries to avoid calling sync().
   368    368     if {$::tcl_platform(wordSize)==8} {
   369         -    sqlite3_release_memory 10177
          369  +    sqlite3_release_memory 10500
   370    370     } else {
   371    371       sqlite3_release_memory 9900
   372    372     }
   373    373     list [nPage db] [nPage db2]
   374    374   } {0 3}
   375    375   do_test malloc5-6.3.5 {
   376    376     # But if we are really insistent, SQLite will consent to call sync()

Changes to test/multiplex.test.

   158    158   #   multiplex-2.7.*: Disable/enable tests.
   159    159   #
   160    160   
   161    161   sqlite3_multiplex_initialize "" 1
   162    162   multiplex_set db main 32768 16
   163    163   
   164    164   forcedelete test.x
          165  +foreach f [glob -nocomplain {test.x*[0-9][0-9][0-9]}] {
          166  +  forcedelete $f
          167  +}
   165    168   do_test multiplex-2.1.2 {
   166    169     sqlite3 db test.x
   167    170     execsql {
   168    171       PRAGMA page_size=1024;
   169    172       PRAGMA auto_vacuum=OFF;
   170    173       PRAGMA journal_mode=DELETE;
   171    174     }
................................................................................
   194    197   do_test multiplex-2.4.1 {
   195    198     sqlite3_multiplex_shutdown
   196    199   } {SQLITE_MISUSE}
   197    200   do_test multiplex-2.4.2 {
   198    201     execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
   199    202   } {}
   200    203   do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
   201         -do_test multiplex-2.4.99 {
          204  +do_test multiplex-2.4.5 {
          205  +  db close
          206  +  sqlite3 db test.x
          207  +  db eval vacuum
   202    208     db close
          209  +  glob test.x*
          210  +} {test.x}
          211  +do_test multiplex-2.4.99 {
   203    212     sqlite3_multiplex_shutdown
   204    213   } {SQLITE_OK}
   205         -
   206    214   
   207    215   do_test multiplex-2.5.1 {
   208    216     multiplex_delete test.x
   209    217     sqlite3_multiplex_initialize "" 1
   210    218     sqlite3 db test.x
   211    219     multiplex_set db main 4096 16
   212    220   } {SQLITE_OK}

Changes to test/permutations.test.

   138    138     test_set $allquicktests -exclude *malloc* *ioerr* *fault*
   139    139   ]
   140    140   
   141    141   test_suite "valgrind" -prefix "" -description {
   142    142     Run the "veryquick" test suite with a couple of multi-process tests (that
   143    143     fail under valgrind) omitted.
   144    144   } -files [
   145         -  test_set $allquicktests -exclude *malloc* *ioerr* *fault*
          145  +  test_set $allquicktests -exclude *malloc* *ioerr* *fault* wal.test
   146    146   ] -initialize {
   147    147     set ::G(valgrind) 1
   148    148   } -shutdown {
   149    149     unset -nocomplain ::G(valgrind)
   150    150   }
   151    151   
   152    152   test_suite "quick" -prefix "" -description {
................................................................................
   525    525     journal3.test 8_3_names.test
   526    526   
   527    527     pager1.test async4.test corrupt.test filefmt.test pager2.test
   528    528     corrupt5.test corruptA.test pageropt.test
   529    529   
   530    530     # Exclude stmt.test, which expects sub-journals to use temporary files.
   531    531     stmt.test
          532  +
          533  +  zerodamage.test
   532    534   
   533    535     # WAL mode is different.
   534    536     wal* tkt-2d1a5c67d.test backcompat.test
   535    537   }]
   536    538   
   537    539   ifcapable mem3 {
   538    540     test_suite "memsys3" -description {

Changes to test/shrink.test.

    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   unset -nocomplain baseline
    20     20   do_test shrink-1.1 {
    21     21     db eval {
           22  +    PRAGMA cache_size = 2000;
    22     23       CREATE TABLE t1(x,y);
    23     24       INSERT INTO t1 VALUES(randomblob(1000000),1);
    24     25     }
    25     26     set ::baseline sqlite3_memory_used
    26     27     sqlite3_db_release_memory db
    27     28     expr {$::baseline > [sqlite3_memory_used]+500000}
    28     29   } {1}

Changes to test/tclsqlite.test.

    21     21   source $testdir/tester.tcl
    22     22   
    23     23   # Check the error messages generated by tclsqlite
    24     24   #
    25     25   if {[sqlite3 -has-codec]} {
    26     26     set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?"
    27     27   } else {
    28         -  set r "sqlite_orig HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?"
           28  +  set r "sqlite_orig HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
    29     29   }
    30     30   do_test tcl-1.1 {
    31     31     set v [catch {sqlite3 bogus} msg]
    32     32     regsub {really_sqlite3} $msg {sqlite3} msg
    33     33     lappend v $msg
    34     34   } [list 1 "wrong # args: should be \"$r\""]
    35     35   do_test tcl-1.2 {

Changes to test/unixexcl.test.

    81     81   }
    82     82   
    83     83   do_multiclient_test tn {
    84     84     do_test unixexcl-3.$tn.1 {
    85     85       code1 { db close; sqlite3 db file:test.db?psow=0 -vfs unix-excl -uri 1 }
    86     86       code2 { db2 close; sqlite3 db2 file:test.db?psow=0 -vfs unix-excl -uri 1 }
    87     87       sql1 {
           88  +      PRAGMA auto_vacuum = 0;
    88     89         PRAGMA journal_mode = WAL;
    89     90         CREATE TABLE t1(a, b);
    90     91         INSERT INTO t1 VALUES(1, 2);
    91     92       }
    92     93     } {wal}
    93     94   
    94     95     if {$tn==1} {

Changes to test/walcrash.test.

    76     76   }
    77     77   
    78     78   # walcrash-2.*
    79     79   #
    80     80   for {set i 1} {$i < $REPEATS} {incr i} {
    81     81     forcedelete test.db test.db-wal
    82     82     do_test walcrash-2.$i.1 {
    83         -    crashsql -delay 4 -file test.db-wal -seed [incr seed] {
           83  +    crashsql -delay 5 -file test.db-wal -seed [incr seed] {
    84     84         PRAGMA journal_mode = WAL;
    85     85         CREATE TABLE t1(a PRIMARY KEY, b);
    86     86         INSERT INTO t1 VALUES(1, 2);
    87     87         INSERT INTO t1 VALUES(3, 4);
    88     88         INSERT INTO t1 VALUES(5, 9);
    89     89       }
    90     90     } {1 {child process exited abnormally}}
................................................................................
   147    147   # walcrash-4.*
   148    148   #
   149    149   for {set i 1} {$i < $REPEATS} {incr i} {
   150    150     forcedelete test.db test.db-wal
   151    151     forcedelete test2.db test2.db-wal
   152    152   
   153    153     do_test walcrash-4.$i.1 {
   154         -    crashsql -delay 3 -file test.db-wal -seed [incr seed] -blocksize 4096 {
          154  +    crashsql -delay 4 -file test.db-wal -seed [incr seed] -blocksize 4096 {
   155    155         PRAGMA journal_mode = WAL;
   156    156         PRAGMA page_size = 1024;
   157    157         CREATE TABLE t1(a PRIMARY KEY, b);
   158    158         INSERT INTO t1 VALUES(1, 2);
   159    159         INSERT INTO t1 VALUES(3, 4);
   160    160       }
   161    161     } {1 {child process exited abnormally}}
................................................................................
   175    175   # walcrash-5.*
   176    176   #
   177    177   for {set i 1} {$i < $REPEATS} {incr i} {
   178    178     forcedelete test.db test.db-wal
   179    179     forcedelete test2.db test2.db-wal
   180    180   
   181    181     do_test walcrash-5.$i.1 {
   182         -    crashsql -delay 11 -file test.db-wal -seed [incr seed] -blocksize 4096 {
          182  +    crashsql -delay 13 -file test.db-wal -seed [incr seed] -blocksize 4096 {
   183    183         PRAGMA journal_mode = WAL;
   184    184         PRAGMA page_size = 1024;
   185    185         BEGIN;
   186    186           CREATE TABLE t1(x PRIMARY KEY);
   187    187           INSERT INTO t1 VALUES(randomblob(900));
   188    188           INSERT INTO t1 VALUES(randomblob(900));
   189    189           INSERT INTO t1 SELECT randomblob(900) FROM t1;           /* 4 */
................................................................................
   216    216   # walcrash-6.*
   217    217   #
   218    218   for {set i 1} {$i < $REPEATS} {incr i} {
   219    219     forcedelete test.db test.db-wal
   220    220     forcedelete test2.db test2.db-wal
   221    221   
   222    222     do_test walcrash-6.$i.1 {
   223         -    crashsql -delay 12 -file test.db-wal -seed [incr seed] -blocksize 512 {
          223  +    crashsql -delay 14 -file test.db-wal -seed [incr seed] -blocksize 512 {
   224    224         PRAGMA journal_mode = WAL;
   225    225         PRAGMA page_size = 1024;
   226    226         BEGIN;
   227    227           CREATE TABLE t1(x PRIMARY KEY);
   228    228           INSERT INTO t1 VALUES(randomblob(900));
   229    229           INSERT INTO t1 VALUES(randomblob(900));
   230    230           INSERT INTO t1 SELECT randomblob(900) FROM t1;           /* 4 */
................................................................................
   234    234         INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4;   /* 16 */
   235    235         INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4;   /* 20 */
   236    236         INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4;   /* 24 */
   237    237         INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4;   /* 28 */
   238    238         INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4;   /* 32 */
   239    239   
   240    240         PRAGMA wal_checkpoint;
   241         -      INSERT INTO t1 VALUES(randomblob(900));
   242         -      INSERT INTO t1 VALUES(randomblob(900));
   243         -      INSERT INTO t1 VALUES(randomblob(900));
          241  +      INSERT INTO t1 VALUES(randomblob(9000));
          242  +      INSERT INTO t1 VALUES(randomblob(9000));
          243  +      INSERT INTO t1 VALUES(randomblob(9000));
   244    244       }
   245    245     } {1 {child process exited abnormally}}
   246    246   
   247    247     do_test walcrash-6.$i.2 {
   248    248       sqlite3 db test.db
   249    249       execsql { SELECT count(*)==34 OR count(*)==35 FROM t1 WHERE x != 1 }
   250    250     } {1}

Changes to test/walfault.test.

   123    123       PRAGMA wal_checkpoint;
   124    124     }
   125    125     set {} {}
   126    126   } -test {
   127    127     faultsim_test_result {0 {}}
   128    128   }
   129    129   
   130         -
   131    130   #--------------------------------------------------------------------------
   132    131   #
   133    132   if {[permutation] != "inmemory_journal"} {
   134    133     faultsim_delete_and_reopen
   135    134     faultsim_save_and_close
   136    135     do_faultsim_test walfault-4 -prep {
   137    136       faultsim_restore_and_reopen
................................................................................
   141    140         PRAGMA journal_mode = WAL;
   142    141         CREATE TABLE t1(a PRIMARY KEY, b);
   143    142         INSERT INTO t1 VALUES('a', 'b');
   144    143         PRAGMA wal_checkpoint;
   145    144         SELECT * FROM t1;
   146    145       }
   147    146     } -test {
   148         -    faultsim_test_result {0 {wal 0 7 7 a b}}
          147  +    # Update: The following changed from {0 {wal 0 7 7 a b}} as a result
          148  +    # of PSOW being set by default.
          149  +    faultsim_test_result {0 {wal 0 5 5 a b}}
   149    150       faultsim_integrity_check
   150    151     } 
   151    152   }
   152    153   
   153    154   #--------------------------------------------------------------------------
   154    155   #
   155    156   do_test walfault-5-pre-1 {
................................................................................
   542    543     faultsim_restore_and_reopen
   543    544   } -body {
   544    545     db eval { 
   545    546       PRAGMA wal_checkpoint = full;
   546    547       INSERT INTO abc VALUES(randomblob(1500));
   547    548     }
   548    549   } -test {
   549         -  faultsim_test_result {0 {0 10 10}}
          550  +  faultsim_test_result {0 {0 9 9}}
   550    551     faultsim_integrity_check
   551    552     set nRow [db eval {SELECT count(*) FROM abc}]
   552    553     if {!(($nRow==2 && $testrc) || $nRow==3)} { error "Bad db content" }
   553    554   }
          555  +finish_test
   554    556   
   555    557   finish_test

Changes to test/zerodamage.test.

    15     15   # The name of this file comes from the fact that we used to call the
    16     16   # POWERSAFE_OVERWRITE property ZERO_DAMAGE.
    17     17   #
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21     21   set testprefix wal5
           22  +
           23  +ifcapable !vtab {
           24  +  finish_test
           25  +  return
           26  +}
    22     27   
    23     28   # POWERSAFE_OVERWRITE defaults to true
    24     29   #
    25     30   do_test zerodamage-1.0 {
    26     31     file_control_powersafe_overwrite db -1
    27     32   } {0 1}
    28     33   
................................................................................
   106    111     db close
   107    112     sqlite3 db file:test.db?psow=FALSE -uri 1
   108    113     db eval {
   109    114        UPDATE t1 SET y=randomblob(50) WHERE x=124;
   110    115     }
   111    116     file size test.db-wal
   112    117   } {8416}
          118  +
          119  +finish_test

Changes to tool/lemon.c.

   113    113   void CompressTables(struct lemon *);
   114    114   void ResortStates(struct lemon *);
   115    115   
   116    116   /********** From the file "set.h" ****************************************/
   117    117   void  SetSize(int);             /* All sets will be of size N */
   118    118   char *SetNew(void);               /* A new set for element 0..N */
   119    119   void  SetFree(char*);             /* Deallocate a set */
   120         -
   121         -char *SetNew(void);               /* A new set for element 0..N */
   122    120   int SetAdd(char*,int);            /* Add element to a set */
   123    121   int SetUnion(char *,char *);    /* A <- A U B, thru element N */
   124    122   #define SetFind(X,Y) (X[Y])       /* True if Y is in set X */
   125    123   
   126    124   /********** From the file "struct.h" *************************************/
   127    125   /*
   128    126   ** Principal data structures for the LEMON parser generator.
................................................................................
   682    680   
   683    681     /* First compute all lambdas */
   684    682     do{
   685    683       progress = 0;
   686    684       for(rp=lemp->rule; rp; rp=rp->next){
   687    685         if( rp->lhs->lambda ) continue;
   688    686         for(i=0; i<rp->nrhs; i++){
   689         -         struct symbol *sp = rp->rhs[i];
   690         -         if( sp->type!=TERMINAL || sp->lambda==LEMON_FALSE ) break;
          687  +        struct symbol *sp = rp->rhs[i];
          688  +        assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
          689  +        if( sp->lambda==LEMON_FALSE ) break;
   691    690         }
   692    691         if( i==rp->nrhs ){
   693    692           rp->lhs->lambda = LEMON_TRUE;
   694    693           progress = 1;
   695    694         }
   696    695       }
   697    696     }while( progress );
................................................................................
   964    963   	}
   965    964           cfp->status = COMPLETE;
   966    965         }
   967    966       }
   968    967     }while( progress );
   969    968   }
   970    969   
   971         -static int resolve_conflict(struct action *,struct action *, struct symbol *);
          970  +static int resolve_conflict(struct action *,struct action *);
   972    971   
   973    972   /* Compute the reduce actions, and resolve conflicts.
   974    973   */
   975    974   void FindActions(struct lemon *lemp)
   976    975   {
   977    976     int i,j;
   978    977     struct config *cfp;
................................................................................
  1018   1017       stp = lemp->sorted[i];
  1019   1018       /* assert( stp->ap ); */
  1020   1019       stp->ap = Action_sort(stp->ap);
  1021   1020       for(ap=stp->ap; ap && ap->next; ap=ap->next){
  1022   1021         for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
  1023   1022            /* The two actions "ap" and "nap" have the same lookahead.
  1024   1023            ** Figure out which one should be used */
  1025         -         lemp->nconflict += resolve_conflict(ap,nap,lemp->errsym);
         1024  +         lemp->nconflict += resolve_conflict(ap,nap);
  1026   1025         }
  1027   1026       }
  1028   1027     }
  1029   1028   
  1030   1029     /* Report an error for each rule that can never be reduced. */
  1031   1030     for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
  1032   1031     for(i=0; i<lemp->nstate; i++){
................................................................................
  1053   1052   **   use precedence to resolve the conflict.
  1054   1053   **
  1055   1054   ** If either action is a SHIFT, then it must be apx.  This
  1056   1055   ** function won't work if apx->type==REDUCE and apy->type==SHIFT.
  1057   1056   */
  1058   1057   static int resolve_conflict(
  1059   1058     struct action *apx,
  1060         -  struct action *apy,
  1061         -  struct symbol *errsym   /* The error symbol (if defined.  NULL otherwise) */
         1059  +  struct action *apy
  1062   1060   ){
  1063   1061     struct symbol *spx, *spy;
  1064   1062     int errcnt = 0;
  1065   1063     assert( apx->sp==apy->sp );  /* Otherwise there would be no conflict */
  1066   1064     if( apx->type==SHIFT && apy->type==SHIFT ){
  1067   1065       apy->type = SSCONFLICT;
  1068   1066       errcnt++;
................................................................................
  1991   1989           psp->lhs = Symbol_new(x);
  1992   1990           psp->nrhs = 0;
  1993   1991           psp->lhsalias = 0;
  1994   1992           psp->state = WAITING_FOR_ARROW;
  1995   1993         }else if( x[0]=='{' ){
  1996   1994           if( psp->prevrule==0 ){
  1997   1995             ErrorMsg(psp->filename,psp->tokenlineno,
  1998         -"There is no prior rule opon which to attach the code \
         1996  +"There is no prior rule upon which to attach the code \
  1999   1997   fragment which begins on this line.");
  2000   1998             psp->errorcnt++;
  2001   1999   	}else if( psp->prevrule->code!=0 ){
  2002   2000             ErrorMsg(psp->filename,psp->tokenlineno,
  2003   2001   "Code fragment beginning on this line is not the first \
  2004   2002   to follow the previous rule.");
  2005   2003             psp->errorcnt++;