/ Check-in [e051e8f2]
Login

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

Overview
Comment:Merge latest trunk with this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | schemalint
Files: files | file ages | folders
SHA3-256: e051e8f21b909c5e89a7203eef6501fecd64f82a634c4e10398b5ef7d716eb37
User & Date: dan 2017-04-08 19:00:37
Context
2017-04-09
08:38
Fix the -file option on the sqlite3_expert program. check-in: 0857c48e user: dan tags: schemalint
2017-04-08
19:00
Merge latest trunk with this branch. check-in: e051e8f2 user: dan tags: schemalint
18:56
Rename shell6.test to expert1.test. Have it invoke the sqlite3_expert binary if it is present. check-in: be0deff9 user: dan tags: schemalint
18:18
Disallow leading zeros on numeric constants in JSON. Fix for ticket [b93be8729a895a528e2] check-in: 204e72f0 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to configure.

 11248  11248   if test "${enable_debug+set}" = set; then :
 11249  11249     enableval=$enable_debug; use_debug=$enableval
 11250  11250   else
 11251  11251     use_debug=no
 11252  11252   fi
 11253  11253   
 11254  11254   if test "${use_debug}" = "yes" ; then
 11255         -  TARGET_DEBUG="-DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE"
        11255  +  TARGET_DEBUG="-DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE -O0"
 11256  11256   else
 11257  11257     TARGET_DEBUG="-DNDEBUG"
 11258  11258   fi
 11259  11259   
 11260  11260   
 11261  11261   #########
 11262  11262   # See whether we should use the amalgamation to build
................................................................................
 11352  11352   else
 11353  11353     enable_memsys5=no
 11354  11354   fi
 11355  11355   
 11356  11356   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support MEMSYS5" >&5
 11357  11357   $as_echo_n "checking whether to support MEMSYS5... " >&6; }
 11358  11358   if test "${enable_memsys5}" = "yes"; then
 11359         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS5"
        11359  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_MEMSYS5"
 11360  11360     { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 11361  11361   $as_echo "yes" >&6; }
 11362  11362   else
 11363  11363     { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 11364  11364   $as_echo "no" >&6; }
 11365  11365   fi
 11366  11366   # Check whether --enable-memsys3 was given.
................................................................................
 11369  11369   else
 11370  11370     enable_memsys3=no
 11371  11371   fi
 11372  11372   
 11373  11373   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support MEMSYS3" >&5
 11374  11374   $as_echo_n "checking whether to support MEMSYS3... " >&6; }
 11375  11375   if test "${enable_memsys3}" = "yes" -a "${enable_memsys5}" = "no"; then
 11376         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS3"
        11376  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_MEMSYS3"
 11377  11377     { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 11378  11378   $as_echo "yes" >&6; }
 11379  11379   else
 11380  11380     { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 11381  11381   $as_echo "no" >&6; }
 11382  11382   fi
 11383  11383   
................................................................................
 11387  11387   if test "${enable_fts3+set}" = set; then :
 11388  11388     enableval=$enable_fts3; enable_fts3=yes
 11389  11389   else
 11390  11390     enable_fts3=no
 11391  11391   fi
 11392  11392   
 11393  11393   if test "${enable_fts3}" = "yes" ; then
 11394         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS3"
        11394  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3"
 11395  11395   fi
 11396  11396   # Check whether --enable-fts4 was given.
 11397  11397   if test "${enable_fts4+set}" = set; then :
 11398  11398     enableval=$enable_fts4; enable_fts4=yes
 11399  11399   else
 11400  11400     enable_fts4=no
 11401  11401   fi
 11402  11402   
 11403  11403   if test "${enable_fts4}" = "yes" ; then
 11404         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS4"
        11404  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS4"
 11405  11405     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing log" >&5
 11406  11406   $as_echo_n "checking for library containing log... " >&6; }
 11407  11407   if ${ac_cv_search_log+:} false; then :
 11408  11408     $as_echo_n "(cached) " >&6
 11409  11409   else
 11410  11410     ac_func_search_save_LIBS=$LIBS
 11411  11411   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
................................................................................
 11463  11463   if test "${enable_fts5+set}" = set; then :
 11464  11464     enableval=$enable_fts5; enable_fts5=yes
 11465  11465   else
 11466  11466     enable_fts5=no
 11467  11467   fi
 11468  11468   
 11469  11469   if test "${enable_fts5}" = "yes" ; then
 11470         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS5"
        11470  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS5"
 11471  11471     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing log" >&5
 11472  11472   $as_echo_n "checking for library containing log... " >&6; }
 11473  11473   if ${ac_cv_search_log+:} false; then :
 11474  11474     $as_echo_n "(cached) " >&6
 11475  11475   else
 11476  11476     ac_func_search_save_LIBS=$LIBS
 11477  11477   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
................................................................................
 11532  11532   if test "${enable_json1+set}" = set; then :
 11533  11533     enableval=$enable_json1; enable_json1=yes
 11534  11534   else
 11535  11535     enable_json1=no
 11536  11536   fi
 11537  11537   
 11538  11538   if test "${enable_json1}" = "yes" ; then
 11539         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_JSON1"
        11539  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_JSON1"
 11540  11540   fi
 11541  11541   
 11542  11542   #########
 11543  11543   # See whether we should enable RTREE
 11544  11544   # Check whether --enable-rtree was given.
 11545  11545   if test "${enable_rtree+set}" = set; then :
 11546  11546     enableval=$enable_rtree; enable_rtree=yes
 11547  11547   else
 11548  11548     enable_rtree=no
 11549  11549   fi
 11550  11550   
 11551  11551   if test "${enable_rtree}" = "yes" ; then
 11552         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_RTREE"
        11552  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE"
 11553  11553   fi
 11554  11554   
 11555  11555   #########
 11556  11556   # See whether we should enable the SESSION extension
 11557  11557   # Check whether --enable-session was given.
 11558  11558   if test "${enable_session+set}" = set; then :
 11559  11559     enableval=$enable_session; enable_session=yes
 11560  11560   else
 11561  11561     enable_session=no
 11562  11562   fi
 11563  11563   
 11564  11564   if test "${enable_session}" = "yes" ; then
 11565         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_SESSION"
 11566         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_PREUPDATE_HOOK"
        11565  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SESSION"
        11566  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PREUPDATE_HOOK"
 11567  11567   fi
 11568  11568   
 11569  11569   #########
 11570  11570   # attempt to duplicate any OMITS and ENABLES into the $(OPT_FEATURE_FLAGS) parameter
 11571  11571   for option in $CFLAGS $CPPFLAGS
 11572  11572   do
 11573  11573     case $option in

Changes to configure.ac.

   556    556   AC_SEARCH_LIBS(fdatasync, [rt])
   557    557   
   558    558   #########
   559    559   # check for debug enabled
   560    560   AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug],[enable debugging & verbose explain]),
   561    561         [use_debug=$enableval],[use_debug=no])
   562    562   if test "${use_debug}" = "yes" ; then
   563         -  TARGET_DEBUG="-DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE"
          563  +  TARGET_DEBUG="-DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE -O0"
   564    564   else
   565    565     TARGET_DEBUG="-DNDEBUG"
   566    566   fi
   567    567   AC_SUBST(TARGET_DEBUG)
   568    568   
   569    569   #########
   570    570   # See whether we should use the amalgamation to build
................................................................................
   592    592   # Do we want to support memsys3 and/or memsys5
   593    593   #
   594    594   AC_ARG_ENABLE(memsys5, 
   595    595     AC_HELP_STRING([--enable-memsys5],[Enable MEMSYS5]),
   596    596     [enable_memsys5=yes],[enable_memsys5=no])
   597    597   AC_MSG_CHECKING([whether to support MEMSYS5])
   598    598   if test "${enable_memsys5}" = "yes"; then
   599         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS5"
          599  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_MEMSYS5"
   600    600     AC_MSG_RESULT([yes])
   601    601   else
   602    602     AC_MSG_RESULT([no])
   603    603   fi
   604    604   AC_ARG_ENABLE(memsys3, 
   605    605     AC_HELP_STRING([--enable-memsys3],[Enable MEMSYS3]),
   606    606     [enable_memsys3=yes],[enable_memsys3=no])
   607    607   AC_MSG_CHECKING([whether to support MEMSYS3])
   608    608   if test "${enable_memsys3}" = "yes" -a "${enable_memsys5}" = "no"; then
   609         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS3"
          609  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_MEMSYS3"
   610    610     AC_MSG_RESULT([yes])
   611    611   else
   612    612     AC_MSG_RESULT([no])
   613    613   fi
   614    614   
   615    615   #########
   616    616   # See whether we should enable Full Text Search extensions
   617    617   AC_ARG_ENABLE(fts3, AC_HELP_STRING([--enable-fts3],
   618    618         [Enable the FTS3 extension]),
   619    619         [enable_fts3=yes],[enable_fts3=no])
   620    620   if test "${enable_fts3}" = "yes" ; then
   621         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS3"
          621  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3"
   622    622   fi
   623    623   AC_ARG_ENABLE(fts4, AC_HELP_STRING([--enable-fts4],
   624    624         [Enable the FTS4 extension]),
   625    625         [enable_fts4=yes],[enable_fts4=no])
   626    626   if test "${enable_fts4}" = "yes" ; then
   627         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS4"
          627  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS4"
   628    628     AC_SEARCH_LIBS([log],[m])
   629    629   fi
   630    630   AC_ARG_ENABLE(fts5, AC_HELP_STRING([--enable-fts5],
   631    631         [Enable the FTS5 extension]),
   632    632         [enable_fts5=yes],[enable_fts5=no])
   633    633   if test "${enable_fts5}" = "yes" ; then
   634         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS5"
          634  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS5"
   635    635     AC_SEARCH_LIBS([log],[m])
   636    636   fi
   637    637   
   638    638   #########
   639    639   # See whether we should enable JSON1
   640    640   AC_ARG_ENABLE(json1, AC_HELP_STRING([--enable-json1],
   641    641         [Enable the JSON1 extension]),
   642    642         [enable_json1=yes],[enable_json1=no])
   643    643   if test "${enable_json1}" = "yes" ; then
   644         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_JSON1"
          644  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_JSON1"
   645    645   fi
   646    646   
   647    647   #########
   648    648   # See whether we should enable RTREE
   649    649   AC_ARG_ENABLE(rtree, AC_HELP_STRING([--enable-rtree],
   650    650         [Enable the RTREE extension]),
   651    651         [enable_rtree=yes],[enable_rtree=no])
   652    652   if test "${enable_rtree}" = "yes" ; then
   653         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_RTREE"
          653  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE"
   654    654   fi
   655    655   
   656    656   #########
   657    657   # See whether we should enable the SESSION extension
   658    658   AC_ARG_ENABLE(session, AC_HELP_STRING([--enable-session],
   659    659         [Enable the SESSION extension]),
   660    660         [enable_session=yes],[enable_session=no])
   661    661   if test "${enable_session}" = "yes" ; then
   662         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_SESSION"
   663         -  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_PREUPDATE_HOOK"
          662  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SESSION"
          663  +  OPT_FEATURE_FLAGS="$(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PREUPDATE_HOOK"
   664    664   fi
   665    665   
   666    666   #########
   667    667   # attempt to duplicate any OMITS and ENABLES into the $(OPT_FEATURE_FLAGS) parameter
   668    668   for option in $CFLAGS $CPPFLAGS
   669    669   do
   670    670     case $option in

Changes to ext/fts5/fts5Int.h.

   442    442     Fts5Index *p,                   /* Index to write to */
   443    443     int bDelete,                    /* True if current operation is a delete */
   444    444     i64 iDocid                      /* Docid to add or remove data from */
   445    445   );
   446    446   
   447    447   /*
   448    448   ** Flush any data stored in the in-memory hash tables to the database.
   449         -** If the bCommit flag is true, also close any open blob handles.
          449  +** Also close any open blob handles.
   450    450   */
   451         -int sqlite3Fts5IndexSync(Fts5Index *p, int bCommit);
          451  +int sqlite3Fts5IndexSync(Fts5Index *p);
   452    452   
   453    453   /*
   454    454   ** Discard any data stored in the in-memory hash tables. Do not write it
   455    455   ** to the database. Additionally, assume that the contents of the %_data
   456    456   ** table may have changed on disk. So any in-memory caches of %_data 
   457    457   ** records must be invalidated.
   458    458   */
................................................................................
   614    614   int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
   615    615   void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
   616    616   
   617    617   int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
   618    618   int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
   619    619   int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
   620    620   
   621         -int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit);
          621  +int sqlite3Fts5StorageSync(Fts5Storage *p);
   622    622   int sqlite3Fts5StorageRollback(Fts5Storage *p);
   623    623   
   624    624   int sqlite3Fts5StorageConfigValue(
   625    625       Fts5Storage *p, const char*, sqlite3_value*, int
   626    626   );
   627    627   
   628    628   int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);

Changes to ext/fts5/fts5_index.c.

   624    624     if( p->pReader ){
   625    625       sqlite3_blob *pReader = p->pReader;
   626    626       p->pReader = 0;
   627    627       sqlite3_blob_close(pReader);
   628    628     }
   629    629   }
   630    630   
   631         -
   632    631   /*
   633    632   ** Retrieve a record from the %_data table.
   634    633   **
   635    634   ** If an error occurs, NULL is returned and an error left in the 
   636    635   ** Fts5Index object.
   637    636   */
   638    637   static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
................................................................................
  5127   5126     p->bDelete = bDelete;
  5128   5127     return fts5IndexReturn(p);
  5129   5128   }
  5130   5129   
  5131   5130   /*
  5132   5131   ** Commit data to disk.
  5133   5132   */
  5134         -int sqlite3Fts5IndexSync(Fts5Index *p, int bCommit){
         5133  +int sqlite3Fts5IndexSync(Fts5Index *p){
  5135   5134     assert( p->rc==SQLITE_OK );
  5136   5135     fts5IndexFlush(p);
  5137         -  if( bCommit ) fts5CloseReader(p);
         5136  +  fts5CloseReader(p);
  5138   5137     return fts5IndexReturn(p);
  5139   5138   }
  5140   5139   
  5141   5140   /*
  5142   5141   ** Discard any data stored in the in-memory hash tables. Do not write it
  5143   5142   ** to the database. Additionally, assume that the contents of the %_data
  5144   5143   ** table may have changed on disk. So any in-memory caches of %_data 

Changes to ext/fts5/fts5_main.c.

  1575   1575   */
  1576   1576   static int fts5SyncMethod(sqlite3_vtab *pVtab){
  1577   1577     int rc;
  1578   1578     Fts5Table *pTab = (Fts5Table*)pVtab;
  1579   1579     fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
  1580   1580     pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg;
  1581   1581     fts5TripCursors(pTab);
  1582         -  rc = sqlite3Fts5StorageSync(pTab->pStorage, 1);
         1582  +  rc = sqlite3Fts5StorageSync(pTab->pStorage);
  1583   1583     pTab->pConfig->pzErrmsg = 0;
  1584   1584     return rc;
  1585   1585   }
  1586   1586   
  1587   1587   /*
  1588   1588   ** Implementation of xBegin() method. 
  1589   1589   */
................................................................................
  2386   2386   ** Flush the contents of the pending-terms table to disk.
  2387   2387   */
  2388   2388   static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
  2389   2389     Fts5Table *pTab = (Fts5Table*)pVtab;
  2390   2390     UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
  2391   2391     fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
  2392   2392     fts5TripCursors(pTab);
  2393         -  return sqlite3Fts5StorageSync(pTab->pStorage, 0);
         2393  +  return sqlite3Fts5StorageSync(pTab->pStorage);
  2394   2394   }
  2395   2395   
  2396   2396   /*
  2397   2397   ** The xRelease() method.
  2398   2398   **
  2399   2399   ** This is a no-op.
  2400   2400   */
  2401   2401   static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
  2402   2402     Fts5Table *pTab = (Fts5Table*)pVtab;
  2403   2403     UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
  2404   2404     fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
  2405   2405     fts5TripCursors(pTab);
  2406         -  return sqlite3Fts5StorageSync(pTab->pStorage, 0);
         2406  +  return sqlite3Fts5StorageSync(pTab->pStorage);
  2407   2407   }
  2408   2408   
  2409   2409   /*
  2410   2410   ** The xRollbackTo() method.
  2411   2411   **
  2412   2412   ** Discard the contents of the pending terms table.
  2413   2413   */

Changes to ext/fts5/fts5_storage.c.

   214    214           pConfig->zDb, pConfig->zName, zTail, zName, zTail
   215    215       );
   216    216     }
   217    217   }
   218    218   
   219    219   int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
   220    220     Fts5Config *pConfig = pStorage->pConfig;
   221         -  int rc = sqlite3Fts5StorageSync(pStorage, 1);
          221  +  int rc = sqlite3Fts5StorageSync(pStorage);
   222    222   
   223    223     fts5StorageRenameOne(pConfig, &rc, "data", zName);
   224    224     fts5StorageRenameOne(pConfig, &rc, "idx", zName);
   225    225     fts5StorageRenameOne(pConfig, &rc, "config", zName);
   226    226     if( pConfig->bColumnsize ){
   227    227       fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
   228    228     }
................................................................................
  1077   1077     }
  1078   1078     return rc;
  1079   1079   }
  1080   1080   
  1081   1081   /*
  1082   1082   ** Flush any data currently held in-memory to disk.
  1083   1083   */
  1084         -int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
         1084  +int sqlite3Fts5StorageSync(Fts5Storage *p){
  1085   1085     int rc = SQLITE_OK;
  1086   1086     i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
  1087   1087     if( p->bTotalsValid ){
  1088   1088       rc = fts5StorageSaveTotals(p);
  1089         -    if( bCommit ) p->bTotalsValid = 0;
         1089  +    p->bTotalsValid = 0;
  1090   1090     }
  1091   1091     if( rc==SQLITE_OK ){
  1092         -    rc = sqlite3Fts5IndexSync(p->pIndex, bCommit);
         1092  +    rc = sqlite3Fts5IndexSync(p->pIndex);
  1093   1093     }
  1094   1094     sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
  1095   1095     return rc;
  1096   1096   }
  1097   1097   
  1098   1098   int sqlite3Fts5StorageRollback(Fts5Storage *p){
  1099   1099     p->bTotalsValid = 0;

Changes to ext/fts5/test/fts5aa.test.

   556    556   ]
   557    557   do_test 20.1 {
   558    558     foreach id $::ids {
   559    559       execsql { INSERT INTO tmp(rowid, x) VALUES($id, 'x y z') }
   560    560     }
   561    561     execsql { SELECT rowid FROM tmp WHERE tmp MATCH 'y' }
   562    562   } $::ids
          563  +
          564  +#--------------------------------------------------------------------
          565  +# Test that a DROP TABLE may be executed within a transaction that
          566  +# writes to an FTS5 table.
          567  +#
          568  +do_execsql_test 21.0 {
          569  +  CREATE TEMP TABLE t8(a, b);
          570  +  CREATE VIRTUAL TABLE ft USING fts5(x, detail=%DETAIL%);
          571  +}
          572  +
          573  +do_execsql_test 21.1 {
          574  +  BEGIN;
          575  +    INSERT INTO ft VALUES('a b c');
          576  +    DROP TABLE t8;
          577  +  COMMIT;
          578  +}
   563    579   
   564    580   }
   565    581   
   566    582   
   567    583   finish_test
   568    584   
   569    585   

Changes to ext/misc/json1.c.

   723    723   */
   724    724   static int jsonParseValue(JsonParse *pParse, u32 i){
   725    725     char c;
   726    726     u32 j;
   727    727     int iThis;
   728    728     int x;
   729    729     JsonNode *pNode;
   730         -  while( safe_isspace(pParse->zJson[i]) ){ i++; }
   731         -  if( (c = pParse->zJson[i])=='{' ){
          730  +  const char *z = pParse->zJson;
          731  +  while( safe_isspace(z[i]) ){ i++; }
          732  +  if( (c = z[i])=='{' ){
   732    733       /* Parse object */
   733    734       iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
   734    735       if( iThis<0 ) return -1;
   735    736       for(j=i+1;;j++){
   736         -      while( safe_isspace(pParse->zJson[j]) ){ j++; }
          737  +      while( safe_isspace(z[j]) ){ j++; }
   737    738         x = jsonParseValue(pParse, j);
   738    739         if( x<0 ){
   739    740           if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
   740    741           return -1;
   741    742         }
   742    743         if( pParse->oom ) return -1;
   743    744         pNode = &pParse->aNode[pParse->nNode-1];
   744    745         if( pNode->eType!=JSON_STRING ) return -1;
   745    746         pNode->jnFlags |= JNODE_LABEL;
   746    747         j = x;
   747         -      while( safe_isspace(pParse->zJson[j]) ){ j++; }
   748         -      if( pParse->zJson[j]!=':' ) return -1;
          748  +      while( safe_isspace(z[j]) ){ j++; }
          749  +      if( z[j]!=':' ) return -1;
   749    750         j++;
   750    751         x = jsonParseValue(pParse, j);
   751    752         if( x<0 ) return -1;
   752    753         j = x;
   753         -      while( safe_isspace(pParse->zJson[j]) ){ j++; }
   754         -      c = pParse->zJson[j];
          754  +      while( safe_isspace(z[j]) ){ j++; }
          755  +      c = z[j];
   755    756         if( c==',' ) continue;
   756    757         if( c!='}' ) return -1;
   757    758         break;
   758    759       }
   759    760       pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
   760    761       return j+1;
   761    762     }else if( c=='[' ){
   762    763       /* Parse array */
   763    764       iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
   764    765       if( iThis<0 ) return -1;
   765    766       for(j=i+1;;j++){
   766         -      while( safe_isspace(pParse->zJson[j]) ){ j++; }
          767  +      while( safe_isspace(z[j]) ){ j++; }
   767    768         x = jsonParseValue(pParse, j);
   768    769         if( x<0 ){
   769    770           if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
   770    771           return -1;
   771    772         }
   772    773         j = x;
   773         -      while( safe_isspace(pParse->zJson[j]) ){ j++; }
   774         -      c = pParse->zJson[j];
          774  +      while( safe_isspace(z[j]) ){ j++; }
          775  +      c = z[j];
   775    776         if( c==',' ) continue;
   776    777         if( c!=']' ) return -1;
   777    778         break;
   778    779       }
   779    780       pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
   780    781       return j+1;
   781    782     }else if( c=='"' ){
   782    783       /* Parse string */
   783    784       u8 jnFlags = 0;
   784    785       j = i+1;
   785    786       for(;;){
   786         -      c = pParse->zJson[j];
          787  +      c = z[j];
   787    788         if( c==0 ) return -1;
   788    789         if( c=='\\' ){
   789         -        c = pParse->zJson[++j];
          790  +        c = z[++j];
   790    791           if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
   791    792              || c=='n' || c=='r' || c=='t'
   792         -           || (c=='u' && jsonIs4Hex(pParse->zJson+j+1)) ){
          793  +           || (c=='u' && jsonIs4Hex(z+j+1)) ){
   793    794             jnFlags = JNODE_ESCAPE;
   794    795           }else{
   795    796             return -1;
   796    797           }
   797    798         }else if( c=='"' ){
   798    799           break;
   799    800         }
   800    801         j++;
   801    802       }
   802         -    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
          803  +    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
   803    804       if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
   804    805       return j+1;
   805    806     }else if( c=='n'
   806         -         && strncmp(pParse->zJson+i,"null",4)==0
   807         -         && !safe_isalnum(pParse->zJson[i+4]) ){
          807  +         && strncmp(z+i,"null",4)==0
          808  +         && !safe_isalnum(z[i+4]) ){
   808    809       jsonParseAddNode(pParse, JSON_NULL, 0, 0);
   809    810       return i+4;
   810    811     }else if( c=='t'
   811         -         && strncmp(pParse->zJson+i,"true",4)==0
   812         -         && !safe_isalnum(pParse->zJson[i+4]) ){
          812  +         && strncmp(z+i,"true",4)==0
          813  +         && !safe_isalnum(z[i+4]) ){
   813    814       jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
   814    815       return i+4;
   815    816     }else if( c=='f'
   816         -         && strncmp(pParse->zJson+i,"false",5)==0
   817         -         && !safe_isalnum(pParse->zJson[i+5]) ){
          817  +         && strncmp(z+i,"false",5)==0
          818  +         && !safe_isalnum(z[i+5]) ){
   818    819       jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
   819    820       return i+5;
   820    821     }else if( c=='-' || (c>='0' && c<='9') ){
   821    822       /* Parse number */
   822    823       u8 seenDP = 0;
   823    824       u8 seenE = 0;
          825  +    assert( '-' < '0' );
          826  +    if( c<='0' ){
          827  +      j = c=='-' ? i+1 : i;
          828  +      if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
          829  +    }
   824    830       j = i+1;
   825    831       for(;; j++){
   826         -      c = pParse->zJson[j];
          832  +      c = z[j];
   827    833         if( c>='0' && c<='9' ) continue;
   828    834         if( c=='.' ){
   829         -        if( pParse->zJson[j-1]=='-' ) return -1;
          835  +        if( z[j-1]=='-' ) return -1;
   830    836           if( seenDP ) return -1;
   831    837           seenDP = 1;
   832    838           continue;
   833    839         }
   834    840         if( c=='e' || c=='E' ){
   835         -        if( pParse->zJson[j-1]<'0' ) return -1;
          841  +        if( z[j-1]<'0' ) return -1;
   836    842           if( seenE ) return -1;
   837    843           seenDP = seenE = 1;
   838         -        c = pParse->zJson[j+1];
          844  +        c = z[j+1];
   839    845           if( c=='+' || c=='-' ){
   840    846             j++;
   841         -          c = pParse->zJson[j+1];
          847  +          c = z[j+1];
   842    848           }
   843    849           if( c<'0' || c>'9' ) return -1;
   844    850           continue;
   845    851         }
   846    852         break;
   847    853       }
   848         -    if( pParse->zJson[j-1]<'0' ) return -1;
          854  +    if( z[j-1]<'0' ) return -1;
   849    855       jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
   850         -                        j - i, &pParse->zJson[i]);
          856  +                        j - i, &z[i]);
   851    857       return j;
   852    858     }else if( c=='}' ){
   853    859       return -2;  /* End of {...} */
   854    860     }else if( c==']' ){
   855    861       return -3;  /* End of [...] */
   856    862     }else if( c==0 ){
   857    863       return 0;   /* End of file */

Changes to ext/rtree/rtree.c.

  3194   3194       "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
  3195   3195       "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
  3196   3196       , pRtree->zDb, pRtree->zName, zNewName 
  3197   3197       , pRtree->zDb, pRtree->zName, zNewName 
  3198   3198       , pRtree->zDb, pRtree->zName, zNewName
  3199   3199     );
  3200   3200     if( zSql ){
         3201  +    nodeBlobReset(pRtree);
  3201   3202       rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
  3202   3203       sqlite3_free(zSql);
  3203   3204     }
  3204   3205     return rc;
  3205   3206   }
  3206   3207   
         3208  +/*
         3209  +** The xSavepoint method.
         3210  +**
         3211  +** This module does not need to do anything to support savepoints. However,
         3212  +** it uses this hook to close any open blob handle. This is done because a 
         3213  +** DROP TABLE command - which fortunately always opens a savepoint - cannot 
         3214  +** succeed if there are any open blob handles. i.e. if the blob handle were
         3215  +** not closed here, the following would fail:
         3216  +**
         3217  +**   BEGIN;
         3218  +**     INSERT INTO rtree...
         3219  +**     DROP TABLE <tablename>;    -- Would fail with SQLITE_LOCKED
         3220  +**   COMMIT;
         3221  +*/
         3222  +static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
         3223  +  Rtree *pRtree = (Rtree *)pVtab;
         3224  +  int iwt = pRtree->inWrTrans;
         3225  +  pRtree->inWrTrans = 0;
         3226  +  nodeBlobReset(pRtree);
         3227  +  pRtree->inWrTrans = iwt;
         3228  +  return SQLITE_OK;
         3229  +}
  3207   3230   
  3208   3231   /*
  3209   3232   ** This function populates the pRtree->nRowEst variable with an estimate
  3210   3233   ** of the number of rows in the virtual table. If possible, this is based
  3211   3234   ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
  3212   3235   */
  3213   3236   static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
................................................................................
  3246   3269       sqlite3_free(zSql);
  3247   3270     }
  3248   3271   
  3249   3272     return rc;
  3250   3273   }
  3251   3274   
  3252   3275   static sqlite3_module rtreeModule = {
  3253         -  0,                          /* iVersion */
         3276  +  2,                          /* iVersion */
  3254   3277     rtreeCreate,                /* xCreate - create a table */
  3255   3278     rtreeConnect,               /* xConnect - connect to an existing table */
  3256   3279     rtreeBestIndex,             /* xBestIndex - Determine search strategy */
  3257   3280     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
  3258   3281     rtreeDestroy,               /* xDestroy - Drop a table */
  3259   3282     rtreeOpen,                  /* xOpen - open a cursor */
  3260   3283     rtreeClose,                 /* xClose - close a cursor */
................................................................................
  3266   3289     rtreeUpdate,                /* xUpdate - write data */
  3267   3290     rtreeBeginTransaction,      /* xBegin - begin transaction */
  3268   3291     rtreeEndTransaction,        /* xSync - sync transaction */
  3269   3292     rtreeEndTransaction,        /* xCommit - commit transaction */
  3270   3293     rtreeEndTransaction,        /* xRollback - rollback transaction */
  3271   3294     0,                          /* xFindFunction - function overloading */
  3272   3295     rtreeRename,                /* xRename - rename the table */
  3273         -  0,                          /* xSavepoint */
         3296  +  rtreeSavepoint,             /* xSavepoint */
  3274   3297     0,                          /* xRelease */
  3275   3298     0,                          /* xRollbackTo */
  3276   3299   };
  3277   3300   
  3278   3301   static int rtreeSqlInit(
  3279   3302     Rtree *pRtree, 
  3280   3303     sqlite3 *db, 

Changes to ext/rtree/rtree1.test.

    35     35   #   rtree-12.*: Test that on-conflict clauses are supported.
    36     36   #   rtree-13.*: Test that bug [d2889096e7bdeac6d] has been fixed.
    37     37   #   rtree-14.*: Test if a non-integer is inserted into the PK column of an
    38     38   #               r-tree table, it is converted to an integer before being
    39     39   #               inserted. Also that if a non-numeric is inserted into one
    40     40   #               of the min/max dimension columns, it is converted to the
    41     41   #               required type before being inserted.
           42  +#   rtree-15.*: Check that DROP TABLE works within a transaction that
           43  +#               writes to an r-tree table.
    42     44   #
    43     45   
    44     46   ifcapable !rtree {
    45     47     finish_test
    46     48     return
    47     49   }
    48     50   
................................................................................
   587    589   do_execsql_test 14.5 {
   588    590     SELECT * FROM t10;
   589    591   } {
   590    592     1 0 0
   591    593     2 52 81
   592    594     3 42 49
   593    595   }
          596  +
          597  +#-------------------------------------------------------------------------
          598  +#
          599  +do_execsql_test 15.0 {
          600  +  CREATE VIRTUAL TABLE rt USING rtree(id, x1,x2, y1,y2);
          601  +  CREATE TEMP TABLE t13(a, b, c);
          602  +}
          603  +do_execsql_test 15.1 {
          604  +  BEGIN;
          605  +  INSERT INTO rt VALUES(1,2,3,4,5);
          606  +}
          607  +breakpoint
          608  +do_execsql_test 15.2 {
          609  +  DROP TABLE t13;
          610  +  COMMIT;
          611  +}
   594    612   
   595    613   finish_test

Changes to src/btree.c.

  4313   4313   ** Using this cache reduces the number of calls to btreeParseCell().
  4314   4314   */
  4315   4315   #ifndef NDEBUG
  4316   4316     static void assertCellInfo(BtCursor *pCur){
  4317   4317       CellInfo info;
  4318   4318       int iPage = pCur->iPage;
  4319   4319       memset(&info, 0, sizeof(info));
  4320         -    btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
         4320  +    btreeParseCell(pCur->apPage[iPage], pCur->ix, &info);
  4321   4321       assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
  4322   4322     }
  4323   4323   #else
  4324   4324     #define assertCellInfo(x)
  4325   4325   #endif
  4326   4326   static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
  4327   4327     if( pCur->info.nSize==0 ){
  4328   4328       int iPage = pCur->iPage;
  4329   4329       pCur->curFlags |= BTCF_ValidNKey;
  4330         -    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
         4330  +    btreeParseCell(pCur->apPage[iPage],pCur->ix,&pCur->info);
  4331   4331     }else{
  4332   4332       assertCellInfo(pCur);
  4333   4333     }
  4334   4334   }
  4335   4335   
  4336   4336   #ifndef NDEBUG  /* The next routine used only within assert() statements */
  4337   4337   /*
................................................................................
  4530   4530   #ifdef SQLITE_DIRECT_OVERFLOW_READ
  4531   4531     unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
  4532   4532   #endif
  4533   4533   
  4534   4534     assert( pPage );
  4535   4535     assert( eOp==0 || eOp==1 );
  4536   4536     assert( pCur->eState==CURSOR_VALID );
  4537         -  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
         4537  +  assert( pCur->ix<pPage->nCell );
  4538   4538     assert( cursorHoldsMutex(pCur) );
  4539   4539   
  4540   4540     getCellInfo(pCur);
  4541   4541     aPayload = pCur->info.pPayload;
  4542   4542     assert( offset+amt <= pCur->info.nPayload );
  4543   4543   
  4544   4544     assert( aPayload > pPage->aData );
................................................................................
  4717   4717   ** wrong.  An error is returned if "offset+amt" is larger than
  4718   4718   ** the available payload.
  4719   4719   */
  4720   4720   int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
  4721   4721     assert( cursorHoldsMutex(pCur) );
  4722   4722     assert( pCur->eState==CURSOR_VALID );
  4723   4723     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
  4724         -  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
         4724  +  assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
  4725   4725     return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
  4726   4726   }
  4727   4727   
  4728   4728   /*
  4729   4729   ** This variant of sqlite3BtreePayload() works even if the cursor has not
  4730   4730   ** in the CURSOR_VALID state.  It is only used by the sqlite3_blob_read()
  4731   4731   ** interface.
................................................................................
  4779   4779     u32 *pAmt            /* Write the number of available bytes here */
  4780   4780   ){
  4781   4781     u32 amt;
  4782   4782     assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
  4783   4783     assert( pCur->eState==CURSOR_VALID );
  4784   4784     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4785   4785     assert( cursorOwnsBtShared(pCur) );
  4786         -  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
         4786  +  assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
  4787   4787     assert( pCur->info.nSize>0 );
  4788   4788     assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
  4789   4789     assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
  4790   4790     amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
  4791   4791     if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
  4792   4792     *pAmt = amt;
  4793   4793     return (void*)pCur->info.pPayload;
................................................................................
  4830   4830     assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
  4831   4831     assert( pCur->iPage>=0 );
  4832   4832     if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
  4833   4833       return SQLITE_CORRUPT_BKPT;
  4834   4834     }
  4835   4835     pCur->info.nSize = 0;
  4836   4836     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  4837         -  pCur->iPage++;
  4838         -  pCur->aiIdx[pCur->iPage] = 0;
         4837  +  pCur->aiIdx[pCur->iPage++] = pCur->ix;
         4838  +  pCur->ix = 0;
  4839   4839     return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
  4840   4840                           pCur, pCur->curPagerFlags);
  4841   4841   }
  4842   4842   
  4843   4843   #ifdef SQLITE_DEBUG
  4844   4844   /*
  4845   4845   ** Page pParent is an internal (non-leaf) tree page. This function 
................................................................................
  4879   4879       pCur->apPage[pCur->iPage-1], 
  4880   4880       pCur->aiIdx[pCur->iPage-1], 
  4881   4881       pCur->apPage[pCur->iPage]->pgno
  4882   4882     );
  4883   4883     testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
  4884   4884     pCur->info.nSize = 0;
  4885   4885     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
         4886  +  pCur->ix = pCur->aiIdx[pCur->iPage-1];
  4886   4887     releasePageNotNull(pCur->apPage[pCur->iPage--]);
  4887   4888   }
  4888   4889   
  4889   4890   /*
  4890   4891   ** Move the cursor to point to the root page of its b-tree structure.
  4891   4892   **
  4892   4893   ** If the table has a virtual root page, then the cursor is moved to point
................................................................................
  4960   4961     ** (or the freelist).  */
  4961   4962     assert( pRoot->intKey==1 || pRoot->intKey==0 );
  4962   4963     if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
  4963   4964       return SQLITE_CORRUPT_BKPT;
  4964   4965     }
  4965   4966   
  4966   4967   skip_init:  
  4967         -  pCur->aiIdx[0] = 0;
         4968  +  pCur->ix = 0;
  4968   4969     pCur->info.nSize = 0;
  4969   4970     pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
  4970   4971   
  4971   4972     pRoot = pCur->apPage[0];
  4972   4973     if( pRoot->nCell>0 ){
  4973   4974       pCur->eState = CURSOR_VALID;
  4974   4975     }else if( !pRoot->leaf ){
................................................................................
  4994   4995     Pgno pgno;
  4995   4996     int rc = SQLITE_OK;
  4996   4997     MemPage *pPage;
  4997   4998   
  4998   4999     assert( cursorOwnsBtShared(pCur) );
  4999   5000     assert( pCur->eState==CURSOR_VALID );
  5000   5001     while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
  5001         -    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  5002         -    pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
         5002  +    assert( pCur->ix<pPage->nCell );
         5003  +    pgno = get4byte(findCell(pPage, pCur->ix));
  5003   5004       rc = moveToChild(pCur, pgno);
  5004   5005     }
  5005   5006     return rc;
  5006   5007   }
  5007   5008   
  5008   5009   /*
  5009   5010   ** Move the cursor down to the right-most leaf entry beneath the
................................................................................
  5020   5021     int rc = SQLITE_OK;
  5021   5022     MemPage *pPage = 0;
  5022   5023   
  5023   5024     assert( cursorOwnsBtShared(pCur) );
  5024   5025     assert( pCur->eState==CURSOR_VALID );
  5025   5026     while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
  5026   5027       pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
  5027         -    pCur->aiIdx[pCur->iPage] = pPage->nCell;
         5028  +    pCur->ix = pPage->nCell;
  5028   5029       rc = moveToChild(pCur, pgno);
  5029   5030       if( rc ) return rc;
  5030   5031     }
  5031         -  pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
         5032  +  pCur->ix = pPage->nCell-1;
  5032   5033     assert( pCur->info.nSize==0 );
  5033   5034     assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
  5034   5035     return SQLITE_OK;
  5035   5036   }
  5036   5037   
  5037   5038   /* Move the cursor to the first entry in the table.  Return SQLITE_OK
  5038   5039   ** on success.  Set *pRes to 0 if the cursor actually points to something
................................................................................
  5072   5073   #ifdef SQLITE_DEBUG
  5073   5074       /* This block serves to assert() that the cursor really does point 
  5074   5075       ** to the last entry in the b-tree. */
  5075   5076       int ii;
  5076   5077       for(ii=0; ii<pCur->iPage; ii++){
  5077   5078         assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
  5078   5079       }
  5079         -    assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
         5080  +    assert( pCur->ix==pCur->apPage[pCur->iPage]->nCell-1 );
  5080   5081       assert( pCur->apPage[pCur->iPage]->leaf );
  5081   5082   #endif
  5082   5083       return SQLITE_OK;
  5083   5084     }
  5084   5085   
  5085   5086     rc = moveToRoot(pCur);
  5086   5087     if( rc==SQLITE_OK ){
................................................................................
  5219   5220       ** a moveToChild() or moveToRoot() call would have detected corruption.  */
  5220   5221       assert( pPage->nCell>0 );
  5221   5222       assert( pPage->intKey==(pIdxKey==0) );
  5222   5223       lwr = 0;
  5223   5224       upr = pPage->nCell-1;
  5224   5225       assert( biasRight==0 || biasRight==1 );
  5225   5226       idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
  5226         -    pCur->aiIdx[pCur->iPage] = (u16)idx;
         5227  +    pCur->ix = (u16)idx;
  5227   5228       if( xRecordCompare==0 ){
  5228   5229         for(;;){
  5229   5230           i64 nCellKey;
  5230   5231           pCell = findCellPastPtr(pPage, idx);
  5231   5232           if( pPage->intKeyLeaf ){
  5232   5233             while( 0x80 <= *(pCell++) ){
  5233   5234               if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
................................................................................
  5238   5239             lwr = idx+1;
  5239   5240             if( lwr>upr ){ c = -1; break; }
  5240   5241           }else if( nCellKey>intKey ){
  5241   5242             upr = idx-1;
  5242   5243             if( lwr>upr ){ c = +1; break; }
  5243   5244           }else{
  5244   5245             assert( nCellKey==intKey );
  5245         -          pCur->aiIdx[pCur->iPage] = (u16)idx;
         5246  +          pCur->ix = (u16)idx;
  5246   5247             if( !pPage->leaf ){
  5247   5248               lwr = idx;
  5248   5249               goto moveto_next_layer;
  5249   5250             }else{
  5250   5251               pCur->curFlags |= BTCF_ValidNKey;
  5251   5252               pCur->info.nKey = nCellKey;
  5252   5253               pCur->info.nSize = 0;
................................................................................
  5307   5308               goto moveto_finish;
  5308   5309             }
  5309   5310             pCellKey = sqlite3Malloc( nCell+18 );
  5310   5311             if( pCellKey==0 ){
  5311   5312               rc = SQLITE_NOMEM_BKPT;
  5312   5313               goto moveto_finish;
  5313   5314             }
  5314         -          pCur->aiIdx[pCur->iPage] = (u16)idx;
         5315  +          pCur->ix = (u16)idx;
  5315   5316             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
  5316   5317             pCur->curFlags &= ~BTCF_ValidOvfl;
  5317   5318             if( rc ){
  5318   5319               sqlite3_free(pCellKey);
  5319   5320               goto moveto_finish;
  5320   5321             }
  5321   5322             c = xRecordCompare(nCell, pCellKey, pIdxKey);
................................................................................
  5329   5330             lwr = idx+1;
  5330   5331           }else if( c>0 ){
  5331   5332             upr = idx-1;
  5332   5333           }else{
  5333   5334             assert( c==0 );
  5334   5335             *pRes = 0;
  5335   5336             rc = SQLITE_OK;
  5336         -          pCur->aiIdx[pCur->iPage] = (u16)idx;
         5337  +          pCur->ix = (u16)idx;
  5337   5338             if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
  5338   5339             goto moveto_finish;
  5339   5340           }
  5340   5341           if( lwr>upr ) break;
  5341   5342           assert( lwr+upr>=0 );
  5342   5343           idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
  5343   5344         }
  5344   5345       }
  5345   5346       assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
  5346   5347       assert( pPage->isInit );
  5347   5348       if( pPage->leaf ){
  5348         -      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  5349         -      pCur->aiIdx[pCur->iPage] = (u16)idx;
         5349  +      assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
         5350  +      pCur->ix = (u16)idx;
  5350   5351         *pRes = c;
  5351   5352         rc = SQLITE_OK;
  5352   5353         goto moveto_finish;
  5353   5354       }
  5354   5355   moveto_next_layer:
  5355   5356       if( lwr>=pPage->nCell ){
  5356   5357         chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
  5357   5358       }else{
  5358   5359         chldPg = get4byte(findCell(pPage, lwr));
  5359   5360       }
  5360         -    pCur->aiIdx[pCur->iPage] = (u16)lwr;
         5361  +    pCur->ix = (u16)lwr;
  5361   5362       rc = moveToChild(pCur, chldPg);
  5362   5363       if( rc ) break;
  5363   5364     }
  5364   5365   moveto_finish:
  5365   5366     pCur->info.nSize = 0;
  5366   5367     assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
  5367   5368     return rc;
................................................................................
  5454   5455           return SQLITE_OK;
  5455   5456         }
  5456   5457         pCur->skipNext = 0;
  5457   5458       }
  5458   5459     }
  5459   5460   
  5460   5461     pPage = pCur->apPage[pCur->iPage];
  5461         -  idx = ++pCur->aiIdx[pCur->iPage];
         5462  +  idx = ++pCur->ix;
  5462   5463     assert( pPage->isInit );
  5463   5464   
  5464   5465     /* If the database file is corrupt, it is possible for the value of idx 
  5465   5466     ** to be invalid here. This can only occur if a second cursor modifies
  5466   5467     ** the page while cursor pCur is holding a reference to it. Which can
  5467   5468     ** only happen if the database is corrupt in such a way as to link the
  5468   5469     ** page into more than one b-tree structure. */
................................................................................
  5478   5479         if( pCur->iPage==0 ){
  5479   5480           *pRes = 1;
  5480   5481           pCur->eState = CURSOR_INVALID;
  5481   5482           return SQLITE_OK;
  5482   5483         }
  5483   5484         moveToParent(pCur);
  5484   5485         pPage = pCur->apPage[pCur->iPage];
  5485         -    }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
         5486  +    }while( pCur->ix>=pPage->nCell );
  5486   5487       if( pPage->intKey ){
  5487   5488         return sqlite3BtreeNext(pCur, pRes);
  5488   5489       }else{
  5489   5490         return SQLITE_OK;
  5490   5491       }
  5491   5492     }
  5492   5493     if( pPage->leaf ){
................................................................................
  5502   5503     assert( *pRes==0 || *pRes==1 );
  5503   5504     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  5504   5505     pCur->info.nSize = 0;
  5505   5506     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  5506   5507     *pRes = 0;
  5507   5508     if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
  5508   5509     pPage = pCur->apPage[pCur->iPage];
  5509         -  if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){
  5510         -    pCur->aiIdx[pCur->iPage]--;
         5510  +  if( (++pCur->ix)>=pPage->nCell ){
         5511  +    pCur->ix--;
  5511   5512       return btreeNext(pCur, pRes);
  5512   5513     }
  5513   5514     if( pPage->leaf ){
  5514   5515       return SQLITE_OK;
  5515   5516     }else{
  5516   5517       return moveToLeftmost(pCur);
  5517   5518     }
................................................................................
  5567   5568         pCur->skipNext = 0;
  5568   5569       }
  5569   5570     }
  5570   5571   
  5571   5572     pPage = pCur->apPage[pCur->iPage];
  5572   5573     assert( pPage->isInit );
  5573   5574     if( !pPage->leaf ){
  5574         -    int idx = pCur->aiIdx[pCur->iPage];
         5575  +    int idx = pCur->ix;
  5575   5576       rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
  5576   5577       if( rc ) return rc;
  5577   5578       rc = moveToRightmost(pCur);
  5578   5579     }else{
  5579         -    while( pCur->aiIdx[pCur->iPage]==0 ){
         5580  +    while( pCur->ix==0 ){
  5580   5581         if( pCur->iPage==0 ){
  5581   5582           pCur->eState = CURSOR_INVALID;
  5582   5583           *pRes = 1;
  5583   5584           return SQLITE_OK;
  5584   5585         }
  5585   5586         moveToParent(pCur);
  5586   5587       }
  5587   5588       assert( pCur->info.nSize==0 );
  5588   5589       assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
  5589   5590   
  5590         -    pCur->aiIdx[pCur->iPage]--;
         5591  +    pCur->ix--;
  5591   5592       pPage = pCur->apPage[pCur->iPage];
  5592   5593       if( pPage->intKey && !pPage->leaf ){
  5593   5594         rc = sqlite3BtreePrevious(pCur, pRes);
  5594   5595       }else{
  5595   5596         rc = SQLITE_OK;
  5596   5597       }
  5597   5598     }
................................................................................
  5602   5603     assert( pRes!=0 );
  5603   5604     assert( *pRes==0 || *pRes==1 );
  5604   5605     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  5605   5606     *pRes = 0;
  5606   5607     pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
  5607   5608     pCur->info.nSize = 0;
  5608   5609     if( pCur->eState!=CURSOR_VALID
  5609         -   || pCur->aiIdx[pCur->iPage]==0
         5610  +   || pCur->ix==0
  5610   5611      || pCur->apPage[pCur->iPage]->leaf==0
  5611   5612     ){
  5612   5613       return btreePrevious(pCur, pRes);
  5613   5614     }
  5614         -  pCur->aiIdx[pCur->iPage]--;
         5615  +  pCur->ix--;
  5615   5616     return SQLITE_OK;
  5616   5617   }
  5617   5618   
  5618   5619   /*
  5619   5620   ** Allocate a new page from the database file.
  5620   5621   **
  5621   5622   ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
................................................................................
  7929   7930           ** next iteration of the do-loop will balance the child page.
  7930   7931           */ 
  7931   7932           assert( balance_deeper_called==0 );
  7932   7933           VVA_ONLY( balance_deeper_called++ );
  7933   7934           rc = balance_deeper(pPage, &pCur->apPage[1]);
  7934   7935           if( rc==SQLITE_OK ){
  7935   7936             pCur->iPage = 1;
         7937  +          pCur->ix = 0;
  7936   7938             pCur->aiIdx[0] = 0;
  7937         -          pCur->aiIdx[1] = 0;
  7938   7939             assert( pCur->apPage[1]->nOverflow );
  7939   7940           }
  7940   7941         }else{
  7941   7942           break;
  7942   7943         }
  7943   7944       }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
  7944   7945         break;
................................................................................
  8159   8160     assert( pPage->isInit );
  8160   8161     newCell = pBt->pTmpSpace;
  8161   8162     assert( newCell!=0 );
  8162   8163     rc = fillInCell(pPage, newCell, pX, &szNew);
  8163   8164     if( rc ) goto end_insert;
  8164   8165     assert( szNew==pPage->xCellSize(pPage, newCell) );
  8165   8166     assert( szNew <= MX_CELL_SIZE(pBt) );
  8166         -  idx = pCur->aiIdx[pCur->iPage];
         8167  +  idx = pCur->ix;
  8167   8168     if( loc==0 ){
  8168   8169       CellInfo info;
  8169   8170       assert( idx<pPage->nCell );
  8170   8171       rc = sqlite3PagerWrite(pPage->pDbPage);
  8171   8172       if( rc ){
  8172   8173         goto end_insert;
  8173   8174       }
................................................................................
  8187   8188         memcpy(oldCell, newCell, szNew);
  8188   8189         return SQLITE_OK;
  8189   8190       }
  8190   8191       dropCell(pPage, idx, info.nSize, &rc);
  8191   8192       if( rc ) goto end_insert;
  8192   8193     }else if( loc<0 && pPage->nCell>0 ){
  8193   8194       assert( pPage->leaf );
  8194         -    idx = ++pCur->aiIdx[pCur->iPage];
         8195  +    idx = ++pCur->ix;
  8195   8196     }else{
  8196   8197       assert( pPage->leaf );
  8197   8198     }
  8198   8199     insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
  8199   8200     assert( pPage->nOverflow==0 || rc==SQLITE_OK );
  8200   8201     assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
  8201   8202   
................................................................................
  8283   8284   
  8284   8285     assert( cursorOwnsBtShared(pCur) );
  8285   8286     assert( pBt->inTransaction==TRANS_WRITE );
  8286   8287     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  8287   8288     assert( pCur->curFlags & BTCF_WriteFlag );
  8288   8289     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  8289   8290     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
  8290         -  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
         8291  +  assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
  8291   8292     assert( pCur->eState==CURSOR_VALID );
  8292   8293     assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
  8293   8294   
  8294   8295     iCellDepth = pCur->iPage;
  8295         -  iCellIdx = pCur->aiIdx[iCellDepth];
         8296  +  iCellIdx = pCur->ix;
  8296   8297     pPage = pCur->apPage[iCellDepth];
  8297   8298     pCell = findCell(pPage, iCellIdx);
  8298   8299   
  8299   8300     /* If the bPreserve flag is set to true, then the cursor position must
  8300   8301     ** be preserved following this delete operation. If the current delete
  8301   8302     ** will cause a b-tree rebalance, then this is done by saving the cursor
  8302   8303     ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
................................................................................
  8405   8406       if( bSkipnext ){
  8406   8407         assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
  8407   8408         assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB );
  8408   8409         assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
  8409   8410         pCur->eState = CURSOR_SKIPNEXT;
  8410   8411         if( iCellIdx>=pPage->nCell ){
  8411   8412           pCur->skipNext = -1;
  8412         -        pCur->aiIdx[iCellDepth] = pPage->nCell-1;
         8413  +        pCur->ix = pPage->nCell-1;
  8413   8414         }else{
  8414   8415           pCur->skipNext = 1;
  8415   8416         }
  8416   8417       }else{
  8417   8418         rc = moveToRoot(pCur);
  8418   8419         if( bPreserve ){
  8419   8420           pCur->eState = CURSOR_REQUIRESEEK;
................................................................................
  8918   8919         do {
  8919   8920           if( pCur->iPage==0 ){
  8920   8921             /* All pages of the b-tree have been visited. Return successfully. */
  8921   8922             *pnEntry = nEntry;
  8922   8923             return moveToRoot(pCur);
  8923   8924           }
  8924   8925           moveToParent(pCur);
  8925         -      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
         8926  +      }while ( pCur->ix>=pCur->apPage[pCur->iPage]->nCell );
  8926   8927   
  8927         -      pCur->aiIdx[pCur->iPage]++;
         8928  +      pCur->ix++;
  8928   8929         pPage = pCur->apPage[pCur->iPage];
  8929   8930       }
  8930   8931   
  8931   8932       /* Descend to the child node of the cell that the cursor currently 
  8932   8933       ** points at. This is the right-child if (iIdx==pPage->nCell).
  8933   8934       */
  8934         -    iIdx = pCur->aiIdx[pCur->iPage];
         8935  +    iIdx = pCur->ix;
  8935   8936       if( iIdx==pPage->nCell ){
  8936   8937         rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
  8937   8938       }else{
  8938   8939         rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
  8939   8940       }
  8940   8941     }
  8941   8942   

Changes to src/btreeInt.h.

   513    513     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
   514    514     u8 hints;                 /* As configured by CursorSetHints() */
   515    515     /* All fields above are zeroed when the cursor is allocated.  See
   516    516     ** sqlite3BtreeCursorZero().  Fields that follow must be manually
   517    517     ** initialized. */
   518    518     i8 iPage;                 /* Index of current page in apPage */
   519    519     u8 curIntKey;             /* Value of apPage[0]->intKey */
   520         -  struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
   521         -  void *padding1;           /* Make object size a multiple of 16 */
   522         -  u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
   523         -  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
          520  +  u16 ix;                   /* Current index for apPage[iPage] */
          521  +  u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
          522  +  struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
          523  +  MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
   524    524   };
   525    525   
   526    526   /*
   527    527   ** Legal values for BtCursor.curFlags
   528    528   */
   529    529   #define BTCF_WriteFlag    0x01   /* True if a write cursor */
   530    530   #define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */

Changes to src/build.c.

  3619   3619   void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
  3620   3620     int i;
  3621   3621     if( pList==0 ) return;
  3622   3622     for(i=0; i<pList->nId; i++){
  3623   3623       sqlite3DbFree(db, pList->a[i].zName);
  3624   3624     }
  3625   3625     sqlite3DbFree(db, pList->a);
  3626         -  sqlite3DbFree(db, pList);
         3626  +  sqlite3DbFreeNN(db, pList);
  3627   3627   }
  3628   3628   
  3629   3629   /*
  3630   3630   ** Return the index in pList of the identifier named zId.  Return -1
  3631   3631   ** if not found.
  3632   3632   */
  3633   3633   int sqlite3IdListIndex(IdList *pList, const char *zName){
................................................................................
  3809   3809       if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
  3810   3810       if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
  3811   3811       sqlite3DeleteTable(db, pItem->pTab);
  3812   3812       sqlite3SelectDelete(db, pItem->pSelect);
  3813   3813       sqlite3ExprDelete(db, pItem->pOn);
  3814   3814       sqlite3IdListDelete(db, pItem->pUsing);
  3815   3815     }
  3816         -  sqlite3DbFree(db, pList);
         3816  +  sqlite3DbFreeNN(db, pList);
  3817   3817   }
  3818   3818   
  3819   3819   /*
  3820   3820   ** This routine is called by the parser to add a new term to the
  3821   3821   ** end of a growing FROM clause.  The "p" parameter is the part of
  3822   3822   ** the FROM clause that has already been constructed.  "p" is NULL
  3823   3823   ** if this is the first term of the FROM clause.  pTable and pDatabase

Changes to src/expr.c.

  1034   1034         sqlite3SelectDelete(db, p->x.pSelect);
  1035   1035       }else{
  1036   1036         sqlite3ExprListDelete(db, p->x.pList);
  1037   1037       }
  1038   1038     }
  1039   1039     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
  1040   1040     if( !ExprHasProperty(p, EP_Static) ){
  1041         -    sqlite3DbFree(db, p);
         1041  +    sqlite3DbFreeNN(db, p);
  1042   1042     }
  1043   1043   }
  1044   1044   void sqlite3ExprDelete(sqlite3 *db, Expr *p){
  1045   1045     if( p ) sqlite3ExprDeleteNN(db, p);
  1046   1046   }
  1047   1047   
  1048   1048   /*
................................................................................
  1301   1301   ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
  1302   1302     ExprList *pNew;
  1303   1303     struct ExprList_item *pItem, *pOldItem;
  1304   1304     int i;
  1305   1305     Expr *pPriorSelectCol = 0;
  1306   1306     assert( db!=0 );
  1307   1307     if( p==0 ) return 0;
  1308         -  pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
         1308  +  pNew = sqlite3DbMallocRawNN(db, 
         1309  +             sizeof(*pNew)+sizeof(pNew->a[0])*(p->nExpr-1) );
  1309   1310     if( pNew==0 ) return 0;
  1310         -  pNew->nExpr = i = p->nExpr;
  1311         -  if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
  1312         -  pNew->a = pItem = sqlite3DbMallocRawNN(db,  i*sizeof(p->a[0]) );
  1313         -  if( pItem==0 ){
  1314         -    sqlite3DbFree(db, pNew);
  1315         -    return 0;
  1316         -  } 
         1311  +  pNew->nAlloc = pNew->nExpr = p->nExpr;
         1312  +  pItem = pNew->a;
  1317   1313     pOldItem = p->a;
  1318   1314     for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
  1319   1315       Expr *pOldExpr = pOldItem->pExpr;
  1320   1316       Expr *pNewExpr;
  1321   1317       pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
  1322   1318       if( pOldExpr 
  1323   1319        && pOldExpr->op==TK_SELECT_COLUMN
................................................................................
  1400   1396     assert( db!=0 );
  1401   1397     if( p==0 ) return 0;
  1402   1398     pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
  1403   1399     if( pNew==0 ) return 0;
  1404   1400     pNew->nId = p->nId;
  1405   1401     pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
  1406   1402     if( pNew->a==0 ){
  1407         -    sqlite3DbFree(db, pNew);
         1403  +    sqlite3DbFreeNN(db, pNew);
  1408   1404       return 0;
  1409   1405     }
  1410   1406     /* Note that because the size of the allocation for p->a[] is not
  1411   1407     ** necessarily a power of two, sqlite3IdListAppend() may not be called
  1412   1408     ** on the duplicate created by this function. */
  1413   1409     for(i=0; i<p->nId; i++){
  1414   1410       struct IdList_item *pNewItem = &pNew->a[i];
................................................................................
  1471   1467   ** that the new entry was successfully appended.
  1472   1468   */
  1473   1469   ExprList *sqlite3ExprListAppend(
  1474   1470     Parse *pParse,          /* Parsing context */
  1475   1471     ExprList *pList,        /* List to which to append. Might be NULL */
  1476   1472     Expr *pExpr             /* Expression to be appended. Might be NULL */
  1477   1473   ){
         1474  +  struct ExprList_item *pItem;
  1478   1475     sqlite3 *db = pParse->db;
  1479   1476     assert( db!=0 );
  1480   1477     if( pList==0 ){
  1481   1478       pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
  1482   1479       if( pList==0 ){
  1483   1480         goto no_mem;
  1484   1481       }
  1485   1482       pList->nExpr = 0;
  1486         -    pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
  1487         -    if( pList->a==0 ) goto no_mem;
  1488         -  }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
  1489         -    struct ExprList_item *a;
  1490         -    assert( pList->nExpr>0 );
  1491         -    a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
  1492         -    if( a==0 ){
         1483  +    pList->nAlloc = 1;
         1484  +  }else if( pList->nExpr==pList->nAlloc ){
         1485  +    ExprList *pNew;
         1486  +    pNew = sqlite3DbRealloc(db, pList, 
         1487  +             sizeof(*pList)+(2*pList->nAlloc - 1)*sizeof(pList->a[0]));
         1488  +    if( pNew==0 ){
  1493   1489         goto no_mem;
  1494   1490       }
  1495         -    pList->a = a;
         1491  +    pList = pNew;
         1492  +    pList->nAlloc *= 2;
  1496   1493     }
  1497         -  assert( pList->a!=0 );
  1498         -  if( 1 ){
  1499         -    struct ExprList_item *pItem = &pList->a[pList->nExpr++];
  1500         -    memset(pItem, 0, sizeof(*pItem));
  1501         -    pItem->pExpr = pExpr;
  1502         -  }
         1494  +  pItem = &pList->a[pList->nExpr++];
         1495  +  memset(pItem, 0, sizeof(*pItem));
         1496  +  pItem->pExpr = pExpr;
  1503   1497     return pList;
  1504   1498   
  1505   1499   no_mem:     
  1506   1500     /* Avoid leaking memory if malloc has failed. */
  1507   1501     sqlite3ExprDelete(db, pExpr);
  1508   1502     sqlite3ExprListDelete(db, pList);
  1509   1503     return 0;
................................................................................
  1552   1546       if( pList ){
  1553   1547         assert( pList->nExpr==iFirst+i+1 );
  1554   1548         pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
  1555   1549         pColumns->a[i].zName = 0;
  1556   1550       }
  1557   1551     }
  1558   1552   
  1559         -  if( pExpr->op==TK_SELECT ){
  1560         -    if( pList && pList->a[iFirst].pExpr ){
  1561         -      Expr *pFirst = pList->a[iFirst].pExpr;
  1562         -      assert( pFirst->op==TK_SELECT_COLUMN );
         1553  +  if( pExpr->op==TK_SELECT && pList ){
         1554  +    Expr *pFirst = pList->a[iFirst].pExpr;
         1555  +    assert( pFirst!=0 );
         1556  +    assert( pFirst->op==TK_SELECT_COLUMN );
  1563   1557        
  1564         -      /* Store the SELECT statement in pRight so it will be deleted when
  1565         -      ** sqlite3ExprListDelete() is called */
  1566         -      pFirst->pRight = pExpr;
  1567         -      pExpr = 0;
         1558  +    /* Store the SELECT statement in pRight so it will be deleted when
         1559  +    ** sqlite3ExprListDelete() is called */
         1560  +    pFirst->pRight = pExpr;
         1561  +    pExpr = 0;
  1568   1562   
  1569         -      /* Remember the size of the LHS in iTable so that we can check that
  1570         -      ** the RHS and LHS sizes match during code generation. */
  1571         -      pFirst->iTable = pColumns->nId;
  1572         -    }
         1563  +    /* Remember the size of the LHS in iTable so that we can check that
         1564  +    ** the RHS and LHS sizes match during code generation. */
         1565  +    pFirst->iTable = pColumns->nId;
  1573   1566     }
  1574   1567   
  1575   1568   vector_append_error:
  1576   1569     sqlite3ExprDelete(db, pExpr);
  1577   1570     sqlite3IdListDelete(db, pColumns);
  1578   1571     return pList;
  1579   1572   }
................................................................................
  1659   1652     }
  1660   1653   }
  1661   1654   
  1662   1655   /*
  1663   1656   ** Delete an entire expression list.
  1664   1657   */
  1665   1658   static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
  1666         -  int i;
  1667         -  struct ExprList_item *pItem;
  1668         -  assert( pList->a!=0 || pList->nExpr==0 );
  1669         -  for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
         1659  +  int i = pList->nExpr;
         1660  +  struct ExprList_item *pItem =  pList->a;
         1661  +  assert( pList->nExpr>0 );
         1662  +  do{
  1670   1663       sqlite3ExprDelete(db, pItem->pExpr);
  1671   1664       sqlite3DbFree(db, pItem->zName);
  1672   1665       sqlite3DbFree(db, pItem->zSpan);
  1673         -  }
  1674         -  sqlite3DbFree(db, pList->a);
  1675         -  sqlite3DbFree(db, pList);
         1666  +    pItem++;
         1667  +  }while( --i>0 );
         1668  +  sqlite3DbFreeNN(db, pList);
  1676   1669   }
  1677   1670   void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
  1678   1671     if( pList ) exprListDeleteNN(db, pList);
  1679   1672   }
  1680   1673   
  1681   1674   /*
  1682   1675   ** Return the bitwise-OR of all Expr.flags fields in the given

Changes to src/malloc.c.

   470    470   */
   471    471   static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
   472    472     *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
   473    473   }
   474    474   
   475    475   /*
   476    476   ** Free memory that might be associated with a particular database
   477         -** connection.
          477  +** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
          478  +** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
   478    479   */
   479         -void sqlite3DbFree(sqlite3 *db, void *p){
          480  +void sqlite3DbFreeNN(sqlite3 *db, void *p){
   480    481     assert( db==0 || sqlite3_mutex_held(db->mutex) );
   481         -  if( p==0 ) return;
          482  +  assert( p!=0 );
   482    483     if( db ){
   483    484       if( db->pnBytesFreed ){
   484    485         measureAllocationSize(db, p);
   485    486         return;
   486    487       }
   487    488       if( isLookaside(db, p) ){
   488    489         LookasideSlot *pBuf = (LookasideSlot*)p;
................................................................................
   497    498       }
   498    499     }
   499    500     assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   500    501     assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   501    502     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   502    503     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   503    504     sqlite3_free(p);
          505  +}
          506  +void sqlite3DbFree(sqlite3 *db, void *p){
          507  +  assert( db==0 || sqlite3_mutex_held(db->mutex) );
          508  +  if( p ) sqlite3DbFreeNN(db, p);
   504    509   }
   505    510   
   506    511   /*
   507    512   ** Change the size of an existing memory allocation
   508    513   */
   509    514   void *sqlite3Realloc(void *pOld, u64 nBytes){
   510    515     int nOld, nNew, nDiff;

Changes to src/pragma.c.

  1538   1538                                 pTab->aCol[j].zName);
  1539   1539             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
  1540   1540             integrityCheckResultRow(v, 3);
  1541   1541             sqlite3VdbeJumpHere(v, jmp2);
  1542   1542           }
  1543   1543           /* Verify CHECK constraints */
  1544   1544           if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
  1545         -          int addrCkFault = sqlite3VdbeMakeLabel(v);
  1546         -          int addrCkOk = sqlite3VdbeMakeLabel(v);
  1547         -          ExprList *pCheck = pTab->pCheck;
  1548         -          char *zErr;
  1549         -          int k;
  1550         -          pParse->iSelfTab = iDataCur;
  1551         -          sqlite3ExprCachePush(pParse);
  1552         -          for(k=pCheck->nExpr-1; k>0; k--){
  1553         -            sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
         1545  +          ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
         1546  +          if( db->mallocFailed==0 ){
         1547  +            int addrCkFault = sqlite3VdbeMakeLabel(v);
         1548  +            int addrCkOk = sqlite3VdbeMakeLabel(v);
         1549  +            char *zErr;
         1550  +            int k;
         1551  +            pParse->iSelfTab = iDataCur;
         1552  +            sqlite3ExprCachePush(pParse);
         1553  +            for(k=pCheck->nExpr-1; k>0; k--){
         1554  +              sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
         1555  +            }
         1556  +            sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
         1557  +                SQLITE_JUMPIFNULL);
         1558  +            sqlite3VdbeResolveLabel(v, addrCkFault);
         1559  +            zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
         1560  +                pTab->zName);
         1561  +            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
         1562  +            integrityCheckResultRow(v, 3);
         1563  +            sqlite3VdbeResolveLabel(v, addrCkOk);
         1564  +            sqlite3ExprCachePop(pParse);
  1554   1565             }
  1555         -          sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
  1556         -                            SQLITE_JUMPIFNULL);
  1557         -          sqlite3VdbeResolveLabel(v, addrCkFault);
  1558         -          zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
  1559         -                                pTab->zName);
  1560         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
  1561         -          integrityCheckResultRow(v, 3);
  1562         -          sqlite3VdbeResolveLabel(v, addrCkOk);
  1563         -          sqlite3ExprCachePop(pParse);
         1566  +          sqlite3ExprListDelete(db, pCheck);
  1564   1567           }
  1565   1568           /* Validate index entries for the current row */
  1566   1569           for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
  1567   1570             int jmp2, jmp3, jmp4, jmp5;
  1568   1571             int ckUniq = sqlite3VdbeMakeLabel(v);
  1569   1572             if( pPk==pIdx ) continue;
  1570   1573             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,

Changes to src/pragma.h.

   413    413     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
   414    414     /* ePragFlg:  */ 0,
   415    415     /* ColNames:  */ 0, 0,
   416    416     /* iArg:      */ 0 },
   417    417   #endif
   418    418    {/* zName:     */ "optimize",
   419    419     /* ePragTyp:  */ PragTyp_OPTIMIZE,
   420         -  /* ePragFlg:  */ PragFlg_Result1,
          420  +  /* ePragFlg:  */ PragFlg_Result1|PragFlg_NeedSchema,
   421    421     /* ColNames:  */ 0, 0,
   422    422     /* iArg:      */ 0 },
   423    423   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   424    424    {/* zName:     */ "page_count",
   425    425     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
   426    426     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
   427    427     /* ColNames:  */ 0, 0,

Changes to src/select.c.

    72     72       sqlite3ExprDelete(db, p->pWhere);
    73     73       sqlite3ExprListDelete(db, p->pGroupBy);
    74     74       sqlite3ExprDelete(db, p->pHaving);
    75     75       sqlite3ExprListDelete(db, p->pOrderBy);
    76     76       sqlite3ExprDelete(db, p->pLimit);
    77     77       sqlite3ExprDelete(db, p->pOffset);
    78     78       if( p->pWith ) sqlite3WithDelete(db, p->pWith);
    79         -    if( bFree ) sqlite3DbFree(db, p);
           79  +    if( bFree ) sqlite3DbFreeNN(db, p);
    80     80       p = pPrior;
    81     81       bFree = 1;
    82     82     }
    83     83   }
    84     84   
    85     85   /*
    86     86   ** Initialize a SelectDest structure.
................................................................................
  1051   1051   /*
  1052   1052   ** Deallocate a KeyInfo object
  1053   1053   */
  1054   1054   void sqlite3KeyInfoUnref(KeyInfo *p){
  1055   1055     if( p ){
  1056   1056       assert( p->nRef>0 );
  1057   1057       p->nRef--;
  1058         -    if( p->nRef==0 ) sqlite3DbFree(p->db, p);
         1058  +    if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
  1059   1059     }
  1060   1060   }
  1061   1061   
  1062   1062   /*
  1063   1063   ** Make a new pointer to a KeyInfo object
  1064   1064   */
  1065   1065   KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
................................................................................
  2896   2896           if( pItem->u.x.iOrderByCol==i ) break;
  2897   2897         }
  2898   2898         if( j==nOrderBy ){
  2899   2899           Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
  2900   2900           if( pNew==0 ) return SQLITE_NOMEM_BKPT;
  2901   2901           pNew->flags |= EP_IntValue;
  2902   2902           pNew->u.iValue = i;
  2903         -        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
         2903  +        p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
  2904   2904           if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
  2905   2905         }
  2906   2906       }
  2907   2907     }
  2908   2908   
  2909   2909     /* Compute the comparison permutation and keyinfo that is used with
  2910   2910     ** the permutation used to determine if the next

Changes to src/shell.c.

  1483   1483   static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
  1484   1484     int i;
  1485   1485     char *zBlob = (char *)pBlob;
  1486   1486     raw_printf(out,"X'");
  1487   1487     for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
  1488   1488     raw_printf(out,"'");
  1489   1489   }
         1490  +
         1491  +/*
         1492  +** Find a string that is not found anywhere in z[].  Return a pointer
         1493  +** to that string.
         1494  +**
         1495  +** Try to use zA and zB first.  If both of those are already found in z[]
         1496  +** then make up some string and store it in the buffer zBuf.
         1497  +*/
         1498  +static const char *unused_string(
         1499  +  const char *z,                    /* Result must not appear anywhere in z */
         1500  +  const char *zA, const char *zB,   /* Try these first */
         1501  +  char *zBuf                        /* Space to store a generated string */
         1502  +){
         1503  +  unsigned i = 0;
         1504  +  if( strstr(z, zA)==0 ) return zA;
         1505  +  if( strstr(z, zB)==0 ) return zB;
         1506  +  do{
         1507  +    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
         1508  +  }while( strstr(z,zBuf)!=0 );
         1509  +  return zBuf;
         1510  +}
  1490   1511   
  1491   1512   /*
  1492   1513   ** Output the given string as a quoted string using SQL quoting conventions.
  1493   1514   **
  1494         -** The "\n" and "\r" characters are converted to char(10) and char(13)
  1495         -** to prevent them from being transformed by end-of-line translators.
         1515  +** See also: output_quoted_escaped_string()
  1496   1516   */
  1497   1517   static void output_quoted_string(FILE *out, const char *z){
  1498   1518     int i;
         1519  +  char c;
         1520  +  setBinaryMode(out, 1);
         1521  +  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
         1522  +  if( c==0 ){
         1523  +    utf8_printf(out,"'%s'",z);
         1524  +  }else{
         1525  +    raw_printf(out, "'");
         1526  +    while( *z ){
         1527  +      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
         1528  +      if( c=='\'' ) i++;
         1529  +      if( i ){
         1530  +        utf8_printf(out, "%.*s", i, z);
         1531  +        z += i;
         1532  +      }
         1533  +      if( c=='\'' ){
         1534  +        raw_printf(out, "'");
         1535  +        continue;
         1536  +      }
         1537  +      if( c==0 ){
         1538  +        break;
         1539  +      }
         1540  +      z++;
         1541  +    }
         1542  +    raw_printf(out, "'");
         1543  +  }
         1544  +  setTextMode(out, 1);
         1545  +}
         1546  +
         1547  +/*
         1548  +** Output the given string as a quoted string using SQL quoting conventions.
         1549  +** Additionallly , escape the "\n" and "\r" characters so that they do not
         1550  +** get corrupted by end-of-line translation facilities in some operating
         1551  +** systems.
         1552  +**
         1553  +** This is like output_quoted_string() but with the addition of the \r\n
         1554  +** escape mechanism.
         1555  +*/
         1556  +static void output_quoted_escaped_string(FILE *out, const char *z){
         1557  +  int i;
  1499   1558     char c;
  1500   1559     setBinaryMode(out, 1);
  1501   1560     for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
  1502   1561     if( c==0 ){
  1503   1562       utf8_printf(out,"'%s'",z);
  1504   1563     }else{
  1505         -    int inQuote = 0;
  1506         -    int bStarted = 0;
         1564  +    const char *zNL = 0;
         1565  +    const char *zCR = 0;
         1566  +    int nNL = 0;
         1567  +    int nCR = 0;
         1568  +    char zBuf1[20], zBuf2[20];
         1569  +    for(i=0; z[i]; i++){
         1570  +      if( z[i]=='\n' ) nNL++;
         1571  +      if( z[i]=='\r' ) nCR++;
         1572  +    }
         1573  +    if( nNL ){
         1574  +      raw_printf(out, "replace(");
         1575  +      zNL = unused_string(z, "\\n", "\\012", zBuf1);
         1576  +    }
         1577  +    if( nCR ){
         1578  +      raw_printf(out, "replace(");
         1579  +      zCR = unused_string(z, "\\r", "\\015", zBuf2);
         1580  +    }
         1581  +    raw_printf(out, "'");
  1507   1582       while( *z ){
  1508   1583         for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
  1509   1584         if( c=='\'' ) i++;
  1510   1585         if( i ){
  1511         -        if( !inQuote ){
  1512         -          if( bStarted ) raw_printf(out, "||");
  1513         -          raw_printf(out, "'");
  1514         -          inQuote = 1;
  1515         -        }
  1516   1586           utf8_printf(out, "%.*s", i, z);
  1517   1587           z += i;
  1518         -        bStarted = 1;
  1519   1588         }
  1520   1589         if( c=='\'' ){
  1521   1590           raw_printf(out, "'");
  1522   1591           continue;
  1523   1592         }
  1524         -      if( inQuote ){
  1525         -        raw_printf(out, "'");
  1526         -        inQuote = 0;
  1527         -      }
  1528   1593         if( c==0 ){
  1529   1594           break;
  1530   1595         }
  1531         -      for(i=0; (c = z[i])=='\r' || c=='\n'; i++){
  1532         -        if( bStarted ) raw_printf(out, "||");
  1533         -        raw_printf(out, "char(%d)", c);
  1534         -        bStarted = 1;
         1596  +      z++;
         1597  +      if( c=='\n' ){
         1598  +        raw_printf(out, "%s", zNL);
         1599  +        continue;
  1535   1600         }
  1536         -      z += i;
         1601  +      raw_printf(out, "%s", zCR);
  1537   1602       }
  1538         -    if( inQuote ) raw_printf(out, "'");
         1603  +    raw_printf(out, "'");
         1604  +    if( nCR ){
         1605  +      raw_printf(out, ",'%s',char(13))", zCR);
         1606  +    }
         1607  +    if( nNL ){
         1608  +      raw_printf(out, ",'%s',char(10))", zNL);
         1609  +    }
  1539   1610     }
  1540   1611     setTextMode(out, 1);
  1541   1612   }
  1542   1613   
  1543   1614   /*
  1544   1615   ** Output the given string as a quoted according to C or TCL quoting rules.
  1545   1616   */
................................................................................
  1994   2065             output_csv(p, azArg[i], i<nArg-1);
  1995   2066           }
  1996   2067           utf8_printf(p->out, "%s", p->rowSeparator);
  1997   2068         }
  1998   2069         setTextMode(p->out, 1);
  1999   2070         break;
  2000   2071       }
  2001         -    case MODE_Quote:
  2002   2072       case MODE_Insert: {
  2003   2073         if( azArg==0 ) break;
  2004         -      if( p->cMode==MODE_Insert ){
  2005         -        utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
  2006         -        if( p->showHeader ){
  2007         -          raw_printf(p->out,"(");
  2008         -          for(i=0; i<nArg; i++){
  2009         -            char *zSep = i>0 ? ",": "";
  2010         -            utf8_printf(p->out, "%s%s", zSep, azCol[i]);
  2011         -          }
  2012         -          raw_printf(p->out,")");
  2013         -        }
  2014         -        raw_printf(p->out," VALUES(");
  2015         -      }else if( p->cnt==0 && p->showHeader ){
         2074  +      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
         2075  +      if( p->showHeader ){
         2076  +        raw_printf(p->out,"(");
         2077  +        for(i=0; i<nArg; i++){
         2078  +          if( i>0 ) raw_printf(p->out, ",");
         2079  +          if( quoteChar(azCol[i]) ){
         2080  +            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
         2081  +            utf8_printf(p->out, "%s", z);
         2082  +            sqlite3_free(z);
         2083  +          }else{
         2084  +            raw_printf(p->out, "%s", azCol[i]);
         2085  +          }
         2086  +        }
         2087  +        raw_printf(p->out,")");
         2088  +      }
         2089  +      p->cnt++;
         2090  +      for(i=0; i<nArg; i++){
         2091  +        raw_printf(p->out, i>0 ? "," : " VALUES(");
         2092  +        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
         2093  +          utf8_printf(p->out,"NULL");
         2094  +        }else if( aiType && aiType[i]==SQLITE_TEXT ){
         2095  +          output_quoted_escaped_string(p->out, azArg[i]);
         2096  +        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
         2097  +          utf8_printf(p->out,"%s", azArg[i]);
         2098  +        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
         2099  +          char z[50];
         2100  +          double r = sqlite3_column_double(p->pStmt, i);
         2101  +          sqlite3_snprintf(50,z,"%!.20g", r);
         2102  +          raw_printf(p->out, "%s", z);
         2103  +        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
         2104  +          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
         2105  +          int nBlob = sqlite3_column_bytes(p->pStmt, i);
         2106  +          output_hex_blob(p->out, pBlob, nBlob);
         2107  +        }else if( isNumber(azArg[i], 0) ){
         2108  +          utf8_printf(p->out,"%s", azArg[i]);
         2109  +        }else{
         2110  +          output_quoted_escaped_string(p->out, azArg[i]);
         2111  +        }
         2112  +      }
         2113  +      raw_printf(p->out,");\n");
         2114  +      break;
         2115  +    }
         2116  +    case MODE_Quote: {
         2117  +      if( azArg==0 ) break;
         2118  +      if( p->cnt==0 && p->showHeader ){
  2016   2119           for(i=0; i<nArg; i++){
  2017   2120             if( i>0 ) raw_printf(p->out, ",");
  2018   2121             output_quoted_string(p->out, azCol[i]);
  2019   2122           }
  2020   2123           raw_printf(p->out,"\n");
  2021   2124         }
  2022   2125         p->cnt++;
  2023   2126         for(i=0; i<nArg; i++){
  2024         -        char *zSep = i>0 ? ",": "";
         2127  +        if( i>0 ) raw_printf(p->out, ",");
  2025   2128           if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
  2026         -          utf8_printf(p->out,"%sNULL",zSep);
         2129  +          utf8_printf(p->out,"NULL");
  2027   2130           }else if( aiType && aiType[i]==SQLITE_TEXT ){
  2028         -          if( zSep[0] ) utf8_printf(p->out,"%s",zSep);
  2029   2131             output_quoted_string(p->out, azArg[i]);
  2030   2132           }else if( aiType && aiType[i]==SQLITE_INTEGER ){
  2031         -          utf8_printf(p->out,"%s%s",zSep, azArg[i]);
         2133  +          utf8_printf(p->out,"%s", azArg[i]);
  2032   2134           }else if( aiType && aiType[i]==SQLITE_FLOAT ){
  2033   2135             char z[50];
  2034   2136             double r = sqlite3_column_double(p->pStmt, i);
  2035   2137             sqlite3_snprintf(50,z,"%!.20g", r);
  2036         -          raw_printf(p->out, "%s%s", zSep, z);
         2138  +          raw_printf(p->out, "%s", z);
  2037   2139           }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
  2038   2140             const void *pBlob = sqlite3_column_blob(p->pStmt, i);
  2039   2141             int nBlob = sqlite3_column_bytes(p->pStmt, i);
  2040         -          if( zSep[0] ) utf8_printf(p->out,"%s",zSep);
  2041   2142             output_hex_blob(p->out, pBlob, nBlob);
  2042   2143           }else if( isNumber(azArg[i], 0) ){
  2043         -          utf8_printf(p->out,"%s%s",zSep, azArg[i]);
         2144  +          utf8_printf(p->out,"%s", azArg[i]);
  2044   2145           }else{
  2045         -          if( zSep[0] ) utf8_printf(p->out,"%s",zSep);
  2046   2146             output_quoted_string(p->out, azArg[i]);
  2047   2147           }
  2048   2148         }
  2049         -      raw_printf(p->out,p->cMode==MODE_Quote?"\n":");\n");
         2149  +      raw_printf(p->out,"\n");
  2050   2150         break;
  2051   2151       }
  2052   2152       case MODE_Ascii: {
  2053   2153         if( p->cnt++==0 && p->showHeader ){
  2054   2154           for(i=0; i<nArg; i++){
  2055   2155             if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
  2056   2156             utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
................................................................................
  4327   4427     **
  4328   4428     ** These six values are used by the C logic below to generate the report.
  4329   4429     */
  4330   4430     const char *zSql =
  4331   4431     "SELECT "
  4332   4432       "     'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '"
  4333   4433       "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
  4334         -    "  || fkey_collate_clause(f.[table], f.[to], s.name, f.[from]),' AND ')"
         4434  +    "  || fkey_collate_clause("
         4435  +    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
  4335   4436       ", "
  4336   4437       "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
  4337   4438       "  || group_concat('*=?', ' AND ') || ')'"
  4338   4439       ", "
  4339   4440       "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
  4340   4441       ", "
  4341         -    "     f.[table] || '(' || group_concat(COALESCE(f.[to], "
  4342         -    "       (SELECT name FROM pragma_table_info(f.[table]) WHERE pk=seq+1)"
  4343         -    "     )) || ')'"
         4442  +    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
  4344   4443       ", "
  4345   4444       "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
  4346   4445       "  || ' ON ' || quote(s.name) || '('"
  4347   4446       "  || group_concat(quote(f.[from]) ||"
  4348         -    "        fkey_collate_clause(f.[table], f.[to], s.name, f.[from]), ', ')"
         4447  +    "        fkey_collate_clause("
         4448  +    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
  4349   4449       "  || ');'"
  4350   4450       ", "
  4351   4451       "     f.[table] "
  4352         -
  4353   4452       "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
         4453  +    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
  4354   4454       "GROUP BY s.name, f.id "
  4355   4455       "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
  4356   4456     ;
         4457  +  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
  4357   4458   
  4358   4459     for(i=2; i<nArg; i++){
  4359   4460       int n = (int)strlen(azArg[i]);
  4360   4461       if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
  4361   4462         bVerbose = 1;
  4362   4463       }
  4363   4464       else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
................................................................................
  4398   4499         const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
  4399   4500         const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
  4400   4501   
  4401   4502         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
  4402   4503         if( rc!=SQLITE_OK ) break;
  4403   4504         if( SQLITE_ROW==sqlite3_step(pExplain) ){
  4404   4505           const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
  4405         -        res = (0==sqlite3_strglob(zGlob, zPlan));
         4506  +        res = (
         4507  +              0==sqlite3_strglob(zGlob, zPlan)
         4508  +           || 0==sqlite3_strglob(zGlobIPK, zPlan)
         4509  +        );
  4406   4510         }
  4407   4511         rc = sqlite3_finalize(pExplain);
  4408   4512         if( rc!=SQLITE_OK ) break;
  4409   4513   
  4410   4514         if( res<0 ){
  4411   4515           raw_printf(stderr, "Error: internal error");
  4412   4516           break;
................................................................................
  4676   4780     if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
  4677   4781       rc = shell_dbinfo_command(p, nArg, azArg);
  4678   4782     }else
  4679   4783   
  4680   4784     if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
  4681   4785       const char *zLike = 0;
  4682   4786       int i;
         4787  +    int savedShowHeader = p->showHeader;
  4683   4788       ShellClearFlag(p, SHFLG_PreserveRowid);
  4684   4789       for(i=1; i<nArg; i++){
  4685   4790         if( azArg[i][0]=='-' ){
  4686   4791           const char *z = azArg[i]+1;
  4687   4792           if( z[0]=='-' ) z++;
  4688   4793           if( strcmp(z,"preserve-rowids")==0 ){
  4689   4794   #ifdef SQLITE_OMIT_VIRTUALTABLE
................................................................................
  4711   4816       open_db(p, 0);
  4712   4817       /* When playing back a "dump", the content might appear in an order
  4713   4818       ** which causes immediate foreign key constraints to be violated.
  4714   4819       ** So disable foreign-key constraint enforcement to prevent problems. */
  4715   4820       raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
  4716   4821       raw_printf(p->out, "BEGIN TRANSACTION;\n");
  4717   4822       p->writableSchema = 0;
         4823  +    p->showHeader = 0;
  4718   4824       /* Set writable_schema=ON since doing so forces SQLite to initialize
  4719   4825       ** as much of the schema as it can even if the sqlite_master table is
  4720   4826       ** corrupt. */
  4721   4827       sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
  4722   4828       p->nErr = 0;
  4723   4829       if( zLike==0 ){
  4724   4830         run_schema_dump_query(p,
................................................................................
  4752   4858       if( p->writableSchema ){
  4753   4859         raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
  4754   4860         p->writableSchema = 0;
  4755   4861       }
  4756   4862       sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
  4757   4863       sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
  4758   4864       raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
         4865  +    p->showHeader = savedShowHeader;
  4759   4866     }else
  4760   4867   
  4761   4868     if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
  4762   4869       if( nArg==2 ){
  4763   4870         setOrClearFlag(p, SHFLG_Echo, azArg[1]);
  4764   4871       }else{
  4765   4872         raw_printf(stderr, "Usage: .echo on|off\n");

Changes to src/sqlite.h.in.

  2206   2206   ** running statements reaches zero are interrupted as if they had been
  2207   2207   ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
  2208   2208   ** that are started after the running statement count reaches zero are
  2209   2209   ** not effected by the sqlite3_interrupt().
  2210   2210   ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
  2211   2211   ** SQL statements is a no-op and has no effect on SQL statements
  2212   2212   ** that are started after the sqlite3_interrupt() call returns.
  2213         -**
  2214         -** If the database connection closes while [sqlite3_interrupt()]
  2215         -** is running then bad things will likely happen.
  2216   2213   */
  2217   2214   void sqlite3_interrupt(sqlite3*);
  2218   2215   
  2219   2216   /*
  2220   2217   ** CAPI3REF: Determine If An SQL Statement Is Complete
  2221   2218   **
  2222   2219   ** These routines are useful during command-line input to determine if the

Changes to src/sqliteInt.h.

  2443   2443   ** column expression as it exists in a SELECT statement.  However, if
  2444   2444   ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
  2445   2445   ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
  2446   2446   ** form is used for name resolution with nested FROM clauses.
  2447   2447   */
  2448   2448   struct ExprList {
  2449   2449     int nExpr;             /* Number of expressions on the list */
         2450  +  int nAlloc;            /* Number of a[] slots allocated */
  2450   2451     struct ExprList_item { /* For each expression in the list */
  2451   2452       Expr *pExpr;            /* The parse tree for this expression */
  2452   2453       char *zName;            /* Token associated with this expression */
  2453   2454       char *zSpan;            /* Original text of the expression */
  2454   2455       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
  2455   2456       unsigned done :1;       /* A flag to indicate when processing is finished */
  2456   2457       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
................................................................................
  2458   2459       union {
  2459   2460         struct {
  2460   2461           u16 iOrderByCol;      /* For ORDER BY, column number in result set */
  2461   2462           u16 iAlias;           /* Index into Parse.aAlias[] for zName */
  2462   2463         } x;
  2463   2464         int iConstExprReg;      /* Register in which Expr value is cached */
  2464   2465       } u;
  2465         -  } *a;                  /* Alloc a power of two greater or equal to nExpr */
         2466  +  } a[1];                  /* One slot for each expression in the list */
  2466   2467   };
  2467   2468   
  2468   2469   /*
  2469   2470   ** An instance of this structure is used by the parser to record both
  2470   2471   ** the parse tree for an expression and the span of input text for an
  2471   2472   ** expression.
  2472   2473   */
................................................................................
  3476   3477   void *sqlite3DbMallocRawNN(sqlite3*, u64);
  3477   3478   char *sqlite3DbStrDup(sqlite3*,const char*);
  3478   3479   char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
  3479   3480   void *sqlite3Realloc(void*, u64);
  3480   3481   void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
  3481   3482   void *sqlite3DbRealloc(sqlite3 *, void *, u64);
  3482   3483   void sqlite3DbFree(sqlite3*, void*);
         3484  +void sqlite3DbFreeNN(sqlite3*, void*);
  3483   3485   int sqlite3MallocSize(void*);
  3484   3486   int sqlite3DbMallocSize(sqlite3*, void*);
  3485   3487   void *sqlite3ScratchMalloc(int);
  3486   3488   void sqlite3ScratchFree(void*);
  3487   3489   void *sqlite3PageMalloc(int);
  3488   3490   void sqlite3PageFree(void*);
  3489   3491   void sqlite3MemSetDefault(void);

Changes to src/tokenize.c.

   600    600   
   601    601     if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
   602    602     sqlite3DeleteTrigger(db, pParse->pNewTrigger);
   603    603     sqlite3DbFree(db, pParse->pVList);
   604    604     while( pParse->pAinc ){
   605    605       AutoincInfo *p = pParse->pAinc;
   606    606       pParse->pAinc = p->pNext;
   607         -    sqlite3DbFree(db, p);
          607  +    sqlite3DbFreeNN(db, p);
   608    608     }
   609    609     while( pParse->pZombieTab ){
   610    610       Table *p = pParse->pZombieTab;
   611    611       pParse->pZombieTab = p->pNextZombie;
   612    612       sqlite3DeleteTable(db, p);
   613    613     }
   614    614     assert( nErr==0 || pParse->rc!=SQLITE_OK );
   615    615     return nErr;
   616    616   }

Changes to src/vdbe.c.

  2436   2436   ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
  2437   2437   ** information about the format of the data.)  Extract the P2-th column
  2438   2438   ** from this record.  If there are less that (P2+1) 
  2439   2439   ** values in the record, extract a NULL.
  2440   2440   **
  2441   2441   ** The value extracted is stored in register P3.
  2442   2442   **
  2443         -** If the column contains fewer than P2 fields, then extract a NULL.  Or,
         2443  +** If the record contains fewer than P2 fields, then extract a NULL.  Or,
  2444   2444   ** if the P4 argument is a P4_MEM use the value of the P4 argument as
  2445   2445   ** the result.
  2446   2446   **
  2447   2447   ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
  2448   2448   ** then the cache of the cursor is reset prior to extracting the column.
  2449   2449   ** The first OP_Column against a pseudo-table after the value of the content
  2450   2450   ** register has changed should have this bit set.
  2451   2451   **
  2452         -** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
         2452  +** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
  2453   2453   ** the result is guaranteed to only be used as the argument of a length()
  2454   2454   ** or typeof() function, respectively.  The loading of large blobs can be
  2455   2455   ** skipped for length() and all content loading can be skipped for typeof().
  2456   2456   */
  2457   2457   case OP_Column: {
  2458   2458     int p2;            /* column number to retrieve */
  2459   2459     VdbeCursor *pC;    /* The VDBE cursor */
................................................................................
  2706   2706   **
  2707   2707   ** P4 is a string that is P2 characters long. The nth character of the
  2708   2708   ** string indicates the column affinity that should be used for the nth
  2709   2709   ** memory cell in the range.
  2710   2710   */
  2711   2711   case OP_Affinity: {
  2712   2712     const char *zAffinity;   /* The affinity to be applied */
  2713         -  char cAff;               /* A single character of affinity */
  2714   2713   
  2715   2714     zAffinity = pOp->p4.z;
  2716   2715     assert( zAffinity!=0 );
         2716  +  assert( pOp->p2>0 );
  2717   2717     assert( zAffinity[pOp->p2]==0 );
  2718   2718     pIn1 = &aMem[pOp->p1];
  2719         -  while( (cAff = *(zAffinity++))!=0 ){
         2719  +  do{
  2720   2720       assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
  2721   2721       assert( memIsValid(pIn1) );
  2722         -    applyAffinity(pIn1, cAff, encoding);
         2722  +    applyAffinity(pIn1, *(zAffinity++), encoding);
  2723   2723       pIn1++;
  2724         -  }
         2724  +  }while( zAffinity[0] );
  2725   2725     break;
  2726   2726   }
  2727   2727   
  2728   2728   /* Opcode: MakeRecord P1 P2 P3 P4 *
  2729   2729   ** Synopsis: r[P3]=mkrec(r[P1@P2])
  2730   2730   **
  2731   2731   ** Convert P2 registers beginning with P1 into the [record format]
................................................................................
  2888   2888     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
  2889   2889     pOut->n = (int)nByte;
  2890   2890     pOut->flags = MEM_Blob;
  2891   2891     if( nZero ){
  2892   2892       pOut->u.nZero = nZero;
  2893   2893       pOut->flags |= MEM_Zero;
  2894   2894     }
  2895         -  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
  2896   2895     REGISTER_TRACE(pOp->p3, pOut);
  2897   2896     UPDATE_MAX_BLOBSIZE(pOut);
  2898   2897     break;
  2899   2898   }
  2900   2899   
  2901   2900   /* Opcode: Count P1 P2 * * *
  2902   2901   ** Synopsis: r[P2]=count()
................................................................................
  4073   4072         if( pIdxKey->aMem[ii].flags & MEM_Null ){
  4074   4073           takeJump = 1;
  4075   4074           break;
  4076   4075         }
  4077   4076       }
  4078   4077     }
  4079   4078     rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
  4080         -  if( pFree ) sqlite3DbFree(db, pFree);
         4079  +  if( pFree ) sqlite3DbFreeNN(db, pFree);
  4081   4080     if( rc!=SQLITE_OK ){
  4082   4081       goto abort_due_to_error;
  4083   4082     }
  4084   4083     pC->seekResult = res;
  4085   4084     alreadyExists = (res==0);
  4086   4085     pC->nullRow = 1-alreadyExists;
  4087   4086     pC->deferredMoveto = 0;
................................................................................
  5383   5382   ** P3==1 then the table to be clear is in the auxiliary database file
  5384   5383   ** that is used to store tables create using CREATE TEMPORARY TABLE.
  5385   5384   **
  5386   5385   ** If AUTOVACUUM is enabled then it is possible that another root page
  5387   5386   ** might be moved into the newly deleted root page in order to keep all
  5388   5387   ** root pages contiguous at the beginning of the database.  The former
  5389   5388   ** value of the root page that moved - its value before the move occurred -
  5390         -** is stored in register P2.  If no page 
  5391         -** movement was required (because the table being dropped was already 
  5392         -** the last one in the database) then a zero is stored in register P2.
  5393         -** If AUTOVACUUM is disabled then a zero is stored in register P2.
         5389  +** is stored in register P2. If no page movement was required (because the
         5390  +** table being dropped was already the last one in the database) then a 
         5391  +** zero is stored in register P2.  If AUTOVACUUM is disabled then a zero 
         5392  +** is stored in register P2.
         5393  +**
         5394  +** This opcode throws an error if there are any active reader VMs when
         5395  +** it is invoked. This is done to avoid the difficulty associated with 
         5396  +** updating existing cursors when a root page is moved in an AUTOVACUUM 
         5397  +** database. This error is thrown even if the database is not an AUTOVACUUM 
         5398  +** db in order to avoid introducing an incompatibility between autovacuum 
         5399  +** and non-autovacuum modes.
  5394   5400   **
  5395   5401   ** See also: Clear
  5396   5402   */
  5397   5403   case OP_Destroy: {     /* out2 */
  5398   5404     int iMoved;
  5399   5405     int iDb;
  5400   5406   
................................................................................
  5591   5597       }else{
  5592   5598         assert( db->init.busy==0 );
  5593   5599         db->init.busy = 1;
  5594   5600         initData.rc = SQLITE_OK;
  5595   5601         assert( !db->mallocFailed );
  5596   5602         rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
  5597   5603         if( rc==SQLITE_OK ) rc = initData.rc;
  5598         -      sqlite3DbFree(db, zSql);
         5604  +      sqlite3DbFreeNN(db, zSql);
  5599   5605         db->init.busy = 0;
  5600   5606       }
  5601   5607     }
  5602   5608     if( rc ){
  5603   5609       sqlite3ResetAllSchemasOfConnection(db);
  5604   5610       if( rc==SQLITE_NOMEM ){
  5605   5611         goto no_mem;

Changes to src/vdbeaux.c.

   807    807   
   808    808   /*
   809    809   ** If the input FuncDef structure is ephemeral, then free it.  If
   810    810   ** the FuncDef is not ephermal, then do nothing.
   811    811   */
   812    812   static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
   813    813     if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
   814         -    sqlite3DbFree(db, pDef);
          814  +    sqlite3DbFreeNN(db, pDef);
   815    815     }
   816    816   }
   817    817   
   818    818   static void vdbeFreeOpArray(sqlite3 *, Op *, int);
   819    819   
   820    820   /*
   821    821   ** Delete a P4 value if necessary.
   822    822   */
   823    823   static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
   824    824     if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
   825         -  sqlite3DbFree(db, p);
          825  +  sqlite3DbFreeNN(db, p);
   826    826   }
   827    827   static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
   828    828     freeEphemeralFunction(db, p->pFunc);
   829         -  sqlite3DbFree(db, p);
          829  + sqlite3DbFreeNN(db, p);
   830    830   }
   831    831   static void freeP4(sqlite3 *db, int p4type, void *p4){
   832    832     assert( db );
   833    833     switch( p4type ){
   834    834       case P4_FUNCCTX: {
   835    835         freeP4FuncCtx(db, (sqlite3_context*)p4);
   836    836         break;
................................................................................
   881    881       Op *pOp;
   882    882       for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
   883    883         if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p);
   884    884   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   885    885         sqlite3DbFree(db, pOp->zComment);
   886    886   #endif     
   887    887       }
          888  +    sqlite3DbFreeNN(db, aOp);
   888    889     }
   889         -  sqlite3DbFree(db, aOp);
   890    890   }
   891    891   
   892    892   /*
   893    893   ** Link the SubProgram object passed as the second argument into the linked
   894    894   ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
   895    895   ** objects when the VM is no longer required.
   896    896   */
................................................................................
  1555   1555         testcase( p->flags & MEM_Agg );
  1556   1556         testcase( p->flags & MEM_Dyn );
  1557   1557         testcase( p->flags & MEM_Frame );
  1558   1558         testcase( p->flags & MEM_RowSet );
  1559   1559         if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
  1560   1560           sqlite3VdbeMemRelease(p);
  1561   1561         }else if( p->szMalloc ){
  1562         -        sqlite3DbFree(db, p->zMalloc);
         1562  +        sqlite3DbFreeNN(db, p->zMalloc);
  1563   1563           p->szMalloc = 0;
  1564   1564         }
  1565   1565   
  1566   1566         p->flags = MEM_Undefined;
  1567   1567       }while( (++p)<pEnd );
  1568   1568     }
  1569   1569   }
................................................................................
  2926   2926           fprintf(out, "%s", zHdr);
  2927   2927           sqlite3VdbePrintOp(out, i, &p->aOp[i]);
  2928   2928         }
  2929   2929         fclose(out);
  2930   2930       }
  2931   2931     }
  2932   2932   #endif
  2933         -  p->iCurrentTime = 0;
  2934   2933     p->magic = VDBE_MAGIC_RESET;
  2935   2934     return p->rc & db->errMask;
  2936   2935   }
  2937   2936    
  2938   2937   /*
  2939   2938   ** Clean up and delete a VDBE after execution.  Return an integer which is
  2940   2939   ** the result code.  Write any error message text into *pzErrMsg.
................................................................................
  3036   3035       db->pVdbe = p->pNext;
  3037   3036     }
  3038   3037     if( p->pNext ){
  3039   3038       p->pNext->pPrev = p->pPrev;
  3040   3039     }
  3041   3040     p->magic = VDBE_MAGIC_DEAD;
  3042   3041     p->db = 0;
  3043         -  sqlite3DbFree(db, p);
         3042  +  sqlite3DbFreeNN(db, p);
  3044   3043   }
  3045   3044   
  3046   3045   /*
  3047   3046   ** The cursor "p" has a pending seek operation that has not yet been
  3048   3047   ** carried out.  Seek the cursor now.  If an error occurs, return
  3049   3048   ** the appropriate error code.
  3050   3049   */
................................................................................
  4595   4594   static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
  4596   4595     if( p ){
  4597   4596       int i;
  4598   4597       for(i=0; i<nField; i++){
  4599   4598         Mem *pMem = &p->aMem[i];
  4600   4599         if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
  4601   4600       }
  4602         -    sqlite3DbFree(db, p);
         4601  +    sqlite3DbFreeNN(db, p);
  4603   4602     }
  4604   4603   }
  4605   4604   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
  4606   4605   
  4607   4606   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  4608   4607   /*
  4609   4608   ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
................................................................................
  4662   4661     vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
  4663   4662     vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
  4664   4663     if( preupdate.aNew ){
  4665   4664       int i;
  4666   4665       for(i=0; i<pCsr->nField; i++){
  4667   4666         sqlite3VdbeMemRelease(&preupdate.aNew[i]);
  4668   4667       }
  4669         -    sqlite3DbFree(db, preupdate.aNew);
         4668  +    sqlite3DbFreeNN(db, preupdate.aNew);
  4670   4669     }
  4671   4670   }
  4672   4671   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */

Changes to src/vdbemem.c.

   127    127          || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
   128    128     if( pMem->szMalloc<n ){
   129    129       if( n<32 ) n = 32;
   130    130       if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
   131    131         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   132    132         bPreserve = 0;
   133    133       }else{
   134         -      if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
          134  +      if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
   135    135         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   136    136       }
   137    137       if( pMem->zMalloc==0 ){
   138    138         sqlite3VdbeMemSetNull(pMem);
   139    139         pMem->z = 0;
   140    140         pMem->szMalloc = 0;
   141    141         return SQLITE_NOMEM_BKPT;
................................................................................
   337    337       t.flags = MEM_Null;
   338    338       t.db = pMem->db;
   339    339       ctx.pOut = &t;
   340    340       ctx.pMem = pMem;
   341    341       ctx.pFunc = pFunc;
   342    342       pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   343    343       assert( (pMem->flags & MEM_Dyn)==0 );
   344         -    if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
          344  +    if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
   345    345       memcpy(pMem, &t, sizeof(t));
   346    346       rc = ctx.isError;
   347    347     }
   348    348     return rc;
   349    349   }
   350    350   
   351    351   /*
................................................................................
   388    388   ** to be freed.
   389    389   */
   390    390   static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
   391    391     if( VdbeMemDynamic(p) ){
   392    392       vdbeMemClearExternAndSetNull(p);
   393    393     }
   394    394     if( p->szMalloc ){
   395         -    sqlite3DbFree(p->db, p->zMalloc);
          395  +    sqlite3DbFreeNN(p->db, p->zMalloc);
   396    396       p->szMalloc = 0;
   397    397     }
   398    398     p->z = 0;
   399    399   }
   400    400   
   401    401   /*
   402    402   ** Release any memory resources held by the Mem.  Both the memory that is
................................................................................
   416    416   }
   417    417   
   418    418   /*
   419    419   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   420    420   ** If the double is out of range of a 64-bit signed integer then
   421    421   ** return the closest available 64-bit signed integer.
   422    422   */
   423         -static i64 doubleToInt64(double r){
          423  +static SQLITE_NOINLINE i64 doubleToInt64(double r){
   424    424   #ifdef SQLITE_OMIT_FLOATING_POINT
   425    425     /* When floating-point is omitted, double and int64 are the same thing */
   426    426     return r;
   427    427   #else
   428    428     /*
   429    429     ** Many compilers we encounter do not define constants for the
   430    430     ** minimum and maximum 64-bit integers, or they define them
................................................................................
   452    452   ** a floating-point then the value returned is the integer part.
   453    453   ** If pMem is a string or blob, then we make an attempt to convert
   454    454   ** it into an integer and return that.  If pMem represents an
   455    455   ** an SQL-NULL value, return 0.
   456    456   **
   457    457   ** If pMem represents a string value, its encoding might be changed.
   458    458   */
          459  +static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
          460  +  i64 value = 0;
          461  +  sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
          462  +  return value;
          463  +}
   459    464   i64 sqlite3VdbeIntValue(Mem *pMem){
   460    465     int flags;
   461    466     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   462    467     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   463    468     flags = pMem->flags;
   464    469     if( flags & MEM_Int ){
   465    470       return pMem->u.i;
   466    471     }else if( flags & MEM_Real ){
   467    472       return doubleToInt64(pMem->u.r);
   468    473     }else if( flags & (MEM_Str|MEM_Blob) ){
   469         -    i64 value = 0;
   470    474       assert( pMem->z || pMem->n==0 );
   471         -    sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
   472         -    return value;
          475  +    return memIntValue(pMem);
   473    476     }else{
   474    477       return 0;
   475    478     }
   476    479   }
   477    480   
   478    481   /*
   479    482   ** Return the best representation of pMem that we can get into a
   480    483   ** double.  If pMem is already a double or an integer, return its
   481    484   ** value.  If it is a string or blob, try to convert it to a double.
   482    485   ** If it is a NULL, return 0.0.
   483    486   */
          487  +static SQLITE_NOINLINE double memRealValue(Mem *pMem){
          488  +  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
          489  +  double val = (double)0;
          490  +  sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
          491  +  return val;
          492  +}
   484    493   double sqlite3VdbeRealValue(Mem *pMem){
   485    494     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   486    495     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   487    496     if( pMem->flags & MEM_Real ){
   488    497       return pMem->u.r;
   489    498     }else if( pMem->flags & MEM_Int ){
   490    499       return (double)pMem->u.i;
   491    500     }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
   492         -    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   493         -    double val = (double)0;
   494         -    sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
   495         -    return val;
          501  +    return memRealValue(pMem);
   496    502     }else{
   497    503       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   498    504       return (double)0;
   499    505     }
   500    506   }
   501    507   
   502    508   /*
................................................................................
  1113   1119             assert( pRec->pKeyInfo->enc==ENC(db) );
  1114   1120             pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
  1115   1121             for(i=0; i<nCol; i++){
  1116   1122               pRec->aMem[i].flags = MEM_Null;
  1117   1123               pRec->aMem[i].db = db;
  1118   1124             }
  1119   1125           }else{
  1120         -          sqlite3DbFree(db, pRec);
         1126  +          sqlite3DbFreeNN(db, pRec);
  1121   1127             pRec = 0;
  1122   1128           }
  1123   1129         }
  1124   1130         if( pRec==0 ) return 0;
  1125   1131         p->ppRec[0] = pRec;
  1126   1132       }
  1127   1133     
................................................................................
  1225   1231     if( rc!=SQLITE_OK ){
  1226   1232       pVal = 0;
  1227   1233     }
  1228   1234     if( apVal ){
  1229   1235       for(i=0; i<nVal; i++){
  1230   1236         sqlite3ValueFree(apVal[i]);
  1231   1237       }
  1232         -    sqlite3DbFree(db, apVal);
         1238  +    sqlite3DbFreeNN(db, apVal);
  1233   1239     }
  1234   1240   
  1235   1241     *ppVal = pVal;
  1236   1242     return rc;
  1237   1243   }
  1238   1244   #else
  1239   1245   # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
................................................................................
  1424   1430     if( aRet==0 ){
  1425   1431       sqlite3_result_error_nomem(context);
  1426   1432     }else{
  1427   1433       aRet[0] = nSerial+1;
  1428   1434       putVarint32(&aRet[1], iSerial);
  1429   1435       sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
  1430   1436       sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
  1431         -    sqlite3DbFree(db, aRet);
         1437  +    sqlite3DbFreeNN(db, aRet);
  1432   1438     }
  1433   1439   }
  1434   1440   
  1435   1441   /*
  1436   1442   ** Register built-in functions used to help read ANALYZE data.
  1437   1443   */
  1438   1444   void sqlite3AnalyzeFunctions(void){
................................................................................
  1651   1657       int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
  1652   1658       Mem *aMem = pRec->aMem;
  1653   1659       sqlite3 *db = aMem[0].db;
  1654   1660       for(i=0; i<nCol; i++){
  1655   1661         sqlite3VdbeMemRelease(&aMem[i]);
  1656   1662       }
  1657   1663       sqlite3KeyInfoUnref(pRec->pKeyInfo);
  1658         -    sqlite3DbFree(db, pRec);
         1664  +    sqlite3DbFreeNN(db, pRec);
  1659   1665     }
  1660   1666   }
  1661   1667   #endif /* ifdef SQLITE_ENABLE_STAT4 */
  1662   1668   
  1663   1669   /*
  1664   1670   ** Change the string value of an sqlite3_value object
  1665   1671   */
................................................................................
  1675   1681   
  1676   1682   /*
  1677   1683   ** Free an sqlite3_value object
  1678   1684   */
  1679   1685   void sqlite3ValueFree(sqlite3_value *v){
  1680   1686     if( !v ) return;
  1681   1687     sqlite3VdbeMemRelease((Mem *)v);
  1682         -  sqlite3DbFree(((Mem*)v)->db, v);
         1688  +  sqlite3DbFreeNN(((Mem*)v)->db, v);
  1683   1689   }
  1684   1690   
  1685   1691   /*
  1686   1692   ** The sqlite3ValueBytes() routine returns the number of bytes in the
  1687   1693   ** sqlite3_value object assuming that it uses the encoding "enc".
  1688   1694   ** The valueBytes() routine is a helper function.
  1689   1695   */

Changes to src/vdbesort.c.

   854    854     const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
   855    855     const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
   856    856     int res;                              /* Return value */
   857    857   
   858    858     assert( (s1>0 && s1<7) || s1==8 || s1==9 );
   859    859     assert( (s2>0 && s2<7) || s2==8 || s2==9 );
   860    860   
   861         -  if( s1>7 && s2>7 ){
          861  +  if( s1==s2 ){
          862  +    /* The two values have the same sign. Compare using memcmp(). */
          863  +    static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
          864  +    const u8 n = aLen[s1];
          865  +    int i;
          866  +    res = 0;
          867  +    for(i=0; i<n; i++){
          868  +      if( (res = v1[i] - v2[i])!=0 ){
          869  +        if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
          870  +          res = v1[0] & 0x80 ? -1 : +1;
          871  +        }
          872  +        break;
          873  +      }
          874  +    }
          875  +  }else if( s1>7 && s2>7 ){
   862    876       res = s1 - s2;
   863    877     }else{
   864         -    if( s1==s2 ){
   865         -      if( (*v1 ^ *v2) & 0x80 ){
   866         -        /* The two values have different signs */
   867         -        res = (*v1 & 0x80) ? -1 : +1;
   868         -      }else{
   869         -        /* The two values have the same sign. Compare using memcmp(). */
   870         -        static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
   871         -        int i;
   872         -        res = 0;
   873         -        for(i=0; i<aLen[s1]; i++){
   874         -          if( (res = v1[i] - v2[i]) ) break;
   875         -        }
   876         -      }
          878  +    if( s2>7 ){
          879  +      res = +1;
          880  +    }else if( s1>7 ){
          881  +      res = -1;
          882  +    }else{
          883  +      res = s1 - s2;
          884  +    }
          885  +    assert( res!=0 );
          886  +
          887  +    if( res>0 ){
          888  +      if( *v1 & 0x80 ) res = -1;
   877    889       }else{
   878         -      if( s2>7 ){
   879         -        res = +1;
   880         -      }else if( s1>7 ){
   881         -        res = -1;
   882         -      }else{
   883         -        res = s1 - s2;
   884         -      }
   885         -      assert( res!=0 );
   886         -
   887         -      if( res>0 ){
   888         -        if( *v1 & 0x80 ) res = -1;
   889         -      }else{
   890         -        if( *v2 & 0x80 ) res = +1;
   891         -      }
          890  +      if( *v2 & 0x80 ) res = +1;
   892    891       }
   893    892     }
   894    893   
   895    894     if( res==0 ){
   896    895       if( pTask->pSorter->pKeyInfo->nField>1 ){
   897    896         res = vdbeSorterCompareTail(
   898    897             pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2

Changes to src/where.c.

  1782   1782     if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
  1783   1783       if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
  1784   1784         sqlite3_free(p->u.vtab.idxStr);
  1785   1785         p->u.vtab.needFree = 0;
  1786   1786         p->u.vtab.idxStr = 0;
  1787   1787       }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
  1788   1788         sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
  1789         -      sqlite3DbFree(db, p->u.btree.pIndex);
         1789  +      sqlite3DbFreeNN(db, p->u.btree.pIndex);
  1790   1790         p->u.btree.pIndex = 0;
  1791   1791       }
  1792   1792     }
  1793   1793   }
  1794   1794   
  1795   1795   /*
  1796   1796   ** Deallocate internal memory used by a WhereLoop object
  1797   1797   */
  1798   1798   static void whereLoopClear(sqlite3 *db, WhereLoop *p){
  1799         -  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
         1799  +  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
  1800   1800     whereLoopClearUnion(db, p);
  1801   1801     whereLoopInit(p);
  1802   1802   }
  1803   1803   
  1804   1804   /*
  1805   1805   ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
  1806   1806   */
................................................................................
  1807   1807   static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
  1808   1808     WhereTerm **paNew;
  1809   1809     if( p->nLSlot>=n ) return SQLITE_OK;
  1810   1810     n = (n+7)&~7;
  1811   1811     paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
  1812   1812     if( paNew==0 ) return SQLITE_NOMEM_BKPT;
  1813   1813     memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
  1814         -  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
         1814  +  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
  1815   1815     p->aLTerm = paNew;
  1816   1816     p->nLSlot = n;
  1817   1817     return SQLITE_OK;
  1818   1818   }
  1819   1819   
  1820   1820   /*
  1821   1821   ** Transfer content from the second pLoop into the first.
................................................................................
  1837   1837   }
  1838   1838   
  1839   1839   /*
  1840   1840   ** Delete a WhereLoop object
  1841   1841   */
  1842   1842   static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
  1843   1843     whereLoopClear(db, p);
  1844         -  sqlite3DbFree(db, p);
         1844  +  sqlite3DbFreeNN(db, p);
  1845   1845   }
  1846   1846   
  1847   1847   /*
  1848   1848   ** Free a WhereInfo structure
  1849   1849   */
  1850   1850   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
  1851   1851     if( ALWAYS(pWInfo) ){
................................................................................
  1858   1858       }
  1859   1859       sqlite3WhereClauseClear(&pWInfo->sWC);
  1860   1860       while( pWInfo->pLoops ){
  1861   1861         WhereLoop *p = pWInfo->pLoops;
  1862   1862         pWInfo->pLoops = p->pNextLoop;
  1863   1863         whereLoopDelete(db, p);
  1864   1864       }
  1865         -    sqlite3DbFree(db, pWInfo);
         1865  +    sqlite3DbFreeNN(db, pWInfo);
  1866   1866     }
  1867   1867   }
  1868   1868   
  1869   1869   /*
  1870   1870   ** Return TRUE if all of the following are true:
  1871   1871   **
  1872   1872   **   (1)  X has the same or lower cost that Y
................................................................................
  3249   3249         WHERETRACE(0x40, ("  VirtualOne: all disabled and w/o IN\n"));
  3250   3250         rc = whereLoopAddVirtualOne(
  3251   3251             pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
  3252   3252       }
  3253   3253     }
  3254   3254   
  3255   3255     if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
  3256         -  sqlite3DbFree(pParse->db, p);
         3256  +  sqlite3DbFreeNN(pParse->db, p);
  3257   3257     return rc;
  3258   3258   }
  3259   3259   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  3260   3260   
  3261   3261   /*
  3262   3262   ** Add WhereLoop entries to handle OR terms.  This works for either
  3263   3263   ** btrees or virtual tables.
................................................................................
  4104   4104       aTo = aFrom;
  4105   4105       aFrom = pFrom;
  4106   4106       nFrom = nTo;
  4107   4107     }
  4108   4108   
  4109   4109     if( nFrom==0 ){
  4110   4110       sqlite3ErrorMsg(pParse, "no query solution");
  4111         -    sqlite3DbFree(db, pSpace);
         4111  +    sqlite3DbFreeNN(db, pSpace);
  4112   4112       return SQLITE_ERROR;
  4113   4113     }
  4114   4114     
  4115   4115     /* Find the lowest cost path.  pFrom will be left pointing to that path */
  4116   4116     pFrom = aFrom;
  4117   4117     for(ii=1; ii<nFrom; ii++){
  4118   4118       if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
................................................................................
  4180   4180       }
  4181   4181     }
  4182   4182   
  4183   4183   
  4184   4184     pWInfo->nRowOut = pFrom->nRow;
  4185   4185   
  4186   4186     /* Free temporary memory and return success */
  4187         -  sqlite3DbFree(db, pSpace);
         4187  +  sqlite3DbFreeNN(db, pSpace);
  4188   4188     return SQLITE_OK;
  4189   4189   }
  4190   4190   
  4191   4191   /*
  4192   4192   ** Most queries use only a single table (they are not joins) and have
  4193   4193   ** simple == constraints against indexed fields.  This routine attempts
  4194   4194   ** to plan those simple cases using much less ceremony than the
................................................................................
  4258   4258         pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
  4259   4259         break;
  4260   4260       }
  4261   4261     }
  4262   4262     if( pLoop->wsFlags ){
  4263   4263       pLoop->nOut = (LogEst)1;
  4264   4264       pWInfo->a[0].pWLoop = pLoop;
  4265         -    pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
         4265  +    assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
         4266  +    pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
  4266   4267       pWInfo->a[0].iTabCur = iCur;
  4267   4268       pWInfo->nRowOut = 1;
  4268   4269       if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
  4269   4270       if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
  4270   4271         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  4271   4272       }
  4272   4273   #ifdef SQLITE_DEBUG

Changes to test/insert4.test.

    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the INSERT transfer optimization.
    13     13   #
    14     14   # $Id: insert4.test,v 1.10 2008/01/21 16:22:46 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
           18  +set testprefix insert4
    18     19   
    19     20   ifcapable !view||!subquery {
    20     21     finish_test
    21     22     return
    22     23   }
    23     24   
    24     25   # The sqlite3_xferopt_count variable is incremented whenever the 
................................................................................
   561    562   } {1 3}
   562    563   
   563    564   do_catchsql_test insert4-9.1 {
   564    565     DROP TABLE IF EXISTS t1;
   565    566     CREATE TABLE t1(x);
   566    567     INSERT INTO t1(x) VALUES(5 COLLATE xyzzy) UNION SELECT 0;
   567    568   } {1 {no such collation sequence: xyzzy}}
          569  +
          570  +#-------------------------------------------------------------------------
          571  +# Check that running an integrity-check does not disable the xfer 
          572  +# optimization for tables with CHECK constraints.
          573  +#
          574  +do_execsql_test 10.1 {
          575  +  CREATE TABLE t8(
          576  +    rid INTEGER,
          577  +    pid INTEGER,
          578  +    mid INTEGER,
          579  +    px INTEGER DEFAULT(0) CHECK(px IN(0, 1))
          580  +  );
          581  +  CREATE TEMP TABLE x(
          582  +    rid INTEGER,
          583  +    pid INTEGER,
          584  +    mid INTEGER,
          585  +    px INTEGER DEFAULT(0) CHECK(px IN(0, 1))
          586  +  );
          587  +}
          588  +do_test 10.2 {
          589  +  set sqlite3_xferopt_count 0
          590  +  execsql { INSERT INTO x SELECT * FROM t8 }
          591  +  set sqlite3_xferopt_count
          592  +} {1}
          593  +
          594  +do_test 10.3 {
          595  +  execsql { PRAGMA integrity_check }
          596  +  set sqlite3_xferopt_count 0
          597  +  execsql { INSERT INTO x     SELECT * FROM t8 }
          598  +  set sqlite3_xferopt_count
          599  +} {1}
          600  +
   568    601   
   569    602   finish_test

Changes to test/json102.test.

   293    293   #
   294    294   for {set i 0} {$i<100} {incr i} {
   295    295     set str abcdef[string repeat \" [expr {$i+50}]]uvwxyz
   296    296     do_test json102-[format %d [expr {$i+1300}]] {
   297    297       db eval {SELECT json_extract(json_array($::str),'$[0]')==$::str}
   298    298     } {1}
   299    299   }
          300  +
          301  +#-------------------------------------------------------------------------
          302  +# 2017-04-08 ticket b93be8729a895a528e2849fca99f7
          303  +# JSON extension accepts invalid numeric values
          304  +#
          305  +# JSON does not allow leading zeros.  But the JSON extension was
          306  +# allowing them.  The following tests verify that the problem is now
          307  +# fixed.
          308  +#
          309  +do_execsql_test json102-1401 { SELECT json_valid('{"x":01}') } 0
          310  +do_execsql_test json102-1402 { SELECT json_valid('{"x":-01}') } 0
          311  +do_execsql_test json102-1403 { SELECT json_valid('{"x":0}') } 1
          312  +do_execsql_test json102-1404 { SELECT json_valid('{"x":-0}') } 1
          313  +do_execsql_test json102-1405 { SELECT json_valid('{"x":0.1}') } 1
          314  +do_execsql_test json102-1406 { SELECT json_valid('{"x":-0.1}') } 1
          315  +do_execsql_test json102-1407 { SELECT json_valid('{"x":0.0000}') } 1
          316  +do_execsql_test json102-1408 { SELECT json_valid('{"x":-0.0000}') } 1
          317  +do_execsql_test json102-1409 { SELECT json_valid('{"x":01.5}') } 0
          318  +do_execsql_test json102-1410 { SELECT json_valid('{"x":-01.5}') } 0
          319  +do_execsql_test json102-1411 { SELECT json_valid('{"x":00}') } 0
          320  +do_execsql_test json102-1412 { SELECT json_valid('{"x":-00}') } 0
   300    321   
   301    322   finish_test

Added test/pragmafault.test.

            1  +# 2010 June 15
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +source $testdir/lock_common.tcl
           16  +source $testdir/malloc_common.tcl
           17  +set testprefix pragmafault
           18  +
           19  +db close
           20  +sqlite3 db test.db
           21  +sqlite3_db_config_lookaside db 0 0 0
           22  +do_execsql_test 1.0 {
           23  +  CREATE TABLE t1(a, b, CHECK(a!=b));
           24  +  INSERT INTO t1 VALUES(1, 2);
           25  +  INSERT INTO t1 VALUES(3, 4);
           26  +}
           27  +faultsim_save_and_close
           28  +
           29  +do_faultsim_test 1 -prep {
           30  +  faultsim_restore_and_reopen
           31  +} -body {
           32  +  catchsql { PRAGMA integrity_check }
           33  +  set {} 0
           34  +} -test {
           35  +  faultsim_test_result {0 0} 
           36  +}
           37  +
           38  +
           39  +finish_test

Added test/shell6.test.

            1  +# 2016 December 15
            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  +# Test the shell tool ".lint fkey-indexes" command.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +ifcapable !vtab {finish_test; return}
           18  +set testprefix shell6
           19  +set CLI [test_find_cli]
           20  +db close
           21  +forcedelete test.db test.db-journal test.db-wal
           22  +
           23  +foreach {tn schema output} {
           24  +  1 {
           25  +    CREATE TABLE p1(a PRIMARY KEY, b);
           26  +    CREATE TABLE c1(x, y REFERENCES p1);
           27  +  } {
           28  +    CREATE INDEX 'c1_y' ON 'c1'('y'); --> p1(a)
           29  +  }
           30  +
           31  +  2 {
           32  +    CREATE TABLE p1(a PRIMARY KEY, b);
           33  +    CREATE TABLE c2(x REFERENCES p1, y REFERENCES p1);
           34  +  } {
           35  +    CREATE INDEX 'c2_y' ON 'c2'('y'); --> p1(a)
           36  +    CREATE INDEX 'c2_x' ON 'c2'('x'); --> p1(a)
           37  +  }
           38  +
           39  +  3 {
           40  +    CREATE TABLE 'p 1'(a, b, c, PRIMARY KEY(c, b));
           41  +    CREATE TABLE 'c 1'(x, y, z, FOREIGN KEY (z, y) REFERENCES 'p 1');
           42  +  } {
           43  +    CREATE INDEX 'c 1_z_y' ON 'c 1'('z', 'y'); --> p 1(c,b)
           44  +  }
           45  +
           46  +  4 {
           47  +    CREATE TABLE p1(a, 'b b b' PRIMARY KEY);
           48  +    CREATE TABLE c1('x y z' REFERENCES p1);
           49  +    CREATE INDEX i1 ON c1('x y z') WHERE "x y z" IS NOT NULL;
           50  +  } {
           51  +  }
           52  +
           53  +  5 {
           54  +    CREATE TABLE p1(a, 'b b b' PRIMARY KEY);
           55  +    CREATE TABLE c1('x y z' REFERENCES p1);
           56  +    CREATE INDEX i1 ON c1('x y z') WHERE "x y z" IS NOT 12;
           57  +  } {
           58  +    CREATE INDEX 'c1_x y z' ON 'c1'('x y z'); --> p1(b b b)
           59  +  }
           60  +
           61  +  6 {
           62  +    CREATE TABLE x1(a, b, c, UNIQUE(a, b));
           63  +    CREATE TABLE y1(a, b, c, FOREIGN KEY(b, a) REFERENCES x1(a, b));
           64  +    CREATE INDEX y1i ON y1(a, c, b);
           65  +  } {
           66  +    CREATE INDEX 'y1_b_a' ON 'y1'('b', 'a'); --> x1(a,b)
           67  +  }
           68  +
           69  +  6 {
           70  +    CREATE TABLE x1(a COLLATE nocase, b, UNIQUE(a));
           71  +    CREATE TABLE y1(a COLLATE rtrim REFERENCES x1(a));
           72  +  } {
           73  +    CREATE INDEX 'y1_a' ON 'y1'('a' COLLATE nocase); --> x1(a)
           74  +  }
           75  +
           76  +  7 {
           77  +    CREATE TABLE x1(a PRIMARY KEY COLLATE nocase, b);
           78  +    CREATE TABLE y1(a REFERENCES x1);
           79  +  } {
           80  +    CREATE INDEX 'y1_a' ON 'y1'('a' COLLATE nocase); --> x1(a)
           81  +  }
           82  +
           83  +  8 {
           84  +    CREATE TABLE x1(a, b COLLATE nocase, c COLLATE rtrim, PRIMARY KEY(c, b, a));
           85  +    CREATE TABLE y1(d, e, f, FOREIGN KEY(d, e, f) REFERENCES x1);
           86  +  } {
           87  +    CREATE INDEX 'y1_d_e_f' ON 'y1'('d' COLLATE rtrim, 'e' COLLATE nocase, 'f'); --> x1(c,b,a)
           88  +  }
           89  +
           90  +  9 {
           91  +    CREATE TABLE p1(a, b UNIQUE);
           92  +    CREATE TABLE c1(x INTEGER PRIMARY KEY REFERENCES p1(b));
           93  +  } {
           94  +  }
           95  +
           96  +} {
           97  +  forcedelete test.db
           98  +  sqlite3 db test.db
           99  +  execsql $schema
          100  +
          101  +  set expected ""
          102  +  foreach line [split $output "\n"] {
          103  +    set line [string trim $line]
          104  +    if {$line!=""} {
          105  +      append expected "$line\n"
          106  +    }
          107  +  }
          108  +
          109  +  do_test 1.$tn.1 {
          110  +    set RES [catchcmd test.db [list .lint fkey-indexes]]
          111  +  } [list 0 [string trim $expected]]
          112  +
          113  +  do_test 1.$tn.2 {
          114  +    execsql [lindex $RES 1]
          115  +    catchcmd test.db [list .lint fkey-indexes]
          116  +  } {0 {}}
          117  +
          118  +  db close
          119  +}
          120  +
          121  +finish_test

Changes to test/sync2.test.

    18     18   set testprefix sync2
    19     19   
    20     20   #
    21     21   # These tests are only applicable when pager pragma are
    22     22   # enabled. Also, since every test uses an ATTACHed database, they
    23     23   # are only run when ATTACH is enabled.
    24     24   #
    25         -ifcapable !pager_pragmas||!attach {
           25  +ifcapable !pager_pragmas||!attach||!dirsync {
    26     26     finish_test
    27     27     return
    28     28   }
    29     29   if {$::tcl_platform(platform)!="unix" 
    30     30     || [permutation] == "journaltest"
    31     31     || [permutation] == "inmemory_journal"
    32     32   } {

Changes to tool/mkpragmatab.tcl.

   359    359     NAME: soft_heap_limit
   360    360     FLAG: Result0
   361    361   
   362    362     NAME: threads
   363    363     FLAG: Result0
   364    364   
   365    365     NAME: optimize
   366         -  FLAG: Result1
          366  +  FLAG: Result1 NeedSchema
   367    367   }
   368    368   
   369    369   # Open the output file
   370    370   #
   371    371   set destfile "[file dir [file dir [file normal $argv0]]]/src/pragma.h"
   372    372   puts "Overwriting $destfile with new pragma table..."
   373    373   set fd [open $destfile wb]