/ Check-in [32477642]
Login

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

Overview
Comment:Merge in all recent preformance enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 32477642d79615fb85680bdac812ad9655cf6902
User & Date: drh 2013-12-14 18:24:46
Context
2013-12-24
12:09
Merge the latest trunk changes into the sessions branch. check-in: cfd110bf user: drh tags: sessions
2013-12-14
18:24
Merge in all recent preformance enhancements from trunk. check-in: 32477642 user: drh tags: sessions
13:44
Allow the SQLITE_DETERMINISTIC flag to be ORed into the preferred text encoding of application-defined functions, to mark the function as deterministic. check-in: 5716fc23 user: drh tags: trunk
2013-12-06
15:49
Update to the 3.8.2 release. check-in: e579661a user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   539    539   # all that automatic generation.
   540    540   #
   541    541   .target_source:	$(SRC) $(TOP)/tool/vdbe-compress.tcl
   542    542   	rm -rf tsrc
   543    543   	mkdir tsrc
   544    544   	cp -f $(SRC) tsrc
   545    545   	rm tsrc/sqlite.h.in tsrc/parse.y
   546         -	$(TCLSH_CMD) $(TOP)/tool/vdbe-compress.tcl <tsrc/vdbe.c >vdbe.new
          546  +	$(TCLSH_CMD) $(TOP)/tool/vdbe-compress.tcl $(OPTS) <tsrc/vdbe.c >vdbe.new
   547    547   	mv vdbe.new tsrc/vdbe.c
   548    548   	touch .target_source
   549    549   
   550    550   sqlite3.c:	.target_source $(TOP)/tool/mksqlite3c.tcl
   551    551   	$(TCLSH_CMD) $(TOP)/tool/mksqlite3c.tcl
   552    552   	cp tsrc/shell.c tsrc/sqlite3ext.h .
   553    553   	cp $(TOP)/ext/session/sqlite3session.h .

Changes to Makefile.msc.

   926    926   # all that automatic generation.
   927    927   #
   928    928   .target_source:	$(SRC) $(TOP)\tool\vdbe-compress.tcl
   929    929   	-rmdir /S/Q tsrc
   930    930   	-mkdir tsrc
   931    931   	for %i in ($(SRC)) do copy /Y %i tsrc
   932    932   	del /Q tsrc\sqlite.h.in tsrc\parse.y
   933         -	$(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl < tsrc\vdbe.c > vdbe.new
          933  +	$(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl $(OPTS) < tsrc\vdbe.c > vdbe.new
   934    934   	move vdbe.new tsrc\vdbe.c
   935    935   	echo > .target_source
   936    936   
   937    937   sqlite3.c:	.target_source $(TOP)\tool\mksqlite3c.tcl
   938    938   	$(TCLSH_CMD) $(TOP)\tool\mksqlite3c.tcl
   939    939   	copy tsrc\shell.c .
   940    940   	copy tsrc\sqlite3ext.h .

Changes to VERSION.

     1         -3.8.2
            1  +3.8.3

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.62 for sqlite 3.8.2.
            3  +# Generated by GNU Autoconf 2.62 for sqlite 3.8.3.
     4      4   #
     5      5   # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
     6      6   # 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
     7      7   # This configure script is free software; the Free Software Foundation
     8      8   # gives unlimited permission to copy, distribute and modify it.
     9      9   ## --------------------- ##
    10     10   ## M4sh Initialization.  ##
................................................................................
   739    739   MFLAGS=
   740    740   MAKEFLAGS=
   741    741   SHELL=${CONFIG_SHELL-/bin/sh}
   742    742   
   743    743   # Identity of this package.
   744    744   PACKAGE_NAME='sqlite'
   745    745   PACKAGE_TARNAME='sqlite'
   746         -PACKAGE_VERSION='3.8.2'
   747         -PACKAGE_STRING='sqlite 3.8.2'
          746  +PACKAGE_VERSION='3.8.3'
          747  +PACKAGE_STRING='sqlite 3.8.3'
   748    748   PACKAGE_BUGREPORT=''
   749    749   
   750    750   # Factoring default headers for most tests.
   751    751   ac_includes_default="\
   752    752   #include <stdio.h>
   753    753   #ifdef HAVE_SYS_TYPES_H
   754    754   # include <sys/types.h>
................................................................................
  1479   1479   #
  1480   1480   # Report the --help message.
  1481   1481   #
  1482   1482   if test "$ac_init_help" = "long"; then
  1483   1483     # Omit some internal or obsolete options to make the list less imposing.
  1484   1484     # This message is too long to be a string in the A/UX 3.1 sh.
  1485   1485     cat <<_ACEOF
  1486         -\`configure' configures sqlite 3.8.2 to adapt to many kinds of systems.
         1486  +\`configure' configures sqlite 3.8.3 to adapt to many kinds of systems.
  1487   1487   
  1488   1488   Usage: $0 [OPTION]... [VAR=VALUE]...
  1489   1489   
  1490   1490   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1491   1491   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1492   1492   
  1493   1493   Defaults for the options are specified in brackets.
................................................................................
  1544   1544     --build=BUILD     configure for building on BUILD [guessed]
  1545   1545     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1546   1546   _ACEOF
  1547   1547   fi
  1548   1548   
  1549   1549   if test -n "$ac_init_help"; then
  1550   1550     case $ac_init_help in
  1551         -     short | recursive ) echo "Configuration of sqlite 3.8.2:";;
         1551  +     short | recursive ) echo "Configuration of sqlite 3.8.3:";;
  1552   1552      esac
  1553   1553     cat <<\_ACEOF
  1554   1554   
  1555   1555   Optional Features:
  1556   1556     --disable-option-checking  ignore unrecognized --enable/--with options
  1557   1557     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1558   1558     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1660   1660       cd "$ac_pwd" || { ac_status=$?; break; }
  1661   1661     done
  1662   1662   fi
  1663   1663   
  1664   1664   test -n "$ac_init_help" && exit $ac_status
  1665   1665   if $ac_init_version; then
  1666   1666     cat <<\_ACEOF
  1667         -sqlite configure 3.8.2
         1667  +sqlite configure 3.8.3
  1668   1668   generated by GNU Autoconf 2.62
  1669   1669   
  1670   1670   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  1671   1671   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  1672   1672   This configure script is free software; the Free Software Foundation
  1673   1673   gives unlimited permission to copy, distribute and modify it.
  1674   1674   _ACEOF
  1675   1675     exit
  1676   1676   fi
  1677   1677   cat >config.log <<_ACEOF
  1678   1678   This file contains any messages produced by compilers while
  1679   1679   running configure, to aid debugging if configure makes a mistake.
  1680   1680   
  1681         -It was created by sqlite $as_me 3.8.2, which was
         1681  +It was created by sqlite $as_me 3.8.3, which was
  1682   1682   generated by GNU Autoconf 2.62.  Invocation command line was
  1683   1683   
  1684   1684     $ $0 $@
  1685   1685   
  1686   1686   _ACEOF
  1687   1687   exec 5>>config.log
  1688   1688   {
................................................................................
 14017  14017   
 14018  14018   exec 6>&1
 14019  14019   
 14020  14020   # Save the log message, to keep $[0] and so on meaningful, and to
 14021  14021   # report actual input values of CONFIG_FILES etc. instead of their
 14022  14022   # values after options handling.
 14023  14023   ac_log="
 14024         -This file was extended by sqlite $as_me 3.8.2, which was
        14024  +This file was extended by sqlite $as_me 3.8.3, which was
 14025  14025   generated by GNU Autoconf 2.62.  Invocation command line was
 14026  14026   
 14027  14027     CONFIG_FILES    = $CONFIG_FILES
 14028  14028     CONFIG_HEADERS  = $CONFIG_HEADERS
 14029  14029     CONFIG_LINKS    = $CONFIG_LINKS
 14030  14030     CONFIG_COMMANDS = $CONFIG_COMMANDS
 14031  14031     $ $0 $@
................................................................................
 14070  14070   $config_commands
 14071  14071   
 14072  14072   Report bugs to <bug-autoconf@gnu.org>."
 14073  14073   
 14074  14074   _ACEOF
 14075  14075   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 14076  14076   ac_cs_version="\\
 14077         -sqlite config.status 3.8.2
        14077  +sqlite config.status 3.8.3
 14078  14078   configured by $0, generated by GNU Autoconf 2.62,
 14079  14079     with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 14080  14080   
 14081  14081   Copyright (C) 2008 Free Software Foundation, Inc.
 14082  14082   This config.status script is free software; the Free Software Foundation
 14083  14083   gives unlimited permission to copy, distribute and modify it."
 14084  14084   

Changes to main.mk.

   404    404   # all that automatic generation.
   405    405   #
   406    406   target_source:	$(SRC) $(TOP)/tool/vdbe-compress.tcl
   407    407   	rm -rf tsrc
   408    408   	mkdir tsrc
   409    409   	cp -f $(SRC) tsrc
   410    410   	rm tsrc/sqlite.h.in tsrc/parse.y
   411         -	tclsh $(TOP)/tool/vdbe-compress.tcl <tsrc/vdbe.c >vdbe.new
          411  +	tclsh $(TOP)/tool/vdbe-compress.tcl $(OPTS) <tsrc/vdbe.c >vdbe.new
   412    412   	mv vdbe.new tsrc/vdbe.c
   413    413   	touch target_source
   414    414   
   415    415   sqlite3.c:	target_source $(TOP)/tool/mksqlite3c.tcl
   416    416   	tclsh $(TOP)/tool/mksqlite3c.tcl
   417    417   	cp tsrc/shell.c tsrc/sqlite3ext.h .
   418    418   	cp $(TOP)/ext/session/sqlite3session.h .

Changes to src/backup.c.

   522    522             }
   523    523             if( rc==SQLITE_OK ){
   524    524               rc = backupTruncateFile(pFile, iSize);
   525    525             }
   526    526   
   527    527             /* Sync the database file to disk. */
   528    528             if( rc==SQLITE_OK ){
   529         -            rc = sqlite3PagerSync(pDestPager);
          529  +            rc = sqlite3PagerSync(pDestPager, 0);
   530    530             }
   531    531           }else{
   532    532             sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
   533    533             rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
   534    534           }
   535    535       
   536    536           /* Finish committing the transaction to the destination database. */
................................................................................
   597    597     }
   598    598   
   599    599     /* If a transaction is still open on the Btree, roll it back. */
   600    600     sqlite3BtreeRollback(p->pDest, SQLITE_OK);
   601    601   
   602    602     /* Set the error code of the destination database handle. */
   603    603     rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
   604         -  sqlite3Error(p->pDestDb, rc, 0);
          604  +  if( p->pDestDb ){
          605  +    sqlite3Error(p->pDestDb, rc, 0);
   605    606   
   606         -  /* Exit the mutexes and free the backup context structure. */
   607         -  if( p->pDestDb ){
          607  +    /* Exit the mutexes and free the backup context structure. */
   608    608       sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
   609    609     }
   610    610     sqlite3BtreeLeave(p->pSrc);
   611    611     if( p->pDestDb ){
   612    612       /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
   613    613       ** call to sqlite3_backup_init() and is destroyed by a call to
   614    614       ** sqlite3_backup_finish(). */

Changes to src/btree.c.

  1648   1648     assert( sqlite3_mutex_held(pBt->mutex) );
  1649   1649     assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
  1650   1650   
  1651   1651     if( pgno>btreePagecount(pBt) ){
  1652   1652       rc = SQLITE_CORRUPT_BKPT;
  1653   1653     }else{
  1654   1654       rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
  1655         -    if( rc==SQLITE_OK ){
         1655  +    if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
  1656   1656         rc = btreeInitPage(*ppPage);
  1657   1657         if( rc!=SQLITE_OK ){
  1658   1658           releasePage(*ppPage);
  1659   1659         }
  1660   1660       }
  1661   1661     }
  1662   1662   
................................................................................
  1669   1669   ** Release a MemPage.  This should be called once for each prior
  1670   1670   ** call to btreeGetPage.
  1671   1671   */
  1672   1672   static void releasePage(MemPage *pPage){
  1673   1673     if( pPage ){
  1674   1674       assert( pPage->aData );
  1675   1675       assert( pPage->pBt );
         1676  +    assert( pPage->pDbPage!=0 );
  1676   1677       assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  1677   1678       assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
  1678   1679       assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1679         -    sqlite3PagerUnref(pPage->pDbPage);
         1680  +    sqlite3PagerUnrefNotNull(pPage->pDbPage);
  1680   1681     }
  1681   1682   }
  1682   1683   
  1683   1684   /*
  1684   1685   ** During a rollback, when the pager reloads information into the cache
  1685   1686   ** so that the cache is restored to its original state at the start of
  1686   1687   ** the transaction, for each page restored this routine is called.
................................................................................
  4188   4189     }
  4189   4190     return rc;
  4190   4191   }
  4191   4192   
  4192   4193   /*
  4193   4194   ** Return a pointer to payload information from the entry that the 
  4194   4195   ** pCur cursor is pointing to.  The pointer is to the beginning of
  4195         -** the key if skipKey==0 and it points to the beginning of data if
  4196         -** skipKey==1.  The number of bytes of available key/data is written
  4197         -** into *pAmt.  If *pAmt==0, then the value returned will not be
  4198         -** a valid pointer.
         4196  +** the key if index btrees (pPage->intKey==0) and is the data for
         4197  +** table btrees (pPage->intKey==1). The number of bytes of available
         4198  +** key/data is written into *pAmt.  If *pAmt==0, then the value
         4199  +** returned will not be a valid pointer.
  4199   4200   **
  4200   4201   ** This routine is an optimization.  It is common for the entire key
  4201   4202   ** and data to fit on the local page and for there to be no overflow
  4202   4203   ** pages.  When that is so, this routine can be used to access the
  4203   4204   ** key and data without making a copy.  If the key and/or data spills
  4204   4205   ** onto overflow pages, then accessPayload() must be used to reassemble
  4205   4206   ** the key/data and copy it into a preallocated buffer.
  4206   4207   **
  4207   4208   ** The pointer returned by this routine looks directly into the cached
  4208   4209   ** page of the database.  The data might change or move the next time
  4209   4210   ** any btree routine is called.
  4210   4211   */
  4211         -static const unsigned char *fetchPayload(
         4212  +static const void *fetchPayload(
  4212   4213     BtCursor *pCur,      /* Cursor pointing to entry to read from */
  4213         -  u32 *pAmt,           /* Write the number of available bytes here */
  4214         -  int skipKey          /* read beginning at data if this is true */
         4214  +  u32 *pAmt            /* Write the number of available bytes here */
  4215   4215   ){
  4216         -  unsigned char *aPayload;
  4217         -  MemPage *pPage;
  4218         -  u32 nKey;
  4219         -  u32 nLocal;
  4220         -
  4221   4216     assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
  4222   4217     assert( pCur->eState==CURSOR_VALID );
         4218  +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4223   4219     assert( cursorHoldsMutex(pCur) );
  4224         -  pPage = pCur->apPage[pCur->iPage];
  4225         -  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
         4220  +  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  4226   4221     if( pCur->info.nSize==0 ){
  4227   4222       btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
  4228   4223                      &pCur->info);
  4229   4224     }
  4230         -  aPayload = pCur->info.pCell;
  4231         -  aPayload += pCur->info.nHeader;
  4232         -  if( pPage->intKey ){
  4233         -    nKey = 0;
  4234         -  }else{
  4235         -    nKey = (int)pCur->info.nKey;
  4236         -  }
  4237         -  if( skipKey ){
  4238         -    aPayload += nKey;
  4239         -    nLocal = pCur->info.nLocal - nKey;
  4240         -  }else{
  4241         -    nLocal = pCur->info.nLocal;
  4242         -    assert( nLocal<=nKey );
  4243         -  }
  4244         -  *pAmt = nLocal;
  4245         -  return aPayload;
         4225  +  *pAmt = pCur->info.nLocal;
         4226  +  return (void*)(pCur->info.pCell + pCur->info.nHeader);
  4246   4227   }
  4247   4228   
  4248   4229   
  4249   4230   /*
  4250   4231   ** For the entry that cursor pCur is point to, return as
  4251   4232   ** many bytes of the key or data as are available on the local
  4252   4233   ** b-tree page.  Write the number of available bytes into *pAmt.
................................................................................
  4257   4238   ** Hence, a mutex on the BtShared should be held prior to calling
  4258   4239   ** this routine.
  4259   4240   **
  4260   4241   ** These routines is used to get quick access to key and data
  4261   4242   ** in the common case where no overflow pages are used.
  4262   4243   */
  4263   4244   const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
  4264         -  const void *p = 0;
  4265         -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4266         -  assert( cursorHoldsMutex(pCur) );
  4267         -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  4268         -    p = (const void*)fetchPayload(pCur, pAmt, 0);
  4269         -  }
  4270         -  return p;
         4245  +  return fetchPayload(pCur, pAmt);
  4271   4246   }
  4272   4247   const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
  4273         -  const void *p = 0;
  4274         -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4275         -  assert( cursorHoldsMutex(pCur) );
  4276         -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  4277         -    p = (const void*)fetchPayload(pCur, pAmt, 1);
  4278         -  }
  4279         -  return p;
         4248  +  return fetchPayload(pCur, pAmt);
  4280   4249   }
  4281   4250   
  4282   4251   
  4283   4252   /*
  4284   4253   ** Move the cursor down to a new child page.  The newPgno argument is the
  4285   4254   ** page number of the child page to move to.
  4286   4255   **
................................................................................
  4391   4360   ** indicating a table b-tree, or if the caller did specify a KeyInfo 
  4392   4361   ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
  4393   4362   ** b-tree).
  4394   4363   */
  4395   4364   static int moveToRoot(BtCursor *pCur){
  4396   4365     MemPage *pRoot;
  4397   4366     int rc = SQLITE_OK;
  4398         -  Btree *p = pCur->pBtree;
  4399         -  BtShared *pBt = p->pBt;
  4400   4367   
  4401   4368     assert( cursorHoldsMutex(pCur) );
  4402   4369     assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
  4403   4370     assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
  4404   4371     assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
  4405   4372     if( pCur->eState>=CURSOR_REQUIRESEEK ){
  4406   4373       if( pCur->eState==CURSOR_FAULT ){
................................................................................
  4407   4374         assert( pCur->skipNext!=SQLITE_OK );
  4408   4375         return pCur->skipNext;
  4409   4376       }
  4410   4377       sqlite3BtreeClearCursor(pCur);
  4411   4378     }
  4412   4379   
  4413   4380     if( pCur->iPage>=0 ){
  4414         -    int i;
  4415         -    for(i=1; i<=pCur->iPage; i++){
  4416         -      releasePage(pCur->apPage[i]);
  4417         -    }
  4418         -    pCur->iPage = 0;
         4381  +    while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
  4419   4382     }else if( pCur->pgnoRoot==0 ){
  4420   4383       pCur->eState = CURSOR_INVALID;
  4421   4384       return SQLITE_OK;
  4422   4385     }else{
  4423         -    rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0],
         4386  +    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
  4424   4387                           pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
  4425   4388       if( rc!=SQLITE_OK ){
  4426   4389         pCur->eState = CURSOR_INVALID;
  4427   4390         return rc;
  4428   4391       }
  4429   4392       pCur->iPage = 0;
  4430   4393   
................................................................................
  4449   4412     assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
  4450   4413   
  4451   4414     pCur->aiIdx[0] = 0;
  4452   4415     pCur->info.nSize = 0;
  4453   4416     pCur->atLast = 0;
  4454   4417     pCur->validNKey = 0;
  4455   4418   
  4456         -  if( pRoot->nCell==0 && !pRoot->leaf ){
         4419  +  if( pRoot->nCell>0 ){
         4420  +    pCur->eState = CURSOR_VALID;
         4421  +  }else if( !pRoot->leaf ){
  4457   4422       Pgno subpage;
  4458   4423       if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
  4459   4424       subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
  4460   4425       pCur->eState = CURSOR_VALID;
  4461   4426       rc = moveToChild(pCur, subpage);
  4462   4427     }else{
  4463         -    pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
         4428  +    pCur->eState = CURSOR_INVALID;
  4464   4429     }
  4465   4430     return rc;
  4466   4431   }
  4467   4432   
  4468   4433   /*
  4469   4434   ** Move the cursor down to the left-most leaf entry beneath the
  4470   4435   ** entry to which it is currently pointing.
................................................................................
  4712   4677           ** page is less than 16384 bytes and may be stored as a 2-byte
  4713   4678           ** varint. This information is used to attempt to avoid parsing 
  4714   4679           ** the entire cell by checking for the cases where the record is 
  4715   4680           ** stored entirely within the b-tree page by inspecting the first 
  4716   4681           ** 2 bytes of the cell.
  4717   4682           */
  4718   4683           nCell = pCell[0];
  4719         -        if( nCell<=pPage->max1bytePayload
  4720         -         /* && (pCell+nCell)<pPage->aDataEnd */
  4721         -        ){
         4684  +        if( nCell<=pPage->max1bytePayload ){
  4722   4685             /* This branch runs if the record-size field of the cell is a
  4723   4686             ** single byte varint and the record fits entirely on the main
  4724   4687             ** b-tree page.  */
  4725   4688             testcase( pCell+nCell+1==pPage->aDataEnd );
  4726   4689             c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
  4727   4690           }else if( !(pCell[1] & 0x80) 
  4728   4691             && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
  4729         -          /* && (pCell+nCell+2)<=pPage->aDataEnd */
  4730   4692           ){
  4731   4693             /* The record-size field is a 2 byte varint and the record 
  4732   4694             ** fits entirely on the main b-tree page.  */
  4733   4695             testcase( pCell+nCell+2==pPage->aDataEnd );
  4734   4696             c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
  4735   4697           }else{
  4736   4698             /* The record flows over onto one or more overflow pages. In
................................................................................
  5546   5508   
  5547   5509     /* Fill in the header. */
  5548   5510     nHeader = 0;
  5549   5511     if( !pPage->leaf ){
  5550   5512       nHeader += 4;
  5551   5513     }
  5552   5514     if( pPage->hasData ){
  5553         -    nHeader += putVarint(&pCell[nHeader], nData+nZero);
         5515  +    nHeader += putVarint32(&pCell[nHeader], nData+nZero);
  5554   5516     }else{
  5555   5517       nData = nZero = 0;
  5556   5518     }
  5557   5519     nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
  5558   5520     btreeParseCellPtr(pPage, pCell, &info);
  5559   5521     assert( info.nHeader==nHeader );
  5560   5522     assert( info.nKey==nKey );
................................................................................
  5674   5636   **
  5675   5637   ** "sz" must be the number of bytes in the cell.
  5676   5638   */
  5677   5639   static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
  5678   5640     u32 pc;         /* Offset to cell content of cell being deleted */
  5679   5641     u8 *data;       /* pPage->aData */
  5680   5642     u8 *ptr;        /* Used to move bytes around within data[] */
  5681         -  u8 *endPtr;     /* End of loop */
  5682   5643     int rc;         /* The return code */
  5683   5644     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
  5684   5645   
  5685   5646     if( *pRC ) return;
  5686   5647   
  5687   5648     assert( idx>=0 && idx<pPage->nCell );
  5688   5649     assert( sz==cellSize(pPage, idx) );
................................................................................
  5699   5660       return;
  5700   5661     }
  5701   5662     rc = freeSpace(pPage, pc, sz);
  5702   5663     if( rc ){
  5703   5664       *pRC = rc;
  5704   5665       return;
  5705   5666     }
  5706         -  endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
  5707         -  assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
  5708         -  while( ptr<endPtr ){
  5709         -    *(u16*)ptr = *(u16*)&ptr[2];
  5710         -    ptr += 2;
  5711         -  }
  5712   5667     pPage->nCell--;
         5668  +  memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
  5713   5669     put2byte(&data[hdr+3], pPage->nCell);
  5714   5670     pPage->nFree += 2;
  5715   5671   }
  5716   5672   
  5717   5673   /*
  5718   5674   ** Insert a new cell on pPage at cell index "i".  pCell points to the
  5719   5675   ** content of the cell.
................................................................................
  5742   5698   ){
  5743   5699     int idx = 0;      /* Where to write new cell content in data[] */
  5744   5700     int j;            /* Loop counter */
  5745   5701     int end;          /* First byte past the last cell pointer in data[] */
  5746   5702     int ins;          /* Index in data[] where new cell pointer is inserted */
  5747   5703     int cellOffset;   /* Address of first cell pointer in data[] */
  5748   5704     u8 *data;         /* The content of the whole page */
  5749         -  u8 *ptr;          /* Used for moving information around in data[] */
  5750         -  u8 *endPtr;       /* End of the loop */
  5751         -
  5752   5705     int nSkip = (iChild ? 4 : 0);
  5753   5706   
  5754   5707     if( *pRC ) return;
  5755   5708   
  5756   5709     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  5757   5710     assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
  5758   5711     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
................................................................................
  5795   5748       assert( idx+sz <= (int)pPage->pBt->usableSize );
  5796   5749       pPage->nCell++;
  5797   5750       pPage->nFree -= (u16)(2 + sz);
  5798   5751       memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
  5799   5752       if( iChild ){
  5800   5753         put4byte(&data[idx], iChild);
  5801   5754       }
  5802         -    ptr = &data[end];
  5803         -    endPtr = &data[ins];
  5804         -    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
  5805         -    while( ptr>endPtr ){
  5806         -      *(u16*)ptr = *(u16*)&ptr[-2];
  5807         -      ptr -= 2;
  5808         -    }
         5755  +    memmove(&data[ins+2], &data[ins], end-ins);
  5809   5756       put2byte(&data[ins], idx);
  5810   5757       put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
  5811   5758   #ifndef SQLITE_OMIT_AUTOVACUUM
  5812   5759       if( pPage->pBt->autoVacuum ){
  5813   5760         /* The cell may contain a pointer to an overflow page. If so, write
  5814   5761         ** the entry for the overflow page into the pointer map.
  5815   5762         */
................................................................................
  7763   7710     pCheck->mxErr--;
  7764   7711     pCheck->nErr++;
  7765   7712     va_start(ap, zFormat);
  7766   7713     if( pCheck->errMsg.nChar ){
  7767   7714       sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
  7768   7715     }
  7769   7716     if( zMsg1 ){
  7770         -    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
         7717  +    sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
  7771   7718     }
  7772   7719     sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
  7773   7720     va_end(ap);
  7774   7721     if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
  7775   7722       pCheck->mallocFailed = 1;
  7776   7723     }
  7777   7724   }

Changes to src/build.c.

  3985   3985     Table *pTab = pIdx->pTable;
  3986   3986   
  3987   3987     sqlite3StrAccumInit(&errMsg, 0, 0, 200);
  3988   3988     errMsg.db = pParse->db;
  3989   3989     for(j=0; j<pIdx->nKeyCol; j++){
  3990   3990       char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
  3991   3991       if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
  3992         -    sqlite3StrAccumAppend(&errMsg, pTab->zName, -1);
         3992  +    sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
  3993   3993       sqlite3StrAccumAppend(&errMsg, ".", 1);
  3994         -    sqlite3StrAccumAppend(&errMsg, zCol, -1);
         3994  +    sqlite3StrAccumAppendAll(&errMsg, zCol);
  3995   3995     }
  3996   3996     zErr = sqlite3StrAccumFinish(&errMsg);
  3997   3997     sqlite3HaltConstraint(pParse, 
  3998   3998       (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
  3999   3999       onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
  4000   4000   }
  4001   4001   
................................................................................
  4179   4179       }else{
  4180   4180         pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
  4181   4181       }
  4182   4182       if( pKey ){
  4183   4183         assert( sqlite3KeyInfoIsWriteable(pKey) );
  4184   4184         for(i=0; i<nCol; i++){
  4185   4185           char *zColl = pIdx->azColl[i];
  4186         -        if( NEVER(zColl==0) ) zColl = "BINARY";
  4187         -        pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
         4186  +        assert( zColl!=0 );
         4187  +        pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 :
         4188  +                          sqlite3LocateCollSeq(pParse, zColl);
  4188   4189           pKey->aSortOrder[i] = pIdx->aSortOrder[i];
  4189   4190         }
  4190   4191         if( pParse->nErr ){
  4191   4192           sqlite3KeyInfoUnref(pKey);
  4192   4193         }else{
  4193   4194           pIdx->pKeyInfo = pKey;
  4194   4195         }
  4195   4196       }
  4196   4197     }
  4197   4198     return sqlite3KeyInfoRef(pIdx->pKeyInfo);
  4198   4199   }

Changes to src/callback.c.

   353    353     FuncDef *p;         /* Iterator variable */
   354    354     FuncDef *pBest = 0; /* Best match found so far */
   355    355     int bestScore = 0;  /* Score of best match */
   356    356     int h;              /* Hash value */
   357    357   
   358    358     assert( nArg>=(-2) );
   359    359     assert( nArg>=(-1) || createFlag==0 );
   360         -  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
   361    360     h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
   362    361   
   363    362     /* First search for a match amongst the application-defined functions.
   364    363     */
   365    364     p = functionSearch(&db->aFunc, h, zName, nName);
   366    365     while( p ){
   367    366       int score = matchQuality(p, nArg, enc);

Changes to src/func.c.

  1507   1507         if( argc==2 ){
  1508   1508           zSep = (char*)sqlite3_value_text(argv[1]);
  1509   1509           nSep = sqlite3_value_bytes(argv[1]);
  1510   1510         }else{
  1511   1511           zSep = ",";
  1512   1512           nSep = 1;
  1513   1513         }
  1514         -      sqlite3StrAccumAppend(pAccum, zSep, nSep);
         1514  +      if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
  1515   1515       }
  1516   1516       zVal = (char*)sqlite3_value_text(argv[0]);
  1517   1517       nVal = sqlite3_value_bytes(argv[0]);
  1518         -    sqlite3StrAccumAppend(pAccum, zVal, nVal);
         1518  +    if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
  1519   1519     }
  1520   1520   }
  1521   1521   static void groupConcatFinalize(sqlite3_context *context){
  1522   1522     StrAccum *pAccum;
  1523   1523     pAccum = sqlite3_aggregate_context(context, 0);
  1524   1524     if( pAccum ){
  1525   1525       if( pAccum->accError==STRACCUM_TOOBIG ){

Changes to src/main.c.

   584    584         db->lookaside.pFree = p;
   585    585         p = (LookasideSlot*)&((u8*)p)[sz];
   586    586       }
   587    587       db->lookaside.pEnd = p;
   588    588       db->lookaside.bEnabled = 1;
   589    589       db->lookaside.bMalloced = pBuf==0 ?1:0;
   590    590     }else{
   591         -    db->lookaside.pEnd = 0;
          591  +    db->lookaside.pStart = db;
          592  +    db->lookaside.pEnd = db;
   592    593       db->lookaside.bEnabled = 0;
   593    594       db->lookaside.bMalloced = 0;
   594    595     }
   595    596     return SQLITE_OK;
   596    597   }
   597    598   
   598    599   /*
................................................................................
   982    983       }
   983    984       sqlite3DbFree(db, pMod);
   984    985     }
   985    986     sqlite3HashClear(&db->aModule);
   986    987   #endif
   987    988   
   988    989     sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
   989         -  if( db->pErr ){
   990         -    sqlite3ValueFree(db->pErr);
   991         -  }
          990  +  sqlite3ValueFree(db->pErr);
   992    991     sqlite3CloseExtensions(db);
   993    992   
   994    993     db->magic = SQLITE_MAGIC_ERROR;
   995    994   
   996    995     /* The temp-database schema is allocated differently from the other schema
   997    996     ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
   998    997     ** So it needs to be freed here. Todo: Why not roll the temp schema into
................................................................................
  1082   1081         case SQLITE_LOCKED:             zName = "SQLITE_LOCKED";            break;
  1083   1082         case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
  1084   1083         case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
  1085   1084         case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
  1086   1085         case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
  1087   1086         case SQLITE_READONLY_CANTLOCK:  zName = "SQLITE_READONLY_CANTLOCK"; break;
  1088   1087         case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
         1088  +      case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
  1089   1089         case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
  1090   1090         case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
  1091   1091         case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
  1092   1092         case SQLITE_IOERR_SHORT_READ:   zName = "SQLITE_IOERR_SHORT_READ";  break;
  1093   1093         case SQLITE_IOERR_WRITE:        zName = "SQLITE_IOERR_WRITE";       break;
  1094   1094         case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
  1095   1095         case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
................................................................................
  1366   1366     void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
  1367   1367     void (*xStep)(sqlite3_context*,int,sqlite3_value **),
  1368   1368     void (*xFinal)(sqlite3_context*),
  1369   1369     FuncDestructor *pDestructor
  1370   1370   ){
  1371   1371     FuncDef *p;
  1372   1372     int nName;
         1373  +  int extraFlags;
  1373   1374   
  1374   1375     assert( sqlite3_mutex_held(db->mutex) );
  1375   1376     if( zFunctionName==0 ||
  1376   1377         (xFunc && (xFinal || xStep)) || 
  1377   1378         (!xFunc && (xFinal && !xStep)) ||
  1378   1379         (!xFunc && (!xFinal && xStep)) ||
  1379   1380         (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
  1380   1381         (255<(nName = sqlite3Strlen30( zFunctionName))) ){
  1381   1382       return SQLITE_MISUSE_BKPT;
  1382   1383     }
         1384  +
         1385  +  assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
         1386  +  extraFlags = enc &  SQLITE_DETERMINISTIC;
         1387  +  enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
  1383   1388     
  1384   1389   #ifndef SQLITE_OMIT_UTF16
  1385   1390     /* If SQLITE_UTF16 is specified as the encoding type, transform this
  1386   1391     ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  1387   1392     ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  1388   1393     **
  1389   1394     ** If SQLITE_ANY is specified, add three versions of the function
  1390   1395     ** to the hash table.
  1391   1396     */
  1392   1397     if( enc==SQLITE_UTF16 ){
  1393   1398       enc = SQLITE_UTF16NATIVE;
  1394   1399     }else if( enc==SQLITE_ANY ){
  1395   1400       int rc;
  1396         -    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
         1401  +    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
  1397   1402            pUserData, xFunc, xStep, xFinal, pDestructor);
  1398   1403       if( rc==SQLITE_OK ){
  1399         -      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
         1404  +      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
  1400   1405             pUserData, xFunc, xStep, xFinal, pDestructor);
  1401   1406       }
  1402   1407       if( rc!=SQLITE_OK ){
  1403   1408         return rc;
  1404   1409       }
  1405   1410       enc = SQLITE_UTF16BE;
  1406   1411     }
................................................................................
  1435   1440     ** being replaced invoke the destructor function here. */
  1436   1441     functionDestroy(db, p);
  1437   1442   
  1438   1443     if( pDestructor ){
  1439   1444       pDestructor->nRef++;
  1440   1445     }
  1441   1446     p->pDestructor = pDestructor;
  1442         -  p->funcFlags &= SQLITE_FUNC_ENCMASK;
         1447  +  p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
         1448  +  testcase( p->funcFlags & SQLITE_DETERMINISTIC );
  1443   1449     p->xFunc = xFunc;
  1444   1450     p->xStep = xStep;
  1445   1451     p->xFinalize = xFinal;
  1446   1452     p->pUserData = pUserData;
  1447   1453     p->nArg = (u16)nArg;
  1448   1454     return SQLITE_OK;
  1449   1455   }
................................................................................
  1886   1892     if( !sqlite3SafetyCheckSickOrOk(db) ){
  1887   1893       return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
  1888   1894     }
  1889   1895     sqlite3_mutex_enter(db->mutex);
  1890   1896     if( db->mallocFailed ){
  1891   1897       z = sqlite3ErrStr(SQLITE_NOMEM);
  1892   1898     }else{
         1899  +    testcase( db->pErr==0 );
  1893   1900       z = (char*)sqlite3_value_text(db->pErr);
  1894   1901       assert( !db->mallocFailed );
  1895   1902       if( z==0 ){
  1896   1903         z = sqlite3ErrStr(db->errCode);
  1897   1904       }
  1898   1905     }
  1899   1906     sqlite3_mutex_leave(db->mutex);
................................................................................
  1927   1934     }
  1928   1935     sqlite3_mutex_enter(db->mutex);
  1929   1936     if( db->mallocFailed ){
  1930   1937       z = (void *)outOfMem;
  1931   1938     }else{
  1932   1939       z = sqlite3_value_text16(db->pErr);
  1933   1940       if( z==0 ){
  1934         -      sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
  1935         -           SQLITE_UTF8, SQLITE_STATIC);
         1941  +      sqlite3Error(db, db->errCode, sqlite3ErrStr(db->errCode));
  1936   1942         z = sqlite3_value_text16(db->pErr);
  1937   1943       }
  1938   1944       /* A malloc() may have failed within the call to sqlite3_value_text16()
  1939   1945       ** above. If this is the case, then the db->mallocFailed flag needs to
  1940   1946       ** be cleared before returning. Do this directly, instead of via
  1941   1947       ** sqlite3ApiExit(), to avoid setting the database handle error message.
  1942   1948       */
................................................................................
  2642   2648   
  2643   2649   #ifdef SQLITE_ENABLE_RTREE
  2644   2650     if( !db->mallocFailed && rc==SQLITE_OK){
  2645   2651       rc = sqlite3RtreeInit(db);
  2646   2652     }
  2647   2653   #endif
  2648   2654   
  2649         -  sqlite3Error(db, rc, 0);
  2650         -
  2651   2655     /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  2652   2656     ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  2653   2657     ** mode.  Doing nothing at all also makes NORMAL the default.
  2654   2658     */
  2655   2659   #ifdef SQLITE_DEFAULT_LOCKING_MODE
  2656   2660     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  2657   2661     sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),

Changes to src/malloc.c.

   429    429   }
   430    430   
   431    431   /*
   432    432   ** TRUE if p is a lookaside memory allocation from db
   433    433   */
   434    434   #ifndef SQLITE_OMIT_LOOKASIDE
   435    435   static int isLookaside(sqlite3 *db, void *p){
   436         -  return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
          436  +  return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
   437    437   }
   438    438   #else
   439    439   #define isLookaside(A,B) 0
   440    440   #endif
   441    441   
   442    442   /*
   443    443   ** Return the size of a memory allocation previously obtained from
................................................................................
   445    445   */
   446    446   int sqlite3MallocSize(void *p){
   447    447     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   448    448     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   449    449     return sqlite3GlobalConfig.m.xSize(p);
   450    450   }
   451    451   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   452         -  assert( db==0 || sqlite3_mutex_held(db->mutex) );
   453         -  if( db && isLookaside(db, p) ){
          452  +  assert( db!=0 );
          453  +  assert( sqlite3_mutex_held(db->mutex) );
          454  +  if( isLookaside(db, p) ){
   454    455       return db->lookaside.sz;
   455    456     }else{
   456    457       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   457    458       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   458    459       assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   459    460       return sqlite3GlobalConfig.m.xSize(p);
   460    461     }

Changes to src/os_unix.c.

  1311   1311     }else{
  1312   1312       pInode->nRef++;
  1313   1313     }
  1314   1314     *ppInode = pInode;
  1315   1315     return SQLITE_OK;
  1316   1316   }
  1317   1317   
         1318  +/*
         1319  +** Return TRUE if pFile has been renamed or unlinked since it was first opened.
         1320  +*/
         1321  +static int fileHasMoved(unixFile *pFile){
         1322  +  struct stat buf;
         1323  +  return pFile->pInode!=0 &&
         1324  +         (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
         1325  +}
         1326  +
  1318   1327   
  1319   1328   /*
  1320   1329   ** Check a unixFile that is a database.  Verify the following:
  1321   1330   **
  1322   1331   ** (1) There is exactly one hard link on the file
  1323   1332   ** (2) The file is not a symbolic link
  1324   1333   ** (3) The file has not been renamed or unlinked
................................................................................
  1345   1354       return;
  1346   1355     }
  1347   1356     if( buf.st_nlink>1 ){
  1348   1357       sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
  1349   1358       pFile->ctrlFlags |= UNIXFILE_WARNED;
  1350   1359       return;
  1351   1360     }
  1352         -  if( pFile->pInode!=0
  1353         -   && ((rc = osStat(pFile->zPath, &buf))!=0
  1354         -       || buf.st_ino!=pFile->pInode->fileId.ino)
  1355         -  ){
         1361  +  if( fileHasMoved(pFile) ){
  1356   1362       sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
  1357   1363       pFile->ctrlFlags |= UNIXFILE_WARNED;
  1358   1364       return;
  1359   1365     }
  1360   1366   }
  1361   1367   
  1362   1368   
................................................................................
  3796   3802       case SQLITE_FCNTL_TEMPFILENAME: {
  3797   3803         char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
  3798   3804         if( zTFile ){
  3799   3805           unixGetTempname(pFile->pVfs->mxPathname, zTFile);
  3800   3806           *(char**)pArg = zTFile;
  3801   3807         }
  3802   3808         return SQLITE_OK;
         3809  +    }
         3810  +    case SQLITE_FCNTL_HAS_MOVED: {
         3811  +      *(int*)pArg = fileHasMoved(pFile);
         3812  +      return SQLITE_OK;
  3803   3813       }
  3804   3814   #if SQLITE_MAX_MMAP_SIZE>0
  3805   3815       case SQLITE_FCNTL_MMAP_SIZE: {
  3806   3816         i64 newLimit = *(i64*)pArg;
  3807   3817         int rc = SQLITE_OK;
  3808   3818         if( newLimit>sqlite3GlobalConfig.mxMmap ){
  3809   3819           newLimit = sqlite3GlobalConfig.mxMmap;

Changes to src/os_win.c.

  3115   3115       }
  3116   3116       case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
  3117   3117         winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
  3118   3118         OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  3119   3119         return SQLITE_OK;
  3120   3120       }
  3121   3121       case SQLITE_FCNTL_VFSNAME: {
  3122         -      *(char**)pArg = sqlite3_mprintf("win32");
         3122  +      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
  3123   3123         OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  3124   3124         return SQLITE_OK;
  3125   3125       }
  3126   3126       case SQLITE_FCNTL_WIN32_AV_RETRY: {
  3127   3127         int *a = (int*)pArg;
  3128   3128         if( a[0]>0 ){
  3129   3129           winIoerrRetry = a[0];

Changes to src/pager.c.

  1020   1020   **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
  1021   1021   **   * The bit corresponding to the page-number is not set in
  1022   1022   **     PagerSavepoint.pInSavepoint.
  1023   1023   */
  1024   1024   static int subjRequiresPage(PgHdr *pPg){
  1025   1025     Pager *pPager = pPg->pPager;
  1026   1026     PagerSavepoint *p;
  1027         -  Pgno pgno;
         1027  +  Pgno pgno = pPg->pgno;
  1028   1028     int i;
  1029         -  if( pPager->nSavepoint ){
  1030         -    pgno = pPg->pgno;
  1031         -    for(i=0; i<pPager->nSavepoint; i++){
  1032         -      p = &pPager->aSavepoint[i];
  1033         -      if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
  1034         -        return 1;
  1035         -      }
         1029  +  for(i=0; i<pPager->nSavepoint; i++){
         1030  +    p = &pPager->aSavepoint[i];
         1031  +    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
         1032  +      return 1;
  1036   1033       }
  1037   1034     }
  1038   1035     return 0;
  1039   1036   }
  1040   1037   
  1041   1038   /*
  1042   1039   ** Return true if the page is already in the journal file.
  1043   1040   */
  1044         -static int pageInJournal(PgHdr *pPg){
  1045         -  return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
         1041  +static int pageInJournal(Pager *pPager, PgHdr *pPg){
         1042  +  return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
  1046   1043   }
  1047   1044   
  1048   1045   /*
  1049   1046   ** Read a 32-bit integer from the given file descriptor.  Store the integer
  1050   1047   ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
  1051   1048   ** error code is something goes wrong.
  1052   1049   **
................................................................................
  1245   1242     unsigned char aMagic[8];   /* A buffer to hold the magic header */
  1246   1243     zMaster[0] = '\0';
  1247   1244   
  1248   1245     if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
  1249   1246      || szJ<16
  1250   1247      || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
  1251   1248      || len>=nMaster 
         1249  +   || len==0 
  1252   1250      || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
  1253   1251      || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
  1254   1252      || memcmp(aMagic, aJournalMagic, 8)
  1255   1253      || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
  1256   1254     ){
  1257   1255       return rc;
  1258   1256     }
................................................................................
  1985   1983   
  1986   1984   #ifdef SQLITE_CHECK_PAGES
  1987   1985     sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
  1988   1986     if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
  1989   1987       PgHdr *p = pager_lookup(pPager, 1);
  1990   1988       if( p ){
  1991   1989         p->pageHash = 0;
  1992         -      sqlite3PagerUnref(p);
         1990  +      sqlite3PagerUnrefNotNull(p);
  1993   1991       }
  1994   1992     }
  1995   1993   #endif
  1996   1994   
  1997   1995     sqlite3BitvecDestroy(pPager->pInJournal);
  1998   1996     pPager->pInJournal = 0;
  1999   1997     pPager->nRec = 0;
................................................................................
  2013   2011       ** At this point the journal has been finalized and the transaction 
  2014   2012       ** successfully committed, but the EXCLUSIVE lock is still held on the
  2015   2013       ** file. So it is safe to truncate the database file to its minimum
  2016   2014       ** required size.  */
  2017   2015       assert( pPager->eLock==EXCLUSIVE_LOCK );
  2018   2016       rc = pager_truncate(pPager, pPager->dbSize);
  2019   2017     }
         2018  +
         2019  +  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
         2020  +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
         2021  +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
         2022  +  }
  2020   2023   
  2021   2024     if( !pPager->exclusiveMode 
  2022   2025      && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
  2023   2026     ){
  2024   2027       rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
  2025   2028       pPager->changeCountDone = 0;
  2026   2029     }
................................................................................
  2827   2830       zMaster = pPager->pTmpSpace;
  2828   2831       rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  2829   2832       testcase( rc!=SQLITE_OK );
  2830   2833     }
  2831   2834     if( rc==SQLITE_OK
  2832   2835      && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
  2833   2836     ){
  2834         -    rc = sqlite3PagerSync(pPager);
         2837  +    rc = sqlite3PagerSync(pPager, 0);
  2835   2838     }
  2836   2839     if( rc==SQLITE_OK ){
  2837   2840       rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
  2838   2841       testcase( rc!=SQLITE_OK );
  2839   2842     }
  2840   2843     if( rc==SQLITE_OK && zMaster[0] && res ){
  2841   2844       /* If there was a master journal and this routine will return success,
................................................................................
  2973   2976         rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
  2974   2977         if( rc==SQLITE_OK ){
  2975   2978           rc = readDbPage(pPg, iFrame);
  2976   2979         }
  2977   2980         if( rc==SQLITE_OK ){
  2978   2981           pPager->xReiniter(pPg);
  2979   2982         }
  2980         -      sqlite3PagerUnref(pPg);
         2983  +      sqlite3PagerUnrefNotNull(pPg);
  2981   2984       }
  2982   2985     }
  2983   2986   
  2984   2987     /* Normally, if a transaction is rolled back, any backup processes are
  2985   2988     ** updated as data is copied out of the rollback journal and into the
  2986   2989     ** database. This is not generally possible with a WAL database, as
  2987   2990     ** rollback involves simply truncating the log file. Therefore, if one
................................................................................
  4328   4331     if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
  4329   4332   
  4330   4333       /* Open the sub-journal, if it has not already been opened */
  4331   4334       assert( pPager->useJournal );
  4332   4335       assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
  4333   4336       assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
  4334   4337       assert( pagerUseWal(pPager) 
  4335         -         || pageInJournal(pPg) 
         4338  +         || pageInJournal(pPager, pPg) 
  4336   4339            || pPg->pgno>pPager->dbOrigSize 
  4337   4340       );
  4338   4341       rc = openSubJournal(pPager);
  4339   4342   
  4340   4343       /* If the sub-journal was opened successfully (or was already open),
  4341   4344       ** write the journal record into the file.  */
  4342   4345       if( rc==SQLITE_OK ){
................................................................................
  4792   4795     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  4793   4796     /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
  4794   4797   
  4795   4798     *ppPager = pPager;
  4796   4799     return SQLITE_OK;
  4797   4800   }
  4798   4801   
         4802  +
         4803  +/* Verify that the database file has not be deleted or renamed out from
         4804  +** under the pager.  Return SQLITE_OK if the database is still were it ought
         4805  +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
         4806  +** code from sqlite3OsAccess()) if the database has gone missing.
         4807  +*/
         4808  +static int databaseIsUnmoved(Pager *pPager){
         4809  +  int bHasMoved = 0;
         4810  +  int rc;
         4811  +
         4812  +  if( pPager->tempFile ) return SQLITE_OK;
         4813  +  if( pPager->dbSize==0 ) return SQLITE_OK;
         4814  +  assert( pPager->zFilename && pPager->zFilename[0] );
         4815  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
         4816  +  if( rc==SQLITE_NOTFOUND ){
         4817  +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
         4818  +    ** has not been moved.  That is the historical behavior of SQLite: prior to
         4819  +    ** version 3.8.3, it never checked */
         4820  +    rc = SQLITE_OK;
         4821  +  }else if( rc==SQLITE_OK && bHasMoved ){
         4822  +    rc = SQLITE_READONLY_DBMOVED;
         4823  +  }
         4824  +  return rc;
         4825  +}
  4799   4826   
  4800   4827   
  4801   4828   /*
  4802   4829   ** This function is called after transitioning from PAGER_UNLOCK to
  4803   4830   ** PAGER_SHARED state. It tests if there is a hot journal present in
  4804   4831   ** the file-system for the given pager. A hot journal is one that 
  4805   4832   ** needs to be played back. According to this function, a hot-journal
................................................................................
  5264   5291     }else{
  5265   5292   
  5266   5293       if( bMmapOk && pagerUseWal(pPager) ){
  5267   5294         rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
  5268   5295         if( rc!=SQLITE_OK ) goto pager_acquire_err;
  5269   5296       }
  5270   5297   
  5271         -    if( iFrame==0 && bMmapOk ){
         5298  +    if( bMmapOk && iFrame==0 ){
  5272   5299         void *pData = 0;
  5273   5300   
  5274   5301         rc = sqlite3OsFetch(pPager->fd, 
  5275   5302             (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
  5276   5303         );
  5277   5304   
  5278   5305         if( rc==SQLITE_OK && pData ){
................................................................................
  5405   5432   ** Release a page reference.
  5406   5433   **
  5407   5434   ** If the number of references to the page drop to zero, then the
  5408   5435   ** page is added to the LRU list.  When all references to all pages
  5409   5436   ** are released, a rollback occurs and the lock on the database is
  5410   5437   ** removed.
  5411   5438   */
         5439  +void sqlite3PagerUnrefNotNull(DbPage *pPg){
         5440  +  Pager *pPager;
         5441  +  assert( pPg!=0 );
         5442  +  pPager = pPg->pPager;
         5443  +  if( pPg->flags & PGHDR_MMAP ){
         5444  +    pagerReleaseMapPage(pPg);
         5445  +  }else{
         5446  +    sqlite3PcacheRelease(pPg);
         5447  +  }
         5448  +  pagerUnlockIfUnused(pPager);
         5449  +}
  5412   5450   void sqlite3PagerUnref(DbPage *pPg){
  5413         -  if( pPg ){
  5414         -    Pager *pPager = pPg->pPager;
  5415         -    if( pPg->flags & PGHDR_MMAP ){
  5416         -      pagerReleaseMapPage(pPg);
  5417         -    }else{
  5418         -      sqlite3PcacheRelease(pPg);
  5419         -    }
  5420         -    pagerUnlockIfUnused(pPager);
  5421         -  }
         5451  +  if( pPg ) sqlite3PagerUnrefNotNull(pPg);
  5422   5452   }
  5423   5453   
  5424   5454   /*
  5425   5455   ** This function is called at the start of every write transaction.
  5426   5456   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
  5427   5457   ** file when this routine is called.
  5428   5458   **
................................................................................
  5469   5499         }else{
  5470   5500           const int flags =                   /* VFS flags to open journal file */
  5471   5501             SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
  5472   5502             (pPager->tempFile ? 
  5473   5503               (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
  5474   5504               (SQLITE_OPEN_MAIN_JOURNAL)
  5475   5505             );
  5476         -  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  5477         -        rc = sqlite3JournalOpen(
  5478         -            pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  5479         -        );
  5480         -  #else
  5481         -        rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  5482         -  #endif
         5506  +
         5507  +        /* Verify that the database still has the same name as it did when
         5508  +        ** it was originally opened. */
         5509  +        rc = databaseIsUnmoved(pPager);
         5510  +        if( rc==SQLITE_OK ){
         5511  +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
         5512  +          rc = sqlite3JournalOpen(
         5513  +              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
         5514  +          );
         5515  +#else
         5516  +          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
         5517  +#endif
         5518  +        }
  5483   5519         }
  5484   5520         assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
  5485   5521       }
  5486   5522     
  5487   5523     
  5488   5524       /* Write the first journal header to the journal file and open 
  5489   5525       ** the sub-journal if necessary.
................................................................................
  5596   5632   ** Mark a single data page as writeable. The page is written into the 
  5597   5633   ** main journal or sub-journal as required. If the page is written into
  5598   5634   ** one of the journals, the corresponding bit is set in the 
  5599   5635   ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
  5600   5636   ** of any open savepoints as appropriate.
  5601   5637   */
  5602   5638   static int pager_write(PgHdr *pPg){
  5603         -  void *pData = pPg->pData;
  5604   5639     Pager *pPager = pPg->pPager;
  5605   5640     int rc = SQLITE_OK;
         5641  +  int inJournal;
  5606   5642   
  5607   5643     /* This routine is not called unless a write-transaction has already 
  5608   5644     ** been started. The journal file may or may not be open at this point.
  5609   5645     ** It is never called in the ERROR state.
  5610   5646     */
  5611   5647     assert( pPager->eState==PAGER_WRITER_LOCKED
  5612   5648          || pPager->eState==PAGER_WRITER_CACHEMOD
  5613   5649          || pPager->eState==PAGER_WRITER_DBMOD
  5614   5650     );
  5615   5651     assert( assert_pager_state(pPager) );
  5616         -
  5617         -  /* If an error has been previously detected, report the same error
  5618         -  ** again. This should not happen, but the check provides robustness. */
  5619         -  if( NEVER(pPager->errCode) )  return pPager->errCode;
  5620         -
  5621         -  /* Higher-level routines never call this function if database is not
  5622         -  ** writable.  But check anyway, just for robustness. */
  5623         -  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
         5652  +  assert( pPager->errCode==0 );
         5653  +  assert( pPager->readOnly==0 );
  5624   5654   
  5625   5655     CHECK_PAGE(pPg);
  5626   5656   
  5627   5657     /* The journal file needs to be opened. Higher level routines have already
  5628   5658     ** obtained the necessary locks to begin the write-transaction, but the
  5629   5659     ** rollback journal might not yet be open. Open it now if this is the case.
  5630   5660     **
................................................................................
  5640   5670     assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
  5641   5671     assert( assert_pager_state(pPager) );
  5642   5672   
  5643   5673     /* Mark the page as dirty.  If the page has already been written
  5644   5674     ** to the journal then we can return right away.
  5645   5675     */
  5646   5676     sqlite3PcacheMakeDirty(pPg);
  5647         -  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
         5677  +  inJournal = pageInJournal(pPager, pPg);
         5678  +  if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
  5648   5679       assert( !pagerUseWal(pPager) );
  5649   5680     }else{
  5650   5681     
  5651   5682       /* The transaction journal now exists and we have a RESERVED or an
  5652   5683       ** EXCLUSIVE lock on the main database file.  Write the current page to
  5653   5684       ** the transaction journal if it is not there already.
  5654   5685       */
  5655         -    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
         5686  +    if( !inJournal && !pagerUseWal(pPager) ){
  5656   5687         assert( pagerUseWal(pPager)==0 );
  5657   5688         if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
  5658   5689           u32 cksum;
  5659   5690           char *pData2;
  5660   5691           i64 iOff = pPager->journalOff;
  5661   5692   
  5662   5693           /* We should never write to the journal file the page that
  5663   5694           ** contains the database locks.  The following assert verifies
  5664   5695           ** that we do not. */
  5665   5696           assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  5666   5697   
  5667   5698           assert( pPager->journalHdr<=pPager->journalOff );
  5668         -        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
         5699  +        CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  5669   5700           cksum = pager_cksum(pPager, (u8*)pData2);
  5670   5701   
  5671   5702           /* Even if an IO or diskfull error occurs while journalling the
  5672   5703           ** page in the block above, set the need-sync flag for the page.
  5673   5704           ** Otherwise, when the transaction is rolled back, the logic in
  5674   5705           ** playback_one_page() will think that the page needs to be restored
  5675   5706           ** in the database file. And if an IO error occurs while doing so,
................................................................................
  5713   5744       }
  5714   5745     
  5715   5746       /* If the statement journal is open and the page is not in it,
  5716   5747       ** then write the current page to the statement journal.  Note that
  5717   5748       ** the statement journal format differs from the standard journal format
  5718   5749       ** in that it omits the checksums and the header.
  5719   5750       */
  5720         -    if( subjRequiresPage(pPg) ){
         5751  +    if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
  5721   5752         rc = subjournalPage(pPg);
  5722   5753       }
  5723   5754     }
  5724   5755   
  5725   5756     /* Update the database size and return.
  5726   5757     */
  5727   5758     if( pPager->dbSize<pPg->pgno ){
................................................................................
  5745   5776   ** as appropriate. Otherwise, SQLITE_OK.
  5746   5777   */
  5747   5778   int sqlite3PagerWrite(DbPage *pDbPage){
  5748   5779     int rc = SQLITE_OK;
  5749   5780   
  5750   5781     PgHdr *pPg = pDbPage;
  5751   5782     Pager *pPager = pPg->pPager;
  5752         -  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  5753   5783   
  5754   5784     assert( (pPg->flags & PGHDR_MMAP)==0 );
  5755   5785     assert( pPager->eState>=PAGER_WRITER_LOCKED );
  5756   5786     assert( pPager->eState!=PAGER_ERROR );
  5757   5787     assert( assert_pager_state(pPager) );
  5758   5788   
  5759         -  if( nPagePerSector>1 ){
         5789  +  if( pPager->sectorSize > (u32)pPager->pageSize ){
  5760   5790       Pgno nPageCount;          /* Total number of pages in database file */
  5761   5791       Pgno pg1;                 /* First page of the sector pPg is located on. */
  5762   5792       int nPage = 0;            /* Number of pages starting at pg1 to journal */
  5763   5793       int ii;                   /* Loop counter */
  5764   5794       int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
         5795  +    Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  5765   5796   
  5766   5797       /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
  5767   5798       ** a journal header to be written between the pages journaled by
  5768   5799       ** this function.
  5769   5800       */
  5770   5801       assert( !MEMDB );
  5771   5802       assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
................................................................................
  5796   5827           if( pg!=PAGER_MJ_PGNO(pPager) ){
  5797   5828             rc = sqlite3PagerGet(pPager, pg, &pPage);
  5798   5829             if( rc==SQLITE_OK ){
  5799   5830               rc = pager_write(pPage);
  5800   5831               if( pPage->flags&PGHDR_NEED_SYNC ){
  5801   5832                 needSync = 1;
  5802   5833               }
  5803         -            sqlite3PagerUnref(pPage);
         5834  +            sqlite3PagerUnrefNotNull(pPage);
  5804   5835             }
  5805   5836           }
  5806   5837         }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
  5807   5838           if( pPage->flags&PGHDR_NEED_SYNC ){
  5808   5839             needSync = 1;
  5809   5840           }
  5810         -        sqlite3PagerUnref(pPage);
         5841  +        sqlite3PagerUnrefNotNull(pPage);
  5811   5842         }
  5812   5843       }
  5813   5844   
  5814   5845       /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
  5815   5846       ** starting at pg1, then it needs to be set for all of them. Because
  5816   5847       ** writing to any of these nPage pages may damage the others, the
  5817   5848       ** journal file must contain sync()ed copies of all of them
................................................................................
  5819   5850       */
  5820   5851       if( rc==SQLITE_OK && needSync ){
  5821   5852         assert( !MEMDB );
  5822   5853         for(ii=0; ii<nPage; ii++){
  5823   5854           PgHdr *pPage = pager_lookup(pPager, pg1+ii);
  5824   5855           if( pPage ){
  5825   5856             pPage->flags |= PGHDR_NEED_SYNC;
  5826         -          sqlite3PagerUnref(pPage);
         5857  +          sqlite3PagerUnrefNotNull(pPage);
  5827   5858           }
  5828   5859         }
  5829   5860       }
  5830   5861   
  5831   5862       assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
  5832   5863       pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
  5833   5864     }else{
................................................................................
  5972   6003   /*
  5973   6004   ** Sync the database file to disk. This is a no-op for in-memory databases
  5974   6005   ** or pages with the Pager.noSync flag set.
  5975   6006   **
  5976   6007   ** If successful, or if called on a pager for which it is a no-op, this
  5977   6008   ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
  5978   6009   */
  5979         -int sqlite3PagerSync(Pager *pPager){
         6010  +int sqlite3PagerSync(Pager *pPager, const char *zMaster){
  5980   6011     int rc = SQLITE_OK;
  5981         -  if( !pPager->noSync ){
         6012  +
         6013  +  if( isOpen(pPager->fd) ){
         6014  +    void *pArg = (void*)zMaster;
         6015  +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
         6016  +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
         6017  +  }
         6018  +  if( rc==SQLITE_OK && !pPager->noSync ){
  5982   6019       assert( !MEMDB );
  5983   6020       rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
  5984         -  }else if( isOpen(pPager->fd) ){
  5985         -    assert( !MEMDB );
  5986         -    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
  5987         -    if( rc==SQLITE_NOTFOUND ){
  5988         -      rc = SQLITE_OK;
  5989         -    }
  5990   6021     }
  5991   6022     return rc;
  5992   6023   }
  5993   6024   
  5994   6025   /*
  5995   6026   ** This function may only be called while a write-transaction is active in
  5996   6027   ** rollback. If the connection is in WAL mode, this call is a no-op. 
................................................................................
  6181   6212           assert( pPager->eState==PAGER_WRITER_DBMOD );
  6182   6213           rc = pager_truncate(pPager, nNew);
  6183   6214           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
  6184   6215         }
  6185   6216     
  6186   6217         /* Finally, sync the database file. */
  6187   6218         if( !noSync ){
  6188         -        rc = sqlite3PagerSync(pPager);
         6219  +        rc = sqlite3PagerSync(pPager, zMaster);
  6189   6220         }
  6190   6221         IOTRACE(("DBSYNC %p\n", pPager))
  6191   6222       }
  6192   6223     }
  6193   6224   
  6194   6225   commit_phase_one_exit:
  6195   6226     if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
................................................................................
  6310   6341       }
  6311   6342     }else{
  6312   6343       rc = pager_playback(pPager, 0);
  6313   6344     }
  6314   6345   
  6315   6346     assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
  6316   6347     assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
  6317         -          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
         6348  +          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR 
         6349  +          || rc==SQLITE_CANTOPEN
         6350  +  );
  6318   6351   
  6319   6352     /* If an error occurs during a ROLLBACK, we can no longer trust the pager
  6320   6353     ** cache. So call pager_error() on the way out to make any error persistent.
  6321   6354     */
  6322   6355     return pager_error(pPager, rc);
  6323   6356   }
  6324   6357   
................................................................................
  6713   6746     ** If the isCommit flag is set, there is no need to remember that
  6714   6747     ** the journal needs to be sync()ed before database page pPg->pgno 
  6715   6748     ** can be written to. The caller has already promised not to write to it.
  6716   6749     */
  6717   6750     if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
  6718   6751       needSyncPgno = pPg->pgno;
  6719   6752       assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
  6720         -            pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
         6753  +            pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
  6721   6754       assert( pPg->flags&PGHDR_DIRTY );
  6722   6755     }
  6723   6756   
  6724   6757     /* If the cache contains a page with page-number pgno, remove it
  6725   6758     ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
  6726   6759     ** page pgno before the 'move' operation, it needs to be retained 
  6727   6760     ** for the page moved there.
................................................................................
  6747   6780     /* For an in-memory database, make sure the original page continues
  6748   6781     ** to exist, in case the transaction needs to roll back.  Use pPgOld
  6749   6782     ** as the original page since it has already been allocated.
  6750   6783     */
  6751   6784     if( MEMDB ){
  6752   6785       assert( pPgOld );
  6753   6786       sqlite3PcacheMove(pPgOld, origPgno);
  6754         -    sqlite3PagerUnref(pPgOld);
         6787  +    sqlite3PagerUnrefNotNull(pPgOld);
  6755   6788     }
  6756   6789   
  6757   6790     if( needSyncPgno ){
  6758   6791       /* If needSyncPgno is non-zero, then the journal file needs to be 
  6759   6792       ** sync()ed before any data is written to database file page needSyncPgno.
  6760   6793       ** Currently, no such page exists in the page-cache and the 
  6761   6794       ** "is journaled" bitvec flag has been set. This needs to be remedied by
................................................................................
  6776   6809           assert( pPager->pTmpSpace!=0 );
  6777   6810           sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
  6778   6811         }
  6779   6812         return rc;
  6780   6813       }
  6781   6814       pPgHdr->flags |= PGHDR_NEED_SYNC;
  6782   6815       sqlite3PcacheMakeDirty(pPgHdr);
  6783         -    sqlite3PagerUnref(pPgHdr);
         6816  +    sqlite3PagerUnrefNotNull(pPgHdr);
  6784   6817     }
  6785   6818   
  6786   6819     return SQLITE_OK;
  6787   6820   }
  6788   6821   #endif
  6789   6822   
  6790   6823   /*

Changes to src/pager.h.

   132    132   
   133    133   /* Functions used to obtain and release page references. */ 
   134    134   int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
   135    135   #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
   136    136   DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
   137    137   void sqlite3PagerRef(DbPage*);
   138    138   void sqlite3PagerUnref(DbPage*);
          139  +void sqlite3PagerUnrefNotNull(DbPage*);
   139    140   
   140    141   /* Operations on page references. */
   141    142   int sqlite3PagerWrite(DbPage*);
   142    143   void sqlite3PagerDontWrite(DbPage*);
   143    144   int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
   144    145   int sqlite3PagerPageRefcount(DbPage*);
   145    146   void *sqlite3PagerGetData(DbPage *); 
................................................................................
   146    147   void *sqlite3PagerGetExtra(DbPage *); 
   147    148   
   148    149   /* Functions used to manage pager transactions and savepoints. */
   149    150   void sqlite3PagerPagecount(Pager*, int*);
   150    151   int sqlite3PagerBegin(Pager*, int exFlag, int);
   151    152   int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
   152    153   int sqlite3PagerExclusiveLock(Pager*);
   153         -int sqlite3PagerSync(Pager *pPager);
          154  +int sqlite3PagerSync(Pager *pPager, const char *zMaster);
   154    155   int sqlite3PagerCommitPhaseTwo(Pager*);
   155    156   int sqlite3PagerRollback(Pager*);
   156    157   int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
   157    158   int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
   158    159   int sqlite3PagerSharedLock(Pager *pPager);
   159    160   
   160    161   #ifndef SQLITE_OMIT_WAL

Changes to src/pcache1.c.

    92     92   ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
    93     93   ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
    94     94   ** in memory.
    95     95   */
    96     96   struct PgHdr1 {
    97     97     sqlite3_pcache_page page;
    98     98     unsigned int iKey;             /* Key value (page number) */
           99  +  u8 isPinned;                   /* Page in use, not on the LRU list */
    99    100     PgHdr1 *pNext;                 /* Next in hash table chain */
   100    101     PCache1 *pCache;               /* Cache that currently owns this page */
   101    102     PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
   102    103     PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
   103    104   };
   104    105   
   105    106   /*
................................................................................
   420    421   
   421    422   /*
   422    423   ** This function is used internally to remove the page pPage from the 
   423    424   ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
   424    425   ** LRU list, then this function is a no-op.
   425    426   **
   426    427   ** The PGroup mutex must be held when this function is called.
   427         -**
   428         -** If pPage is NULL then this routine is a no-op.
   429    428   */
   430    429   static void pcache1PinPage(PgHdr1 *pPage){
   431    430     PCache1 *pCache;
   432    431     PGroup *pGroup;
   433    432   
   434         -  if( pPage==0 ) return;
          433  +  assert( pPage!=0 );
          434  +  assert( pPage->isPinned==0 );
   435    435     pCache = pPage->pCache;
   436    436     pGroup = pCache->pGroup;
          437  +  assert( pPage->pLruNext || pPage==pGroup->pLruTail );
          438  +  assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
   437    439     assert( sqlite3_mutex_held(pGroup->mutex) );
   438         -  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
   439         -    if( pPage->pLruPrev ){
   440         -      pPage->pLruPrev->pLruNext = pPage->pLruNext;
   441         -    }
   442         -    if( pPage->pLruNext ){
   443         -      pPage->pLruNext->pLruPrev = pPage->pLruPrev;
   444         -    }
   445         -    if( pGroup->pLruHead==pPage ){
   446         -      pGroup->pLruHead = pPage->pLruNext;
   447         -    }
   448         -    if( pGroup->pLruTail==pPage ){
   449         -      pGroup->pLruTail = pPage->pLruPrev;
   450         -    }
   451         -    pPage->pLruNext = 0;
   452         -    pPage->pLruPrev = 0;
   453         -    pPage->pCache->nRecyclable--;
          440  +  if( pPage->pLruPrev ){
          441  +    pPage->pLruPrev->pLruNext = pPage->pLruNext;
          442  +  }else{
          443  +    pGroup->pLruHead = pPage->pLruNext;
   454    444     }
          445  +  if( pPage->pLruNext ){
          446  +    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
          447  +  }else{
          448  +    pGroup->pLruTail = pPage->pLruPrev;
          449  +  }
          450  +  pPage->pLruNext = 0;
          451  +  pPage->pLruPrev = 0;
          452  +  pPage->isPinned = 1;
          453  +  pCache->nRecyclable--;
   455    454   }
   456    455   
   457    456   
   458    457   /*
   459    458   ** Remove the page supplied as an argument from the hash table 
   460    459   ** (PCache1.apHash structure) that it is currently stored in.
   461    460   **
................................................................................
   479    478   ** to recycle pages to reduce the number allocated to nMaxPage.
   480    479   */
   481    480   static void pcache1EnforceMaxPage(PGroup *pGroup){
   482    481     assert( sqlite3_mutex_held(pGroup->mutex) );
   483    482     while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
   484    483       PgHdr1 *p = pGroup->pLruTail;
   485    484       assert( p->pCache->pGroup==pGroup );
          485  +    assert( p->isPinned==0 );
   486    486       pcache1PinPage(p);
   487    487       pcache1RemoveFromHash(p);
   488    488       pcache1FreePage(p);
   489    489     }
   490    490   }
   491    491   
   492    492   /*
................................................................................
   506    506     for(h=0; h<pCache->nHash; h++){
   507    507       PgHdr1 **pp = &pCache->apHash[h]; 
   508    508       PgHdr1 *pPage;
   509    509       while( (pPage = *pp)!=0 ){
   510    510         if( pPage->iKey>=iLimit ){
   511    511           pCache->nPage--;
   512    512           *pp = pPage->pNext;
   513         -        pcache1PinPage(pPage);
          513  +        if( !pPage->isPinned ) pcache1PinPage(pPage);
   514    514           pcache1FreePage(pPage);
   515    515         }else{
   516    516           pp = &pPage->pNext;
   517    517           TESTONLY( nPage++; )
   518    518         }
   519    519       }
   520    520     }
................................................................................
   729    729     /* Step 1: Search the hash table for an existing entry. */
   730    730     if( pCache->nHash>0 ){
   731    731       unsigned int h = iKey % pCache->nHash;
   732    732       for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
   733    733     }
   734    734   
   735    735     /* Step 2: Abort if no existing page is found and createFlag is 0 */
   736         -  if( pPage || createFlag==0 ){
   737         -    pcache1PinPage(pPage);
          736  +  if( pPage ){
          737  +    if( !pPage->isPinned ) pcache1PinPage(pPage);
          738  +    goto fetch_out;
          739  +  }
          740  +  if( createFlag==0 ){
   738    741       goto fetch_out;
   739    742     }
   740    743   
   741    744     /* The pGroup local variable will normally be initialized by the
   742    745     ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
   743    746     ** then pcache1EnterMutex() is a no-op, so we have to initialize the
   744    747     ** local variable here.  Delaying the initialization of pGroup is an
................................................................................
   771    774     if( pCache->bPurgeable && pGroup->pLruTail && (
   772    775            (pCache->nPage+1>=pCache->nMax)
   773    776         || pGroup->nCurrentPage>=pGroup->nMaxPage
   774    777         || pcache1UnderMemoryPressure(pCache)
   775    778     )){
   776    779       PCache1 *pOther;
   777    780       pPage = pGroup->pLruTail;
          781  +    assert( pPage->isPinned==0 );
   778    782       pcache1RemoveFromHash(pPage);
   779    783       pcache1PinPage(pPage);
   780    784       pOther = pPage->pCache;
   781    785   
   782    786       /* We want to verify that szPage and szExtra are the same for pOther
   783    787       ** and pCache.  Assert that we can verify this by comparing sums. */
   784    788       assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
................................................................................
   807    811       unsigned int h = iKey % pCache->nHash;
   808    812       pCache->nPage++;
   809    813       pPage->iKey = iKey;
   810    814       pPage->pNext = pCache->apHash[h];
   811    815       pPage->pCache = pCache;
   812    816       pPage->pLruPrev = 0;
   813    817       pPage->pLruNext = 0;
          818  +    pPage->isPinned = 1;
   814    819       *(void **)pPage->page.pExtra = 0;
   815    820       pCache->apHash[h] = pPage;
   816    821     }
   817    822   
   818    823   fetch_out:
   819    824     if( pPage && iKey>pCache->iMaxKey ){
   820    825       pCache->iMaxKey = iKey;
................................................................................
   842    847     pcache1EnterMutex(pGroup);
   843    848   
   844    849     /* It is an error to call this function if the page is already 
   845    850     ** part of the PGroup LRU list.
   846    851     */
   847    852     assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
   848    853     assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
          854  +  assert( pPage->isPinned==1 );
   849    855   
   850    856     if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
   851    857       pcache1RemoveFromHash(pPage);
   852    858       pcache1FreePage(pPage);
   853    859     }else{
   854    860       /* Add the page to the PGroup LRU list. */
   855    861       if( pGroup->pLruHead ){
................................................................................
   857    863         pPage->pLruNext = pGroup->pLruHead;
   858    864         pGroup->pLruHead = pPage;
   859    865       }else{
   860    866         pGroup->pLruTail = pPage;
   861    867         pGroup->pLruHead = pPage;
   862    868       }
   863    869       pCache->nRecyclable++;
          870  +    pPage->isPinned = 0;
   864    871     }
   865    872   
   866    873     pcache1LeaveMutex(pCache->pGroup);
   867    874   }
   868    875   
   869    876   /*
   870    877   ** Implementation of the sqlite3_pcache.xRekey method. 
................................................................................
   983    990       PgHdr1 *p;
   984    991       pcache1EnterMutex(&pcache1.grp);
   985    992       while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
   986    993         nFree += pcache1MemSize(p->page.pBuf);
   987    994   #ifdef SQLITE_PCACHE_SEPARATE_HEADER
   988    995         nFree += sqlite3MemSize(p);
   989    996   #endif
          997  +      assert( p->isPinned==0 );
   990    998         pcache1PinPage(p);
   991    999         pcache1RemoveFromHash(p);
   992   1000         pcache1FreePage(p);
   993   1001       }
   994   1002       pcache1LeaveMutex(&pcache1.grp);
   995   1003     }
   996   1004     return nFree;
................................................................................
  1007   1015     int *pnMax,          /* OUT: Global maximum cache size */
  1008   1016     int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
  1009   1017     int *pnRecyclable    /* OUT: Total number of pages available for recycling */
  1010   1018   ){
  1011   1019     PgHdr1 *p;
  1012   1020     int nRecyclable = 0;
  1013   1021     for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
         1022  +    assert( p->isPinned==0 );
  1014   1023       nRecyclable++;
  1015   1024     }
  1016   1025     *pnCurrent = pcache1.grp.nCurrentPage;
  1017   1026     *pnMax = (int)pcache1.grp.nMaxPage;
  1018   1027     *pnMin = (int)pcache1.grp.nMinPage;
  1019   1028     *pnRecyclable = nRecyclable;
  1020   1029   }
  1021   1030   #endif

Changes to src/printf.c.

   143    143       sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
   144    144       N -= sizeof(zSpaces)-1;
   145    145     }
   146    146     if( N>0 ){
   147    147       sqlite3StrAccumAppend(pAccum, zSpaces, N);
   148    148     }
   149    149   }
          150  +
          151  +/*
          152  +** Set the StrAccum object to an error mode.
          153  +*/
          154  +void setStrAccumError(StrAccum *p, u8 eError){
          155  +  p->accError = eError;
          156  +  p->nAlloc = 0;
          157  +}
   150    158   
   151    159   /*
   152    160   ** On machines with a small stack size, you can redefine the
   153    161   ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
   154    162   */
   155    163   #ifndef SQLITE_PRINT_BUF_SIZE
   156    164   # define SQLITE_PRINT_BUF_SIZE 70
................................................................................
   355    363           if( precision<etBUFSIZE-10 ){
   356    364             nOut = etBUFSIZE;
   357    365             zOut = buf;
   358    366           }else{
   359    367             nOut = precision + 10;
   360    368             zOut = zExtra = sqlite3Malloc( nOut );
   361    369             if( zOut==0 ){
   362         -            pAccum->accError = STRACCUM_NOMEM;
          370  +            setStrAccumError(pAccum, STRACCUM_NOMEM);
   363    371               return;
   364    372             }
   365    373           }
   366    374           bufpt = &zOut[nOut-1];
   367    375           if( xtype==etORDINAL ){
   368    376             static const char zOrd[] = "thstndrd";
   369    377             int x = (int)(longvalue % 10);
................................................................................
   467    475             e2 = 0;
   468    476           }else{
   469    477             e2 = exp;
   470    478           }
   471    479           if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
   472    480             bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
   473    481             if( bufpt==0 ){
   474         -            pAccum->accError = STRACCUM_NOMEM;
          482  +            setStrAccumError(pAccum, STRACCUM_NOMEM);
   475    483               return;
   476    484             }
   477    485           }
   478    486           zOut = bufpt;
   479    487           nsd = 16 + flag_altform2*10;
   480    488           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
   481    489           /* The sign in front of the number */
................................................................................
   602    610             if( ch==q )  n++;
   603    611           }
   604    612           needQuote = !isnull && xtype==etSQLESCAPE2;
   605    613           n += i + 1 + needQuote*2;
   606    614           if( n>etBUFSIZE ){
   607    615             bufpt = zExtra = sqlite3Malloc( n );
   608    616             if( bufpt==0 ){
   609         -            pAccum->accError = STRACCUM_NOMEM;
          617  +            setStrAccumError(pAccum, STRACCUM_NOMEM);
   610    618               return;
   611    619             }
   612    620           }else{
   613    621             bufpt = buf;
   614    622           }
   615    623           j = 0;
   616    624           if( needQuote ) bufpt[j++] = q;
................................................................................
   625    633           /* The precision in %q and %Q means how many input characters to
   626    634           ** consume, not the length of the output...
   627    635           ** if( precision>=0 && precision<length ) length = precision; */
   628    636           break;
   629    637         }
   630    638         case etTOKEN: {
   631    639           Token *pToken = va_arg(ap, Token*);
   632         -        if( pToken ){
          640  +        if( pToken && pToken->n ){
   633    641             sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
   634    642           }
   635    643           length = width = 0;
   636    644           break;
   637    645         }
   638    646         case etSRCLIST: {
   639    647           SrcList *pSrc = va_arg(ap, SrcList*);
   640    648           int k = va_arg(ap, int);
   641    649           struct SrcList_item *pItem = &pSrc->a[k];
   642    650           assert( k>=0 && k<pSrc->nSrc );
   643    651           if( pItem->zDatabase ){
   644         -          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
          652  +          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
   645    653             sqlite3StrAccumAppend(pAccum, ".", 1);
   646    654           }
   647         -        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
          655  +        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
   648    656           length = width = 0;
   649    657           break;
   650    658         }
   651    659         default: {
   652    660           assert( xtype==etINVALID );
   653    661           return;
   654    662         }
................................................................................
   671    679       if( flag_leftjustify ){
   672    680         register int nspace;
   673    681         nspace = width-length;
   674    682         if( nspace>0 ){
   675    683           sqlite3AppendSpace(pAccum, nspace);
   676    684         }
   677    685       }
   678         -    sqlite3_free(zExtra);
          686  +    if( zExtra ) sqlite3_free(zExtra);
   679    687     }/* End for loop over the format string */
   680    688   } /* End of function */
   681    689   
   682    690   /*
   683    691   ** Append N bytes of text from z to the StrAccum object.
   684    692   */
   685    693   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   686         -  assert( z!=0 || N==0 );
   687         -  if( p->accError ){
   688         -    testcase(p->accError==STRACCUM_TOOBIG);
   689         -    testcase(p->accError==STRACCUM_NOMEM);
   690         -    return;
   691         -  }
   692         -  assert( p->zText!=0 || p->nChar==0 );
   693         -  if( N<=0 ){
   694         -    if( N==0 || z[0]==0 ) return;
   695         -    N = sqlite3Strlen30(z);
   696         -  }
          694  +  assert( z!=0 );
          695  +  assert( p->zText!=0 || p->nChar==0 || p->accError );
          696  +  assert( N>=0 );
          697  +  assert( p->accError==0 || p->nAlloc==0 );
   697    698     if( p->nChar+N >= p->nAlloc ){
   698    699       char *zNew;
          700  +    if( p->accError ){
          701  +      testcase(p->accError==STRACCUM_TOOBIG);
          702  +      testcase(p->accError==STRACCUM_NOMEM);
          703  +      return;
          704  +    }
   699    705       if( !p->useMalloc ){
   700         -      p->accError = STRACCUM_TOOBIG;
   701    706         N = p->nAlloc - p->nChar - 1;
          707  +      setStrAccumError(p, STRACCUM_TOOBIG);
   702    708         if( N<=0 ){
   703    709           return;
   704    710         }
   705    711       }else{
   706    712         char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   707    713         i64 szNew = p->nChar;
   708    714         szNew += N + 1;
   709    715         if( szNew > p->mxAlloc ){
   710    716           sqlite3StrAccumReset(p);
   711         -        p->accError = STRACCUM_TOOBIG;
          717  +        setStrAccumError(p, STRACCUM_TOOBIG);
   712    718           return;
   713    719         }else{
   714    720           p->nAlloc = (int)szNew;
   715    721         }
   716    722         if( p->useMalloc==1 ){
   717    723           zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   718    724         }else{
   719    725           zNew = sqlite3_realloc(zOld, p->nAlloc);
   720    726         }
   721    727         if( zNew ){
   722    728           if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
   723    729           p->zText = zNew;
   724    730         }else{
   725         -        p->accError = STRACCUM_NOMEM;
   726    731           sqlite3StrAccumReset(p);
          732  +        setStrAccumError(p, STRACCUM_NOMEM);
   727    733           return;
   728    734         }
   729    735       }
   730    736     }
   731    737     assert( p->zText );
   732    738     memcpy(&p->zText[p->nChar], z, N);
   733    739     p->nChar += N;
   734    740   }
          741  +
          742  +/*
          743  +** Append the complete text of zero-terminated string z[] to the p string.
          744  +*/
          745  +void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
          746  +  sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
          747  +}
          748  +
   735    749   
   736    750   /*
   737    751   ** Finish off a string by making sure it is zero-terminated.
   738    752   ** Return a pointer to the resulting string.  Return a NULL
   739    753   ** pointer if any kind of error was encountered.
   740    754   */
   741    755   char *sqlite3StrAccumFinish(StrAccum *p){
................................................................................
   746    760           p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   747    761         }else{
   748    762           p->zText = sqlite3_malloc(p->nChar+1);
   749    763         }
   750    764         if( p->zText ){
   751    765           memcpy(p->zText, p->zBase, p->nChar+1);
   752    766         }else{
   753         -        p->accError = STRACCUM_NOMEM;
          767  +        setStrAccumError(p, STRACCUM_NOMEM);
   754    768         }
   755    769       }
   756    770     }
   757    771     return p->zText;
   758    772   }
   759    773   
   760    774   /*

Changes to src/shell.c.

  1832   1832   **   +  Use p->cSep as the separator.  The default is ",".
  1833   1833   **   +  Keep track of the line number in p->nLine.
  1834   1834   **   +  Store the character that terminates the field in p->cTerm.  Store
  1835   1835   **      EOF on end-of-file.
  1836   1836   **   +  Report syntax errors on stderr
  1837   1837   */
  1838   1838   static char *csv_read_one_field(CSVReader *p){
  1839         -  int c, pc;
         1839  +  int c, pc, ppc;
  1840   1840     int cSep = p->cSeparator;
  1841   1841     p->n = 0;
  1842   1842     c = fgetc(p->in);
  1843   1843     if( c==EOF || seenInterrupt ){
  1844   1844       p->cTerm = EOF;
  1845   1845       return 0;
  1846   1846     }
  1847   1847     if( c=='"' ){
  1848   1848       int startLine = p->nLine;
  1849   1849       int cQuote = c;
  1850         -    pc = 0;
         1850  +    pc = ppc = 0;
  1851   1851       while( 1 ){
  1852   1852         c = fgetc(p->in);
  1853   1853         if( c=='\n' ) p->nLine++;
  1854   1854         if( c==cQuote ){
  1855   1855           if( pc==cQuote ){
  1856   1856             pc = 0;
  1857   1857             continue;
  1858   1858           }
  1859   1859         }
  1860   1860         if( (c==cSep && pc==cQuote)
  1861   1861          || (c=='\n' && pc==cQuote)
  1862         -       || (c=='\n' && pc=='\r' && p->n>=2 && p->z[p->n-2]==cQuote)
         1862  +       || (c=='\n' && pc=='\r' && ppc==cQuote)
  1863   1863          || (c==EOF && pc==cQuote)
  1864   1864         ){
  1865   1865           do{ p->n--; }while( p->z[p->n]!=cQuote );
  1866   1866           p->cTerm = c;
  1867   1867           break;
  1868   1868         }
  1869   1869         if( pc==cQuote && c!='\r' ){
................................................................................
  1873   1873         if( c==EOF ){
  1874   1874           fprintf(stderr, "%s:%d: unterminated %c-quoted field\n",
  1875   1875                   p->zFile, startLine, cQuote);
  1876   1876           p->cTerm = EOF;
  1877   1877           break;
  1878   1878         }
  1879   1879         csv_append_char(p, c);
         1880  +      ppc = pc;
  1880   1881         pc = c;
  1881   1882       }
  1882   1883     }else{
  1883   1884       while( c!=EOF && c!=cSep && c!='\n' ){
  1884   1885         csv_append_char(p, c);
  1885   1886         c = fgetc(p->in);
  1886   1887       }

Changes to src/sqlite.h.in.

   482    482   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
   483    483   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
   484    484   #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
   485    485   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   486    486   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   487    487   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   488    488   #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
          489  +#define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
   489    490   #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
   490    491   #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
   491    492   #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
   492    493   #define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
   493    494   #define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
   494    495   #define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
   495    496   #define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
................................................................................
   549    550   ** first then the size of the file is extended, never the other
   550    551   ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   551    552   ** information is written to disk in the same order as calls
   552    553   ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   553    554   ** after reboot following a crash or power loss, the only bytes in a
   554    555   ** file that were written at the application level might have changed
   555    556   ** and that adjacent bytes, even bytes within the same sector are
   556         -** guaranteed to be unchanged.
          557  +** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
          558  +** flag indicate that a file cannot be deleted when open.
   557    559   */
   558    560   #define SQLITE_IOCAP_ATOMIC                 0x00000001
   559    561   #define SQLITE_IOCAP_ATOMIC512              0x00000002
   560    562   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
   561    563   #define SQLITE_IOCAP_ATOMIC2K               0x00000008
   562    564   #define SQLITE_IOCAP_ATOMIC4K               0x00000010
   563    565   #define SQLITE_IOCAP_ATOMIC8K               0x00000020
................................................................................
   780    782   ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
   781    783   ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
   782    784   ** to the [sqlite3_file] object associated with a particular database
   783    785   ** connection.  See the [sqlite3_file_control()] documentation for
   784    786   ** additional information.
   785    787   **
   786    788   ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
   787         -** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
   788         -** SQLite and sent to all VFSes in place of a call to the xSync method
   789         -** when the database connection has [PRAGMA synchronous] set to OFF.)^
   790         -** Some specialized VFSes need this signal in order to operate correctly
   791         -** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
   792         -** VFSes do not need this signal and should silently ignore this opcode.
   793         -** Applications should not call [sqlite3_file_control()] with this
   794         -** opcode as doing so may disrupt the operation of the specialized VFSes
   795         -** that do require it.  
          789  +** No longer in use.
          790  +**
          791  +** <li>[[SQLITE_FCNTL_SYNC]]
          792  +** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
          793  +** sent to the VFS immediately before the xSync method is invoked on a
          794  +** database file descriptor. Or, if the xSync method is not invoked 
          795  +** because the user has configured SQLite with 
          796  +** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place 
          797  +** of the xSync method. In most cases, the pointer argument passed with
          798  +** this file-control is NULL. However, if the database file is being synced
          799  +** as part of a multi-database commit, the argument points to a nul-terminated
          800  +** string containing the transactions master-journal file name. VFSes that 
          801  +** do not need this signal should silently ignore this opcode. Applications 
          802  +** should not call [sqlite3_file_control()] with this opcode as doing so may 
          803  +** disrupt the operation of the specialized VFSes that do require it.  
          804  +**
          805  +** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
          806  +** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
          807  +** and sent to the VFS after a transaction has been committed immediately
          808  +** but before the database is unlocked. VFSes that do not need this signal
          809  +** should silently ignore this opcode. Applications should not call
          810  +** [sqlite3_file_control()] with this opcode as doing so may disrupt the 
          811  +** operation of the specialized VFSes that do require it.  
   796    812   **
   797    813   ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
   798    814   ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
   799    815   ** retry counts and intervals for certain disk I/O operations for the
   800    816   ** windows [VFS] in order to provide robustness in the presence of
   801    817   ** anti-virus programs.  By default, the windows VFS will retry file read,
   802    818   ** file write, and file delete operations up to 10 times, with a delay
................................................................................
   912    928   ** The [SQLITE_FCNTL_TRACE] file control provides advisory information
   913    929   ** to the VFS about what the higher layers of the SQLite stack are doing.
   914    930   ** This file control is used by some VFS activity tracing [shims].
   915    931   ** The argument is a zero-terminated string.  Higher layers in the
   916    932   ** SQLite stack may generate instances of this file control if
   917    933   ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
   918    934   **
          935  +** <li>[[SQLITE_FCNTL_HAS_MOVED]]
          936  +** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
          937  +** pointer to an integer and it writes a boolean into that integer depending
          938  +** on whether or not the file has been renamed, moved, or deleted since it
          939  +** was first opened.
          940  +**
   919    941   ** </ul>
   920    942   */
   921    943   #define SQLITE_FCNTL_LOCKSTATE               1
   922    944   #define SQLITE_GET_LOCKPROXYFILE             2
   923    945   #define SQLITE_SET_LOCKPROXYFILE             3
   924    946   #define SQLITE_LAST_ERRNO                    4
   925    947   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
   932    954   #define SQLITE_FCNTL_VFSNAME                12
   933    955   #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
   934    956   #define SQLITE_FCNTL_PRAGMA                 14
   935    957   #define SQLITE_FCNTL_BUSYHANDLER            15
   936    958   #define SQLITE_FCNTL_TEMPFILENAME           16
   937    959   #define SQLITE_FCNTL_MMAP_SIZE              18
   938    960   #define SQLITE_FCNTL_TRACE                  19
          961  +#define SQLITE_FCNTL_HAS_MOVED              20
          962  +#define SQLITE_FCNTL_SYNC                   21
          963  +#define SQLITE_FCNTL_COMMIT_PHASETWO        22
   939    964   
   940    965   /*
   941    966   ** CAPI3REF: Mutex Handle
   942    967   **
   943    968   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
   944    969   ** abstract type for a mutex object.  The SQLite core never looks
   945    970   ** at the internal representation of an [sqlite3_mutex].  It only
................................................................................
  3948   3973   ** aggregate may take any number of arguments between 0 and the limit
  3949   3974   ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
  3950   3975   ** parameter is less than -1 or greater than 127 then the behavior is
  3951   3976   ** undefined.
  3952   3977   **
  3953   3978   ** ^The fourth parameter, eTextRep, specifies what
  3954   3979   ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
  3955         -** its parameters.  Every SQL function implementation must be able to work
  3956         -** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
  3957         -** more efficient with one encoding than another.  ^An application may
  3958         -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
  3959         -** times with the same function but with different values of eTextRep.
         3980  +** its parameters.  The application should set this parameter to
         3981  +** [SQLITE_UTF16LE] if the function implementation invokes 
         3982  +** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
         3983  +** implementation invokes [sqlite3_value_text16be()] on an input, or
         3984  +** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
         3985  +** otherwise.  ^The same SQL function may be registered multiple times using
         3986  +** different preferred text encodings, with different implementations for
         3987  +** each encoding.
  3960   3988   ** ^When multiple implementations of the same function are available, SQLite
  3961   3989   ** will pick the one that involves the least amount of data conversion.
  3962         -** If there is only a single implementation which does not care what text
  3963         -** encoding is used, then the fourth argument should be [SQLITE_ANY].
         3990  +**
         3991  +** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
         3992  +** to signal that the function will always return the same result given
         3993  +** the same inputs within a single SQL statement.  Most SQL functions are
         3994  +** deterministic.  The built-in [random()] SQL function is an example of a
         3995  +** function that is not deterministic.  The SQLite query planner is able to
         3996  +** perform additional optimizations on deterministic functions, so use
         3997  +** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
  3964   3998   **
  3965   3999   ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
  3966   4000   ** function can gain access to this pointer using [sqlite3_user_data()].)^
  3967   4001   **
  3968   4002   ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  3969   4003   ** pointers to C-language functions that implement the SQL function or
  3970   4004   ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
................................................................................
  4042   4076   ** These constant define integer codes that represent the various
  4043   4077   ** text encodings supported by SQLite.
  4044   4078   */
  4045   4079   #define SQLITE_UTF8           1
  4046   4080   #define SQLITE_UTF16LE        2
  4047   4081   #define SQLITE_UTF16BE        3
  4048   4082   #define SQLITE_UTF16          4    /* Use native byte order */
  4049         -#define SQLITE_ANY            5    /* sqlite3_create_function only */
         4083  +#define SQLITE_ANY            5    /* Deprecated */
  4050   4084   #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
  4051   4085   
         4086  +/*
         4087  +** CAPI3REF: Function Flags
         4088  +**
         4089  +** These constants may be ORed together with the 
         4090  +** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
         4091  +** to [sqlite3_create_function()], [sqlite3_create_function16()], or
         4092  +** [sqlite3_create_function_v2()].
         4093  +*/
         4094  +#define SQLITE_DETERMINISTIC    0x800
         4095  +
  4052   4096   /*
  4053   4097   ** CAPI3REF: Deprecated Functions
  4054   4098   ** DEPRECATED
  4055   4099   **
  4056   4100   ** These functions are [deprecated].  In order to maintain
  4057   4101   ** backwards compatibility with older code, these functions continue 
  4058   4102   ** to be supported.  However, new applications should avoid

Changes to src/sqliteInt.h.

  3165   3165   #endif
  3166   3166   u8 sqlite3GetBoolean(const char *z,int);
  3167   3167   
  3168   3168   const void *sqlite3ValueText(sqlite3_value*, u8);
  3169   3169   int sqlite3ValueBytes(sqlite3_value*, u8);
  3170   3170   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  3171   3171                           void(*)(void*));
         3172  +void sqlite3ValueSetNull(sqlite3_value*);
  3172   3173   void sqlite3ValueFree(sqlite3_value*);
  3173   3174   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  3174   3175   char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  3175   3176   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  3176   3177   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  3177   3178   #ifndef SQLITE_AMALGAMATION
  3178   3179   extern const unsigned char sqlite3OpcodeProperty[];
................................................................................
  3230   3231     FuncDestructor *pDestructor
  3231   3232   );
  3232   3233   int sqlite3ApiExit(sqlite3 *db, int);
  3233   3234   int sqlite3OpenTempDatabase(Parse *);
  3234   3235   
  3235   3236   void sqlite3StrAccumInit(StrAccum*, char*, int, int);
  3236   3237   void sqlite3StrAccumAppend(StrAccum*,const char*,int);
         3238  +void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  3237   3239   void sqlite3AppendSpace(StrAccum*,int);
  3238   3240   char *sqlite3StrAccumFinish(StrAccum*);
  3239   3241   void sqlite3StrAccumReset(StrAccum*);
  3240   3242   void sqlite3SelectDestInit(SelectDest*,int,int);
  3241   3243   Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
  3242   3244   
  3243   3245   void sqlite3BackupRestart(sqlite3_backup *);

Changes to src/test1.c.

   938    938       p2 = (const void*)sqlite3_value_blob(argv[0]);
   939    939     }else{
   940    940       return;
   941    941     }
   942    942     sqlite3_result_int(context, p1!=p2);
   943    943   }
   944    944   
          945  +/*
          946  +** This SQL function returns a different answer each time it is called, even if
          947  +** the arguments are the same.
          948  +*/
          949  +static void nondeterministicFunction(
          950  +  sqlite3_context *context, 
          951  +  int argc,  
          952  +  sqlite3_value **argv
          953  +){
          954  +  static int cnt = 0;
          955  +  sqlite3_result_int(context, cnt++);
          956  +}
   945    957   
   946    958   /*
   947         -** Usage:  sqlite_test_create_function DB
          959  +** Usage:  sqlite3_create_function DB
   948    960   **
   949    961   ** Call the sqlite3_create_function API on the given database in order
   950    962   ** to create a function named "x_coalesce".  This function does the same thing
   951    963   ** as the "coalesce" function.  This function also registers an SQL function
   952    964   ** named "x_sqlite_exec" that invokes sqlite3_exec().  Invoking sqlite3_exec()
   953    965   ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
   954    966   ** The effect is similar to trying to use the same database connection from
................................................................................
   969    981   
   970    982     if( argc!=2 ){
   971    983       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   972    984          " DB\"", 0);
   973    985       return TCL_ERROR;
   974    986     }
   975    987     if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   976         -  rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, 
          988  +  rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0, 
   977    989           t1_ifnullFunc, 0, 0);
   978    990     if( rc==SQLITE_OK ){
   979         -    rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, 
   980         -          hex8Func, 0, 0);
          991  +    rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
          992  +          0, hex8Func, 0, 0);
   981    993     }
   982    994   #ifndef SQLITE_OMIT_UTF16
   983    995     if( rc==SQLITE_OK ){
   984         -    rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, 
   985         -          hex16Func, 0, 0);
          996  +    rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
          997  +          0, hex16Func, 0, 0);
   986    998     }
   987    999   #endif
   988   1000     if( rc==SQLITE_OK ){
   989   1001       rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, 
   990   1002             tkt2213Function, 0, 0);
   991   1003     }
   992   1004     if( rc==SQLITE_OK ){
   993   1005       rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, 
   994   1006             ptrChngFunction, 0, 0);
   995   1007     }
         1008  +
         1009  +  /* Functions counter1() and counter2() have the same implementation - they
         1010  +  ** both return an ascending integer with each call.  But counter1() is marked
         1011  +  ** as non-deterministic and counter2() is marked as deterministic.
         1012  +  */
         1013  +  if( rc==SQLITE_OK ){
         1014  +    rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
         1015  +          0, nondeterministicFunction, 0, 0);
         1016  +  }
         1017  +  if( rc==SQLITE_OK ){
         1018  +    rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
         1019  +          0, nondeterministicFunction, 0, 0);
         1020  +  }
   996   1021   
   997   1022   #ifndef SQLITE_OMIT_UTF16
   998   1023     /* Use the sqlite3_create_function16() API here. Mainly for fun, but also 
   999   1024     ** because it is not tested anywhere else. */
  1000   1025     if( rc==SQLITE_OK ){
  1001   1026       const void *zUtf16;
  1002   1027       sqlite3_value *pVal;

Changes to src/test6.c.

   169    169   /*
   170    170   ** Wrapper around the sqlite3OsWrite() function that avoids writing to the
   171    171   ** 512 byte block begining at offset PENDING_BYTE.
   172    172   */
   173    173   static int writeDbFile(CrashFile *p, u8 *z, i64 iAmt, i64 iOff){
   174    174     int rc = SQLITE_OK;
   175    175     int iSkip = 0;
   176         -  if( iOff==PENDING_BYTE && (p->flags&SQLITE_OPEN_MAIN_DB) ){
   177         -    iSkip = 512;
   178         -  }
   179    176     if( (iAmt-iSkip)>0 ){
   180    177       rc = sqlite3OsWrite(p->pRealFile, &z[iSkip], (int)(iAmt-iSkip), iOff+iSkip);
   181    178     }
   182    179     return rc;
   183    180   }
   184    181   
   185    182   /*
................................................................................
   405    402   static int cfRead(
   406    403     sqlite3_file *pFile, 
   407    404     void *zBuf, 
   408    405     int iAmt, 
   409    406     sqlite_int64 iOfst
   410    407   ){
   411    408     CrashFile *pCrash = (CrashFile *)pFile;
          409  +  int nCopy = (int)MIN((i64)iAmt, (pCrash->iSize - iOfst));
          410  +
          411  +  if( nCopy>0 ){
          412  +    memcpy(zBuf, &pCrash->zData[iOfst], nCopy);
          413  +  }
   412    414   
   413    415     /* Check the file-size to see if this is a short-read */
   414         -  if( pCrash->iSize<(iOfst+iAmt) ){
          416  +  if( nCopy<iAmt ){
   415    417       return SQLITE_IOERR_SHORT_READ;
   416    418     }
   417    419   
   418         -  memcpy(zBuf, &pCrash->zData[iOfst], iAmt);
   419    420     return SQLITE_OK;
   420    421   }
   421    422   
   422    423   /*
   423    424   ** Write data to a crash-file.
   424    425   */
   425    426   static int cfWrite(
................................................................................
   628    629         ** to read data from the 512-byte locking region of a file opened
   629    630         ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
   630    631         ** never contains valid data anyhow. So avoid doing such a read here.
   631    632         **
   632    633         ** UPDATE: It also contains an assert() verifying that each call
   633    634         ** to the xRead() method reads less than 128KB of data.
   634    635         */
   635         -      const int isDb = (flags&SQLITE_OPEN_MAIN_DB);
   636    636         i64 iOff;
   637    637   
   638    638         memset(pWrapper->zData, 0, pWrapper->nData);
   639    639         for(iOff=0; iOff<pWrapper->iSize; iOff += 512){
   640    640           int nRead = (int)(pWrapper->iSize - iOff);
   641    641           if( nRead>512 ) nRead = 512;
   642         -        if( isDb && iOff==PENDING_BYTE ) continue;
   643    642           rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff);
   644    643         }
   645    644       }else{
   646    645         rc = SQLITE_NOMEM;
   647    646       }
   648    647     }
   649    648     if( rc!=SQLITE_OK && pWrapper->pMethod ){

Changes to src/test_vfstrace.c.

   254    254                                  zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
   255    255       case SQLITE_IOERR_LOCK:         zVal = "SQLITE_IOERR_LOCK";         break;
   256    256       case SQLITE_IOERR_CLOSE:        zVal = "SQLITE_IOERR_CLOSE";        break;
   257    257       case SQLITE_IOERR_DIR_CLOSE:    zVal = "SQLITE_IOERR_DIR_CLOSE";    break;
   258    258       case SQLITE_IOERR_SHMOPEN:      zVal = "SQLITE_IOERR_SHMOPEN";      break;
   259    259       case SQLITE_IOERR_SHMSIZE:      zVal = "SQLITE_IOERR_SHMSIZE";      break;
   260    260       case SQLITE_IOERR_SHMLOCK:      zVal = "SQLITE_IOERR_SHMLOCK";      break;
          261  +    case SQLITE_IOERR_SHMMAP:       zVal = "SQLITE_IOERR_SHMMAP";       break;
          262  +    case SQLITE_IOERR_SEEK:         zVal = "SQLITE_IOERR_SEEK";         break;
          263  +    case SQLITE_IOERR_GETTEMPPATH:  zVal = "SQLITE_IOERR_GETTEMPPATH";  break;
          264  +    case SQLITE_IOERR_CONVPATH:     zVal = "SQLITE_IOERR_CONVPATH";     break;
          265  +    case SQLITE_READONLY_DBMOVED:   zVal = "SQLITE_READONLY_DBMOVED";   break;
   261    266       case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
   262    267       case SQLITE_BUSY_RECOVERY:      zVal = "SQLITE_BUSY_RECOVERY";      break;
   263    268       case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
   264    269       default: {
   265    270          sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
   266    271          zVal = zBuf;
   267    272          break;

Changes to src/util.c.

   111    111   ** encoded in UTF-8.
   112    112   **
   113    113   ** To clear the most recent error for sqlite handle "db", sqlite3Error
   114    114   ** should be called with err_code set to SQLITE_OK and zFormat set
   115    115   ** to NULL.
   116    116   */
   117    117   void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
   118         -  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
   119         -    db->errCode = err_code;
   120         -    if( zFormat ){
   121         -      char *z;
   122         -      va_list ap;
   123         -      va_start(ap, zFormat);
   124         -      z = sqlite3VMPrintf(db, zFormat, ap);
   125         -      va_end(ap);
   126         -      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
   127         -    }else{
   128         -      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
   129         -    }
          118  +  assert( db!=0 );
          119  +  db->errCode = err_code;
          120  +  if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
          121  +    char *z;
          122  +    va_list ap;
          123  +    va_start(ap, zFormat);
          124  +    z = sqlite3VMPrintf(db, zFormat, ap);
          125  +    va_end(ap);
          126  +    sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
          127  +  }else if( db->pErr ){
          128  +    sqlite3ValueSetNull(db->pErr);
   130    129     }
   131    130   }
   132    131   
   133    132   /*
   134    133   ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
   135    134   ** The following formatting characters are allowed:
   136    135   **

Changes to src/vdbe.c.

  2502   2502         pDest->z = sMem.z;
  2503   2503         pDest->zMalloc = sMem.zMalloc;
  2504   2504       }
  2505   2505     }
  2506   2506     pDest->enc = encoding;
  2507   2507   
  2508   2508   op_column_out:
  2509         -  rc = sqlite3VdbeMemMakeWriteable(pDest);
         2509  +  Deephemeralize(pDest);
  2510   2510   op_column_error:
  2511   2511     UPDATE_MAX_BLOBSIZE(pDest);
  2512   2512     REGISTER_TRACE(pOp->p3, pDest);
  2513   2513     break;
  2514   2514   }
  2515   2515   
  2516   2516   /* Opcode: Affinity P1 P2 * P4 *
................................................................................
  2566   2566     int nVarint;           /* Number of bytes in a varint */
  2567   2567     u32 serial_type;       /* Type field */
  2568   2568     Mem *pData0;           /* First field to be combined into the record */
  2569   2569     Mem *pLast;            /* Last field of the record */
  2570   2570     int nField;            /* Number of fields in the record */
  2571   2571     char *zAffinity;       /* The affinity string for the record */
  2572   2572     int file_format;       /* File format to use for encoding */
  2573         -  int i;                 /* Space used in zNewRecord[] */
         2573  +  int i;                 /* Space used in zNewRecord[] header */
         2574  +  int j;                 /* Space used in zNewRecord[] content */
  2574   2575     int len;               /* Length of a field */
  2575   2576   
  2576   2577     /* Assuming the record contains N fields, the record format looks
  2577   2578     ** like this:
  2578   2579     **
  2579   2580     ** ------------------------------------------------------------------------
  2580   2581     ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 
................................................................................
  2599   2600     pLast = &pData0[nField-1];
  2600   2601     file_format = p->minWriteFileFormat;
  2601   2602   
  2602   2603     /* Identify the output register */
  2603   2604     assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
  2604   2605     pOut = &aMem[pOp->p3];
  2605   2606     memAboutToChange(p, pOut);
         2607  +
         2608  +  /* Apply the requested affinity to all inputs
         2609  +  */
         2610  +  assert( pData0<=pLast );
         2611  +  if( zAffinity ){
         2612  +    pRec = pData0;
         2613  +    do{
         2614  +      applyAffinity(pRec, *(zAffinity++), encoding);
         2615  +    }while( (++pRec)<=pLast );
         2616  +  }
  2606   2617   
  2607   2618     /* Loop through the elements that will make up the record to figure
  2608   2619     ** out how much space is required for the new record.
  2609   2620     */
  2610         -  for(pRec=pData0; pRec<=pLast; pRec++){
         2621  +  pRec = pLast;
         2622  +  do{
  2611   2623       assert( memIsValid(pRec) );
  2612         -    if( zAffinity ){
  2613         -      applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
  2614         -    }
  2615         -    if( pRec->flags&MEM_Zero && pRec->n>0 ){
  2616         -      sqlite3VdbeMemExpandBlob(pRec);
  2617         -    }
  2618   2624       serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2619   2625       len = sqlite3VdbeSerialTypeLen(serial_type);
  2620         -    nData += len;
  2621         -    nHdr += sqlite3VarintLen(serial_type);
  2622   2626       if( pRec->flags & MEM_Zero ){
  2623         -      /* Only pure zero-filled BLOBs can be input to this Opcode.
  2624         -      ** We do not allow blobs with a prefix and a zero-filled tail. */
  2625         -      nZero += pRec->u.nZero;
  2626         -    }else if( len ){
  2627         -      nZero = 0;
         2627  +      if( nData ){
         2628  +        sqlite3VdbeMemExpandBlob(pRec);
         2629  +      }else{
         2630  +        nZero += pRec->u.nZero;
         2631  +        len -= pRec->u.nZero;
         2632  +      }
  2628   2633       }
  2629         -  }
         2634  +    nData += len;
         2635  +    testcase( serial_type==127 );
         2636  +    testcase( serial_type==128 );
         2637  +    nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
         2638  +  }while( (--pRec)>=pData0 );
  2630   2639   
  2631   2640     /* Add the initial header varint and total the size */
  2632         -  nHdr += nVarint = sqlite3VarintLen(nHdr);
  2633         -  if( nVarint<sqlite3VarintLen(nHdr) ){
  2634         -    nHdr++;
         2641  +  testcase( nHdr==126 );
         2642  +  testcase( nHdr==127 );
         2643  +  if( nHdr<=126 ){
         2644  +    /* The common case */
         2645  +    nHdr += 1;
         2646  +  }else{
         2647  +    /* Rare case of a really large header */
         2648  +    nVarint = sqlite3VarintLen(nHdr);
         2649  +    nHdr += nVarint;
         2650  +    if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
  2635   2651     }
  2636         -  nByte = nHdr+nData-nZero;
         2652  +  nByte = nHdr+nData;
  2637   2653     if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2638   2654       goto too_big;
  2639   2655     }
  2640   2656   
  2641   2657     /* Make sure the output register has a buffer large enough to store 
  2642   2658     ** the new record. The output register (pOp->p3) is not allowed to
  2643   2659     ** be one of the input registers (because the following call to
................................................................................
  2646   2662     if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
  2647   2663       goto no_mem;
  2648   2664     }
  2649   2665     zNewRecord = (u8 *)pOut->z;
  2650   2666   
  2651   2667     /* Write the record */
  2652   2668     i = putVarint32(zNewRecord, nHdr);
  2653         -  for(pRec=pData0; pRec<=pLast; pRec++){
         2669  +  j = nHdr;
         2670  +  assert( pData0<=pLast );
         2671  +  pRec = pData0;
         2672  +  do{
  2654   2673       serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2655         -    i += putVarint32(&zNewRecord[i], serial_type);      /* serial type */
  2656         -  }
  2657         -  for(pRec=pData0; pRec<=pLast; pRec++){  /* serial data */
  2658         -    i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
  2659         -  }
  2660         -  assert( i==nByte );
         2674  +    i += putVarint32(&zNewRecord[i], serial_type);            /* serial type */
         2675  +    j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
         2676  +  }while( (++pRec)<=pLast );
         2677  +  assert( i==nHdr );
         2678  +  assert( j==nByte );
  2661   2679   
  2662   2680     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2663   2681     pOut->n = (int)nByte;
  2664   2682     pOut->flags = MEM_Blob | MEM_Dyn;
  2665   2683     pOut->xDel = 0;
  2666   2684     if( nZero ){
  2667   2685       pOut->u.nZero = nZero;
................................................................................
  2682   2700   #ifndef SQLITE_OMIT_BTREECOUNT
  2683   2701   case OP_Count: {         /* out2-prerelease */
  2684   2702     i64 nEntry;
  2685   2703     BtCursor *pCrsr;
  2686   2704   
  2687   2705     pCrsr = p->apCsr[pOp->p1]->pCursor;
  2688   2706     assert( pCrsr );
         2707  +  nEntry = 0;  /* Not needed.  Only used to silence a warning. */
  2689   2708     rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2690   2709     pOut->u.i = nEntry;
  2691   2710     break;
  2692   2711   }
  2693   2712   #endif
  2694   2713   
  2695   2714   /* Opcode: Savepoint P1 * * P4 *
................................................................................
  3702   3721     UnpackedRecord r;
  3703   3722     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
  3704   3723   
  3705   3724   #ifdef SQLITE_TEST
  3706   3725     if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
  3707   3726   #endif
  3708   3727   
  3709         -  alreadyExists = 0;
  3710   3728     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3711   3729     assert( pOp->p4type==P4_INT32 );
  3712   3730     pC = p->apCsr[pOp->p1];
  3713   3731     assert( pC!=0 );
  3714   3732     pIn3 = &aMem[pOp->p3];
  3715   3733     assert( pC->pCursor!=0 );
  3716   3734     assert( pC->isTable==0 );
         3735  +  pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
  3717   3736     if( pOp->p4.i>0 ){
  3718   3737       r.pKeyInfo = pC->pKeyInfo;
  3719   3738       r.nField = (u16)pOp->p4.i;
  3720   3739       r.aMem = pIn3;
  3721   3740   #ifdef SQLITE_DEBUG
  3722   3741       {
  3723   3742         int i;
................................................................................
  4693   4712     assert( pCrsr!=0 );
  4694   4713     pOut->flags = MEM_Null;
  4695   4714     rc = sqlite3VdbeCursorMoveto(pC);
  4696   4715     if( NEVER(rc) ) goto abort_due_to_error;
  4697   4716     assert( pC->deferredMoveto==0 );
  4698   4717     assert( pC->isTable==0 );
  4699   4718     if( !pC->nullRow ){
         4719  +    rowid = 0;  /* Not needed.  Only used to silence a warning. */
  4700   4720       rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
  4701   4721       if( rc!=SQLITE_OK ){
  4702   4722         goto abort_due_to_error;
  4703   4723       }
  4704   4724       pOut->u.i = rowid;
  4705   4725       pOut->flags = MEM_Int;
  4706   4726     }
................................................................................
  4756   4776     }else{
  4757   4777       r.flags = UNPACKED_PREFIX_MATCH;
  4758   4778     }
  4759   4779     r.aMem = &aMem[pOp->p3];
  4760   4780   #ifdef SQLITE_DEBUG
  4761   4781     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4762   4782   #endif
         4783  +  res = 0;  /* Not needed.  Only used to silence a warning. */
  4763   4784     rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
  4764   4785     if( pOp->opcode==OP_IdxLT ){
  4765   4786       res = -res;
  4766   4787     }else{
  4767   4788       assert( pOp->opcode==OP_IdxGE );
  4768   4789       res++;
  4769   4790     }
................................................................................
  4816   4837     if( iCnt>1 ){
  4817   4838       rc = SQLITE_LOCKED;
  4818   4839       p->errorAction = OE_Abort;
  4819   4840     }else{
  4820   4841       iDb = pOp->p3;
  4821   4842       assert( iCnt==1 );
  4822   4843       assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
         4844  +    iMoved = 0;  /* Not needed.  Only to silence a warning. */
  4823   4845       rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
  4824   4846       pOut->flags = MEM_Int;
  4825   4847       pOut->u.i = iMoved;
  4826   4848   #ifndef SQLITE_OMIT_AUTOVACUUM
  4827   4849       if( rc==SQLITE_OK && iMoved!=0 ){
  4828   4850         sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
  4829   4851         /* All OP_Destroy operations occur on the same btree */
................................................................................
  5388   5410   ** within a sub-program). Set the value of register P1 to the maximum of 
  5389   5411   ** its current value and the value in register P2.
  5390   5412   **
  5391   5413   ** This instruction throws an error if the memory cell is not initially
  5392   5414   ** an integer.
  5393   5415   */
  5394   5416   case OP_MemMax: {        /* in2 */
  5395         -  Mem *pIn1;
  5396   5417     VdbeFrame *pFrame;
  5397   5418     if( p->pFrame ){
  5398   5419       for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
  5399   5420       pIn1 = &pFrame->aMem[pOp->p1];
  5400   5421     }else{
  5401   5422       pIn1 = &aMem[pOp->p1];
  5402   5423     }

Changes to src/vdbeInt.h.

   404    404   void sqliteVdbePopStack(Vdbe*,int);
   405    405   int sqlite3VdbeCursorMoveto(VdbeCursor*);
   406    406   #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
   407    407   void sqlite3VdbePrintOp(FILE*, int, Op*);
   408    408   #endif
   409    409   u32 sqlite3VdbeSerialTypeLen(u32);
   410    410   u32 sqlite3VdbeSerialType(Mem*, int);
   411         -u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
          411  +u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
   412    412   u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   413    413   void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
   414    414   
   415    415   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   416    416   int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
   417    417   int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
   418    418   int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);

Changes to src/vdbeapi.c.

   482    482     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   483    483            && cnt++ < SQLITE_MAX_SCHEMA_RETRY
   484    484            && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
   485    485       sqlite3_reset(pStmt);
   486    486       v->doingRerun = 1;
   487    487       assert( v->expired==0 );
   488    488     }
   489         -  if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
          489  +  if( rc2!=SQLITE_OK ){
   490    490       /* This case occurs after failing to recompile an sql statement. 
   491    491       ** The error message from the SQL compiler has already been loaded 
   492    492       ** into the database handle. This block copies the error message 
   493    493       ** from the database handle into the statement and sets the statement
   494    494       ** program counter to 0 to ensure that when the statement is 
   495    495       ** finalized or reset the parser error message is available via
   496    496       ** sqlite3_errmsg() and sqlite3_errcode().
   497    497       */
   498    498       const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
          499  +    assert( zErr!=0 || db->mallocFailed );
   499    500       sqlite3DbFree(db, v->zErrMsg);
   500    501       if( !db->mallocFailed ){
   501    502         v->zErrMsg = sqlite3DbStrDup(db, zErr);
   502    503         v->rc = rc2;
   503    504       } else {
   504    505         v->zErrMsg = 0;
   505    506         v->rc = rc = SQLITE_NOMEM;
................................................................................
  1328   1329     const void *pKey
  1329   1330   ){
  1330   1331     char *dummy;                    /* Dummy argument for AllocUnpackedRecord() */
  1331   1332     UnpackedRecord *pRet;           /* Return value */
  1332   1333   
  1333   1334     pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo, 0, 0, &dummy);
  1334   1335     if( pRet ){
         1336  +    memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
  1335   1337       sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
  1336   1338     }
  1337   1339     return pRet;
  1338   1340   }
  1339   1341   
  1340   1342   /*
  1341   1343   ** This function is called from within a pre-update callback to retrieve

Changes to src/vdbeaux.c.

  2417   2417   */
  2418   2418   int sqlite3VdbeTransferError(Vdbe *p){
  2419   2419     sqlite3 *db = p->db;
  2420   2420     int rc = p->rc;
  2421   2421     if( p->zErrMsg ){
  2422   2422       u8 mallocFailed = db->mallocFailed;
  2423   2423       sqlite3BeginBenignMalloc();
         2424  +    if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
  2424   2425       sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
  2425   2426       sqlite3EndBenignMalloc();
  2426   2427       db->mallocFailed = mallocFailed;
  2427   2428       db->errCode = rc;
  2428   2429     }else{
  2429   2430       sqlite3Error(db, rc, 0);
  2430   2431     }
................................................................................
  2485   2486       p->zErrMsg = 0;
  2486   2487       if( p->runOnlyOnce ) p->expired = 1;
  2487   2488     }else if( p->rc && p->expired ){
  2488   2489       /* The expired flag was set on the VDBE before the first call
  2489   2490       ** to sqlite3_step(). For consistency (since sqlite3_step() was
  2490   2491       ** called), set the database error in this case as well.
  2491   2492       */
  2492         -    sqlite3Error(db, p->rc, 0);
  2493         -    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
         2493  +    sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
  2494   2494       sqlite3DbFree(db, p->zErrMsg);
  2495   2495       p->zErrMsg = 0;
  2496   2496     }
  2497   2497   
  2498   2498     /* Reclaim all memory used by the VDBE
  2499   2499     */
  2500   2500     Cleanup(p);
................................................................................
  2824   2824   #endif
  2825   2825   
  2826   2826   /*
  2827   2827   ** Write the serialized data blob for the value stored in pMem into 
  2828   2828   ** buf. It is assumed that the caller has allocated sufficient space.
  2829   2829   ** Return the number of bytes written.
  2830   2830   **
  2831         -** nBuf is the amount of space left in buf[].  nBuf must always be
  2832         -** large enough to hold the entire field.  Except, if the field is
  2833         -** a blob with a zero-filled tail, then buf[] might be just the right
  2834         -** size to hold everything except for the zero-filled tail.  If buf[]
  2835         -** is only big enough to hold the non-zero prefix, then only write that
  2836         -** prefix into buf[].  But if buf[] is large enough to hold both the
  2837         -** prefix and the tail then write the prefix and set the tail to all
  2838         -** zeros.
         2831  +** nBuf is the amount of space left in buf[].  The caller is responsible
         2832  +** for allocating enough space to buf[] to hold the entire field, exclusive
         2833  +** of the pMem->u.nZero bytes for a MEM_Zero value.
  2839   2834   **
  2840   2835   ** Return the number of bytes actually written into buf[].  The number
  2841   2836   ** of bytes in the zero-filled tail is included in the return value only
  2842   2837   ** if those bytes were zeroed in buf[].
  2843   2838   */ 
  2844         -u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
  2845         -  u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
         2839  +u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
  2846   2840     u32 len;
  2847   2841   
  2848   2842     /* Integer and Real */
  2849   2843     if( serial_type<=7 && serial_type>0 ){
  2850   2844       u64 v;
  2851   2845       u32 i;
  2852   2846       if( serial_type==7 ){
................................................................................
  2853   2847         assert( sizeof(v)==sizeof(pMem->r) );
  2854   2848         memcpy(&v, &pMem->r, sizeof(v));
  2855   2849         swapMixedEndianFloat(v);
  2856   2850       }else{
  2857   2851         v = pMem->u.i;
  2858   2852       }
  2859   2853       len = i = sqlite3VdbeSerialTypeLen(serial_type);
  2860         -    assert( len<=(u32)nBuf );
  2861   2854       while( i-- ){
  2862   2855         buf[i] = (u8)(v&0xFF);
  2863   2856         v >>= 8;
  2864   2857       }
  2865   2858       return len;
  2866   2859     }
  2867   2860   
  2868   2861     /* String or blob */
  2869   2862     if( serial_type>=12 ){
  2870   2863       assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
  2871   2864                == (int)sqlite3VdbeSerialTypeLen(serial_type) );
  2872         -    assert( pMem->n<=nBuf );
  2873   2865       len = pMem->n;
  2874   2866       memcpy(buf, pMem->z, len);
  2875         -    if( pMem->flags & MEM_Zero ){
  2876         -      len += pMem->u.nZero;
  2877         -      assert( nBuf>=0 );
  2878         -      if( len > (u32)nBuf ){
  2879         -        len = (u32)nBuf;
  2880         -      }
  2881         -      memset(&buf[pMem->n], 0, len-pMem->n);
  2882         -    }
  2883   2867       return len;
  2884   2868     }
  2885   2869   
  2886   2870     /* NULL or constants 0 or 1 */
  2887   2871     return 0;
  2888   2872   }
  2889   2873   

Changes to src/vdbemem.c.

    55     55     assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
    56     56     return rc;
    57     57   #endif
    58     58   }
    59     59   
    60     60   /*
    61     61   ** Make sure pMem->z points to a writable allocation of at least 
    62         -** n bytes.
           62  +** min(n,32) bytes.
    63     63   **
    64         -** If the third argument passed to this function is true, then memory
    65         -** cell pMem must contain a string or blob. In this case the content is
    66         -** preserved. Otherwise, if the third parameter to this function is false,
    67         -** any current string or blob value may be discarded.
    68         -**
    69         -** This function sets the MEM_Dyn flag and clears any xDel callback.
    70         -** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
    71         -** not set, Mem.n is zeroed.
           64  +** If the bPreserve argument is true, then copy of the content of
           65  +** pMem->z into the new allocation.  pMem must be either a string or
           66  +** blob if bPreserve is true.  If bPreserve is false, any prior content
           67  +** in pMem->z is discarded.
    72     68   */
    73         -int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
           69  +int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
    74     70     assert( 1 >=
    75     71       ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
    76     72       (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
    77     73       ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
    78     74       ((pMem->flags&MEM_Static) ? 1 : 0)
    79     75     );
    80     76     assert( (pMem->flags&MEM_RowSet)==0 );
    81     77   
    82         -  /* If the preserve flag is set to true, then the memory cell must already
           78  +  /* If the bPreserve flag is set to true, then the memory cell must already
    83     79     ** contain a valid string or blob value.  */
    84         -  assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
           80  +  assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
           81  +  testcase( bPreserve && pMem->z==0 );
    85     82   
    86         -  if( n<32 ) n = 32;
    87         -  if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
    88         -    if( preserve && pMem->z==pMem->zMalloc ){
           83  +  if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
           84  +    if( n<32 ) n = 32;
           85  +    if( bPreserve && pMem->z==pMem->zMalloc ){
    89     86         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
    90         -      preserve = 0;
           87  +      bPreserve = 0;
    91     88       }else{
    92     89         sqlite3DbFree(pMem->db, pMem->zMalloc);
    93     90         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
    94     91       }
           92  +    if( pMem->zMalloc==0 ){
           93  +      sqlite3VdbeMemRelease(pMem);
           94  +      pMem->flags = MEM_Null;  
           95  +      return SQLITE_NOMEM;
           96  +    }
    95     97     }
    96     98   
    97         -  if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
           99  +  if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
    98    100       memcpy(pMem->zMalloc, pMem->z, pMem->n);
    99    101     }
   100         -  if( pMem->flags&MEM_Dyn && pMem->xDel ){
          102  +  if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){
   101    103       assert( pMem->xDel!=SQLITE_DYNAMIC );
   102    104       pMem->xDel((void *)(pMem->z));
   103    105     }
   104    106   
   105    107     pMem->z = pMem->zMalloc;
   106         -  if( pMem->z==0 ){
   107         -    pMem->flags = MEM_Null;
   108         -  }else{
   109         -    pMem->flags &= ~(MEM_Ephem|MEM_Static);
   110         -  }
          108  +  pMem->flags &= ~(MEM_Ephem|MEM_Static);
   111    109     pMem->xDel = 0;
   112         -  return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
          110  +  return SQLITE_OK;
   113    111   }
   114    112   
   115    113   /*
   116    114   ** Make the given Mem object MEM_Dyn.  In other words, make it so
   117    115   ** that any TEXT or BLOB content is stored in memory obtained from
   118    116   ** malloc().  In this way, we know that the memory is safe to be
   119    117   ** overwritten or altered.
................................................................................
   291    289   /*
   292    290   ** Release any memory held by the Mem. This may leave the Mem in an
   293    291   ** inconsistent state, for example with (Mem.z==0) and
   294    292   ** (Mem.type==SQLITE_TEXT).
   295    293   */
   296    294   void sqlite3VdbeMemRelease(Mem *p){
   297    295     VdbeMemRelease(p);
   298         -  sqlite3DbFree(p->db, p->zMalloc);
          296  +  if( p->zMalloc ){
          297  +    sqlite3DbFree(p->db, p->zMalloc);
          298  +    p->zMalloc = 0;
          299  +  }
   299    300     p->z = 0;
   300         -  p->zMalloc = 0;
   301         -  p->xDel = 0;
          301  +  assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */
   302    302   }
   303    303   
   304    304   /*
   305    305   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   306    306   ** If the double is out of range of a 64-bit signed integer then
   307    307   ** return the closest available 64-bit signed integer.
   308    308   */
................................................................................
   477    477       pFrame->v->pDelFrame = pFrame;
   478    478     }
   479    479     if( pMem->flags & MEM_RowSet ){
   480    480       sqlite3RowSetClear(pMem->u.pRowSet);
   481    481     }
   482    482     MemSetTypeFlag(pMem, MEM_Null);
   483    483     pMem->type = SQLITE_NULL;
          484  +}
          485  +void sqlite3ValueSetNull(sqlite3_value *p){
          486  +  sqlite3VdbeMemSetNull((Mem*)p); 
   484    487   }
   485    488   
   486    489   /*
   487    490   ** Delete any previous value and set the value to be a BLOB of length
   488    491   ** n containing all zeros.
   489    492   */
   490    493   void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
................................................................................
  1215   1218     nRet = 1 + nSerial + nVal;
  1216   1219     aRet = sqlite3DbMallocRaw(db, nRet);
  1217   1220     if( aRet==0 ){
  1218   1221       sqlite3_result_error_nomem(context);
  1219   1222     }else{
  1220   1223       aRet[0] = nSerial+1;
  1221   1224       sqlite3PutVarint(&aRet[1], iSerial);
  1222         -    sqlite3VdbeSerialPut(&aRet[1+nSerial], nVal, argv[0], file_format);
         1225  +    sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
  1223   1226       sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
  1224   1227       sqlite3DbFree(db, aRet);
  1225   1228     }
  1226   1229   }
  1227   1230   
  1228   1231   /*
  1229   1232   ** Register built-in functions used to help read ANALYZE data.

Changes to src/vdbetrace.c.

    88     88                         db->aLimit[SQLITE_LIMIT_LENGTH]);
    89     89     out.db = db;
    90     90     if( db->nVdbeExec>1 ){
    91     91       while( *zRawSql ){
    92     92         const char *zStart = zRawSql;
    93     93         while( *(zRawSql++)!='\n' && *zRawSql );
    94     94         sqlite3StrAccumAppend(&out, "-- ", 3);
           95  +      assert( (zRawSql - zStart) > 0 );
    95     96         sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
    96     97       }
    97     98     }else{
    98     99       while( zRawSql[0] ){
    99    100         n = findNextHostParameter(zRawSql, &nToken);
   100    101         assert( n>0 );
   101    102         sqlite3StrAccumAppend(&out, zRawSql, n);

Changes to src/where.c.

  2564   2564   static void explainAppendTerm(
  2565   2565     StrAccum *pStr,             /* The text expression being built */
  2566   2566     int iTerm,                  /* Index of this term.  First is zero */
  2567   2567     const char *zColumn,        /* Name of the column */
  2568   2568     const char *zOp             /* Name of the operator */
  2569   2569   ){
  2570   2570     if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  2571         -  sqlite3StrAccumAppend(pStr, zColumn, -1);
         2571  +  sqlite3StrAccumAppendAll(pStr, zColumn);
  2572   2572     sqlite3StrAccumAppend(pStr, zOp, 1);
  2573   2573     sqlite3StrAccumAppend(pStr, "?", 1);
  2574   2574   }
  2575   2575   
  2576   2576   /*
  2577   2577   ** Argument pLevel describes a strategy for scanning table pTab. This 
  2578   2578   ** function returns a pointer to a string buffer containing a description
................................................................................
  2610   2610     for(i=0; i<nEq; i++){
  2611   2611       char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
  2612   2612       if( i>=nSkip ){
  2613   2613         explainAppendTerm(&txt, i, z, "=");
  2614   2614       }else{
  2615   2615         if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
  2616   2616         sqlite3StrAccumAppend(&txt, "ANY(", 4);
  2617         -      sqlite3StrAccumAppend(&txt, z, -1);
         2617  +      sqlite3StrAccumAppendAll(&txt, z);
  2618   2618         sqlite3StrAccumAppend(&txt, ")", 1);
  2619   2619       }
  2620   2620     }
  2621   2621   
  2622   2622     j = i;
  2623   2623     if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
  2624   2624       char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;

Changes to test/capi3.test.

  1187   1187     db2 close
  1188   1188     sqlite3_step $STMT
  1189   1189   } {SQLITE_ERROR}
  1190   1190   do_test capi3-18.2 {
  1191   1191     sqlite3_reset $STMT
  1192   1192     sqlite3_errcode db
  1193   1193   } {SQLITE_SCHEMA}
         1194  +breakpoint
  1194   1195   do_test capi3-18.3 {
  1195   1196     sqlite3_errmsg db
  1196   1197   } {database schema has changed}
  1197   1198   # The error persist on retry when sqlite3_prepare() has been used.
  1198   1199   do_test capi3-18.4 {
  1199   1200     sqlite3_step $STMT
  1200   1201   } {SQLITE_ERROR}

Changes to test/func5.test.

     5      5   #
     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #*************************************************************************
    11     11   #
    12         -# Verify that constant string expressions that get factored into initializing
    13         -# code are not reused between function parameters and other values in the
    14         -# VDBE program, as the function might have changed the encoding.
           12  +# Testing of function factoring and the SQLITE_DETERMINISTIC flag.
    15     13   #
    16     14   set testdir [file dirname $argv0]
    17     15   source $testdir/tester.tcl
    18     16   
           17  +# Verify that constant string expressions that get factored into initializing
           18  +# code are not reused between function parameters and other values in the
           19  +# VDBE program, as the function might have changed the encoding.
           20  +#
    19     21   do_execsql_test func5-1.1 {
    20     22     PRAGMA encoding=UTF16le;
    21     23     CREATE TABLE t1(x,a,b,c);
    22     24     INSERT INTO t1 VALUES(1,'ab','cd',1);
    23     25     INSERT INTO t1 VALUES(2,'gh','ef',5);
    24     26     INSERT INTO t1 VALUES(3,'pqr','fuzzy',99);
    25     27     INSERT INTO t1 VALUES(4,'abcdefg','xy',22);
    26     28     INSERT INTO t1 VALUES(5,'shoe','mayer',2953);
    27     29     SELECT x FROM t1 WHERE c=instr('abcdefg',b) OR a='abcdefg' ORDER BY +x;
    28     30   } {2 4}
    29     31   do_execsql_test func5-1.2 {
    30     32     SELECT x FROM t1 WHERE a='abcdefg' OR c=instr('abcdefg',b) ORDER BY +x;
    31     33   } {2 4}
           34  +
           35  +# Verify that SQLITE_DETERMINISTIC functions get factored out of the
           36  +# evaluation loop whereas non-deterministic functions do not.  counter1()
           37  +# is marked as non-deterministic and so is not factored out of the loop,
           38  +# and it really is non-deterministic, returning a different result each
           39  +# time.  But counter2() is marked as deterministic, so it does get factored
           40  +# out of the loop.  counter2() has the same implementation as counter1(),
           41  +# returning a different result on each invocation, but because it is 
           42  +# only invoked once outside of the loop, it appears to return the same
           43  +# result multiple times.
           44  +#
           45  +do_execsql_test func5-2.1 {
           46  +  CREATE TABLE t2(x,y);
           47  +  INSERT INTO t2 VALUES(1,2),(3,4),(5,6),(7,8);
           48  +  SELECT x, y FROM t2 WHERE x+5=5+x ORDER BY +x;
           49  +} {1 2 3 4 5 6 7 8}
           50  +sqlite3_create_function db
           51  +do_execsql_test func5-2.2 {
           52  +  SELECT x, y FROM t2
           53  +   WHERE x+counter1('hello')=counter1('hello')+x
           54  +   ORDER BY +x;
           55  +} {}
           56  +do_execsql_test func5-2.3 {
           57  +  SELECT x, y FROM t2
           58  +   WHERE x+counter2('hello')=counter2('hello')+x
           59  +   ORDER BY +x;
           60  +} {1 2 3 4 5 6 7 8}
           61  +
    32     62   
    33     63   finish_test

Added test/pager4.test.

            1  +# 2013-12-06
            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  +# Tests for the SQLITE_IOERR_NODB error condition: the database file file
           13  +# is unlinked or renamed out from under SQLite.
           14  +#
           15  +
           16  +if {$tcl_platform(platform)!="unix"} return
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +
           21  +# Create a database file for testing
           22  +#
           23  +do_execsql_test pager4-1.1 {
           24  +  CREATE TABLE t1(a,b,c);
           25  +  INSERT INTO t1 VALUES(673,'stone','philips');
           26  +  SELECT * FROM t1;
           27  +} {673 stone philips}
           28  +
           29  +# After renaming the database file while it is open, one can still
           30  +# read from the database, but writing returns a READONLY error.
           31  +#
           32  +file delete -force test-xyz.db
           33  +file rename test.db test-xyz.db
           34  +do_catchsql_test pager4-1.2 {
           35  +  SELECT * FROM t1;
           36  +} {0 {673 stone philips}}
           37  +do_catchsql_test pager4-1.3 {
           38  +  UPDATE t1 SET a=537;
           39  +} {1 {attempt to write a readonly database}}
           40  +
           41  +# Creating a different database file with the same name of the original
           42  +# is detected and still leaves the database read-only.
           43  +#
           44  +sqlite3 db2 test.db
           45  +db2 eval {CREATE TABLE t2(x,y,z)}
           46  +do_catchsql_test pager4-1.4 {
           47  +  UPDATE t1 SET a=948;
           48  +} {1 {attempt to write a readonly database}}
           49  +
           50  +# Changing the name back clears the READONLY error
           51  +#
           52  +db2 close
           53  +file delete -force test.db
           54  +file rename test-xyz.db test.db
           55  +do_catchsql_test pager4-1.5 {
           56  +  SELECT * FROM t1;
           57  +} {0 {673 stone philips}}
           58  +do_catchsql_test pager4-1.6 {
           59  +  UPDATE t1 SET a=537;
           60  +  SELECT * FROM t1;
           61  +} {0 {537 stone philips}}
           62  +
           63  +# We can write to a renamed database if journal_mode=OFF or
           64  +# journal_mode=MEMORY.
           65  +#
           66  +file rename test.db test-xyz.db
           67  +do_catchsql_test pager4-1.7 {
           68  +  PRAGMA journal_mode=OFF;
           69  +  UPDATE t1 SET a=107;
           70  +  SELECT * FROM t1;
           71  +} {0 {off 107 stone philips}}
           72  +do_catchsql_test pager4-1.8 {
           73  +  PRAGMA journal_mode=MEMORY;
           74  +  UPDATE t1 SET b='magpie';
           75  +  SELECT * FROM t1;
           76  +} {0 {memory 107 magpie philips}}
           77  +
           78  +# Any other journal mode gives a READONLY error
           79  +#
           80  +do_catchsql_test pager4-1.9 {
           81  +  PRAGMA journal_mode=DELETE;
           82  +  UPDATE t1 SET c='jaguar';
           83  +} {1 {attempt to write a readonly database}}
           84  +do_catchsql_test pager4-1.10 {
           85  +  PRAGMA journal_mode=TRUNCATE;
           86  +  UPDATE t1 SET c='jaguar';
           87  +} {1 {attempt to write a readonly database}}
           88  +do_catchsql_test pager4-1.11 {
           89  +  PRAGMA journal_mode=PERSIST;
           90  +  UPDATE t1 SET c='jaguar';
           91  +} {1 {attempt to write a readonly database}}
           92  +
           93  +
           94  +finish_test

Changes to test/shell5.test.

   262    262     catchcmd test.db {.mode csv
   263    263       CREATE TABLE t1(a,b,c);
   264    264   .import shell5.csv t1
   265    265     }
   266    266     sqlite3 db test.db
   267    267     db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
   268    268   } {1 {} 11 | 2 x 22 | 3 {"} 33 | 4 hello 44 | 5 55 {} | 6 66 x | 7 77 {"} | 8 88 hello | {} 9 99 | x 10 110 | {"} 11 121 | hello 12 132 |}
          269  +db close
          270  +
          271  +# Import columns containing quoted strings
          272  +do_test shell5-1.10 {
          273  +  set out [open shell5.csv w]
          274  +  fconfigure $out -translation lf
          275  +  puts $out {column1,column2,column3,column4}
          276  +  puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4"
          277  +  puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4"
          278  +  close $out
          279  +  forcedelete test.db
          280  +  catchcmd test.db {.mode csv
          281  +    CREATE TABLE t1(a,b,c,d);
          282  +.import shell5.csv t1
          283  +  }
          284  +  sqlite3 db test.db
          285  +  db eval {SELECT hex(c) FROM t1 ORDER BY rowid}
          286  +} {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033}
          287  +
   269    288   db close
   270    289   
   271    290   finish_test

Changes to test/speedtest1.c.

   661    661     speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz);
   662    662     speedtest1_exec("REPLACE INTO t2(a,b,c) SELECT a,b,c FROM t1");
   663    663     speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM t1");
   664    664     speedtest1_end_test();
   665    665   
   666    666   
   667    667     n = sz/5;
   668         -  speedtest1_begin_test(290, "%d four-ways joins", n);
          668  +  speedtest1_begin_test(300, "%d four-ways joins", n);
   669    669     speedtest1_exec("BEGIN");
   670    670     speedtest1_prepare(
   671    671       "SELECT t1.c FROM t1, t2, t3, t4\n"
   672    672       " WHERE t4.a BETWEEN ?1 AND ?2\n"
   673    673       "   AND t3.a=t4.b\n"
   674    674       "   AND t2.a=t3.b\n"
   675    675       "   AND t1.c=t2.c"

Changes to test/tester.tcl.

  1253   1253   #   crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
  1254   1254   #
  1255   1255   proc crashsql {args} {
  1256   1256   
  1257   1257     set blocksize ""
  1258   1258     set crashdelay 1
  1259   1259     set prngseed 0
         1260  +  set opendb { sqlite3 db test.db -vfs crash }
  1260   1261     set tclbody {}
  1261   1262     set crashfile ""
  1262   1263     set dc ""
  1263   1264     set sql [lindex $args end]
  1264   1265   
  1265   1266     for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
  1266   1267       set z [lindex $args $ii]
  1267   1268       set n [string length $z]
  1268   1269       set z2 [lindex $args [expr $ii+1]]
  1269   1270   
  1270   1271       if     {$n>1 && [string first $z -delay]==0}     {set crashdelay $z2} \
         1272  +    elseif {$n>1 && [string first $z -opendb]==0}    {set opendb $z2} \
  1271   1273       elseif {$n>1 && [string first $z -seed]==0}      {set prngseed $z2} \
  1272   1274       elseif {$n>1 && [string first $z -file]==0}      {set crashfile $z2}  \
  1273   1275       elseif {$n>1 && [string first $z -tclbody]==0}   {set tclbody $z2}  \
  1274   1276       elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
  1275   1277       elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
  1276   1278       else   { error "Unrecognized option: $z" }
  1277   1279     }
................................................................................
  1285   1287     # default, so here we force it to the "nativename" format.
  1286   1288     set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
  1287   1289   
  1288   1290     set f [open crash.tcl w]
  1289   1291     puts $f "sqlite3_crash_enable 1"
  1290   1292     puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
  1291   1293     puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
  1292         -  puts $f "sqlite3 db test.db -vfs crash"
         1294  +  puts $f $opendb 
  1293   1295   
  1294   1296     # This block sets the cache size of the main database to 10
  1295   1297     # pages. This is done in case the build is configured to omit
  1296   1298     # "PRAGMA cache_size".
  1297   1299     puts $f {db eval {SELECT * FROM sqlite_master;}}
  1298   1300     puts $f {set bt [btree_from_db db]}
  1299   1301     puts $f {btree_set_cache_size $bt 10}
         1302  +
  1300   1303     if {$prngseed} {
  1301   1304       set seed [expr {$prngseed%10007+1}]
  1302   1305       # puts seed=$seed
  1303   1306       puts $f "db eval {SELECT randomblob($seed)}"
  1304   1307     }
  1305   1308   
  1306   1309     if {[string length $tclbody]>0} {

Changes to test/win32longpath.test.

    14     14   #
    15     15   
    16     16   if {$tcl_platform(platform)!="windows"} return
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   set testprefix win32longpath
           21  +
           22  +do_test 1.0 {
           23  +  file_control_vfsname db
           24  +} win32
    21     25   
    22     26   db close
    23     27   set path [file nativename [get_pwd]]
    24     28   sqlite3 db [file join $path test.db] -vfs win32-longpath
    25     29   
    26     30   do_test 1.1 {
           31  +  file_control_vfsname db
           32  +} win32-longpath
           33  +
           34  +do_test 1.2 {
    27     35     db eval {
    28     36       BEGIN EXCLUSIVE;
    29     37       CREATE TABLE t1(x);
    30     38       INSERT INTO t1 VALUES(1);
    31     39       INSERT INTO t1 VALUES(2);
    32     40       INSERT INTO t1 VALUES(3);
    33     41       INSERT INTO t1 VALUES(4);
................................................................................
    43     51   make_win32_dir $longPath(2)
    44     52   
    45     53   set longPath(3) $longPath(2)\\[string repeat Y 255]
    46     54   make_win32_dir $longPath(3)
    47     55   
    48     56   set fileName $longPath(3)\\test.db
    49     57   
    50         -do_test 1.2 {
           58  +do_test 1.3 {
    51     59     list [catch {sqlite3 db2 [string range $fileName 4 end]} msg] $msg
    52     60   } {1 {unable to open database file}}
    53     61   
    54     62   sqlite3 db3 $fileName -vfs win32-longpath
    55     63   
    56         -do_test 1.3 {
           64  +do_test 1.4 {
    57     65     db3 eval {
    58     66       BEGIN EXCLUSIVE;
    59     67       CREATE TABLE t1(x);
    60     68       INSERT INTO t1 VALUES(5);
    61     69       INSERT INTO t1 VALUES(6);
    62     70       INSERT INTO t1 VALUES(7);
    63     71       INSERT INTO t1 VALUES(8);
................................................................................
    67     75   } {5 6 7 8}
    68     76   
    69     77   db3 close
    70     78   # puts "  Database exists \{[exists_win32_path $fileName]\}"
    71     79   
    72     80   sqlite3 db3 $fileName -vfs win32-longpath
    73     81   
    74         -do_test 1.4 {
           82  +do_test 1.5 {
    75     83     db3 eval {
    76     84       PRAGMA journal_mode = WAL;
    77     85     }
    78     86   } {wal}
    79     87   
    80         -do_test 1.5 {
           88  +do_test 1.6 {
    81     89     db3 eval {
    82     90       BEGIN EXCLUSIVE;
    83     91       INSERT INTO t1 VALUES(9);
    84     92       INSERT INTO t1 VALUES(10);
    85     93       INSERT INTO t1 VALUES(11);
    86     94       INSERT INTO t1 VALUES(12);
    87     95       SELECT x FROM t1 ORDER BY x;

Changes to tool/vdbe-compress.tcl.

     9      9   # the same result.  The modifications made by this script merely help
    10     10   # the C compiler to generate code for sqlite3VdbeExec() that uses less
    11     11   # stack space.
    12     12   #
    13     13   # Script usage:
    14     14   #
    15     15   #          mv vdbe.c vdbe.c.template
    16         -#          tclsh vdbe-compress.tcl <vdbe.c.template >vdbe.c
           16  +#          tclsh vdbe-compress.tcl $CFLAGS <vdbe.c.template >vdbe.c
    17     17   #
    18     18   # Modifications made:
    19     19   #
    20     20   # All modifications are within the sqlite3VdbeExec() function.  The
    21     21   # modifications seek to reduce the amount of stack space allocated by
    22     22   # this routine by moving local variable declarations out of individual
    23     23   # opcode implementations and into a single large union.  The union contains
................................................................................
    37     37   #
    38     38   #############################################################################
    39     39   #
    40     40   set beforeUnion {}   ;# C code before union
    41     41   set unionDef {}      ;# C code of the union
    42     42   set afterUnion {}    ;# C code after the union
    43     43   set sCtr 0           ;# Context counter
           44  +
           45  +# If the SQLITE_SMALL_STACK compile-time option is missing, then
           46  +# this transformation becomes a no-op.
           47  +#
           48  +if {![regexp {SQLITE_SMALL_STACK} $argv]} {
           49  +  while {![eof stdin]} {
           50  +    puts [gets stdin]
           51  +  }
           52  +  exit
           53  +}
    44     54   
    45     55   # Read program text up to the spot where the union should be
    46     56   # inserted.
    47     57   #
    48     58   while {![eof stdin]} {
    49     59     set line [gets stdin]
    50     60     if {[regexp {INSERT STACK UNION HERE} $line]} break