/ Check-in [21ca87f6]
Login

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

Overview
Comment:Merge in changes up to and including the 3.6.23.1 release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1:21ca87f69125a9e7124c6ddc566d17f64661b0d3
User & Date: drh 2010-06-16 19:48:57
Context
2010-08-17
23:13
Cherrypick the changes for enhancement requests [e090183531fc27474] (use indices on LIKE with no wildcards) and [4711020446da7d93d993] (use nocase index for LIKE even if the column is binary) into the 3.6.23.1 release of the Apple-OSX branch. check-in: 220cca50 user: drh tags: apple-osx-3623
2010-08-07
11:46
Merge in all changes up to the 3.7.0.1 release. check-in: f88c6367 user: drh tags: apple-osx
2010-06-16
19:48
Merge in changes up to and including the 3.6.23.1 release. check-in: 21ca87f6 user: drh tags: apple-osx
2010-05-19
22:09
Cherry-pick the SQLITE_FCNTL_SIZE_HINT patch (check-in [2b7e3b4a30d6a7c4a8] and bump the version number to 3.6.23.2. check-in: 776679af user: drh tags: branch-3.6.23
2010-02-26
22:05
fix merge error and compiler warning check-in: 5c0afe70 user: adam tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   160    160   
   161    161   # You should not have to change anything below this line
   162    162   ###############################################################################
   163    163   
   164    164   # Object files for the SQLite library (non-amalgamation).
   165    165   #
   166    166   OBJS0 = alter.lo analyze.lo attach.lo auth.lo backup.lo bitvec.lo btmutex.lo \
   167         -        btree.lo build.lo callback.lo complete.lo date.lo \
          167  +        btree.lo build.lo callback.lo complete.lo ctime.lo date.lo \
   168    168           delete.lo expr.lo fault.lo fkey.lo func.lo global.lo \
   169    169           hash.lo journal.lo insert.lo legacy.lo loadext.lo \
   170    170           main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   171    171           memjournal.lo \
   172    172           mutex.lo mutex_noop.lo mutex_os2.lo mutex_unix.lo mutex_w32.lo \
   173    173           notify.lo opcodes.lo os.lo os_unix.lo os_win.lo os_os2.lo \
   174    174           pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
................................................................................
   200    200     $(TOP)/src/btmutex.c \
   201    201     $(TOP)/src/btree.c \
   202    202     $(TOP)/src/btree.h \
   203    203     $(TOP)/src/btreeInt.h \
   204    204     $(TOP)/src/build.c \
   205    205     $(TOP)/src/callback.c \
   206    206     $(TOP)/src/complete.c \
          207  +  $(TOP)/src/ctime.c \
   207    208     $(TOP)/src/date.c \
   208    209     $(TOP)/src/delete.c \
   209    210     $(TOP)/src/expr.c \
   210    211     $(TOP)/src/fault.c \
   211    212     $(TOP)/src/fkey.c \
   212    213     $(TOP)/src/func.c \
   213    214     $(TOP)/src/global.c \
................................................................................
   338    339   TESTSRC2 = \
   339    340     $(TOP)/src/attach.c \
   340    341     $(TOP)/src/backup.c \
   341    342     $(TOP)/src/bitvec.c \
   342    343     $(TOP)/src/btree.c \
   343    344     $(TOP)/src/build.c \
   344    345     $(TOP)/src/date.c \
          346  +  $(TOP)/src/ctime.c \
   345    347     $(TOP)/src/expr.c \
   346    348     $(TOP)/src/func.c \
   347    349     $(TOP)/src/insert.c \
   348    350     $(TOP)/src/mem5.c \
   349    351     $(TOP)/src/os.c \
   350    352     $(TOP)/src/os_os2.c \
   351    353     $(TOP)/src/os_unix.c \
................................................................................
   543    545   
   544    546   callback.lo:	$(TOP)/src/callback.c $(HDR)
   545    547   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/callback.c
   546    548   
   547    549   complete.lo:	$(TOP)/src/complete.c $(HDR)
   548    550   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/complete.c
   549    551   
          552  +ctime.lo:	$(TOP)/src/ctime.c $(HDR)
          553  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/ctime.c
          554  +
   550    555   date.lo:	$(TOP)/src/date.c $(HDR)
   551    556   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/date.c
   552    557   
   553    558   delete.lo:	$(TOP)/src/delete.c $(HDR)
   554    559   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/delete.c
   555    560   
   556    561   expr.lo:	$(TOP)/src/expr.c $(HDR)

Changes to Makefile.vxworks.

   227    227     $(TOP)/src/btmutex.c \
   228    228     $(TOP)/src/btree.c \
   229    229     $(TOP)/src/btree.h \
   230    230     $(TOP)/src/btreeInt.h \
   231    231     $(TOP)/src/build.c \
   232    232     $(TOP)/src/callback.c \
   233    233     $(TOP)/src/complete.c \
          234  +  $(TOP)/src/ctime.c \
   234    235     $(TOP)/src/date.c \
   235    236     $(TOP)/src/delete.c \
   236    237     $(TOP)/src/expr.c \
   237    238     $(TOP)/src/fault.c \
   238    239     $(TOP)/src/func.c \
   239    240     $(TOP)/src/global.c \
   240    241     $(TOP)/src/hash.c \
................................................................................
   388    389     $(TOP)/src/test_wsd.c \
   389    390   
   390    391   #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
   391    392   #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c
   392    393   
   393    394   TESTSRC2 = \
   394    395     $(TOP)/src/attach.c $(TOP)/src/backup.c $(TOP)/src/btree.c                   \
   395         -  $(TOP)/src/build.c $(TOP)/src/date.c                                         \
          396  +  $(TOP)/src/build.c $(TOP)/src/ctime.c $(TOP)/src/date.c                      \
   396    397     $(TOP)/src/expr.c $(TOP)/src/func.c $(TOP)/src/insert.c $(TOP)/src/os.c      \
   397    398     $(TOP)/src/os_os2.c $(TOP)/src/os_unix.c $(TOP)/src/os_win.c                 \
   398    399     $(TOP)/src/pager.c $(TOP)/src/pragma.c $(TOP)/src/prepare.c                  \
   399    400     $(TOP)/src/printf.c $(TOP)/src/random.c $(TOP)/src/pcache.c                  \
   400    401     $(TOP)/src/pcache1.c $(TOP)/src/select.c $(TOP)/src/tokenize.c               \
   401    402     $(TOP)/src/utf.c $(TOP)/src/util.c $(TOP)/src/vdbeapi.c $(TOP)/src/vdbeaux.c \
   402    403     $(TOP)/src/vdbe.c $(TOP)/src/vdbemem.c $(TOP)/src/where.c parse.c            \

Changes to VERSION.

     1         -3.6.22
            1  +3.6.23.2

Changes to configure.

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

Changes to ext/fts3/fts3.c.

   595    595       fts3DbExec(&rc, db, 
   596    596           "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
   597    597           p->zDb, p->zName
   598    598       );
   599    599     }
   600    600     return rc;
   601    601   }
          602  +
          603  +/*
          604  +** An sqlite3_exec() callback for fts3TableExists.
          605  +*/
          606  +static int fts3TableExistsCallback(void *pArg, int n, char **pp1, char **pp2){
          607  +  *(int*)pArg = 1;
          608  +  return 1;
          609  +}
   602    610   
   603    611   /*
   604    612   ** Determine if a table currently exists in the database.
   605    613   */
   606    614   static void fts3TableExists(
   607    615     int *pRc,             /* Success code */
   608    616     sqlite3 *db,          /* The database connection to test */
   609    617     const char *zDb,      /* ATTACHed database within the connection */
   610    618     const char *zName,    /* Name of the FTS3 table */
   611    619     const char *zSuffix,  /* Shadow table extension */
   612    620     u8 *pResult           /* Write results here */
   613    621   ){
   614    622     int rc = SQLITE_OK;
          623  +  int res = 0;
          624  +  char *zSql;
   615    625     if( *pRc ) return;
   616         -  fts3DbExec(&rc, db, "SELECT 1 FROM %Q.'%q%s'", zDb, zName, zSuffix);
   617         -  *pResult = (rc==SQLITE_OK) ? 1 : 0;
   618         -  if( rc!=SQLITE_ERROR ) *pRc = rc;
          626  +  zSql = sqlite3_mprintf(
          627  +    "SELECT 1 FROM %Q.sqlite_master WHERE name='%q%s'",
          628  +    zDb, zName, zSuffix
          629  +  );    
          630  +  rc = sqlite3_exec(db, zSql, fts3TableExistsCallback, &res, 0);
          631  +  sqlite3_free(zSql);
          632  +  *pResult = res & 0xff;
          633  +  if( rc!=SQLITE_ABORT ) *pRc = rc;
   619    634   }
   620    635   
   621    636   /*
   622    637   ** This function is the implementation of both the xConnect and xCreate
   623    638   ** methods of the FTS3 virtual table.
   624    639   **
   625    640   ** The argv[] array contains the following:
................................................................................
  1033   1048     assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
  1034   1049     *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
  1035   1050     *piPrev = iVal;
  1036   1051   }
  1037   1052   
  1038   1053   /*
  1039   1054   ** When this function is called, *ppPoslist is assumed to point to the 
  1040         -** start of a position-list.
         1055  +** start of a position-list. After it returns, *ppPoslist points to the
         1056  +** first byte after the position-list.
         1057  +**
         1058  +** If pp is not NULL, then the contents of the position list are copied
         1059  +** to *pp. *pp is set to point to the first byte past the last byte copied
         1060  +** before this function returns.
  1041   1061   */
  1042   1062   static void fts3PoslistCopy(char **pp, char **ppPoslist){
  1043   1063     char *pEnd = *ppPoslist;
  1044   1064     char c = 0;
  1045   1065   
  1046   1066     /* The end of a position list is marked by a zero encoded as an FTS3 
  1047   1067     ** varint. A single 0x00 byte. Except, if the 0x00 byte is preceded by
................................................................................
  2024   2044       if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  2025   2045         return SQLITE_NOMEM;
  2026   2046       }
  2027   2047   
  2028   2048       rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn, 
  2029   2049           iCol, zQuery, -1, &pCsr->pExpr
  2030   2050       );
  2031         -    if( rc!=SQLITE_OK ) return rc;
         2051  +    if( rc!=SQLITE_OK ){
         2052  +      if( rc==SQLITE_ERROR ){
         2053  +        p->base.zErrMsg = sqlite3_mprintf("malformed MATCH expression: [%s]",
         2054  +                                          zQuery);
         2055  +      }
         2056  +      return rc;
         2057  +    }
  2032   2058   
  2033   2059       rc = evalFts3Expr(p, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0);
  2034   2060       pCsr->pNextId = pCsr->aDoclist;
  2035   2061       pCsr->iPrevId = 0;
  2036   2062     }
  2037   2063   
  2038   2064     if( rc!=SQLITE_OK ) return rc;
................................................................................
  2177   2203       char *pEnd = &pExpr->aDoclist[pExpr->nDoclist];
  2178   2204       char *pCsr = pExpr->pCurrent;
  2179   2205   
  2180   2206       assert( pCsr );
  2181   2207       while( pCsr<pEnd ){
  2182   2208         if( pExpr->iCurrent<iDocid ){
  2183   2209           fts3PoslistCopy(0, &pCsr);
  2184         -        fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
         2210  +        if( pCsr<pEnd ){
         2211  +          fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
         2212  +        }
  2185   2213           pExpr->pCurrent = pCsr;
  2186   2214         }else{
  2187   2215           if( pExpr->iCurrent==iDocid ){
  2188   2216             int iThis = 0;
  2189   2217             if( iCol<0 ){
  2190   2218               /* If iCol is negative, return a pointer to the start of the
  2191   2219               ** position-list (instead of a pointer to the start of a list

Changes to ext/fts3/fts3_snippet.c.

   209    209   ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
   210    210   ** fts3ExprLoadDoclists().
   211    211   */
   212    212   static int fts3ExprLoadDoclistsCb1(Fts3Expr *pExpr, int iPhrase, void *ctx){
   213    213     int rc = SQLITE_OK;
   214    214     LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
   215    215   
          216  +  UNUSED_PARAMETER(iPhrase);
          217  +
   216    218     p->nPhrase++;
   217    219     p->nToken += pExpr->pPhrase->nToken;
   218    220   
   219    221     if( pExpr->isLoaded==0 ){
   220    222       rc = sqlite3Fts3ExprLoadDoclist(p->pTab, pExpr);
   221    223       pExpr->isLoaded = 1;
   222    224       if( rc==SQLITE_OK ){
................................................................................
   229    231   
   230    232   /*
   231    233   ** This is an fts3ExprIterate() callback used while loading the doclists
   232    234   ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
   233    235   ** fts3ExprLoadDoclists().
   234    236   */
   235    237   static int fts3ExprLoadDoclistsCb2(Fts3Expr *pExpr, int iPhrase, void *ctx){
          238  +  UNUSED_PARAMETER(iPhrase);
          239  +  UNUSED_PARAMETER(ctx);
   236    240     if( pExpr->aDoclist ){
   237    241       pExpr->pCurrent = pExpr->aDoclist;
   238    242       pExpr->iCurrent = 0;
   239    243       pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent, &pExpr->iCurrent);
   240    244     }
   241    245     return SQLITE_OK;
   242    246   }
................................................................................
   518    522   */
   519    523   static int fts3StringAppend(
   520    524     StrBuffer *pStr,                /* Buffer to append to */
   521    525     const char *zAppend,            /* Pointer to data to append to buffer */
   522    526     int nAppend                     /* Size of zAppend in bytes (or -1) */
   523    527   ){
   524    528     if( nAppend<0 ){
   525         -    nAppend = strlen(zAppend);
          529  +    nAppend = (int)strlen(zAppend);
   526    530     }
   527    531   
   528    532     /* If there is insufficient space allocated at StrBuffer.z, use realloc()
   529    533     ** to grow the buffer until so that it is big enough to accomadate the
   530    534     ** appended data.
   531    535     */
   532    536     if( pStr->n+nAppend+1>=pStr->nAlloc ){
................................................................................
  1020   1024   static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
  1021   1025     TermOffsetCtx *p = (TermOffsetCtx *)ctx;
  1022   1026     int nTerm;                      /* Number of tokens in phrase */
  1023   1027     int iTerm;                      /* For looping through nTerm phrase terms */
  1024   1028     char *pList;                    /* Pointer to position list for phrase */
  1025   1029     int iPos = 0;                   /* First position in position-list */
  1026   1030   
         1031  +  UNUSED_PARAMETER(iPhrase);
  1027   1032     pList = sqlite3Fts3FindPositions(pExpr, p->iDocid, p->iCol);
  1028   1033     nTerm = pExpr->pPhrase->nToken;
  1029   1034     if( pList ){
  1030   1035       fts3GetDeltaPosition(&pList, &iPos);
  1031   1036       assert( iPos>=0 );
  1032   1037     }
  1033   1038   
................................................................................
  1147   1152           }
  1148   1153           if( rc==SQLITE_OK ){
  1149   1154             char aBuffer[64];
  1150   1155             sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
  1151   1156                 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
  1152   1157             );
  1153   1158             rc = fts3StringAppend(&res, aBuffer, -1);
         1159  +        }else if( rc==SQLITE_DONE ){
         1160  +          rc = SQLITE_CORRUPT;
  1154   1161           }
  1155   1162         }
  1156   1163       }
  1157   1164       if( rc==SQLITE_DONE ){
  1158         -      rc = SQLITE_CORRUPT;
         1165  +      rc = SQLITE_OK;
  1159   1166       }
  1160   1167   
  1161   1168       pMod->xClose(pC);
  1162   1169       if( rc!=SQLITE_OK ) goto offsets_out;
  1163   1170     }
  1164   1171   
  1165   1172    offsets_out:

Changes to ext/fts3/fts3_write.c.

   182    182   /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
   183    183   /* 2  */  "DELETE FROM %Q.'%q_content'",
   184    184   /* 3  */  "DELETE FROM %Q.'%q_segments'",
   185    185   /* 4  */  "DELETE FROM %Q.'%q_segdir'",
   186    186   /* 5  */  "DELETE FROM %Q.'%q_docsize'",
   187    187   /* 6  */  "DELETE FROM %Q.'%q_stat'",
   188    188   /* 7  */  "SELECT * FROM %Q.'%q_content' WHERE rowid=?",
   189         -/* 8  */  "SELECT coalesce(max(idx)+1, 0) FROM %Q.'%q_segdir' WHERE level=?",
          189  +/* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
   190    190   /* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
   191         -/* 10 */  "SELECT coalesce(max(blockid)+1, 1) FROM %Q.'%q_segments'",
          191  +/* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
   192    192   /* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
   193    193   
   194    194             /* Return segments in order from oldest to newest.*/ 
   195    195   /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
   196    196               "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
   197    197   /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
   198    198               "FROM %Q.'%q_segdir' ORDER BY level DESC, idx ASC",
................................................................................
  2270   2270   static void fts3DecodeIntArray(
  2271   2271     int N,             /* The number of integers to decode */
  2272   2272     u32 *a,            /* Write the integer values */
  2273   2273     const char *zBuf,  /* The BLOB containing the varints */
  2274   2274     int nBuf           /* size of the BLOB */
  2275   2275   ){
  2276   2276     int i, j;
         2277  +  UNUSED_PARAMETER(nBuf);
  2277   2278     for(i=j=0; i<N; i++){
  2278   2279       sqlite3_int64 x;
  2279   2280       j += sqlite3Fts3GetVarint(&zBuf[j], &x);
         2281  +    assert(j<=nBuf);
  2280   2282       a[i] = (u32)(x & 0xffffffff);
  2281   2283     }
  2282   2284   }
  2283   2285   
  2284   2286   /*
  2285   2287   ** Fill in the document size auxiliary information for the matchinfo
  2286   2288   ** structure.  The auxiliary information is:
................................................................................
  2417   2419       return;
  2418   2420     }
  2419   2421     if( sqlite3_step(pStmt)==SQLITE_ROW ){
  2420   2422       fts3DecodeIntArray(p->nColumn+1, a,
  2421   2423            sqlite3_column_blob(pStmt, 0),
  2422   2424            sqlite3_column_bytes(pStmt, 0));
  2423   2425     }else{
  2424         -    memset(a, 0, sizeof(int)*(p->nColumn+1) );
         2426  +    memset(a, 0, sizeof(u32)*(p->nColumn+1) );
  2425   2427     }
  2426   2428     sqlite3_reset(pStmt);
  2427         -  if( nChng<0 && a[0]<-nChng ){
         2429  +  if( nChng<0 && a[0]<(u32)(-nChng) ){
  2428   2430       a[0] = 0;
  2429   2431     }else{
  2430   2432       a[0] += nChng;
  2431   2433     }
  2432   2434     for(i=0; i<p->nColumn; i++){
  2433   2435       u32 x = a[i+1];
  2434   2436       if( x+aSzIns[i] < aSzDel[i] ){

Changes to ext/rtree/rtree.c.

  2700   2700     void *pAux,                         /* One of the RTREE_COORD_* constants */
  2701   2701     int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
  2702   2702     sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
  2703   2703     char **pzErr,                       /* OUT: Error message, if any */
  2704   2704     int isCreate                        /* True for xCreate, false for xConnect */
  2705   2705   ){
  2706   2706     int rc = SQLITE_OK;
  2707         -  int iPageSize = 0;
  2708   2707     Rtree *pRtree;
  2709   2708     int nDb;              /* Length of string argv[1] */
  2710   2709     int nName;            /* Length of string argv[2] */
  2711   2710     int eCoordType = (int)(intptr_t)pAux;
  2712   2711   
  2713   2712     const char *aErrMsg[] = {
  2714   2713       0,                                                    /* 0 */

Changes to main.mk.

    48     48   TCCX += -I$(TOP)/ext/rtree -I$(TOP)/ext/icu -I$(TOP)/ext/fts3
    49     49   TCCX += -I$(TOP)/ext/async
    50     50   
    51     51   # Object files for the SQLite library.
    52     52   #
    53     53   LIBOBJ+= alter.o analyze.o attach.o auth.o \
    54     54            backup.o bitvec.o btmutex.o btree.o build.o \
    55         -         callback.o complete.o date.o delete.o expr.o fault.o fkey.o \
           55  +         callback.o complete.o ctime.o date.o delete.o expr.o fault.o fkey.o \
    56     56            fts3.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    57     57            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o fts3_write.o \
    58     58            func.o global.o hash.o \
    59     59            icu.o insert.o journal.o legacy.o loadext.o \
    60     60            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    61     61            memjournal.o \
    62     62            mutex.o mutex_noop.o mutex_os2.o mutex_unix.o mutex_w32.o \
................................................................................
    82     82     $(TOP)/src/btmutex.c \
    83     83     $(TOP)/src/btree.c \
    84     84     $(TOP)/src/btree.h \
    85     85     $(TOP)/src/btreeInt.h \
    86     86     $(TOP)/src/build.c \
    87     87     $(TOP)/src/callback.c \
    88     88     $(TOP)/src/complete.c \
           89  +  $(TOP)/src/ctime.c \
    89     90     $(TOP)/src/date.c \
    90     91     $(TOP)/src/delete.c \
    91     92     $(TOP)/src/expr.c \
    92     93     $(TOP)/src/fault.c \
    93     94     $(TOP)/src/fkey.c \
    94     95     $(TOP)/src/func.c \
    95     96     $(TOP)/src/global.c \

Changes to publish.sh.

    30     30   gcc $CFLAGS -Itsrc sqlite3.c tsrc/shell.c -o sqlite3 -ldl
    31     31   strip sqlite3
    32     32   mv sqlite3 sqlite3-$VERS.bin
    33     33   gzip sqlite3-$VERS.bin
    34     34   chmod 644 sqlite3-$VERS.bin.gz
    35     35   mv sqlite3-$VERS.bin.gz doc
    36     36   
    37         -# Build a source archive useful for windows.
    38         -#
    39         -make target_source
    40         -cd tsrc
    41         -echo '***** BUILDING preprocessed source archives'
    42         -rm fts[12]* icu*
    43         -rm -f ../doc/sqlite-source-$VERSW.zip
    44         -zip ../doc/sqlite-source-$VERSW.zip *
    45         -cd ..
    46         -cp tsrc/sqlite3.h tsrc/sqlite3ext.h .
    47         -pwd
    48         -zip doc/sqlite-amalgamation-$VERSW.zip sqlite3.c sqlite3.h sqlite3ext.h
    49         -
    50     37   # Build the sqlite.so and tclsqlite.so shared libraries
    51     38   # under Linux
    52     39   #
    53     40   TCLDIR=/home/drh/tcltk/846/linux/846linux
    54     41   TCLSTUBLIB=$TCLDIR/libtclstub8.4g.a
    55     42   CFLAGS="-Os -DSQLITE_ENABLE_FTS3=3 -DSQLITE_ENABLE_RTREE=1"
    56     43   CFLAGS="$CFLAGS -DHAVE_LOCALTIME_R=1 -DHAVE_GMTIME_R=1"
................................................................................
    83     70   #
    84     71   OPTS='-DSTATIC_BUILD=1 -DNDEBUG=1 -DSQLITE_THREADSAFE=0'
    85     72   OPTS="$OPTS -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_RTREE=1"
    86     73   i386-mingw32msvc-gcc -Os $OPTS -Itsrc -I$TCLDIR sqlite3.c tsrc/shell.c \
    87     74         -o sqlite3.exe
    88     75   zip doc/sqlite-$VERSW.zip sqlite3.exe
    89     76   
           77  +# Build a source archive useful for windows.
           78  +#
           79  +make target_source
           80  +cd tsrc
           81  +echo '***** BUILDING preprocessed source archives'
           82  +rm fts[12]* icu*
           83  +rm -f ../doc/sqlite-source-$VERSW.zip
           84  +zip ../doc/sqlite-source-$VERSW.zip *
           85  +cd ..
           86  +cp tsrc/sqlite3.h tsrc/sqlite3ext.h .
           87  +cp tsrc/shell.c .
           88  +pwd
           89  +zip doc/sqlite-amalgamation-$VERSW.zip sqlite3.c sqlite3.h sqlite3ext.h shell.c sqlite3.def
           90  +
    90     91   # Construct a tarball of the source tree
    91     92   #
    92     93   echo '***** BUILDING source archive'
    93     94   ORIGIN=`pwd`
    94     95   cd $srcdir
    95     96   chmod +x configure
    96     97   cd ..

Changes to src/analyze.c.

   555    555   
   556    556     /* Load new statistics out of the sqlite_stat1 table */
   557    557     zSql = sqlite3MPrintf(db, 
   558    558         "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
   559    559     if( zSql==0 ){
   560    560       rc = SQLITE_NOMEM;
   561    561     }else{
   562         -    (void)sqlite3SafetyOff(db);
   563    562       rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
   564         -    (void)sqlite3SafetyOn(db);
   565    563       sqlite3DbFree(db, zSql);
   566    564     }
   567    565   
   568    566   
   569    567     /* Load the statistics from the sqlite_stat2 table. */
   570    568   #ifdef SQLITE_ENABLE_STAT2
   571    569     if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
................................................................................
   575    573       sqlite3_stmt *pStmt = 0;
   576    574   
   577    575       zSql = sqlite3MPrintf(db, 
   578    576           "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
   579    577       if( !zSql ){
   580    578         rc = SQLITE_NOMEM;
   581    579       }else{
   582         -      (void)sqlite3SafetyOff(db);
   583    580         rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   584         -      (void)sqlite3SafetyOn(db);
   585    581         sqlite3DbFree(db, zSql);
   586    582       }
   587    583   
   588    584       if( rc==SQLITE_OK ){
   589         -      (void)sqlite3SafetyOff(db);
   590    585         while( sqlite3_step(pStmt)==SQLITE_ROW ){
   591    586           char *zIndex = (char *)sqlite3_column_text(pStmt, 0);
   592    587           Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase);
   593    588           if( pIdx ){
   594    589             int iSample = sqlite3_column_int(pStmt, 1);
   595    590             sqlite3 *dbMem = pIdx->pTable->dbMem;
   596    591             assert( dbMem==db || dbMem==0 );
................................................................................
   632    627                   }
   633    628                 }
   634    629               }
   635    630             }
   636    631           }
   637    632         }
   638    633         rc = sqlite3_finalize(pStmt);
   639         -      (void)sqlite3SafetyOn(db);
   640    634       }
   641    635     }
   642    636   #endif
   643    637   
   644    638     if( rc==SQLITE_NOMEM ){
   645    639       db->mallocFailed = 1;
   646    640     }
   647    641     return rc;
   648    642   }
   649    643   
   650    644   
   651    645   #endif /* SQLITE_OMIT_ANALYZE */

Changes to src/attach.c.

   143    143       }
   144    144       pPager = sqlite3BtreePager(aNew->pBt);
   145    145       sqlite3PagerLockingMode(pPager, db->dfltLockMode);
   146    146       sqlite3PagerJournalMode(pPager, db->dfltJournalMode);
   147    147       sqlite3BtreeSecureDelete(aNew->pBt,
   148    148                                sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
   149    149     }
   150         -  aNew->zName = sqlite3DbStrDup(db, zName);
   151    150     aNew->safety_level = 3;
          151  +  aNew->zName = sqlite3DbStrDup(db, zName);
          152  +  if( rc==SQLITE_OK && aNew->zName==0 ){
          153  +    rc = SQLITE_NOMEM;
          154  +  }
          155  +
   152    156   
   153    157   #ifdef SQLITE_HAS_CODEC
   154    158     if( rc==SQLITE_OK ){
   155    159       extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
   156    160       extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
   157    161       int nKey;
   158    162       char *zKey;
................................................................................
   182    186   
   183    187     /* If the file was opened successfully, read the schema for the new database.
   184    188     ** If this fails, or if opening the file failed, then close the file and 
   185    189     ** remove the entry from the db->aDb[] array. i.e. put everything back the way
   186    190     ** we found it.
   187    191     */
   188    192     if( rc==SQLITE_OK ){
   189         -    (void)sqlite3SafetyOn(db);
   190    193       sqlite3BtreeEnterAll(db);
   191    194       rc = sqlite3Init(db, &zErrDyn);
   192    195       sqlite3BtreeLeaveAll(db);
   193         -    (void)sqlite3SafetyOff(db);
   194    196     }
   195    197     if( rc ){
   196    198       int iDb = db->nDb - 1;
   197    199       assert( iDb>=2 );
   198    200       if( db->aDb[iDb].pBt ){
   199    201         sqlite3BtreeClose(db->aDb[iDb].pBt);
   200    202         db->aDb[iDb].pBt = 0;

Changes to src/backup.c.

    94     94       pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
    95     95       if( pParse==0 ){
    96     96         sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
    97     97         rc = SQLITE_NOMEM;
    98     98       }else{
    99     99         pParse->db = pDb;
   100    100         if( sqlite3OpenTempDatabase(pParse) ){
   101         -        sqlite3ErrorClear(pParse);
   102    101           sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
   103    102           rc = SQLITE_ERROR;
   104    103         }
          104  +      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
   105    105         sqlite3StackFree(pErrorDb, pParse);
   106    106       }
   107    107       if( rc ){
   108    108         return 0;
   109    109       }
   110    110     }
   111    111   

Changes to src/btree.c.

  4924   4924     nFree = get4byte(&pPage1->aData[36]);
  4925   4925     put4byte(&pPage1->aData[36], nFree+1);
  4926   4926   
  4927   4927     if( pBt->secureDelete ){
  4928   4928       /* If the secure_delete option is enabled, then
  4929   4929       ** always fully overwrite deleted information with zeros.
  4930   4930       */
  4931         -    if( (!pPage && (rc = btreeGetPage(pBt, iPage, &pPage, 0)))
  4932         -     ||            (rc = sqlite3PagerWrite(pPage->pDbPage))
         4931  +    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
         4932  +     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
  4933   4933       ){
  4934   4934         goto freepage_out;
  4935   4935       }
  4936   4936       memset(pPage->aData, 0, pPage->pBt->pageSize);
  4937   4937     }
  4938   4938   
  4939   4939     /* If the database supports auto-vacuum, write an entry in the pointer-map
................................................................................
  5057   5057         ** file the database must be corrupt. */
  5058   5058         return SQLITE_CORRUPT_BKPT;
  5059   5059       }
  5060   5060       if( nOvfl ){
  5061   5061         rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
  5062   5062         if( rc ) return rc;
  5063   5063       }
  5064         -    rc = freePage2(pBt, pOvfl, ovflPgno);
         5064  +
         5065  +    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
         5066  +     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
         5067  +    ){
         5068  +      /* There is no reason any cursor should have an outstanding reference 
         5069  +      ** to an overflow page belonging to a cell that is being deleted/updated.
         5070  +      ** So if there exists more than one reference to this page, then it 
         5071  +      ** must not really be an overflow page and the database must be corrupt. 
         5072  +      ** It is helpful to detect this before calling freePage2(), as 
         5073  +      ** freePage2() may zero the page contents if secure-delete mode is
         5074  +      ** enabled. If this 'overflow' page happens to be a page that the
         5075  +      ** caller is iterating through or using in some other way, this
         5076  +      ** can be problematic.
         5077  +      */
         5078  +      rc = SQLITE_CORRUPT_BKPT;
         5079  +    }else{
         5080  +      rc = freePage2(pBt, pOvfl, ovflPgno);
         5081  +    }
         5082  +
  5065   5083       if( pOvfl ){
  5066   5084         sqlite3PagerUnref(pOvfl->pDbPage);
  5067   5085       }
  5068   5086       if( rc ) return rc;
  5069   5087       ovflPgno = iNext;
  5070   5088     }
  5071   5089     return SQLITE_OK;
................................................................................
  5301   5319     int i,            /* New cell becomes the i-th cell of the page */
  5302   5320     u8 *pCell,        /* Content of the new cell */
  5303   5321     int sz,           /* Bytes of content in pCell */
  5304   5322     u8 *pTemp,        /* Temp storage space for pCell, if needed */
  5305   5323     Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
  5306   5324     int *pRC          /* Read and write return code from here */
  5307   5325   ){
  5308         -  int idx;          /* Where to write new cell content in data[] */
         5326  +  int idx = 0;      /* Where to write new cell content in data[] */
  5309   5327     int j;            /* Loop counter */
  5310   5328     int end;          /* First byte past the last cell pointer in data[] */
  5311   5329     int ins;          /* Index in data[] where new cell pointer is inserted */
  5312   5330     int cellOffset;   /* Address of first cell pointer in data[] */
  5313   5331     u8 *data;         /* The content of the whole page */
  5314   5332     u8 *ptr;          /* Used for moving information around in data[] */
  5315   5333   
................................................................................
  5793   5811         **
  5794   5812         ** Unless SQLite is compiled in secure-delete mode. In this case,
  5795   5813         ** the dropCell() routine will overwrite the entire cell with zeroes.
  5796   5814         ** In this case, temporarily copy the cell into the aOvflSpace[]
  5797   5815         ** buffer. It will be copied out again as soon as the aSpace[] buffer
  5798   5816         ** is allocated.  */
  5799   5817         if( pBt->secureDelete ){
  5800         -        memcpy(&aOvflSpace[apDiv[i]-pParent->aData], apDiv[i], szNew[i]);
  5801         -        apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
         5818  +        int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
         5819  +        if( (iOff+szNew[i])>pBt->usableSize ){
         5820  +          rc = SQLITE_CORRUPT_BKPT;
         5821  +          memset(apOld, 0, (i+1)*sizeof(MemPage*));
         5822  +          goto balance_cleanup;
         5823  +        }else{
         5824  +          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
         5825  +          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
         5826  +        }
  5802   5827         }
  5803   5828         dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
  5804   5829       }
  5805   5830     }
  5806   5831   
  5807   5832     /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
  5808   5833     ** alignment */
................................................................................
  5915   5940       subtotal += szCell[i] + 2;
  5916   5941       if( subtotal > usableSpace ){
  5917   5942         szNew[k] = subtotal - szCell[i];
  5918   5943         cntNew[k] = i;
  5919   5944         if( leafData ){ i--; }
  5920   5945         subtotal = 0;
  5921   5946         k++;
  5922         -      if( k>NB+1 ){ rc = SQLITE_CORRUPT; goto balance_cleanup; }
         5947  +      if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
  5923   5948       }
  5924   5949     }
  5925   5950     szNew[k] = subtotal;
  5926   5951     cntNew[k] = nCell;
  5927   5952     k++;
  5928   5953   
  5929   5954     /*
................................................................................
  5969   5994       nOld>=3 ? apOld[2]->pgno : 0
  5970   5995     ));
  5971   5996   
  5972   5997     /*
  5973   5998     ** Allocate k new pages.  Reuse old pages where possible.
  5974   5999     */
  5975   6000     if( apOld[0]->pgno<=1 ){
  5976         -    rc = SQLITE_CORRUPT;
         6001  +    rc = SQLITE_CORRUPT_BKPT;
  5977   6002       goto balance_cleanup;
  5978   6003     }
  5979   6004     pageFlags = apOld[0]->aData[0];
  5980   6005     for(i=0; i<k; i++){
  5981   6006       MemPage *pNew;
  5982   6007       if( i<nOld ){
  5983   6008         pNew = apNew[i] = apOld[i];
................................................................................
  7407   7432   **      7.  Verify that the depth of all children is the same.
  7408   7433   **      8.  Make sure this page is at least 33% full or else it is
  7409   7434   **          the root of the tree.
  7410   7435   */
  7411   7436   static int checkTreePage(
  7412   7437     IntegrityCk *pCheck,  /* Context for the sanity check */
  7413   7438     int iPage,            /* Page number of the page to check */
  7414         -  char *zParentContext  /* Parent context */
         7439  +  char *zParentContext, /* Parent context */
         7440  +  i64 *pnParentMinKey, 
         7441  +  i64 *pnParentMaxKey
  7415   7442   ){
  7416   7443     MemPage *pPage;
  7417   7444     int i, rc, depth, d2, pgno, cnt;
  7418   7445     int hdr, cellStart;
  7419   7446     int nCell;
  7420   7447     u8 *data;
  7421   7448     BtShared *pBt;
  7422   7449     int usableSize;
  7423   7450     char zContext[100];
  7424   7451     char *hit = 0;
         7452  +  i64 nMinKey = 0;
         7453  +  i64 nMaxKey = 0;
  7425   7454   
  7426   7455     sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
  7427   7456   
  7428   7457     /* Check that the page exists
  7429   7458     */
  7430   7459     pBt = pCheck->pBt;
  7431   7460     usableSize = pBt->usableSize;
................................................................................
  7460   7489       */
  7461   7490       sqlite3_snprintf(sizeof(zContext), zContext,
  7462   7491                "On tree page %d cell %d: ", iPage, i);
  7463   7492       pCell = findCell(pPage,i);
  7464   7493       btreeParseCellPtr(pPage, pCell, &info);
  7465   7494       sz = info.nData;
  7466   7495       if( !pPage->intKey ) sz += (int)info.nKey;
         7496  +    /* For intKey pages, check that the keys are in order.
         7497  +    */
         7498  +    else if( i==0 ) nMinKey = nMaxKey = info.nKey;
         7499  +    else{
         7500  +      if( info.nKey <= nMaxKey ){
         7501  +        checkAppendMsg(pCheck, zContext, 
         7502  +            "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
         7503  +      }
         7504  +      nMaxKey = info.nKey;
         7505  +    }
  7467   7506       assert( sz==info.nPayload );
  7468   7507       if( (sz>info.nLocal) 
  7469   7508        && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
  7470   7509       ){
  7471   7510         int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
  7472   7511         Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
  7473   7512   #ifndef SQLITE_OMIT_AUTOVACUUM
................................................................................
  7483   7522       if( !pPage->leaf ){
  7484   7523         pgno = get4byte(pCell);
  7485   7524   #ifndef SQLITE_OMIT_AUTOVACUUM
  7486   7525         if( pBt->autoVacuum ){
  7487   7526           checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
  7488   7527         }
  7489   7528   #endif
  7490         -      d2 = checkTreePage(pCheck, pgno, zContext);
         7529  +      d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
  7491   7530         if( i>0 && d2!=depth ){
  7492   7531           checkAppendMsg(pCheck, zContext, "Child page depth differs");
  7493   7532         }
  7494   7533         depth = d2;
  7495   7534       }
  7496   7535     }
         7536  +
  7497   7537     if( !pPage->leaf ){
  7498   7538       pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
  7499   7539       sqlite3_snprintf(sizeof(zContext), zContext, 
  7500   7540                        "On page %d at right child: ", iPage);
  7501   7541   #ifndef SQLITE_OMIT_AUTOVACUUM
  7502   7542       if( pBt->autoVacuum ){
  7503         -      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
         7543  +      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
  7504   7544       }
  7505   7545   #endif
  7506         -    checkTreePage(pCheck, pgno, zContext);
         7546  +    checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
  7507   7547     }
  7508   7548    
         7549  +  /* For intKey leaf pages, check that the min/max keys are in order
         7550  +  ** with any left/parent/right pages.
         7551  +  */
         7552  +  if( pPage->leaf && pPage->intKey ){
         7553  +    /* if we are a left child page */
         7554  +    if( pnParentMinKey ){
         7555  +      /* if we are the left most child page */
         7556  +      if( !pnParentMaxKey ){
         7557  +        if( nMaxKey > *pnParentMinKey ){
         7558  +          checkAppendMsg(pCheck, zContext, 
         7559  +              "Rowid %lld out of order (max larger than parent min of %lld)",
         7560  +              nMaxKey, *pnParentMinKey);
         7561  +        }
         7562  +      }else{
         7563  +        if( nMinKey <= *pnParentMinKey ){
         7564  +          checkAppendMsg(pCheck, zContext, 
         7565  +              "Rowid %lld out of order (min less than parent min of %lld)",
         7566  +              nMinKey, *pnParentMinKey);
         7567  +        }
         7568  +        if( nMaxKey > *pnParentMaxKey ){
         7569  +          checkAppendMsg(pCheck, zContext, 
         7570  +              "Rowid %lld out of order (max larger than parent max of %lld)",
         7571  +              nMaxKey, *pnParentMaxKey);
         7572  +        }
         7573  +        *pnParentMinKey = nMaxKey;
         7574  +      }
         7575  +    /* else if we're a right child page */
         7576  +    } else if( pnParentMaxKey ){
         7577  +      if( nMinKey <= *pnParentMaxKey ){
         7578  +        checkAppendMsg(pCheck, zContext, 
         7579  +            "Rowid %lld out of order (min less than parent max of %lld)",
         7580  +            nMinKey, *pnParentMaxKey);
         7581  +      }
         7582  +    }
         7583  +  }
         7584  +
  7509   7585     /* Check for complete coverage of the page
  7510   7586     */
  7511   7587     data = pPage->aData;
  7512   7588     hdr = pPage->hdrOffset;
  7513   7589     hit = sqlite3PageMalloc( pBt->pageSize );
  7514   7590     if( hit==0 ){
  7515   7591       pCheck->mallocFailed = 1;
................................................................................
  7525   7601         u16 size = 1024;
  7526   7602         int j;
  7527   7603         if( pc<=usableSize-4 ){
  7528   7604           size = cellSizePtr(pPage, &data[pc]);
  7529   7605         }
  7530   7606         if( (pc+size-1)>=usableSize ){
  7531   7607           checkAppendMsg(pCheck, 0, 
  7532         -            "Corruption detected in cell %d on page %d",i,iPage,0);
         7608  +            "Corruption detected in cell %d on page %d",i,iPage);
  7533   7609         }else{
  7534   7610           for(j=pc+size-1; j>=pc; j--) hit[j]++;
  7535   7611         }
  7536   7612       }
  7537   7613       i = get2byte(&data[hdr+1]);
  7538   7614       while( i>0 ){
  7539   7615         int size, j;
................................................................................
  7631   7707     for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
  7632   7708       if( aRoot[i]==0 ) continue;
  7633   7709   #ifndef SQLITE_OMIT_AUTOVACUUM
  7634   7710       if( pBt->autoVacuum && aRoot[i]>1 ){
  7635   7711         checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
  7636   7712       }
  7637   7713   #endif
  7638         -    checkTreePage(&sCheck, aRoot[i], "List of tree roots: ");
         7714  +    checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
  7639   7715     }
  7640   7716   
  7641   7717     /* Make sure every page in the file is referenced
  7642   7718     */
  7643   7719     for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
  7644   7720   #ifdef SQLITE_OMIT_AUTOVACUUM
  7645   7721       if( sCheck.anRef[i]==0 ){

Changes to src/build.c.

   198    198       *  See ticket [a696379c1f08866] */
   199    199       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   200    200       sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem,
   201    201                            pParse->nTab, pParse->nMaxArg, pParse->explain,
   202    202                            pParse->isMultiWrite && pParse->mayAbort);
   203    203       pParse->rc = SQLITE_DONE;
   204    204       pParse->colNamesSet = 0;
   205         -  }else if( pParse->rc==SQLITE_OK ){
          205  +  }else{
   206    206       pParse->rc = SQLITE_ERROR;
   207    207     }
   208    208     pParse->nTab = 0;
   209    209     pParse->nMem = 0;
   210    210     pParse->nSet = 0;
   211    211     pParse->nVar = 0;
   212    212     pParse->cookieMask = 0;
................................................................................
  1970   1970     int iDb;
  1971   1971   
  1972   1972     if( db->mallocFailed ){
  1973   1973       goto exit_drop_table;
  1974   1974     }
  1975   1975     assert( pParse->nErr==0 );
  1976   1976     assert( pName->nSrc==1 );
         1977  +  if( noErr ) db->suppressErr++;
  1977   1978     pTab = sqlite3LocateTable(pParse, isView, 
  1978   1979                               pName->a[0].zName, pName->a[0].zDatabase);
         1980  +  if( noErr ) db->suppressErr--;
  1979   1981   
  1980   1982     if( pTab==0 ){
  1981         -    if( noErr ){
  1982         -      sqlite3ErrorClear(pParse);
  1983         -    }
  1984   1983       goto exit_drop_table;
  1985   1984     }
  1986   1985     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1987   1986     assert( iDb>=0 && iDb<db->nDb );
  1988   1987   
  1989   1988     /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
  1990   1989     ** it is initialized.

Changes to src/complete.c.


Added src/ctime.c.

            1  +/*
            2  +** 2010 February 23
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file implements routines used to report what compile-time options
           14  +** SQLite was built with.
           15  +*/
           16  +
           17  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
           18  +
           19  +#include "sqliteInt.h"
           20  +
           21  +/*
           22  +** An array of names of all compile-time options.  This array should 
           23  +** be sorted A-Z.
           24  +**
           25  +** This array looks large, but in a typical installation actually uses
           26  +** only a handful of compile-time options, so most times this array is usually
           27  +** rather short and uses little memory space.
           28  +*/
           29  +static const char * const azCompileOpt[] = {
           30  +
           31  +/* These macros are provided to "stringify" the value of the define
           32  +** for those options in which the value is meaningful. */
           33  +#define CTIMEOPT_VAL_(opt) #opt
           34  +#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
           35  +
           36  +#ifdef SQLITE_32BIT_ROWID
           37  +  "32BIT_ROWID",
           38  +#endif
           39  +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
           40  +  "4_BYTE_ALIGNED_MALLOC",
           41  +#endif
           42  +#ifdef SQLITE_CASE_SENSITIVE_LIKE
           43  +  "CASE_SENSITIVE_LIKE",
           44  +#endif
           45  +#ifdef SQLITE_CHECK_PAGES
           46  +  "CHECK_PAGES",
           47  +#endif
           48  +#ifdef SQLITE_COVERAGE_TEST
           49  +  "COVERAGE_TEST",
           50  +#endif
           51  +#ifdef SQLITE_DEBUG
           52  +  "DEBUG",
           53  +#endif
           54  +#ifdef SQLITE_DEFAULT_LOCKING_MODE
           55  +  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
           56  +#endif
           57  +#ifdef SQLITE_DISABLE_DIRSYNC
           58  +  "DISABLE_DIRSYNC",
           59  +#endif
           60  +#ifdef SQLITE_DISABLE_LFS
           61  +  "DISABLE_LFS",
           62  +#endif
           63  +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
           64  +  "ENABLE_ATOMIC_WRITE",
           65  +#endif
           66  +#ifdef SQLITE_ENABLE_CEROD
           67  +  "ENABLE_CEROD",
           68  +#endif
           69  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
           70  +  "ENABLE_COLUMN_METADATA",
           71  +#endif
           72  +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
           73  +  "ENABLE_EXPENSIVE_ASSERT",
           74  +#endif
           75  +#ifdef SQLITE_ENABLE_FTS1
           76  +  "ENABLE_FTS1",
           77  +#endif
           78  +#ifdef SQLITE_ENABLE_FTS2
           79  +  "ENABLE_FTS2",
           80  +#endif
           81  +#ifdef SQLITE_ENABLE_FTS3
           82  +  "ENABLE_FTS3",
           83  +#endif
           84  +#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
           85  +  "ENABLE_FTS3_PARENTHESIS",
           86  +#endif
           87  +#ifdef SQLITE_ENABLE_FTS4
           88  +  "ENABLE_FTS4",
           89  +#endif
           90  +#ifdef SQLITE_ENABLE_ICU
           91  +  "ENABLE_ICU",
           92  +#endif
           93  +#ifdef SQLITE_ENABLE_IOTRACE
           94  +  "ENABLE_IOTRACE",
           95  +#endif
           96  +#ifdef SQLITE_ENABLE_LOAD_EXTENSION
           97  +  "ENABLE_LOAD_EXTENSION",
           98  +#endif
           99  +#ifdef SQLITE_ENABLE_LOCKING_STYLE
          100  +  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
          101  +#endif
          102  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
          103  +  "ENABLE_MEMORY_MANAGEMENT",
          104  +#endif
          105  +#ifdef SQLITE_ENABLE_MEMSYS3
          106  +  "ENABLE_MEMSYS3",
          107  +#endif
          108  +#ifdef SQLITE_ENABLE_MEMSYS5
          109  +  "ENABLE_MEMSYS5",
          110  +#endif
          111  +#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
          112  +  "ENABLE_OVERSIZE_CELL_CHECK",
          113  +#endif
          114  +#ifdef SQLITE_ENABLE_RTREE
          115  +  "ENABLE_RTREE",
          116  +#endif
          117  +#ifdef SQLITE_ENABLE_STAT2
          118  +  "ENABLE_STAT2",
          119  +#endif
          120  +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
          121  +  "ENABLE_UNLOCK_NOTIFY",
          122  +#endif
          123  +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
          124  +  "ENABLE_UPDATE_DELETE_LIMIT",
          125  +#endif
          126  +#ifdef SQLITE_HAS_CODEC
          127  +  "HAS_CODEC",
          128  +#endif
          129  +#ifdef SQLITE_HAVE_ISNAN
          130  +  "HAVE_ISNAN",
          131  +#endif
          132  +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
          133  +  "HOMEGROWN_RECURSIVE_MUTEX",
          134  +#endif
          135  +#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
          136  +  "IGNORE_AFP_LOCK_ERRORS",
          137  +#endif
          138  +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
          139  +  "IGNORE_FLOCK_LOCK_ERRORS",
          140  +#endif
          141  +#ifdef SQLITE_INT64_TYPE
          142  +  "INT64_TYPE",
          143  +#endif
          144  +#ifdef SQLITE_LOCK_TRACE
          145  +  "LOCK_TRACE",
          146  +#endif
          147  +#ifdef SQLITE_MEMDEBUG
          148  +  "MEMDEBUG",
          149  +#endif
          150  +#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
          151  +  "MIXED_ENDIAN_64BIT_FLOAT",
          152  +#endif
          153  +#ifdef SQLITE_NO_SYNC
          154  +  "NO_SYNC",
          155  +#endif
          156  +#ifdef SQLITE_OMIT_ALTERTABLE
          157  +  "OMIT_ALTERTABLE",
          158  +#endif
          159  +#ifdef SQLITE_OMIT_ANALYZE
          160  +  "OMIT_ANALYZE",
          161  +#endif
          162  +#ifdef SQLITE_OMIT_ATTACH
          163  +  "OMIT_ATTACH",
          164  +#endif
          165  +#ifdef SQLITE_OMIT_AUTHORIZATION
          166  +  "OMIT_AUTHORIZATION",
          167  +#endif
          168  +#ifdef SQLITE_OMIT_AUTOINCREMENT
          169  +  "OMIT_AUTOINCREMENT",
          170  +#endif
          171  +#ifdef SQLITE_OMIT_AUTOINIT
          172  +  "OMIT_AUTOINIT",
          173  +#endif
          174  +#ifdef SQLITE_OMIT_AUTOVACUUM
          175  +  "OMIT_AUTOVACUUM",
          176  +#endif
          177  +#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
          178  +  "OMIT_BETWEEN_OPTIMIZATION",
          179  +#endif
          180  +#ifdef SQLITE_OMIT_BLOB_LITERAL
          181  +  "OMIT_BLOB_LITERAL",
          182  +#endif
          183  +#ifdef SQLITE_OMIT_BTREECOUNT
          184  +  "OMIT_BTREECOUNT",
          185  +#endif
          186  +#ifdef SQLITE_OMIT_BUILTIN_TEST
          187  +  "OMIT_BUILTIN_TEST",
          188  +#endif
          189  +#ifdef SQLITE_OMIT_CAST
          190  +  "OMIT_CAST",
          191  +#endif
          192  +#ifdef SQLITE_OMIT_CHECK
          193  +  "OMIT_CHECK",
          194  +#endif
          195  +#ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
          196  +  "OMIT_COMPILEOPTION_DIAGS",
          197  +#endif
          198  +#ifdef SQLITE_OMIT_COMPLETE
          199  +  "OMIT_COMPLETE",
          200  +#endif
          201  +#ifdef SQLITE_OMIT_COMPOUND_SELECT
          202  +  "OMIT_COMPOUND_SELECT",
          203  +#endif
          204  +#ifdef SQLITE_OMIT_DATETIME_FUNCS
          205  +  "OMIT_DATETIME_FUNCS",
          206  +#endif
          207  +#ifdef SQLITE_OMIT_DECLTYPE
          208  +  "OMIT_DECLTYPE",
          209  +#endif
          210  +#ifdef SQLITE_OMIT_DEPRECATED
          211  +  "OMIT_DEPRECATED",
          212  +#endif
          213  +#ifdef SQLITE_OMIT_DISKIO
          214  +  "OMIT_DISKIO",
          215  +#endif
          216  +#ifdef SQLITE_OMIT_EXPLAIN
          217  +  "OMIT_EXPLAIN",
          218  +#endif
          219  +#ifdef SQLITE_OMIT_FLAG_PRAGMAS
          220  +  "OMIT_FLAG_PRAGMAS",
          221  +#endif
          222  +#ifdef SQLITE_OMIT_FLOATING_POINT
          223  +  "OMIT_FLOATING_POINT",
          224  +#endif
          225  +#ifdef SQLITE_OMIT_FOREIGN_KEY
          226  +  "OMIT_FOREIGN_KEY",
          227  +#endif
          228  +#ifdef SQLITE_OMIT_GET_TABLE
          229  +  "OMIT_GET_TABLE",
          230  +#endif
          231  +#ifdef SQLITE_OMIT_GLOBALRECOVER
          232  +  "OMIT_GLOBALRECOVER",
          233  +#endif
          234  +#ifdef SQLITE_OMIT_INCRBLOB
          235  +  "OMIT_INCRBLOB",
          236  +#endif
          237  +#ifdef SQLITE_OMIT_INTEGRITY_CHECK
          238  +  "OMIT_INTEGRITY_CHECK",
          239  +#endif
          240  +#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
          241  +  "OMIT_LIKE_OPTIMIZATION",
          242  +#endif
          243  +#ifdef SQLITE_OMIT_LOAD_EXTENSION
          244  +  "OMIT_LOAD_EXTENSION",
          245  +#endif
          246  +#ifdef SQLITE_OMIT_LOCALTIME
          247  +  "OMIT_LOCALTIME",
          248  +#endif
          249  +#ifdef SQLITE_OMIT_LOOKASIDE
          250  +  "OMIT_LOOKASIDE",
          251  +#endif
          252  +#ifdef SQLITE_OMIT_MEMORYDB
          253  +  "OMIT_MEMORYDB",
          254  +#endif
          255  +#ifdef SQLITE_OMIT_OR_OPTIMIZATION
          256  +  "OMIT_OR_OPTIMIZATION",
          257  +#endif
          258  +#ifdef SQLITE_OMIT_PAGER_PRAGMAS
          259  +  "OMIT_PAGER_PRAGMAS",
          260  +#endif
          261  +#ifdef SQLITE_OMIT_PRAGMA
          262  +  "OMIT_PRAGMA",
          263  +#endif
          264  +#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
          265  +  "OMIT_PROGRESS_CALLBACK",
          266  +#endif
          267  +#ifdef SQLITE_OMIT_QUICKBALANCE
          268  +  "OMIT_QUICKBALANCE",
          269  +#endif
          270  +#ifdef SQLITE_OMIT_REINDEX
          271  +  "OMIT_REINDEX",
          272  +#endif
          273  +#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
          274  +  "OMIT_SCHEMA_PRAGMAS",
          275  +#endif
          276  +#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
          277  +  "OMIT_SCHEMA_VERSION_PRAGMAS",
          278  +#endif
          279  +#ifdef SQLITE_OMIT_SHARED_CACHE
          280  +  "OMIT_SHARED_CACHE",
          281  +#endif
          282  +#ifdef SQLITE_OMIT_SUBQUERY
          283  +  "OMIT_SUBQUERY",
          284  +#endif
          285  +#ifdef SQLITE_OMIT_TCL_VARIABLE
          286  +  "OMIT_TCL_VARIABLE",
          287  +#endif
          288  +#ifdef SQLITE_OMIT_TEMPDB
          289  +  "OMIT_TEMPDB",
          290  +#endif
          291  +#ifdef SQLITE_OMIT_TRACE
          292  +  "OMIT_TRACE",
          293  +#endif
          294  +#ifdef SQLITE_OMIT_TRIGGER
          295  +  "OMIT_TRIGGER",
          296  +#endif
          297  +#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
          298  +  "OMIT_TRUNCATE_OPTIMIZATION",
          299  +#endif
          300  +#ifdef SQLITE_OMIT_UTF16
          301  +  "OMIT_UTF16",
          302  +#endif
          303  +#ifdef SQLITE_OMIT_VACUUM
          304  +  "OMIT_VACUUM",
          305  +#endif
          306  +#ifdef SQLITE_OMIT_VIEW
          307  +  "OMIT_VIEW",
          308  +#endif
          309  +#ifdef SQLITE_OMIT_VIRTUALTABLE
          310  +  "OMIT_VIRTUALTABLE",
          311  +#endif
          312  +#ifdef SQLITE_OMIT_WSD
          313  +  "OMIT_WSD",
          314  +#endif
          315  +#ifdef SQLITE_OMIT_XFER_OPT
          316  +  "OMIT_XFER_OPT",
          317  +#endif
          318  +#ifdef SQLITE_PERFORMANCE_TRACE
          319  +  "PERFORMANCE_TRACE",
          320  +#endif
          321  +#ifdef SQLITE_PROXY_DEBUG
          322  +  "PROXY_DEBUG",
          323  +#endif
          324  +#ifdef SQLITE_SECURE_DELETE
          325  +  "SECURE_DELETE",
          326  +#endif
          327  +#ifdef SQLITE_SMALL_STACK
          328  +  "SMALL_STACK",
          329  +#endif
          330  +#ifdef SQLITE_SOUNDEX
          331  +  "SOUNDEX",
          332  +#endif
          333  +#ifdef SQLITE_TCL
          334  +  "TCL",
          335  +#endif
          336  +#ifdef SQLITE_TEMP_STORE
          337  +  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
          338  +#endif
          339  +#ifdef SQLITE_TEST
          340  +  "TEST",
          341  +#endif
          342  +#ifdef SQLITE_THREADSAFE
          343  +  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
          344  +#endif
          345  +#ifdef SQLITE_USE_ALLOCA
          346  +  "USE_ALLOCA",
          347  +#endif
          348  +#ifdef SQLITE_ZERO_MALLOC
          349  +  "ZERO_MALLOC"
          350  +#endif
          351  +};
          352  +
          353  +/*
          354  +** Given the name of a compile-time option, return true if that option
          355  +** was used and false if not.
          356  +**
          357  +** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
          358  +** is not required for a match.
          359  +*/
          360  +int sqlite3_compileoption_used(const char *zOptName){
          361  +  int i, n;
          362  +  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
          363  +  n = sqlite3Strlen30(zOptName);
          364  +
          365  +  /* Since ArraySize(azCompileOpt) is normally in single digits, a
          366  +  ** linear search is adequate.  No need for a binary search. */
          367  +  for(i=0; i<ArraySize(azCompileOpt); i++){
          368  +    if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
          369  +       && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
          370  +  }
          371  +  return 0;
          372  +}
          373  +
          374  +/*
          375  +** Return the N-th compile-time option string.  If N is out of range,
          376  +** return a NULL pointer.
          377  +*/
          378  +const char *sqlite3_compileoption_get(int N){
          379  +  if( N>=0 && N<ArraySize(azCompileOpt) ){
          380  +    return azCompileOpt[N];
          381  +  }
          382  +  return 0;
          383  +}
          384  +
          385  +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */

Changes to src/expr.c.

   244    244     CollSeq *p4;
   245    245   
   246    246     p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
   247    247     p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
   248    248     addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
   249    249                              (void*)p4, P4_COLLSEQ);
   250    250     sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
   251         -  if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
   252         -    sqlite3ExprCacheAffinityChange(pParse, in1, 1);
   253         -    sqlite3ExprCacheAffinityChange(pParse, in2, 1);
   254         -  }
   255    251     return addr;
   256    252   }
   257    253   
   258    254   #if SQLITE_MAX_EXPR_DEPTH>0
   259    255   /*
   260    256   ** Check that argument nHeight is less than or equal to the maximum
   261    257   ** expression depth allowed. If it is not, leave an error message in

Changes to src/func.c.

   773    773   ){
   774    774     UNUSED_PARAMETER2(NotUsed, NotUsed2);
   775    775     /* IMP: R-24470-31136 This function is an SQL wrapper around the
   776    776     ** sqlite3_sourceid() C interface. */
   777    777     sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
   778    778   }
   779    779   
          780  +/*
          781  +** Implementation of the sqlite_compileoption_used() function.
          782  +** The result is an integer that identifies if the compiler option
          783  +** was used to build SQLite.
          784  +*/
          785  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
          786  +static void compileoptionusedFunc(
          787  +  sqlite3_context *context,
          788  +  int argc,
          789  +  sqlite3_value **argv
          790  +){
          791  +  const char *zOptName;
          792  +  assert( argc==1 );
          793  +  UNUSED_PARAMETER(argc);
          794  +  /* IMP: R-xxxx This function is an SQL wrapper around the
          795  +  ** sqlite3_compileoption_used() C interface. */
          796  +  if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
          797  +    sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
          798  +  }
          799  +}
          800  +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
          801  +
          802  +/*
          803  +** Implementation of the sqlite_compileoption_get() function. 
          804  +** The result is a string that identifies the compiler options 
          805  +** used to build SQLite.
          806  +*/
          807  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
          808  +static void compileoptiongetFunc(
          809  +  sqlite3_context *context,
          810  +  int argc,
          811  +  sqlite3_value **argv
          812  +){
          813  +  int n;
          814  +  assert( argc==1 );
          815  +  UNUSED_PARAMETER(argc);
          816  +  /* IMP: R-xxxx This function is an SQL wrapper around the
          817  +  ** sqlite3_compileoption_get() C interface. */
          818  +  n = sqlite3_value_int(argv[0]);
          819  +  sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
          820  +}
          821  +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
          822  +
   780    823   /* Array for converting from half-bytes (nybbles) into ASCII hex
   781    824   ** digits. */
   782    825   static const char hexdigits[] = {
   783    826     '0', '1', '2', '3', '4', '5', '6', '7',
   784    827     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
   785    828   };
   786    829   
................................................................................
  1504   1547   /*  FUNCTION(ifnull,             2, 0, 0, ifnullFunc       ), */
  1505   1548       {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0},
  1506   1549       FUNCTION(random,             0, 0, 0, randomFunc       ),
  1507   1550       FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
  1508   1551       FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
  1509   1552       FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
  1510   1553       FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
         1554  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
         1555  +    FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
         1556  +    FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
         1557  +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  1511   1558       FUNCTION(quote,              1, 0, 0, quoteFunc        ),
  1512   1559       FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
  1513   1560       FUNCTION(changes,            0, 0, 0, changes          ),
  1514   1561       FUNCTION(total_changes,      0, 0, 0, total_changes    ),
  1515   1562       FUNCTION(replace,            3, 0, 0, replaceFunc      ),
  1516   1563       FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
  1517   1564     #ifdef SQLITE_SOUNDEX

Changes to src/global.c.

   160    160      0,                         /* isInit */
   161    161      0,                         /* inProgress */
   162    162      0,                         /* isMutexInit */
   163    163      0,                         /* isMallocInit */
   164    164      0,                         /* isPCacheInit */
   165    165      0,                         /* pInitMutex */
   166    166      0,                         /* nRefInitMutex */
          167  +   0,                         /* xLog */
          168  +   0,                         /* pLogArg */
   167    169   };
   168    170   
   169    171   
   170    172   /*
   171    173   ** Hash table for global functions - functions common to all
   172    174   ** database connections.  After initialization, this table is
   173    175   ** read-only.

Changes to src/insert.c.

  1257   1257         case OE_Replace: {
  1258   1258           /* If there are DELETE triggers on this table and the
  1259   1259           ** recursive-triggers flag is set, call GenerateRowDelete() to
  1260   1260           ** remove the conflicting row from the the table. This will fire
  1261   1261           ** the triggers and remove both the table and index b-tree entries.
  1262   1262           **
  1263   1263           ** Otherwise, if there are no triggers or the recursive-triggers
  1264         -        ** flag is not set, call GenerateRowIndexDelete(). This removes
  1265         -        ** the index b-tree entries only. The table b-tree entry will be 
  1266         -        ** replaced by the new entry when it is inserted.  */
         1264  +        ** flag is not set, but the table has one or more indexes, call 
         1265  +        ** GenerateRowIndexDelete(). This removes the index b-tree entries 
         1266  +        ** only. The table b-tree entry will be replaced by the new entry 
         1267  +        ** when it is inserted.  
         1268  +        **
         1269  +        ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
         1270  +        ** also invoke MultiWrite() to indicate that this VDBE may require
         1271  +        ** statement rollback (if the statement is aborted after the delete
         1272  +        ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
         1273  +        ** but being more selective here allows statements like:
         1274  +        **
         1275  +        **   REPLACE INTO t(rowid) VALUES($newrowid)
         1276  +        **
         1277  +        ** to run without a statement journal if there are no indexes on the
         1278  +        ** table.
         1279  +        */
  1267   1280           Trigger *pTrigger = 0;
  1268   1281           if( pParse->db->flags&SQLITE_RecTriggers ){
  1269   1282             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  1270   1283           }
  1271         -        sqlite3MultiWrite(pParse);
  1272   1284           if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
         1285  +          sqlite3MultiWrite(pParse);
  1273   1286             sqlite3GenerateRowDelete(
  1274   1287                 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
  1275   1288             );
  1276         -        }else{
         1289  +        }else if( pTab->pIndex ){
         1290  +          sqlite3MultiWrite(pParse);
  1277   1291             sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
  1278   1292           }
  1279   1293           seenReplace = 1;
  1280   1294           break;
  1281   1295         }
  1282   1296         case OE_Ignore: {
  1283   1297           assert( seenReplace==0 );

Changes to src/legacy.c.

    40     40     int rc = SQLITE_OK;         /* Return code */
    41     41     const char *zLeftover;      /* Tail of unprocessed SQL */
    42     42     sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
    43     43     char **azCols = 0;          /* Names of result columns */
    44     44     int nRetry = 0;             /* Number of retry attempts */
    45     45     int callbackIsInit;         /* True if callback data is initialized */
    46     46   
    47         -  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE;
           47  +  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
    48     48     if( zSql==0 ) zSql = "";
    49     49   #ifdef SQLITE_ENABLE_SQLRR
    50     50     SRRecExec(db, zSql);
    51     51   #endif  
    52     52     sqlite3_mutex_enter(db->mutex);
    53     53     sqlite3Error(db, SQLITE_OK, 0);
    54     54     while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){

Changes to src/main.c.

   256    256   */
   257    257   int sqlite3_config(int op, ...){
   258    258     va_list ap;
   259    259     int rc = SQLITE_OK;
   260    260   
   261    261     /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
   262    262     ** the SQLite library is in use. */
   263         -  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
          263  +  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
   264    264   
   265    265     va_start(ap, op);
   266    266     switch( op ){
   267    267   
   268    268       /* Mutex configuration options are only available in a threadsafe
   269    269       ** compile. 
   270    270       */
................................................................................
   377    377   #endif
   378    378   
   379    379       case SQLITE_CONFIG_LOOKASIDE: {
   380    380         sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
   381    381         sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
   382    382         break;
   383    383       }
          384  +    
          385  +    /* Record a pointer to the logger funcction and its first argument.
          386  +    ** The default is NULL.  Logging is disabled if the function pointer is
          387  +    ** NULL.
          388  +    */
          389  +    case SQLITE_CONFIG_LOG: {
          390  +      /* MSVC is picky about pulling func ptrs from va lists.
          391  +      ** http://support.microsoft.com/kb/47961
          392  +      ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
          393  +      */
          394  +      typedef void(*LOGFUNC_t)(void*,int,const char*);
          395  +      sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
          396  +      sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
          397  +      break;
          398  +    }
   384    399   
   385    400       default: {
   386    401         rc = SQLITE_ERROR;
   387    402         break;
   388    403       }
   389    404     }
   390    405     va_end(ap);
................................................................................
   590    605     HashElem *i;
   591    606     int j;
   592    607   
   593    608     if( !db ){
   594    609       return SQLITE_OK;
   595    610     }
   596    611     if( !sqlite3SafetyCheckSickOrOk(db) ){
   597         -    return SQLITE_MISUSE;
          612  +    return SQLITE_MISUSE_BKPT;
   598    613     }
   599    614     sqlite3_mutex_enter(db->mutex);
   600    615   
   601    616     sqlite3ResetInternalSchema(db, 0);
   602    617   
   603    618     /* If a transaction is open, the ResetInternalSchema() call above
   604    619     ** will not have called the xDisconnect() method on any virtual
................................................................................
   941    956     assert( sqlite3_mutex_held(db->mutex) );
   942    957     if( zFunctionName==0 ||
   943    958         (xFunc && (xFinal || xStep)) || 
   944    959         (!xFunc && (xFinal && !xStep)) ||
   945    960         (!xFunc && (!xFinal && xStep)) ||
   946    961         (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
   947    962         (255<(nName = sqlite3Strlen30( zFunctionName))) ){
   948         -    return SQLITE_MISUSE;
          963  +    return SQLITE_MISUSE_BKPT;
   949    964     }
   950    965     
   951    966   #ifndef SQLITE_OMIT_UTF16
   952    967     /* If SQLITE_UTF16 is specified as the encoding type, transform this
   953    968     ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   954    969     ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
   955    970     **
................................................................................
  1038   1053     void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  1039   1054     void (*xFinal)(sqlite3_context*)
  1040   1055   ){
  1041   1056     int rc;
  1042   1057     char *zFunc8;
  1043   1058     sqlite3_mutex_enter(db->mutex);
  1044   1059     assert( !db->mallocFailed );
  1045         -  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
         1060  +  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
  1046   1061     rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
  1047   1062     sqlite3DbFree(db, zFunc8);
  1048   1063     rc = sqlite3ApiExit(db, rc);
  1049   1064     sqlite3_mutex_leave(db->mutex);
  1050   1065     return rc;
  1051   1066   }
  1052   1067   #endif
................................................................................
  1272   1287   */
  1273   1288   const char *sqlite3_errmsg(sqlite3 *db){
  1274   1289     const char *z;
  1275   1290     if( !db ){
  1276   1291       return sqlite3ErrStr(SQLITE_NOMEM);
  1277   1292     }
  1278   1293     if( !sqlite3SafetyCheckSickOrOk(db) ){
  1279         -    return sqlite3ErrStr(SQLITE_MISUSE);
         1294  +    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
  1280   1295     }
  1281   1296     sqlite3_mutex_enter(db->mutex);
  1282   1297     if( db->mallocFailed ){
  1283   1298       z = sqlite3ErrStr(SQLITE_NOMEM);
  1284   1299     }else{
  1285   1300       z = (char*)sqlite3_value_text(db->pErr);
  1286   1301       assert( !db->mallocFailed );
................................................................................
  1341   1356   
  1342   1357   /*
  1343   1358   ** Return the most recent error code generated by an SQLite routine. If NULL is
  1344   1359   ** passed to this function, we assume a malloc() failed during sqlite3_open().
  1345   1360   */
  1346   1361   int sqlite3_errcode(sqlite3 *db){
  1347   1362     if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  1348         -    return SQLITE_MISUSE;
         1363  +    return SQLITE_MISUSE_BKPT;
  1349   1364     }
  1350   1365     if( !db || db->mallocFailed ){
  1351   1366       return SQLITE_NOMEM;
  1352   1367     }
  1353   1368     return db->errCode & db->errMask;
  1354   1369   }
  1355   1370   int sqlite3_extended_errcode(sqlite3 *db){
  1356   1371     if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  1357         -    return SQLITE_MISUSE;
         1372  +    return SQLITE_MISUSE_BKPT;
  1358   1373     }
  1359   1374     if( !db || db->mallocFailed ){
  1360   1375       return SQLITE_NOMEM;
  1361   1376     }
  1362   1377     return db->errCode;
  1363   1378   }
  1364   1379   
................................................................................
  1388   1403     enc2 = enc;
  1389   1404     testcase( enc2==SQLITE_UTF16 );
  1390   1405     testcase( enc2==SQLITE_UTF16_ALIGNED );
  1391   1406     if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
  1392   1407       enc2 = SQLITE_UTF16NATIVE;
  1393   1408     }
  1394   1409     if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
  1395         -    return SQLITE_MISUSE;
         1410  +    return SQLITE_MISUSE_BKPT;
  1396   1411     }
  1397   1412   
  1398   1413     /* Check if this call is removing or replacing an existing collation 
  1399   1414     ** sequence. If so, and there are active VMs, return busy. If there
  1400   1415     ** are no active VMs, invalidate any pre-compiled statements.
  1401   1416     */
  1402   1417     pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
................................................................................
  1879   1894     void* pCtx,
  1880   1895     int(*xCompare)(void*,int,const void*,int,const void*)
  1881   1896   ){
  1882   1897     int rc = SQLITE_OK;
  1883   1898     char *zName8;
  1884   1899     sqlite3_mutex_enter(db->mutex);
  1885   1900     assert( !db->mallocFailed );
  1886         -  zName8 = sqlite3Utf16to8(db, zName, -1);
         1901  +  zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
  1887   1902     if( zName8 ){
  1888   1903       rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
  1889   1904       sqlite3DbFree(db, zName8);
  1890   1905     }
  1891   1906     rc = sqlite3ApiExit(db, rc);
  1892   1907     sqlite3_mutex_leave(db->mutex);
  1893   1908     return rc;
................................................................................
  1950   1965   **
  1951   1966   ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
  1952   1967   */
  1953   1968   int sqlite3_get_autocommit(sqlite3 *db){
  1954   1969     return db->autoCommit;
  1955   1970   }
  1956   1971   
  1957         -#ifdef SQLITE_DEBUG
  1958   1972   /*
  1959         -** The following routine is subtituted for constant SQLITE_CORRUPT in
  1960         -** debugging builds.  This provides a way to set a breakpoint for when
  1961         -** corruption is first detected.
         1973  +** The following routines are subtitutes for constants SQLITE_CORRUPT,
         1974  +** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
         1975  +** constants.  They server two purposes:
         1976  +**
         1977  +**   1.  Serve as a convenient place to set a breakpoint in a debugger
         1978  +**       to detect when version error conditions occurs.
         1979  +**
         1980  +**   2.  Invoke sqlite3_log() to provide the source code location where
         1981  +**       a low-level error is first detected.
  1962   1982   */
  1963         -int sqlite3Corrupt(void){
         1983  +int sqlite3CorruptError(int lineno){
         1984  +  testcase( sqlite3GlobalConfig.xLog!=0 );
         1985  +  sqlite3_log(SQLITE_CORRUPT,
         1986  +              "database corruption found by source line %d", lineno);
  1964   1987     return SQLITE_CORRUPT;
  1965   1988   }
  1966         -#endif
         1989  +int sqlite3MisuseError(int lineno){
         1990  +  testcase( sqlite3GlobalConfig.xLog!=0 );
         1991  +  sqlite3_log(SQLITE_MISUSE, "misuse detected by source line %d", lineno);
         1992  +  return SQLITE_MISUSE;
         1993  +}
         1994  +int sqlite3CantopenError(int lineno){
         1995  +  testcase( sqlite3GlobalConfig.xLog!=0 );
         1996  +  sqlite3_log(SQLITE_CANTOPEN, "cannot open file at source line %d", lineno);
         1997  +  return SQLITE_CANTOPEN;
         1998  +}
         1999  +
  1967   2000   
  1968   2001   #ifndef SQLITE_OMIT_DEPRECATED
  1969   2002   /*
  1970   2003   ** This is a convenience routine that makes sure that all thread-specific
  1971   2004   ** data for this thread has been deallocated.
  1972   2005   **
  1973   2006   ** SQLite no longer uses thread-specific data so this routine is now a
................................................................................
  2003   2036     char const *zCollSeq = 0;
  2004   2037     int notnull = 0;
  2005   2038     int primarykey = 0;
  2006   2039     int autoinc = 0;
  2007   2040   
  2008   2041     /* Ensure the database schema has been loaded */
  2009   2042     sqlite3_mutex_enter(db->mutex);
  2010         -  (void)sqlite3SafetyOn(db);
  2011   2043     sqlite3BtreeEnterAll(db);
  2012   2044     rc = sqlite3Init(db, &zErrMsg);
  2013   2045     if( SQLITE_OK!=rc ){
  2014   2046       goto error_out;
  2015   2047     }
  2016   2048   
  2017   2049     /* Locate the table in question */
................................................................................
  2062   2094     }
  2063   2095     if( !zCollSeq ){
  2064   2096       zCollSeq = "BINARY";
  2065   2097     }
  2066   2098   
  2067   2099   error_out:
  2068   2100     sqlite3BtreeLeaveAll(db);
  2069         -  (void)sqlite3SafetyOff(db);
  2070   2101   
  2071   2102     /* Whether the function call succeeded or failed, set the output parameters
  2072   2103     ** to whatever their local counterparts contain. If an error did occur,
  2073   2104     ** this has the effect of zeroing all output parameters.
  2074   2105     */
  2075   2106     if( pzDataType ) *pzDataType = zDataType;
  2076   2107     if( pzCollSeq ) *pzCollSeq = zCollSeq;

Changes to src/mem1.c.

    60     60     sqlite3_int64 *p;
    61     61     assert( nByte>0 );
    62     62     nByte = ROUND8(nByte);
    63     63     p = SQLITE_MALLOC( nByte+8 );
    64     64     if( p ){
    65     65       p[0] = nByte;
    66     66       p++;
           67  +  }else{
           68  +    testcase( sqlite3GlobalConfig.xLog!=0 );
           69  +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
    67     70     }
    68     71     return (void *)p;
    69     72   }
    70     73   
    71     74   /*
    72     75   ** Like free() but works for allocations obtained from sqlite3MemMalloc()
    73     76   ** or sqlite3MemRealloc().
................................................................................
    78     81   */
    79     82   static void sqlite3MemFree(void *pPrior){
    80     83     sqlite3_int64 *p = (sqlite3_int64*)pPrior;
    81     84     assert( pPrior!=0 );
    82     85     p--;
    83     86     SQLITE_FREE(p);
    84     87   }
           88  +
           89  +/*
           90  +** Report the allocated size of a prior return from xMalloc()
           91  +** or xRealloc().
           92  +*/
           93  +static int sqlite3MemSize(void *pPrior){
           94  +  sqlite3_int64 *p;
           95  +  if( pPrior==0 ) return 0;
           96  +  p = (sqlite3_int64*)pPrior;
           97  +  p--;
           98  +  return (int)p[0];
           99  +}
    85    100   
    86    101   /*
    87    102   ** Like realloc().  Resize an allocation previously obtained from
    88    103   ** sqlite3MemMalloc().
    89    104   **
    90    105   ** For this low-level interface, we know that pPrior!=0.  Cases where
    91    106   ** pPrior==0 while have been intercepted by higher-level routine and
................................................................................
    98    113     assert( pPrior!=0 && nByte>0 );
    99    114     nByte = ROUND8(nByte);
   100    115     p--;
   101    116     p = SQLITE_REALLOC(p, nByte+8 );
   102    117     if( p ){
   103    118       p[0] = nByte;
   104    119       p++;
          120  +  }else{
          121  +    testcase( sqlite3GlobalConfig.xLog!=0 );
          122  +    sqlite3_log(SQLITE_NOMEM,
          123  +      "failed memory resize %u to %u bytes",
          124  +      sqlite3MemSize(pPrior), nByte);
   105    125     }
   106    126     return (void*)p;
   107    127   }
   108    128   
   109         -/*
   110         -** Report the allocated size of a prior return from xMalloc()
   111         -** or xRealloc().
   112         -*/
   113         -static int sqlite3MemSize(void *pPrior){
   114         -  sqlite3_int64 *p;
   115         -  if( pPrior==0 ) return 0;
   116         -  p = (sqlite3_int64*)pPrior;
   117         -  p--;
   118         -  return (int)p[0];
   119         -}
   120         -
   121    129   /*
   122    130   ** Round up a request size to the next valid allocation size.
   123    131   */
   124    132   static int sqlite3MemRoundup(int n){
   125    133     return ROUND8(n);
   126    134   }
   127    135   

Changes to src/mem2.c.

   296    296   /*
   297    297   ** Free memory.
   298    298   */
   299    299   static void sqlite3MemFree(void *pPrior){
   300    300     struct MemBlockHdr *pHdr;
   301    301     void **pBt;
   302    302     char *z;
   303         -  assert( sqlite3GlobalConfig.bMemstat || mem.mutex!=0 );
          303  +  assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 
          304  +       || mem.mutex!=0 );
   304    305     pHdr = sqlite3MemsysGetHeader(pPrior);
   305    306     pBt = (void**)pHdr;
   306    307     pBt -= pHdr->nBacktraceSlots;
   307    308     sqlite3_mutex_enter(mem.mutex);
   308    309     if( pHdr->pPrev ){
   309    310       assert( pHdr->pPrev->pNext==pHdr );
   310    311       pHdr->pPrev->pNext = pHdr->pNext;

Changes to src/mem5.c.

   264    264     for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
   265    265   
   266    266     /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
   267    267     ** block.  If not, then split a block of the next larger power of
   268    268     ** two in order to create a new free block of size iLogsize.
   269    269     */
   270    270     for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
   271         -  if( iBin>LOGMAX ) return 0;
          271  +  if( iBin>LOGMAX ){
          272  +    testcase( sqlite3GlobalConfig.xLog!=0 );
          273  +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
          274  +    return 0;
          275  +  }
   272    276     i = memsys5UnlinkFirst(iBin);
   273    277     while( iBin>iLogsize ){
   274    278       int newSize;
   275    279   
   276    280       iBin--;
   277    281       newSize = 1 << iBin;
   278    282       mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;

Changes to src/mutex_w32.c.

   242    242   #ifdef SQLITE_DEBUG
   243    243     if( p->trace ){
   244    244       printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   245    245     }
   246    246   #endif
   247    247   }
   248    248   static int winMutexTry(sqlite3_mutex *p){
          249  +#ifndef NDEBUG
   249    250     DWORD tid = GetCurrentThreadId(); 
          251  +#endif
   250    252     int rc = SQLITE_BUSY;
   251    253     assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   252    254     /*
   253    255     ** The sqlite3_mutex_try() routine is very rarely used, and when it
   254    256     ** is used it is merely an optimization.  So it is OK for it to always
   255    257     ** fail.  
   256    258     **
................................................................................
   280    282   /*
   281    283   ** The sqlite3_mutex_leave() routine exits a mutex that was
   282    284   ** previously entered by the same thread.  The behavior
   283    285   ** is undefined if the mutex is not currently entered or
   284    286   ** is not currently allocated.  SQLite will never do either.
   285    287   */
   286    288   static void winMutexLeave(sqlite3_mutex *p){
   287         -  DWORD tid = GetCurrentThreadId(); 
          289  +#ifndef NDEBUG
          290  +  DWORD tid = GetCurrentThreadId();
          291  +#endif
   288    292     assert( p->nRef>0 );
   289    293     assert( p->owner==tid );
   290    294     p->nRef--;
   291    295     assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   292    296     LeaveCriticalSection(&p->mutex);
   293    297   #ifdef SQLITE_DEBUG
   294    298     if( p->trace ){

Changes to src/os_unix.c.

   124    124   # include <sys/ioctl.h>
   125    125   # if OS_VXWORKS
   126    126   #  include <semaphore.h>
   127    127   #  include <limits.h>
   128    128   # else
   129    129   #  include <sys/file.h>
   130    130   #  include <sys/param.h>
   131         -#  include <sys/mount.h>
   132    131   # endif
   133    132   #endif /* SQLITE_ENABLE_LOCKING_STYLE */
          133  +
          134  +#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
          135  +# include <sys/mount.h>
          136  +#endif
   134    137   
   135    138   /*
   136    139   ** Allowed values of unixFile.fsFlags
   137    140   */
   138    141   #define SQLITE_FSFLAGS_IS_MSDOS     0x1
   139    142   
   140    143   /*
................................................................................
  1107   1110     if( pthread_equal(pFile->tid, hSelf) ){
  1108   1111       /* We are still in the same thread */
  1109   1112       OSTRACE1("No-transfer, same thread\n");
  1110   1113       return SQLITE_OK;
  1111   1114     }
  1112   1115     if( pFile->locktype!=NO_LOCK ){
  1113   1116       /* We cannot change ownership while we are holding a lock! */
  1114         -    return SQLITE_MISUSE;
         1117  +    return SQLITE_MISUSE_BKPT;
  1115   1118     }
  1116   1119     OSTRACE4("Transfer ownership of %d from %d to %d\n",
  1117   1120               pFile->h, pFile->tid, hSelf);
  1118   1121     pFile->tid = hSelf;
  1119   1122     if (pFile->pLock != NULL) {
  1120   1123       releaseLockInfo(pFile->pLock);
  1121   1124       rc = findLockInfo(pFile, &pFile->pLock, 0);
................................................................................
  1244   1247     ** even if the locking primitive used is always a write-lock.
  1245   1248     */
  1246   1249     int rc = SQLITE_OK;
  1247   1250     unixFile *pFile = (unixFile*)id;
  1248   1251     struct unixLockInfo *pLock = pFile->pLock;
  1249   1252     struct flock lock;
  1250   1253     int s = 0;
  1251         -  int tErrno;
         1254  +  int tErrno = 0;
  1252   1255   
  1253   1256     assert( pFile );
  1254   1257     OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
  1255   1258         locktypeName(locktype), locktypeName(pFile->locktype),
  1256   1259         locktypeName(pLock->locktype), pLock->cnt , getpid());
  1257   1260   
  1258   1261     /* If there is already a lock of this type or more restrictive on the
................................................................................
  1508   1511         pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1509   1512   
  1510   1513     assert( locktype<=SHARED_LOCK );
  1511   1514     if( pFile->locktype<=locktype ){
  1512   1515       return SQLITE_OK;
  1513   1516     }
  1514   1517     if( CHECK_THREADID(pFile) ){
  1515         -    return SQLITE_MISUSE;
         1518  +    return SQLITE_MISUSE_BKPT;
  1516   1519     }
  1517   1520     unixEnterMutex();
  1518   1521     h = pFile->h;
  1519   1522     pLock = pFile->pLock;
  1520   1523     assert( pLock->cnt!=0 );
  1521   1524     if( pFile->locktype>SHARED_LOCK ){
  1522   1525       assert( pLock->locktype==pFile->locktype );
................................................................................
  1553   1556           off_t divSize = SHARED_SIZE - 1;
  1554   1557           
  1555   1558           lock.l_type = F_UNLCK;
  1556   1559           lock.l_whence = SEEK_SET;
  1557   1560           lock.l_start = SHARED_FIRST;
  1558   1561           lock.l_len = divSize;
  1559   1562           if( fcntl(h, F_SETLK, &lock)==(-1) ){
  1560         -          int tErrno = errno;
         1563  +          tErrno = errno;
  1561   1564             rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1562   1565             if( IS_LOCK_ERROR(rc) ){
  1563   1566               pFile->lastErrno = tErrno;
  1564   1567             }
  1565   1568             goto end_unlock;
  1566   1569           }
  1567   1570           lock.l_type = F_RDLCK;
  1568   1571           lock.l_whence = SEEK_SET;
  1569   1572           lock.l_start = SHARED_FIRST;
  1570   1573           lock.l_len = divSize;
  1571   1574           if( fcntl(h, F_SETLK, &lock)==(-1) ){
  1572         -          int tErrno = errno;
         1575  +          tErrno = errno;
  1573   1576             rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
  1574   1577             if( IS_LOCK_ERROR(rc) ){
  1575   1578               pFile->lastErrno = tErrno;
  1576   1579             }
  1577   1580             goto end_unlock;
  1578   1581           }
  1579   1582           lock.l_type = F_UNLCK;
  1580   1583           lock.l_whence = SEEK_SET;
  1581   1584           lock.l_start = SHARED_FIRST+divSize;
  1582   1585           lock.l_len = SHARED_SIZE-divSize;
  1583   1586           if( fcntl(h, F_SETLK, &lock)==(-1) ){
  1584         -          int tErrno = errno;
         1587  +          tErrno = errno;
  1585   1588             rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
  1586   1589             if( IS_LOCK_ERROR(rc) ){
  1587   1590               pFile->lastErrno = tErrno;
  1588   1591             }
  1589   1592             goto end_unlock;
  1590   1593           }
  1591   1594         }else{
  1592   1595           lock.l_type = F_RDLCK;
  1593   1596           lock.l_whence = SEEK_SET;
  1594   1597           lock.l_start = SHARED_FIRST;
  1595   1598           lock.l_len = SHARED_SIZE;
  1596   1599           if( fcntl(h, F_SETLK, &lock)==(-1) ){
  1597         -          int tErrno = errno;
         1600  +          tErrno = errno;
  1598   1601             rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
  1599   1602             if( IS_LOCK_ERROR(rc) ){
  1600   1603               pFile->lastErrno = tErrno;
  1601   1604             }
  1602   1605             goto end_unlock;
  1603   1606           }
  1604   1607         }
................................................................................
  2731   2734              pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  2732   2735   
  2733   2736     assert( locktype<=SHARED_LOCK );
  2734   2737     if( pFile->locktype<=locktype ){
  2735   2738       return SQLITE_OK;
  2736   2739     }
  2737   2740     if( CHECK_THREADID(pFile) ){
  2738         -    return SQLITE_MISUSE;
         2741  +    return SQLITE_MISUSE_BKPT;
  2739   2742     }
  2740   2743     unixEnterMutex();
  2741   2744     pLock = pFile->pLock;
  2742   2745     assert( pLock->cnt!=0 );
  2743   2746     if( pFile->locktype>SHARED_LOCK ){
  2744   2747       assert( pLock->locktype==pFile->locktype );
  2745   2748       SimulateIOErrorBenign(1);
................................................................................
  3342   3345         *(int*)pArg = ((unixFile*)id)->locktype;
  3343   3346         return SQLITE_OK;
  3344   3347       }
  3345   3348       case SQLITE_LAST_ERRNO: {
  3346   3349         *(int*)pArg = ((unixFile*)id)->lastErrno;
  3347   3350         return SQLITE_OK;
  3348   3351       }
         3352  +    case SQLITE_FCNTL_SIZE_HINT: {
         3353  +      sqlite3_int64 szFile = *(sqlite3_int64*)pArg;
         3354  +      unixFile *pFile = (unixFile*)id;
         3355  +      ftruncate(pFile->h, szFile);
         3356  +      return SQLITE_OK;
         3357  +    }
  3349   3358   #ifndef NDEBUG
  3350   3359       /* The pager calls this method to signal that it has done
  3351   3360       ** a rollback and that the database is therefore unchanged and
  3352   3361       ** it hence it is OK for the transaction change counter to be
  3353   3362       ** unchanged.
  3354   3363       */
  3355   3364       case SQLITE_FCNTL_DB_UNCHANGED: {
................................................................................
  3863   3872   #ifdef FD_CLOEXEC
  3864   3873         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
  3865   3874   #endif
  3866   3875         OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
  3867   3876       }
  3868   3877     }
  3869   3878     *pFd = fd;
  3870         -  return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
         3879  +  return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT);
  3871   3880   }
  3872   3881   
  3873   3882   /*
  3874   3883   ** Create a temporary file name in zBuf.  zBuf must be allocated
  3875   3884   ** by the calling process and must be big enough to hold at least
  3876   3885   ** pVfs->mxPathname bytes.
  3877   3886   */
................................................................................
  4126   4135         flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
  4127   4136         openFlags &= ~(O_RDWR|O_CREAT);
  4128   4137         flags |= SQLITE_OPEN_READONLY;
  4129   4138         openFlags |= O_RDONLY;
  4130   4139         fd = open(zName, openFlags, openMode);
  4131   4140       }
  4132   4141       if( fd<0 ){
  4133         -      rc = SQLITE_CANTOPEN;
         4142  +      rc = SQLITE_CANTOPEN_BKPT;
  4134   4143         goto open_finished;
  4135   4144       }
  4136   4145     }
  4137   4146     assert( fd>=0 );
  4138   4147     if( pOutFlags ){
  4139   4148       *pOutFlags = flags;
  4140   4149     }
................................................................................
  4350   4359   
  4351   4360     zOut[nOut-1] = '\0';
  4352   4361     if( zPath[0]=='/' ){
  4353   4362       sqlite3_snprintf(nOut, zOut, "%s", zPath);
  4354   4363     }else{
  4355   4364       int nCwd;
  4356   4365       if( getcwd(zOut, nOut-1)==0 ){
  4357         -      return SQLITE_CANTOPEN;
         4366  +      return SQLITE_CANTOPEN_BKPT;
  4358   4367       }
  4359   4368       nCwd = (int)strlen(zOut);
  4360   4369       sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
  4361   4370     }
  4362   4371     return SQLITE_OK;
  4363   4372   }
  4364   4373   
................................................................................
  4861   4870       }
  4862   4871       switch (terrno) {
  4863   4872         case EACCES:
  4864   4873           return SQLITE_PERM;
  4865   4874         case EIO: 
  4866   4875           return SQLITE_IOERR_LOCK; /* even though it is the conch */
  4867   4876         default:
  4868         -        return SQLITE_CANTOPEN;
         4877  +        return SQLITE_CANTOPEN_BKPT;
  4869   4878       }
  4870   4879     }
  4871   4880     
  4872   4881     pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
  4873   4882     if( pNew==NULL ){
  4874   4883       rc = SQLITE_NOMEM;
  4875   4884       goto end_create_proxy;
................................................................................
  5245   5254           pFile->h = -1;
  5246   5255           int fd = open(pCtx->dbPath, pFile->openFlags,
  5247   5256                         SQLITE_DEFAULT_FILE_PERMISSIONS);
  5248   5257           OSTRACE2("TRANSPROXY: OPEN  %d\n", fd);
  5249   5258           if( fd>=0 ){
  5250   5259             pFile->h = fd;
  5251   5260           }else{
  5252         -          rc=SQLITE_CANTOPEN; /* SQLITE_BUSY? proxyTakeConch called
         5261  +          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
  5253   5262              during locking */
  5254   5263           }
  5255   5264         }
  5256   5265         if( rc==SQLITE_OK && !pCtx->lockProxy ){
  5257   5266           char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
  5258   5267           rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
  5259   5268           if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){

Changes to src/os_win.c.

  1108   1108         *(int*)pArg = ((winFile*)id)->locktype;
  1109   1109         return SQLITE_OK;
  1110   1110       }
  1111   1111       case SQLITE_LAST_ERRNO: {
  1112   1112         *(int*)pArg = (int)((winFile*)id)->lastErrno;
  1113   1113         return SQLITE_OK;
  1114   1114       }
         1115  +    case SQLITE_FCNTL_SIZE_HINT: {
         1116  +      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
         1117  +      winTruncate(id, sz);
         1118  +      return SQLITE_OK;
         1119  +    }
  1115   1120     }
  1116   1121     return SQLITE_ERROR;
  1117   1122   }
  1118   1123   
  1119   1124   /*
  1120   1125   ** Return the sector size in bytes of the underlying block device for
  1121   1126   ** the specified file. This is almost always 512 bytes, but may be
................................................................................
  1413   1418     }
  1414   1419     if( h==INVALID_HANDLE_VALUE ){
  1415   1420       free(zConverted);
  1416   1421       if( flags & SQLITE_OPEN_READWRITE ){
  1417   1422         return winOpen(pVfs, zName, id, 
  1418   1423                ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
  1419   1424       }else{
  1420         -      return SQLITE_CANTOPEN;
         1425  +      return SQLITE_CANTOPEN_BKPT;
  1421   1426       }
  1422   1427     }
  1423   1428     if( pOutFlags ){
  1424   1429       if( flags & SQLITE_OPEN_READWRITE ){
  1425   1430         *pOutFlags = SQLITE_OPEN_READWRITE;
  1426   1431       }else{
  1427   1432         *pOutFlags = SQLITE_OPEN_READONLY;
................................................................................
  1435   1440   #if SQLITE_OS_WINCE
  1436   1441     if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
  1437   1442                  (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
  1438   1443          && !winceCreateLock(zName, pFile)
  1439   1444     ){
  1440   1445       CloseHandle(h);
  1441   1446       free(zConverted);
  1442         -    return SQLITE_CANTOPEN;
         1447  +    return SQLITE_CANTOPEN_BKPT;
  1443   1448     }
  1444   1449     if( isTemp ){
  1445   1450       pFile->zDeleteOnClose = zConverted;
  1446   1451     }else
  1447   1452   #endif
  1448   1453     {
  1449   1454       free(zConverted);

Changes to src/pager.c.

  2015   2015     pPager->changeCountDone = pPager->tempFile;
  2016   2016   
  2017   2017     if( rc==SQLITE_OK ){
  2018   2018       zMaster = pPager->pTmpSpace;
  2019   2019       rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  2020   2020       testcase( rc!=SQLITE_OK );
  2021   2021     }
         2022  +  if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
         2023  +    rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
         2024  +  }
  2022   2025     if( rc==SQLITE_OK ){
  2023   2026       rc = pager_end_transaction(pPager, zMaster[0]!='\0');
  2024   2027       testcase( rc!=SQLITE_OK );
  2025   2028     }
  2026   2029     if( rc==SQLITE_OK && zMaster[0] && res ){
  2027   2030       /* If there was a master journal and this routine will return success,
  2028   2031       ** see if it is possible to delete the master journal.
................................................................................
  2861   2864     ** is not possible for rc to be other than SQLITE_OK if this branch
  2862   2865     ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
  2863   2866     */
  2864   2867     if( !isOpen(pPager->fd) ){
  2865   2868       assert( pPager->tempFile && rc==SQLITE_OK );
  2866   2869       rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
  2867   2870     }
         2871  +
         2872  +  /* Before the first write, give the VFS a hint of what the final
         2873  +  ** file size will be.
         2874  +  */
         2875  +  if( rc==SQLITE_OK
         2876  +   && pPager->dbSize>(pPager->dbFileSize+1)
         2877  +   && isOpen(pPager->fd)
         2878  +  ){
         2879  +    sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
         2880  +    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
         2881  +  }
  2868   2882   
  2869   2883     while( rc==SQLITE_OK && pList ){
  2870   2884       Pgno pgno = pList->pgno;
  2871   2885   
  2872   2886       /* If there are dirty pages in the page cache with page numbers greater
  2873   2887       ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
  2874   2888       ** make the file smaller (presumably by auto-vacuum code). Do not write
................................................................................
  3158   3172       if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
  3159   3173         /* This branch is taken when the journal path required by
  3160   3174         ** the database being opened will be more than pVfs->mxPathname
  3161   3175         ** bytes in length. This means the database cannot be opened,
  3162   3176         ** as it will not be possible to open the journal file or even
  3163   3177         ** check for a hot-journal before reading.
  3164   3178         */
  3165         -      rc = SQLITE_CANTOPEN;
         3179  +      rc = SQLITE_CANTOPEN_BKPT;
  3166   3180       }
  3167   3181       if( rc!=SQLITE_OK ){
  3168   3182         sqlite3_free(zPathname);
  3169   3183         return rc;
  3170   3184       }
  3171   3185     }
  3172   3186   
................................................................................
  3335   3349     }else if( memDb ){
  3336   3350       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
  3337   3351     }
  3338   3352     /* pPager->xBusyHandler = 0; */
  3339   3353     /* pPager->pBusyHandlerArg = 0; */
  3340   3354     pPager->xReiniter = xReinit;
  3341   3355     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
         3356  +
  3342   3357     *ppPager = pPager;
  3343   3358     return SQLITE_OK;
  3344   3359   }
  3345   3360   
  3346   3361   
  3347   3362   
  3348   3363   /*
................................................................................
  3484   3499     }
  3485   3500     iOffset = (pgno-1)*(i64)pPager->pageSize;
  3486   3501     rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
  3487   3502     if( rc==SQLITE_IOERR_SHORT_READ ){
  3488   3503       rc = SQLITE_OK;
  3489   3504     }
  3490   3505     if( pgno==1 ){
  3491         -    u8 *dbFileVers = &((u8*)pPg->pData)[24];
  3492         -    memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
         3506  +    if( rc ){
         3507  +      /* If the read is unsuccessful, set the dbFileVers[] to something
         3508  +      ** that will never be a valid file version.  dbFileVers[] is a copy
         3509  +      ** of bytes 24..39 of the database.  Bytes 28..31 should always be
         3510  +      ** zero.  Bytes 32..35 and 35..39 should be page numbers which are
         3511  +      ** never 0xffffffff.  So filling pPager->dbFileVers[] with all 0xff
         3512  +      ** bytes should suffice.
         3513  +      **
         3514  +      ** For an encrypted database, the situation is more complex:  bytes
         3515  +      ** 24..39 of the database are white noise.  But the probability of
         3516  +      ** white noising equaling 16 bytes of 0xff is vanishingly small so
         3517  +      ** we should still be ok.
         3518  +      */
         3519  +      memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
         3520  +    }else{
         3521  +      u8 *dbFileVers = &((u8*)pPg->pData)[24];
         3522  +      memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
         3523  +    }
  3493   3524     }
  3494   3525     CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
  3495   3526   
  3496   3527     PAGER_INCR(sqlite3_pager_readdb_count);
  3497   3528     PAGER_INCR(pPager->nRead);
  3498   3529     IOTRACE(("PGIN %p %d\n", pPager, pgno));
  3499   3530     PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
................................................................................
  3617   3648             if( res ){
  3618   3649               int fout = 0;
  3619   3650               int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
  3620   3651               assert( !pPager->tempFile );
  3621   3652               rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
  3622   3653               assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
  3623   3654               if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
  3624         -              rc = SQLITE_CANTOPEN;
         3655  +              rc = SQLITE_CANTOPEN_BKPT;
  3625   3656                 sqlite3OsClose(pPager->jfd);
  3626   3657               }
  3627   3658             }else{
  3628   3659               /* If the journal does not exist, it usually means that some 
  3629   3660               ** other connection managed to get in and roll it back before 
  3630   3661               ** this connection obtained the exclusive lock above. Or, it 
  3631   3662               ** may mean that the pager was in the error-state when this
................................................................................
  3836   3867       }
  3837   3868   
  3838   3869       rc = sqlite3PagerPagecount(pPager, &nMax);
  3839   3870       if( rc!=SQLITE_OK ){
  3840   3871         goto pager_acquire_err;
  3841   3872       }
  3842   3873   
  3843         -    if( MEMDB || nMax<(int)pgno || noContent ){
         3874  +    if( MEMDB || nMax<(int)pgno || noContent || !isOpen(pPager->fd) ){
  3844   3875         if( pgno>pPager->mxPgno ){
  3845   3876   	rc = SQLITE_FULL;
  3846   3877   	goto pager_acquire_err;
  3847   3878         }
  3848   3879         if( noContent ){
  3849   3880           /* Failure to set the bits in the InJournal bit-vectors is benign.
  3850   3881           ** It merely means that we might do some extra work to journal a 
................................................................................
  4636   4667             PgHdr *pPage;             /* Page to journal */
  4637   4668             rc = sqlite3PagerGet(pPager, i, &pPage);
  4638   4669             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
  4639   4670             rc = sqlite3PagerWrite(pPage);
  4640   4671             sqlite3PagerUnref(pPage);
  4641   4672             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
  4642   4673           }
  4643         -      } 
         4674  +      }
  4644   4675         pPager->dbSize = dbSize;
  4645   4676       }
  4646   4677   #endif
  4647   4678   
  4648   4679       /* Write the master journal name into the journal file. If a master 
  4649   4680       ** journal file name has already been written to the journal file, 
  4650   4681       ** or if zMaster is NULL (no master journal), then this call is a no-op.
................................................................................
  4986   5017       /* If this is a release of the outermost savepoint, truncate 
  4987   5018       ** the sub-journal to zero bytes in size. */
  4988   5019       if( op==SAVEPOINT_RELEASE ){
  4989   5020         if( nNew==0 && isOpen(pPager->sjfd) ){
  4990   5021           /* Only truncate if it is an in-memory sub-journal. */
  4991   5022           if( sqlite3IsMemJournal(pPager->sjfd) ){
  4992   5023             rc = sqlite3OsTruncate(pPager->sjfd, 0);
         5024  +          assert( rc==SQLITE_OK );
  4993   5025           }
  4994   5026           pPager->nSubRec = 0;
  4995   5027         }
  4996   5028       }
  4997   5029       /* Else this is a rollback operation, playback the specified savepoint.
  4998   5030       ** If this is a temp-file, it is possible that the journal file has
  4999   5031       ** not yet been opened. In this case there have been no changes to

Changes to src/pcache.c.

   185    185   ** are no outstanding page references when this function is called.
   186    186   */
   187    187   void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
   188    188     assert( pCache->nRef==0 && pCache->pDirty==0 );
   189    189     if( pCache->pCache ){
   190    190       sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
   191    191       pCache->pCache = 0;
          192  +    pCache->pPage1 = 0;
   192    193     }
   193    194     pCache->szPage = szPage;
   194    195   }
   195    196   
   196    197   /*
   197    198   ** Try to obtain a page from the cache.
   198    199   */

Changes to src/pragma.c.

   282    282     const char *zDb = 0;   /* The database name */
   283    283     Token *pId;            /* Pointer to <id> token */
   284    284     int iDb;               /* Database index for <database> */
   285    285     sqlite3 *db = pParse->db;
   286    286     Db *pDb;
   287    287     Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
   288    288     if( v==0 ) return;
          289  +  sqlite3VdbeRunOnlyOnce(v);
   289    290     pParse->nMem = 2;
   290    291   
   291    292     /* Interpret the [database.] part of the pragma statement. iDb is the
   292    293     ** index of the database this pragma is being applied to in db.aDb[]. */
   293    294     iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
   294    295     if( iDb<0 ) return;
   295    296     pDb = &db->aDb[iDb];
................................................................................
  1359   1360         sqlite3VdbeChangeP3(v, addr+1, iCookie);
  1360   1361         sqlite3VdbeSetNumCols(v, 1);
  1361   1362         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
  1362   1363       }
  1363   1364     }else
  1364   1365   #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
  1365   1366   
         1367  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
         1368  +  /*
         1369  +  **   PRAGMA compile_options
         1370  +  **
         1371  +  ** Return the names of all compile-time options used in this build,
         1372  +  ** one option per row.
         1373  +  */
         1374  +  if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
         1375  +    int i = 0;
         1376  +    const char *zOpt;
         1377  +    sqlite3VdbeSetNumCols(v, 1);
         1378  +    pParse->nMem = 1;
         1379  +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
         1380  +    while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
         1381  +      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
         1382  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
         1383  +    }
         1384  +  }else
         1385  +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
         1386  +
  1366   1387   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1367   1388     /*
  1368   1389     ** Report the current state of file logs for all databases
  1369   1390     */
  1370   1391     if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
  1371   1392       static const char *const azLockName[] = {
  1372   1393         "unlocked", "shared", "reserved", "pending", "exclusive"
................................................................................
  1434   1455   #endif
  1435   1456     }else
  1436   1457   #endif
  1437   1458   
  1438   1459    
  1439   1460     {/* Empty ELSE clause */}
  1440   1461   
  1441         -  /* Code an OP_Expire at the end of each PRAGMA program to cause
  1442         -  ** the VDBE implementing the pragma to expire. Most (all?) pragmas
  1443         -  ** are only valid for a single execution.
  1444         -  */
  1445         -  sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
  1446         -
  1447   1462     /*
  1448   1463     ** Reset the safety level, in case the fullfsync flag or synchronous
  1449   1464     ** setting changed.
  1450   1465     */
  1451   1466   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1452   1467     if( db->autoCommit ){
  1453   1468       sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,

Changes to src/prepare.c.

   191    191     azArg[1] = "1";
   192    192     azArg[2] = zMasterSchema;
   193    193     azArg[3] = 0;
   194    194     initData.db = db;
   195    195     initData.iDb = iDb;
   196    196     initData.rc = SQLITE_OK;
   197    197     initData.pzErrMsg = pzErrMsg;
   198         -  (void)sqlite3SafetyOff(db);
   199    198     sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
   200         -  (void)sqlite3SafetyOn(db);
   201    199     if( initData.rc ){
   202    200       rc = initData.rc;
   203    201       goto error_out;
   204    202     }
   205    203     pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
   206    204     if( ALWAYS(pTab) ){
   207    205       pTab->tabFlags |= TF_Readonly;
................................................................................
   316    314     */
   317    315     assert( db->init.busy );
   318    316     {
   319    317       char *zSql;
   320    318       zSql = sqlite3MPrintf(db, 
   321    319           "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
   322    320           db->aDb[iDb].zName, zMasterName);
   323         -    (void)sqlite3SafetyOff(db);
   324    321   #ifndef SQLITE_OMIT_AUTHORIZATION
   325    322       {
   326    323         int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
   327    324         xAuth = db->xAuth;
   328    325         db->xAuth = 0;
   329    326   #endif
   330    327         rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
   331    328   #ifndef SQLITE_OMIT_AUTHORIZATION
   332    329         db->xAuth = xAuth;
   333    330       }
   334    331   #endif
   335    332       if( rc==SQLITE_OK ) rc = initData.rc;
   336         -    (void)sqlite3SafetyOn(db);
   337    333       sqlite3DbFree(db, zSql);
   338    334   #ifndef SQLITE_OMIT_ANALYZE
   339    335       if( rc==SQLITE_OK ){
   340    336         sqlite3AnalysisLoad(db, iDb);
   341    337       }
   342    338   #endif
   343    339     }
................................................................................
   538    534     /* Allocate the parsing context */
   539    535     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   540    536     if( pParse==0 ){
   541    537       rc = SQLITE_NOMEM;
   542    538       goto end_prepare;
   543    539     }
   544    540     pParse->pReprepare = pReprepare;
   545         -
   546         -  if( sqlite3SafetyOn(db) ){
   547         -    rc = SQLITE_MISUSE;
   548         -    goto end_prepare;
   549         -  }
   550    541     assert( ppStmt && *ppStmt==0 );
   551    542     assert( !db->mallocFailed );
   552    543     assert( sqlite3_mutex_held(db->mutex) );
   553    544   
   554    545     /* Check to verify that it is possible to get a read lock on all
   555    546     ** database schemas.  The inability to get a read lock indicates that
   556    547     ** some other database connection is holding a write-lock, which in
................................................................................
   578    569       Btree *pBt = db->aDb[i].pBt;
   579    570       if( pBt ){
   580    571         assert( sqlite3BtreeHoldsMutex(pBt) );
   581    572         rc = sqlite3BtreeSchemaLocked(pBt);
   582    573         if( rc ){
   583    574           const char *zDb = db->aDb[i].zName;
   584    575           sqlite3Error(db, rc, "database schema is locked: %s", zDb);
   585         -        (void)sqlite3SafetyOff(db);
   586    576           testcase( db->flags & SQLITE_ReadUncommitted );
   587    577           goto end_prepare;
   588    578         }
   589    579       }
   590    580     }
   591    581   
   592    582     sqlite3VtabUnlockList(db);
................................................................................
   595    585     if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
   596    586       char *zSqlCopy;
   597    587       int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   598    588       testcase( nBytes==mxLen );
   599    589       testcase( nBytes==mxLen+1 );
   600    590       if( nBytes>mxLen ){
   601    591         sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
   602         -      (void)sqlite3SafetyOff(db);
   603    592         rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
   604    593         goto end_prepare;
   605    594       }
   606    595       zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
   607    596       if( zSqlCopy ){
   608    597         sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
   609    598         sqlite3DbFree(db, zSqlCopy);
................................................................................
   652    641       for(i=iFirst; i<mx; i++){
   653    642         sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
   654    643                               azColName[i], SQLITE_STATIC);
   655    644       }
   656    645     }
   657    646   #endif
   658    647   
   659         -  if( sqlite3SafetyOff(db) ){
   660         -    rc = SQLITE_MISUSE;
   661         -  }
   662         -
   663    648     assert( db->init.busy==0 || saveSqlFlag==0 );
   664    649     if( db->init.busy==0 ){
   665    650       Vdbe *pVdbe = pParse->pVdbe;
   666    651       sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
   667    652     }
   668    653     if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
   669    654       sqlite3VdbeFinalize(pParse->pVdbe);
................................................................................
   703    688     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   704    689     const char **pzTail       /* OUT: End of parsed string */
   705    690   ){
   706    691     int rc;
   707    692     assert( ppStmt!=0 );
   708    693     *ppStmt = 0;
   709    694     if( !sqlite3SafetyCheckOk(db) ){
   710         -    return SQLITE_MISUSE;
          695  +    return SQLITE_MISUSE_BKPT;
   711    696     }
   712    697     sqlite3_mutex_enter(db->mutex);
   713    698     sqlite3BtreeEnterAll(db);
   714    699     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
   715    700     if( rc==SQLITE_SCHEMA ){
   716    701       sqlite3_finalize(*ppStmt);
   717    702       rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
................................................................................
   742    727     assert( sqlite3_mutex_held(db->mutex) );
   743    728     rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
   744    729     if( rc ){
   745    730       if( rc==SQLITE_NOMEM ){
   746    731         db->mallocFailed = 1;
   747    732       }
   748    733       assert( pNew==0 );
   749         -    return (rc==SQLITE_LOCKED) ? SQLITE_LOCKED : SQLITE_SCHEMA;
          734  +    return rc;
   750    735     }else{
   751    736       assert( pNew!=0 );
   752    737     }
   753    738     sqlite3VdbeSwap((Vdbe*)pNew, p);
   754    739     sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
   755    740     sqlite3VdbeResetStepResult((Vdbe*)pNew);
   756    741     sqlite3VdbeFinalize((Vdbe*)pNew);
................................................................................
   817    802     char *zSql8;
   818    803     const char *zTail8 = 0;
   819    804     int rc = SQLITE_OK;
   820    805   
   821    806     assert( ppStmt );
   822    807     *ppStmt = 0;
   823    808     if( !sqlite3SafetyCheckOk(db) ){
   824         -    return SQLITE_MISUSE;
          809  +    return SQLITE_MISUSE_BKPT;
   825    810     }
   826    811     sqlite3_mutex_enter(db->mutex);
   827         -  zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
          812  +  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
   828    813     if( zSql8 ){
   829    814       rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
   830    815     }
   831    816   #ifdef SQLITE_ENABLE_SQLRR
   832    817     SRRecPrepare(db, zSql8, -1, 1, *ppStmt);
   833    818   #endif
   834    819     if( zTail8 && pzTail ){

Changes to src/printf.c.

   456    456           }
   457    457           length = (int)(&buf[etBUFSIZE-1]-bufpt);
   458    458           break;
   459    459         case etFLOAT:
   460    460         case etEXP:
   461    461         case etGENERIC:
   462    462           realvalue = va_arg(ap,double);
   463         -#ifndef SQLITE_OMIT_FLOATING_POINT
          463  +#ifdef SQLITE_OMIT_FLOATING_POINT
          464  +        length = 0;
          465  +#else
   464    466           if( precision<0 ) precision = 6;         /* Set default precision */
   465    467           if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
   466    468           if( realvalue<0.0 ){
   467    469             realvalue = -realvalue;
   468    470             prefix = '-';
   469    471           }else{
   470    472             if( flag_plussign )          prefix = '+';
................................................................................
   602    604             for(i=width; i>=nPad; i--){
   603    605               bufpt[i] = bufpt[i-nPad];
   604    606             }
   605    607             i = prefix!=0;
   606    608             while( nPad-- ) bufpt[i++] = '0';
   607    609             length = width;
   608    610           }
   609         -#else
   610         -        length = 0;
   611         -#endif /* SQLITE_OMIT_FLOATING_POINT */
          611  +#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
   612    612           break;
   613    613         case etSIZE:
   614    614           *(va_arg(ap,int*)) = pAccum->nChar;
   615    615           length = width = 0;
   616    616           break;
   617    617         case etPERCENT:
   618    618           buf[0] = '%';
................................................................................
   651    651           int needQuote;
   652    652           char ch;
   653    653           char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
   654    654           char *escarg = va_arg(ap,char*);
   655    655           isnull = escarg==0;
   656    656           if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
   657    657           k = precision;
   658         -        for(i=n=0; (ch=escarg[i])!=0 && k!=0; i++, k--){
          658  +        for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
   659    659             if( ch==q )  n++;
   660    660           }
   661    661           needQuote = !isnull && xtype==etSQLESCAPE2;
   662    662           n += i + 1 + needQuote*2;
   663    663           if( n>etBUFSIZE ){
   664    664             bufpt = zExtra = sqlite3Malloc( n );
   665    665             if( bufpt==0 ){
................................................................................
   934    934     acc.useMalloc = 0;
   935    935     va_start(ap,zFormat);
   936    936     sqlite3VXPrintf(&acc, 0, zFormat, ap);
   937    937     va_end(ap);
   938    938     z = sqlite3StrAccumFinish(&acc);
   939    939     return z;
   940    940   }
          941  +
          942  +/*
          943  +** This is the routine that actually formats the sqlite3_log() message.
          944  +** We house it in a separate routine from sqlite3_log() to avoid using
          945  +** stack space on small-stack systems when logging is disabled.
          946  +**
          947  +** sqlite3_log() must render into a static buffer.  It cannot dynamically
          948  +** allocate memory because it might be called while the memory allocator
          949  +** mutex is held.
          950  +*/
          951  +static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
          952  +  StrAccum acc;                          /* String accumulator */
          953  +  char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
          954  +
          955  +  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
          956  +  acc.useMalloc = 0;
          957  +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
          958  +  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
          959  +                           sqlite3StrAccumFinish(&acc));
          960  +}
          961  +
          962  +/*
          963  +** Format and write a message to the log if logging is enabled.
          964  +*/
          965  +void sqlite3_log(int iErrCode, const char *zFormat, ...){
          966  +  va_list ap;                             /* Vararg list */
          967  +  if( sqlite3GlobalConfig.xLog ){
          968  +    va_start(ap, zFormat);
          969  +    renderLogMsg(iErrCode, zFormat, ap);
          970  +    va_end(ap);
          971  +  }
          972  +}
   941    973   
   942    974   #if defined(SQLITE_DEBUG)
   943    975   /*
   944    976   ** A version of printf() that understands %lld.  Used for debugging.
   945    977   ** The printf() built into some versions of windows does not understand %lld
   946    978   ** and segfaults if you give it a long long int.
   947    979   */

Changes to src/resolve.c.

   660    660     Parse *pParse,     /* Parsing context for error messages */
   661    661     Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
   662    662     Expr *pE           /* The specific ORDER BY term */
   663    663   ){
   664    664     int i;             /* Loop counter */
   665    665     ExprList *pEList;  /* The columns of the result set */
   666    666     NameContext nc;    /* Name context for resolving pE */
          667  +  sqlite3 *db;       /* Database connection */
          668  +  int rc;            /* Return code from subprocedures */
          669  +  u8 savedSuppErr;   /* Saved value of db->suppressErr */
   667    670   
   668    671     assert( sqlite3ExprIsInteger(pE, &i)==0 );
   669    672     pEList = pSelect->pEList;
   670    673   
   671    674     /* Resolve all names in the ORDER BY term expression
   672    675     */
   673    676     memset(&nc, 0, sizeof(nc));
   674    677     nc.pParse = pParse;
   675    678     nc.pSrcList = pSelect->pSrc;
   676    679     nc.pEList = pEList;
   677    680     nc.allowAgg = 1;
   678    681     nc.nErr = 0;
   679         -  if( sqlite3ResolveExprNames(&nc, pE) ){
   680         -    sqlite3ErrorClear(pParse);
   681         -    return 0;
   682         -  }
          682  +  db = pParse->db;
          683  +  savedSuppErr = db->suppressErr;
          684  +  db->suppressErr = 1;
          685  +  rc = sqlite3ResolveExprNames(&nc, pE);
          686  +  db->suppressErr = savedSuppErr;
          687  +  if( rc ) return 0;
   683    688   
   684    689     /* Try to match the ORDER BY expression against an expression
   685    690     ** in the result set.  Return an 1-based index of the matching
   686    691     ** result-set entry.
   687    692     */
   688    693     for(i=0; i<pEList->nExpr; i++){
   689    694       if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){

Changes to src/select.c.

  3323   3323   static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
  3324   3324     Parse *pParse;
  3325   3325     int i;
  3326   3326     SrcList *pTabList;
  3327   3327     struct SrcList_item *pFrom;
  3328   3328   
  3329   3329     assert( p->selFlags & SF_Resolved );
  3330         -  assert( (p->selFlags & SF_HasTypeInfo)==0 );
  3331         -  p->selFlags |= SF_HasTypeInfo;
  3332         -  pParse = pWalker->pParse;
  3333         -  pTabList = p->pSrc;
  3334         -  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3335         -    Table *pTab = pFrom->pTab;
  3336         -    if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
  3337         -      /* A sub-query in the FROM clause of a SELECT */
  3338         -      Select *pSel = pFrom->pSelect;
  3339         -      assert( pSel );
  3340         -      while( pSel->pPrior ) pSel = pSel->pPrior;
  3341         -      selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
         3330  +  if( (p->selFlags & SF_HasTypeInfo)==0 ){
         3331  +    p->selFlags |= SF_HasTypeInfo;
         3332  +    pParse = pWalker->pParse;
         3333  +    pTabList = p->pSrc;
         3334  +    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
         3335  +      Table *pTab = pFrom->pTab;
         3336  +      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
         3337  +        /* A sub-query in the FROM clause of a SELECT */
         3338  +        Select *pSel = pFrom->pSelect;
         3339  +        assert( pSel );
         3340  +        while( pSel->pPrior ) pSel = pSel->pPrior;
         3341  +        selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
         3342  +      }
  3342   3343       }
  3343   3344     }
  3344   3345     return WRC_Continue;
  3345   3346   }
  3346   3347   #endif
  3347   3348   
  3348   3349   

Changes to src/shell.c.

   191    191   #endif
   192    192   
   193    193   /*
   194    194   ** Used to prevent warnings about unused parameters
   195    195   */
   196    196   #define UNUSED_PARAMETER(x) (void)(x)
   197    197   
   198         -
   199         -/**************************************************************************
   200         -***************************************************************************
   201         -** Begin genfkey logic.
   202         -*/
   203         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined SQLITE_OMIT_SUBQUERY
   204         -
   205         -#define GENFKEY_ERROR         1
   206         -#define GENFKEY_DROPTRIGGER   2
   207         -#define GENFKEY_CREATETRIGGER 3
   208         -static int genfkey_create_triggers(sqlite3 *, const char *, void *,
   209         -  int (*)(void *, int, const char *)
   210         -);
   211         -
   212         -struct GenfkeyCb {
   213         -  void *pCtx;
   214         -  int eType;
   215         -  int (*xData)(void *, int, const char *);
   216         -};
   217         -typedef struct GenfkeyCb GenfkeyCb;
   218         -
   219         -/* The code in this file defines a sqlite3 virtual-table module that
   220         -** provides a read-only view of the current database schema. There is one
   221         -** row in the schema table for each column in the database schema.
   222         -*/
   223         -#define SCHEMA \
   224         -"CREATE TABLE x("                                                            \
   225         -  "database,"          /* Name of database (i.e. main, temp etc.) */         \
   226         -  "tablename,"         /* Name of table */                                   \
   227         -  "cid,"               /* Column number (from left-to-right, 0 upward) */    \
   228         -  "name,"              /* Column name */                                     \
   229         -  "type,"              /* Specified type (i.e. VARCHAR(32)) */               \
   230         -  "not_null,"          /* Boolean. True if NOT NULL was specified */         \
   231         -  "dflt_value,"        /* Default value for this column */                   \
   232         -  "pk"                 /* True if this column is part of the primary key */  \
   233         -")"
   234         -
   235         -#define SCHEMA2 \
   236         -"CREATE TABLE x("                                                            \
   237         -  "database,"          /* Name of database (i.e. main, temp etc.) */         \
   238         -  "from_tbl,"          /* Name of table */                                   \
   239         -  "fkid,"                                                                    \
   240         -  "seq,"                                                                     \
   241         -  "to_tbl,"                                                                  \
   242         -  "from_col,"                                                                \
   243         -  "to_col,"                                                                  \
   244         -  "on_update,"                                                               \
   245         -  "on_delete,"                                                               \
   246         -  "match"                                                                    \
   247         -")"
   248         -
   249         -#define SCHEMA3 \
   250         -"CREATE TABLE x("                                                            \
   251         -  "database,"          /* Name of database (i.e. main, temp etc.) */         \
   252         -  "tablename,"         /* Name of table */                                   \
   253         -  "seq,"                                                                     \
   254         -  "name,"                                                                    \
   255         -  "isunique"                                                                 \
   256         -")"
   257         -
   258         -#define SCHEMA4 \
   259         -"CREATE TABLE x("                                                            \
   260         -  "database,"          /* Name of database (i.e. main, temp etc.) */         \
   261         -  "indexname,"         /* Name of table */                                   \
   262         -  "seqno,"                                                                   \
   263         -  "cid,"                                                                     \
   264         -  "name"                                                                     \
   265         -")"
   266         -
   267         -#define SCHEMA5 \
   268         -"CREATE TABLE x("                                                            \
   269         -  "database,"          /* Name of database (i.e. main, temp etc.) */         \
   270         -  "triggername,"       /* Name of trigger */                                 \
   271         -  "dummy"              /* Unused */                                          \
   272         -")"
   273         -
   274         -typedef struct SchemaTable SchemaTable;
   275         -static struct SchemaTable {
   276         -  const char *zName;
   277         -  const char *zObject;
   278         -  const char *zPragma;
   279         -  const char *zSchema;
   280         -} aSchemaTable[] = {
   281         -  { "table_info",       "table", "PRAGMA %Q.table_info(%Q)",       SCHEMA },
   282         -  { "foreign_key_list", "table", "PRAGMA %Q.foreign_key_list(%Q)", SCHEMA2 },
   283         -  { "index_list",       "table", "PRAGMA %Q.index_list(%Q)",       SCHEMA3 },
   284         -  { "index_info",       "index", "PRAGMA %Q.index_info(%Q)",       SCHEMA4 },
   285         -  { "trigger_list",     "trigger", "SELECT 1",                     SCHEMA5 },
   286         -  { 0, 0, 0, 0 }
   287         -};
   288         -
   289         -typedef struct schema_vtab schema_vtab;
   290         -typedef struct schema_cursor schema_cursor;
   291         -
   292         -/* A schema table object */
   293         -struct schema_vtab {
   294         -  sqlite3_vtab base;
   295         -  sqlite3 *db;
   296         -  SchemaTable *pType;
   297         -};
   298         -
   299         -/* A schema table cursor object */
   300         -struct schema_cursor {
   301         -  sqlite3_vtab_cursor base;
   302         -  sqlite3_stmt *pDbList;
   303         -  sqlite3_stmt *pTableList;
   304         -  sqlite3_stmt *pColumnList;
   305         -  int rowid;
   306         -};
   307         -
   308         -/*
   309         -** Table destructor for the schema module.
   310         -*/
   311         -static int schemaDestroy(sqlite3_vtab *pVtab){
   312         -  sqlite3_free(pVtab);
   313         -  return 0;
   314         -}
   315         -
   316         -/*
   317         -** Table constructor for the schema module.
   318         -*/
   319         -static int schemaCreate(
   320         -  sqlite3 *db,
   321         -  void *pAux,
   322         -  int argc, const char *const*argv,
   323         -  sqlite3_vtab **ppVtab,
   324         -  char **pzErr
   325         -){
   326         -  int rc = SQLITE_NOMEM;
   327         -  schema_vtab *pVtab;
   328         -  SchemaTable *pType = &aSchemaTable[0];
   329         -
   330         -  UNUSED_PARAMETER(pzErr);
   331         -  if( argc>3 ){
   332         -    int i;
   333         -    pType = 0;
   334         -    for(i=0; aSchemaTable[i].zName; i++){ 
   335         -      if( 0==strcmp(argv[3], aSchemaTable[i].zName) ){
   336         -        pType = &aSchemaTable[i];
   337         -      }
   338         -    }
   339         -    if( !pType ){
   340         -      return SQLITE_ERROR;
   341         -    }
   342         -  }
   343         -
   344         -  pVtab = sqlite3_malloc(sizeof(schema_vtab));
   345         -  if( pVtab ){
   346         -    memset(pVtab, 0, sizeof(schema_vtab));
   347         -    pVtab->db = (sqlite3 *)pAux;
   348         -    pVtab->pType = pType;
   349         -    rc = sqlite3_declare_vtab(db, pType->zSchema);
   350         -  }
   351         -  *ppVtab = (sqlite3_vtab *)pVtab;
   352         -  return rc;
   353         -}
   354         -
   355         -/*
   356         -** Open a new cursor on the schema table.
   357         -*/
   358         -static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
   359         -  int rc = SQLITE_NOMEM;
   360         -  schema_cursor *pCur;
   361         -  UNUSED_PARAMETER(pVTab);
   362         -  pCur = sqlite3_malloc(sizeof(schema_cursor));
   363         -  if( pCur ){
   364         -    memset(pCur, 0, sizeof(schema_cursor));
   365         -    *ppCursor = (sqlite3_vtab_cursor *)pCur;
   366         -    rc = SQLITE_OK;
   367         -  }
   368         -  return rc;
   369         -}
   370         -
   371         -/*
   372         -** Close a schema table cursor.
   373         -*/
   374         -static int schemaClose(sqlite3_vtab_cursor *cur){
   375         -  schema_cursor *pCur = (schema_cursor *)cur;
   376         -  sqlite3_finalize(pCur->pDbList);
   377         -  sqlite3_finalize(pCur->pTableList);
   378         -  sqlite3_finalize(pCur->pColumnList);
   379         -  sqlite3_free(pCur);
   380         -  return SQLITE_OK;
   381         -}
   382         -
   383         -static void columnToResult(sqlite3_context *ctx, sqlite3_stmt *pStmt, int iCol){
   384         -  switch( sqlite3_column_type(pStmt, iCol) ){
   385         -    case SQLITE_NULL:
   386         -      sqlite3_result_null(ctx);
   387         -      break;
   388         -    case SQLITE_INTEGER:
   389         -      sqlite3_result_int64(ctx, sqlite3_column_int64(pStmt, iCol));
   390         -      break;
   391         -    case SQLITE_FLOAT:
   392         -      sqlite3_result_double(ctx, sqlite3_column_double(pStmt, iCol));
   393         -      break;
   394         -    case SQLITE_TEXT: {
   395         -      const char *z = (const char *)sqlite3_column_text(pStmt, iCol);
   396         -      sqlite3_result_text(ctx, z, -1, SQLITE_TRANSIENT);
   397         -      break;
   398         -    }
   399         -  }
   400         -}
   401         -
   402         -/*
   403         -** Retrieve a column of data.
   404         -*/
   405         -static int schemaColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
   406         -  schema_cursor *pCur = (schema_cursor *)cur;
   407         -  switch( i ){
   408         -    case 0:
   409         -      columnToResult(ctx, pCur->pDbList, 1);
   410         -      break;
   411         -    case 1:
   412         -      columnToResult(ctx, pCur->pTableList, 0);
   413         -      break;
   414         -    default:
   415         -      columnToResult(ctx, pCur->pColumnList, i-2);
   416         -      break;
   417         -  }
   418         -  return SQLITE_OK;
   419         -}
   420         -
   421         -/*
   422         -** Retrieve the current rowid.
   423         -*/
   424         -static int schemaRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
   425         -  schema_cursor *pCur = (schema_cursor *)cur;
   426         -  *pRowid = pCur->rowid;
   427         -  return SQLITE_OK;
   428         -}
   429         -
   430         -static int finalize(sqlite3_stmt **ppStmt){
   431         -  int rc = sqlite3_finalize(*ppStmt);
   432         -  *ppStmt = 0;
   433         -  return rc;
   434         -}
   435         -
   436         -static int schemaEof(sqlite3_vtab_cursor *cur){
   437         -  schema_cursor *pCur = (schema_cursor *)cur;
   438         -  return (pCur->pDbList ? 0 : 1);
   439         -}
   440         -
   441         -/*
   442         -** Advance the cursor to the next row.
   443         -*/
   444         -static int schemaNext(sqlite3_vtab_cursor *cur){
   445         -  int rc = SQLITE_OK;
   446         -  schema_cursor *pCur = (schema_cursor *)cur;
   447         -  schema_vtab *pVtab = (schema_vtab *)(cur->pVtab);
   448         -  char *zSql = 0;
   449         -
   450         -  while( !pCur->pColumnList || SQLITE_ROW!=sqlite3_step(pCur->pColumnList) ){
   451         -    if( SQLITE_OK!=(rc = finalize(&pCur->pColumnList)) ) goto next_exit;
   452         -
   453         -    while( !pCur->pTableList || SQLITE_ROW!=sqlite3_step(pCur->pTableList) ){
   454         -      if( SQLITE_OK!=(rc = finalize(&pCur->pTableList)) ) goto next_exit;
   455         -
   456         -      assert(pCur->pDbList);
   457         -      while( SQLITE_ROW!=sqlite3_step(pCur->pDbList) ){
   458         -        rc = finalize(&pCur->pDbList);
   459         -        goto next_exit;
   460         -      }
   461         -
   462         -      /* Set zSql to the SQL to pull the list of tables from the 
   463         -      ** sqlite_master (or sqlite_temp_master) table of the database
   464         -      ** identfied by the row pointed to by the SQL statement pCur->pDbList
   465         -      ** (iterating through a "PRAGMA database_list;" statement).
   466         -      */
   467         -      if( sqlite3_column_int(pCur->pDbList, 0)==1 ){
   468         -        zSql = sqlite3_mprintf(
   469         -            "SELECT name FROM sqlite_temp_master WHERE type=%Q",
   470         -            pVtab->pType->zObject
   471         -        );
   472         -      }else{
   473         -        sqlite3_stmt *pDbList = pCur->pDbList;
   474         -        zSql = sqlite3_mprintf(
   475         -            "SELECT name FROM %Q.sqlite_master WHERE type=%Q",
   476         -             sqlite3_column_text(pDbList, 1), pVtab->pType->zObject
   477         -        );
   478         -      }
   479         -      if( !zSql ){
   480         -        rc = SQLITE_NOMEM;
   481         -        goto next_exit;
   482         -      }
   483         -
   484         -      rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pTableList, 0);
   485         -      sqlite3_free(zSql);
   486         -      if( rc!=SQLITE_OK ) goto next_exit;
   487         -    }
   488         -
   489         -    /* Set zSql to the SQL to the table_info pragma for the table currently
   490         -    ** identified by the rows pointed to by statements pCur->pDbList and
   491         -    ** pCur->pTableList.
   492         -    */
   493         -    zSql = sqlite3_mprintf(pVtab->pType->zPragma,
   494         -        sqlite3_column_text(pCur->pDbList, 1),
   495         -        sqlite3_column_text(pCur->pTableList, 0)
   496         -    );
   497         -
   498         -    if( !zSql ){
   499         -      rc = SQLITE_NOMEM;
   500         -      goto next_exit;
   501         -    }
   502         -    rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pColumnList, 0);
   503         -    sqlite3_free(zSql);
   504         -    if( rc!=SQLITE_OK ) goto next_exit;
   505         -  }
   506         -  pCur->rowid++;
   507         -
   508         -next_exit:
   509         -  /* TODO: Handle rc */
   510         -  return rc;
   511         -}
   512         -
   513         -/*
   514         -** Reset a schema table cursor.
   515         -*/
   516         -static int schemaFilter(
   517         -  sqlite3_vtab_cursor *pVtabCursor, 
   518         -  int idxNum, const char *idxStr,
   519         -  int argc, sqlite3_value **argv
   520         -){
   521         -  int rc;
   522         -  schema_vtab *pVtab = (schema_vtab *)(pVtabCursor->pVtab);
   523         -  schema_cursor *pCur = (schema_cursor *)pVtabCursor;
   524         -  UNUSED_PARAMETER(idxNum);
   525         -  UNUSED_PARAMETER(idxStr);
   526         -  UNUSED_PARAMETER(argc);
   527         -  UNUSED_PARAMETER(argv);
   528         -  pCur->rowid = 0;
   529         -  finalize(&pCur->pTableList);
   530         -  finalize(&pCur->pColumnList);
   531         -  finalize(&pCur->pDbList);
   532         -  rc = sqlite3_prepare(pVtab->db,"SELECT 0, 'main'", -1, &pCur->pDbList, 0);
   533         -  return (rc==SQLITE_OK ? schemaNext(pVtabCursor) : rc);
   534         -}
   535         -
   536         -/*
   537         -** Analyse the WHERE condition.
   538         -*/
   539         -static int schemaBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
   540         -  UNUSED_PARAMETER(tab);
   541         -  UNUSED_PARAMETER(pIdxInfo);
   542         -  return SQLITE_OK;
   543         -}
   544         -
   545         -/*
   546         -** A virtual table module that merely echos method calls into TCL
   547         -** variables.
   548         -*/
   549         -static sqlite3_module schemaModule = {
   550         -  0,                           /* iVersion */
   551         -  schemaCreate,
   552         -  schemaCreate,
   553         -  schemaBestIndex,
   554         -  schemaDestroy,
   555         -  schemaDestroy,
   556         -  schemaOpen,                  /* xOpen - open a cursor */
   557         -  schemaClose,                 /* xClose - close a cursor */
   558         -  schemaFilter,                /* xFilter - configure scan constraints */
   559         -  schemaNext,                  /* xNext - advance a cursor */
   560         -  schemaEof,                   /* xEof */
   561         -  schemaColumn,                /* xColumn - read data */
   562         -  schemaRowid,                 /* xRowid - read data */
   563         -  0,                           /* xUpdate */
   564         -  0,                           /* xBegin */
   565         -  0,                           /* xSync */
   566         -  0,                           /* xCommit */
   567         -  0,                           /* xRollback */
   568         -  0,                           /* xFindMethod */
   569         -  0,                           /* xRename */
   570         -};
   571         -
   572         -/*
   573         -** Extension load function.
   574         -*/
   575         -static int installSchemaModule(sqlite3 *db, sqlite3 *sdb){
   576         -  sqlite3_create_module(db, "schema", &schemaModule, (void *)sdb);
   577         -  return 0;
   578         -}
   579         -
   580         -/*
   581         -**   sj(zValue, zJoin)
   582         -**
   583         -** The following block contains the implementation of an aggregate 
   584         -** function that returns a string. Each time the function is stepped, 
   585         -** it appends data to an internal buffer. When the aggregate is finalized,
   586         -** the contents of the buffer are returned.
   587         -**
   588         -** The first time the aggregate is stepped the buffer is set to a copy
   589         -** of the first argument. The second time and subsequent times it is
   590         -** stepped a copy of the second argument is appended to the buffer, then
   591         -** a copy of the first.
   592         -**
   593         -** Example:
   594         -**
   595         -**   INSERT INTO t1(a) VALUES('1');
   596         -**   INSERT INTO t1(a) VALUES('2');
   597         -**   INSERT INTO t1(a) VALUES('3');
   598         -**   SELECT sj(a, ', ') FROM t1;
   599         -**
   600         -**     =>  "1, 2, 3"
   601         -**
   602         -*/
   603         -struct StrBuffer {
   604         -  char *zBuf;
   605         -};
   606         -typedef struct StrBuffer StrBuffer;
   607         -static void joinFinalize(sqlite3_context *context){
   608         -  StrBuffer *p;
   609         -  p = (StrBuffer *)sqlite3_aggregate_context(context, sizeof(StrBuffer));
   610         -  sqlite3_result_text(context, p->zBuf, -1, SQLITE_TRANSIENT);
   611         -  sqlite3_free(p->zBuf);
   612         -}
   613         -static void joinStep(
   614         -  sqlite3_context *context,
   615         -  int argc,
   616         -  sqlite3_value **argv
   617         -){
   618         -  StrBuffer *p;
   619         -  UNUSED_PARAMETER(argc);
   620         -  p = (StrBuffer *)sqlite3_aggregate_context(context, sizeof(StrBuffer));
   621         -  if( p->zBuf==0 ){
   622         -    p->zBuf = sqlite3_mprintf("%s", sqlite3_value_text(argv[0]));
   623         -  }else{
   624         -    char *zTmp = p->zBuf;
   625         -    p->zBuf = sqlite3_mprintf("%s%s%s", 
   626         -        zTmp, sqlite3_value_text(argv[1]), sqlite3_value_text(argv[0])
   627         -    );
   628         -    sqlite3_free(zTmp);
   629         -  }
   630         -}
   631         -
   632         -/*
   633         -**   dq(zString)
   634         -**
   635         -** This scalar function accepts a single argument and interprets it as
   636         -** a text value. The return value is the argument enclosed in double
   637         -** quotes. If any double quote characters are present in the argument, 
   638         -** these are escaped.
   639         -**
   640         -**   dq('the raven "Nevermore."') == '"the raven ""Nevermore."""'
   641         -*/
   642         -static void doublequote(
   643         -  sqlite3_context *context, 
   644         -  int argc, 
   645         -  sqlite3_value **argv
   646         -){
   647         -  int ii;
   648         -  char *zOut;
   649         -  char *zCsr;
   650         -  const char *zIn = (const char *)sqlite3_value_text(argv[0]);
   651         -  int nIn = sqlite3_value_bytes(argv[0]);
   652         -
   653         -  UNUSED_PARAMETER(argc);
   654         -  zOut = sqlite3_malloc(nIn*2+3);
   655         -  zCsr = zOut;
   656         -  *zCsr++ = '"';
   657         -  for(ii=0; ii<nIn; ii++){
   658         -    *zCsr++ = zIn[ii];
   659         -    if( zIn[ii]=='"' ){
   660         -      *zCsr++ = '"';
   661         -    }
   662         -  }
   663         -  *zCsr++ = '"';
   664         -  *zCsr++ = '\0';
   665         -
   666         -  sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT);
   667         -  sqlite3_free(zOut);
   668         -}
   669         -
   670         -/*
   671         -**   multireplace(zString, zSearch1, zReplace1, ...)
   672         -*/
   673         -static void multireplace(
   674         -  sqlite3_context *context, 
   675         -  int argc, 
   676         -  sqlite3_value **argv
   677         -){
   678         -  int i = 0;
   679         -  char *zOut = 0;
   680         -  int nOut = 0;
   681         -  int nMalloc = 0;
   682         -  const char *zIn = (const char *)sqlite3_value_text(argv[0]);
   683         -  int nIn = sqlite3_value_bytes(argv[0]);
   684         -
   685         -  while( i<nIn ){
   686         -    const char *zCopy = &zIn[i];
   687         -    int nCopy = 1;
   688         -    int nReplace = 1;
   689         -    int j;
   690         -    for(j=1; j<(argc-1); j+=2){
   691         -      const char *z = (const char *)sqlite3_value_text(argv[j]);
   692         -      int n = sqlite3_value_bytes(argv[j]);
   693         -      if( n<=(nIn-i) && 0==strncmp(z, zCopy, n) ){
   694         -        zCopy = (const char *)sqlite3_value_text(argv[j+1]);
   695         -        nCopy = sqlite3_value_bytes(argv[j+1]);
   696         -        nReplace = n;
   697         -        break;
   698         -      }
   699         -    }
   700         -    if( (nOut+nCopy)>nMalloc ){
   701         -      char *zNew;
   702         -      nMalloc = 16 + (nOut+nCopy)*2;
   703         -      zNew = (char*)sqlite3_realloc(zOut, nMalloc);
   704         -      if( zNew==0 ){
   705         -        sqlite3_result_error_nomem(context);
   706         -        return;
   707         -      }else{
   708         -        zOut = zNew;
   709         -      }
   710         -    }
   711         -    assert( nMalloc>=(nOut+nCopy) );
   712         -    memcpy(&zOut[nOut], zCopy, nCopy);
   713         -    i += nReplace;
   714         -    nOut += nCopy;
   715         -  }
   716         -
   717         -  sqlite3_result_text(context, zOut, nOut, SQLITE_TRANSIENT);
   718         -  sqlite3_free(zOut);
   719         -}
   720         -
   721         -/*
   722         -** A callback for sqlite3_exec() invokes the callback specified by the
   723         -** GenfkeyCb structure pointed to by the void* passed as the first argument.
   724         -*/
   725         -static int invokeCallback(void *p, int nArg, char **azArg, char **azCol){
   726         -  GenfkeyCb *pCb = (GenfkeyCb *)p;
   727         -  UNUSED_PARAMETER(nArg);
   728         -  UNUSED_PARAMETER(azCol);
   729         -  return pCb->xData(pCb->pCtx, pCb->eType, azArg[0]);
   730         -}
   731         -
   732         -static int detectSchemaProblem(
   733         -  sqlite3 *db,                   /* Database connection */
   734         -  const char *zMessage,          /* English language error message */
   735         -  const char *zSql,              /* SQL statement to run */
   736         -  GenfkeyCb *pCb
   737         -){
   738         -  sqlite3_stmt *pStmt;
   739         -  int rc;
   740         -  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   741         -  if( rc!=SQLITE_OK ){
   742         -    return rc;
   743         -  }
   744         -  while( SQLITE_ROW==sqlite3_step(pStmt) ){
   745         -    char *zDel;
   746         -    int iFk = sqlite3_column_int(pStmt, 0);
   747         -    const char *zTab = (const char *)sqlite3_column_text(pStmt, 1);
   748         -    zDel = sqlite3_mprintf("Error in table %s: %s", zTab, zMessage);
   749         -    rc = pCb->xData(pCb->pCtx, pCb->eType, zDel);
   750         -    sqlite3_free(zDel);
   751         -    if( rc!=SQLITE_OK ) return rc;
   752         -    zDel = sqlite3_mprintf(
   753         -        "DELETE FROM temp.fkey WHERE from_tbl = %Q AND fkid = %d"
   754         -        , zTab, iFk
   755         -    );
   756         -    sqlite3_exec(db, zDel, 0, 0, 0);
   757         -    sqlite3_free(zDel);
   758         -  }
   759         -  sqlite3_finalize(pStmt);
   760         -  return SQLITE_OK;
   761         -}
   762         -
   763         -/*
   764         -** Create and populate temporary table "fkey".
   765         -*/
   766         -static int populateTempTable(sqlite3 *db, GenfkeyCb *pCallback){
   767         -  int rc;
   768         -  
   769         -  rc = sqlite3_exec(db, 
   770         -      "CREATE VIRTUAL TABLE temp.v_fkey USING schema(foreign_key_list);"
   771         -      "CREATE VIRTUAL TABLE temp.v_col USING schema(table_info);"
   772         -      "CREATE VIRTUAL TABLE temp.v_idxlist USING schema(index_list);"
   773         -      "CREATE VIRTUAL TABLE temp.v_idxinfo USING schema(index_info);"
   774         -      "CREATE VIRTUAL TABLE temp.v_triggers USING schema(trigger_list);"
   775         -      "CREATE TABLE temp.fkey AS "
   776         -        "SELECT from_tbl, to_tbl, fkid, from_col, to_col, on_update, on_delete "
   777         -        "FROM temp.v_fkey WHERE database = 'main';"
   778         -      , 0, 0, 0
   779         -  );
   780         -  if( rc!=SQLITE_OK ) return rc;
   781         -
   782         -  rc = detectSchemaProblem(db, "foreign key columns do not exist",
   783         -    "SELECT fkid, from_tbl "
   784         -    "FROM temp.fkey "
   785         -    "WHERE to_col IS NOT NULL AND NOT EXISTS (SELECT 1 "
   786         -        "FROM temp.v_col WHERE tablename=to_tbl AND name==to_col"
   787         -    ")", pCallback
   788         -  );
   789         -  if( rc!=SQLITE_OK ) return rc;
   790         -
   791         -  /* At this point the temp.fkey table is mostly populated. If any foreign
   792         -  ** keys were specified so that they implicitly refer to they primary
   793         -  ** key of the parent table, the "to_col" values of the temp.fkey rows
   794         -  ** are still set to NULL.
   795         -  **
   796         -  ** This is easily fixed for single column primary keys, but not for
   797         -  ** composites. With a composite primary key, there is no way to reliably
   798         -  ** query sqlite for the order in which the columns that make up the
   799         -  ** composite key were declared i.e. there is no way to tell if the
   800         -  ** schema actually contains "PRIMARY KEY(a, b)" or "PRIMARY KEY(b, a)".
   801         -  ** Therefore, this case is not handled. The following function call
   802         -  ** detects instances of this case.
   803         -  */
   804         -  rc = detectSchemaProblem(db, "implicit mapping to composite primary key",
   805         -    "SELECT fkid, from_tbl "
   806         -    "FROM temp.fkey "
   807         -    "WHERE to_col IS NULL "
   808         -    "GROUP BY fkid, from_tbl HAVING count(*) > 1", pCallback
   809         -  );
   810         -  if( rc!=SQLITE_OK ) return rc;
   811         -
   812         -  /* Detect attempts to implicitly map to the primary key of a table 
   813         -  ** that has no primary key column.
   814         -  */
   815         -  rc = detectSchemaProblem(db, "implicit mapping to non-existant primary key",
   816         -    "SELECT fkid, from_tbl "
   817         -    "FROM temp.fkey "
   818         -    "WHERE to_col IS NULL AND NOT EXISTS "
   819         -      "(SELECT 1 FROM temp.v_col WHERE pk AND tablename = temp.fkey.to_tbl)"
   820         -    , pCallback
   821         -  );
   822         -  if( rc!=SQLITE_OK ) return rc;
   823         -
   824         -  /* Fix all the implicit primary key mappings in the temp.fkey table. */
   825         -  rc = sqlite3_exec(db, 
   826         -    "UPDATE temp.fkey SET to_col = "
   827         -      "(SELECT name FROM temp.v_col WHERE pk AND tablename=temp.fkey.to_tbl)"
   828         -    " WHERE to_col IS NULL;"
   829         -    , 0, 0, 0
   830         -  );
   831         -  if( rc!=SQLITE_OK ) return rc;
   832         -
   833         -  /* Now check that all all parent keys are either primary keys or 
   834         -  ** subject to a unique constraint.
   835         -  */
   836         -  rc = sqlite3_exec(db, 
   837         -    "CREATE TABLE temp.idx2 AS SELECT "
   838         -      "il.tablename AS tablename,"
   839         -      "ii.indexname AS indexname,"
   840         -      "ii.name AS col "
   841         -      "FROM temp.v_idxlist AS il, temp.v_idxinfo AS ii "
   842         -      "WHERE il.isunique AND il.database='main' AND ii.indexname = il.name;"
   843         -    "INSERT INTO temp.idx2 "
   844         -      "SELECT tablename, 'pk', name FROM temp.v_col WHERE pk;"
   845         -
   846         -    "CREATE TABLE temp.idx AS SELECT "
   847         -      "tablename, indexname, sj(dq(col),',') AS cols "
   848         -      "FROM (SELECT * FROM temp.idx2 ORDER BY col) " 
   849         -      "GROUP BY tablename, indexname;"
   850         -
   851         -    "CREATE TABLE temp.fkey2 AS SELECT "
   852         -        "fkid, from_tbl, to_tbl, sj(dq(to_col),',') AS cols "
   853         -        "FROM (SELECT * FROM temp.fkey ORDER BY to_col) " 
   854         -        "GROUP BY fkid, from_tbl;"
   855         -
   856         -    "CREATE TABLE temp.triggers AS SELECT "
   857         -        "triggername FROM temp.v_triggers WHERE database='main' AND "
   858         -        "triggername LIKE 'genfkey%';"
   859         -    , 0, 0, 0
   860         -  );
   861         -  if( rc!=SQLITE_OK ) return rc;
   862         -  rc = detectSchemaProblem(db, "foreign key is not unique",
   863         -    "SELECT fkid, from_tbl "
   864         -    "FROM temp.fkey2 "
   865         -    "WHERE NOT EXISTS (SELECT 1 "
   866         -        "FROM temp.idx WHERE tablename=to_tbl AND fkey2.cols==idx.cols"
   867         -    ")", pCallback
   868         -  );
   869         -  if( rc!=SQLITE_OK ) return rc;
   870         -
   871         -  return rc;
   872         -}
   873         -
   874         -#define GENFKEY_ERROR         1
   875         -#define GENFKEY_DROPTRIGGER   2
   876         -#define GENFKEY_CREATETRIGGER 3
   877         -static int genfkey_create_triggers(
   878         -  sqlite3 *sdb,                        /* Connection to read schema from */
   879         -  const char *zDb,                     /* Name of db to read ("main", "temp") */
   880         -  void *pCtx,                          /* Context pointer to pass to xData */
   881         -  int (*xData)(void *, int, const char *)
   882         -){
   883         -  const char *zSql =
   884         -    "SELECT multireplace('"
   885         -
   886         -      "-- Triggers for foreign key mapping:\n"
   887         -      "--\n"
   888         -      "--     /from_readable/ REFERENCES /to_readable/\n"
   889         -      "--     on delete /on_delete/\n"
   890         -      "--     on update /on_update/\n"
   891         -      "--\n"
   892         -
   893         -      /* The "BEFORE INSERT ON <referencing>" trigger. This trigger's job is to
   894         -      ** throw an exception if the user tries to insert a row into the
   895         -      ** referencing table for which there is no corresponding row in
   896         -      ** the referenced table.
   897         -      */
   898         -      "CREATE TRIGGER /name/_insert_referencing BEFORE INSERT ON /tbl/ WHEN \n"
   899         -      "    /key_notnull/ AND NOT EXISTS (SELECT 1 FROM /ref/ WHERE /cond1/)\n" 
   900         -      "BEGIN\n"
   901         -        "  SELECT RAISE(ABORT, ''constraint failed'');\n"
   902         -      "END;\n"
   903         -
   904         -      /* The "BEFORE UPDATE ON <referencing>" trigger. This trigger's job 
   905         -      ** is to throw an exception if the user tries to update a row in the
   906         -      ** referencing table causing it to correspond to no row in the
   907         -      ** referenced table.
   908         -      */
   909         -      "CREATE TRIGGER /name/_update_referencing BEFORE\n"
   910         -      "    UPDATE OF /rkey_list/ ON /tbl/ WHEN \n"
   911         -      "    /key_notnull/ AND \n"
   912         -      "    NOT EXISTS (SELECT 1 FROM /ref/ WHERE /cond1/)\n" 
   913         -      "BEGIN\n"
   914         -        "  SELECT RAISE(ABORT, ''constraint failed'');\n"
   915         -      "END;\n"
   916         -
   917         -
   918         -      /* The "BEFORE DELETE ON <referenced>" trigger. This trigger's job 
   919         -      ** is to detect when a row is deleted from the referenced table to 
   920         -      ** which rows in the referencing table correspond. The action taken
   921         -      ** depends on the value of the 'ON DELETE' clause.
   922         -      */
   923         -      "CREATE TRIGGER /name/_delete_referenced BEFORE DELETE ON /ref/ WHEN\n"
   924         -      "    EXISTS (SELECT 1 FROM /tbl/ WHERE /cond2/)\n"
   925         -      "BEGIN\n"
   926         -      "  /delete_action/\n"
   927         -      "END;\n"
   928         -
   929         -      /* The "AFTER UPDATE ON <referenced>" trigger. This trigger's job 
   930         -      ** is to detect when the key columns of a row in the referenced table 
   931         -      ** to which one or more rows in the referencing table correspond are
   932         -      ** updated. The action taken depends on the value of the 'ON UPDATE' 
   933         -      ** clause.
   934         -      */
   935         -      "CREATE TRIGGER /name/_update_referenced AFTER\n"
   936         -      "    UPDATE OF /fkey_list/ ON /ref/ WHEN \n"
   937         -      "    EXISTS (SELECT 1 FROM /tbl/ WHERE /cond2/)\n"
   938         -      "BEGIN\n"
   939         -      "  /update_action/\n"
   940         -      "END;\n"
   941         -    "'"
   942         -
   943         -    /* These are used in the SQL comment written above each set of triggers */
   944         -    ", '/from_readable/',  from_tbl || '(' || sj(from_col, ', ') || ')'"
   945         -    ", '/to_readable/',    to_tbl || '(' || sj(to_col, ', ') || ')'"
   946         -    ", '/on_delete/', on_delete"
   947         -    ", '/on_update/', on_update"
   948         -
   949         -    ", '/name/',   'genfkey' || min(rowid)"
   950         -    ", '/tbl/',    dq(from_tbl)"
   951         -    ", '/ref/',    dq(to_tbl)"
   952         -    ", '/key_notnull/', sj('new.' || dq(from_col) || ' IS NOT NULL', ' AND ')"
   953         -
   954         -    ", '/fkey_list/', sj(dq(to_col), ', ')"
   955         -    ", '/rkey_list/', sj(dq(from_col), ', ')"
   956         -
   957         -    ", '/cond1/',  sj(multireplace('new./from/ == /to/'"
   958         -                   ", '/from/', dq(from_col)"
   959         -                   ", '/to/',   dq(to_col)"
   960         -                   "), ' AND ')"
   961         -    ", '/cond2/',  sj(multireplace('old./to/ == /from/'"
   962         -                   ", '/from/', dq(from_col)"
   963         -                   ", '/to/',   dq(to_col)"
   964         -                   "), ' AND ')"
   965         -
   966         -    ", '/update_action/', CASE on_update "
   967         -      "WHEN 'SET NULL' THEN "
   968         -        "multireplace('UPDATE /tbl/ SET /setlist/ WHERE /where/;' "
   969         -        ", '/setlist/', sj(dq(from_col)||' = NULL',', ')"
   970         -        ", '/tbl/',     dq(from_tbl)"
   971         -        ", '/where/',   sj(dq(from_col)||' = old.'||dq(to_col),' AND ')"
   972         -        ")"
   973         -      "WHEN 'CASCADE' THEN "
   974         -        "multireplace('UPDATE /tbl/ SET /setlist/ WHERE /where/;' "
   975         -        ", '/setlist/', sj(dq(from_col)||' = new.'||dq(to_col),', ')"
   976         -        ", '/tbl/',     dq(from_tbl)"
   977         -        ", '/where/',   sj(dq(from_col)||' = old.'||dq(to_col),' AND ')"
   978         -        ")"
   979         -      "ELSE "
   980         -      "  'SELECT RAISE(ABORT, ''constraint failed'');'"
   981         -      "END "
   982         -
   983         -    ", '/delete_action/', CASE on_delete "
   984         -      "WHEN 'SET NULL' THEN "
   985         -        "multireplace('UPDATE /tbl/ SET /setlist/ WHERE /where/;' "
   986         -        ", '/setlist/', sj(dq(from_col)||' = NULL',', ')"
   987         -        ", '/tbl/',     dq(from_tbl)"
   988         -        ", '/where/',   sj(dq(from_col)||' = old.'||dq(to_col),' AND ')"
   989         -        ")"
   990         -      "WHEN 'CASCADE' THEN "
   991         -        "multireplace('DELETE FROM /tbl/ WHERE /where/;' "
   992         -        ", '/tbl/',     dq(from_tbl)"
   993         -        ", '/where/',   sj(dq(from_col)||' = old.'||dq(to_col),' AND ')"
   994         -        ")"
   995         -      "ELSE "
   996         -      "  'SELECT RAISE(ABORT, ''constraint failed'');'"
   997         -      "END "
   998         -
   999         -    ") FROM temp.fkey "
  1000         -    "GROUP BY from_tbl, fkid"
  1001         -  ;
  1002         -
  1003         -  int rc;
  1004         -  const int enc = SQLITE_UTF8;
  1005         -  sqlite3 *db = 0;
  1006         -
  1007         -  GenfkeyCb cb;
  1008         -  cb.xData = xData;
  1009         -  cb.pCtx = pCtx;
  1010         -
  1011         -  UNUSED_PARAMETER(zDb);
  1012         -
  1013         -  /* Open the working database handle. */
  1014         -  rc = sqlite3_open(":memory:", &db);
  1015         -  if( rc!=SQLITE_OK ) goto genfkey_exit;
  1016         -
  1017         -  /* Create the special scalar and aggregate functions used by this program. */
  1018         -  sqlite3_create_function(db, "dq", 1, enc, 0, doublequote, 0, 0);
  1019         -  sqlite3_create_function(db, "multireplace", -1, enc, db, multireplace, 0, 0);
  1020         -  sqlite3_create_function(db, "sj", 2, enc, 0, 0, joinStep, joinFinalize);
  1021         -
  1022         -  /* Install the "schema" virtual table module */
  1023         -  installSchemaModule(db, sdb);
  1024         -
  1025         -  /* Create and populate a temp table with the information required to
  1026         -  ** build the foreign key triggers. See function populateTempTable()
  1027         -  ** for details.
  1028         -  */
  1029         -  cb.eType = GENFKEY_ERROR;
  1030         -  rc = populateTempTable(db, &cb);
  1031         -  if( rc!=SQLITE_OK ) goto genfkey_exit;
  1032         -
  1033         -  /* Unless the --no-drop option was specified, generate DROP TRIGGER
  1034         -  ** statements to drop any triggers in the database generated by a
  1035         -  ** previous run of this program.
  1036         -  */
  1037         -  cb.eType = GENFKEY_DROPTRIGGER;
  1038         -  rc = sqlite3_exec(db, 
  1039         -    "SELECT 'DROP TRIGGER main.' || dq(triggername) || ';' FROM triggers"
  1040         -    ,invokeCallback, (void *)&cb, 0
  1041         -  );
  1042         -  if( rc!=SQLITE_OK ) goto genfkey_exit;
  1043         -
  1044         -  /* Run the main query to create the trigger definitions. */
  1045         -  cb.eType = GENFKEY_CREATETRIGGER;
  1046         -  rc = sqlite3_exec(db, zSql, invokeCallback, (void *)&cb, 0);
  1047         -  if( rc!=SQLITE_OK ) goto genfkey_exit;
  1048         -
  1049         -genfkey_exit:
  1050         -  sqlite3_close(db);
  1051         -  return rc;
  1052         -}
  1053         -
  1054         -
  1055         -#endif
  1056         -/* End genfkey logic. */
  1057         -/*************************************************************************/
  1058         -/*************************************************************************/
  1059         -
  1060    198   /*
  1061    199   ** If the following flag is set, then command execution stops
  1062    200   ** at an error if we are not interactive.
  1063    201   */
  1064    202   static int bail_on_error = 0;
  1065    203   
  1066    204   /*
................................................................................
  1273    411                            ** the database */
  1274    412     struct previous_mode_data explainPrev;
  1275    413                            /* Holds the mode information just before
  1276    414                            ** .explain ON */
  1277    415     char outfile[FILENAME_MAX]; /* Filename for *out */
  1278    416     const char *zDbFilename;    /* name of the database file */
  1279    417     sqlite3_stmt *pStmt;   /* Current statement if any. */
          418  +  FILE *pLog;            /* Write log output here */
  1280    419   };
  1281    420   
  1282    421   /*
  1283    422   ** These are the allowed modes.
  1284    423   */
  1285    424   #define MODE_Line     0  /* One column per line.  Blank line between records */
  1286    425   #define MODE_Column   1  /* One record per line in neat columns */
................................................................................
  1314    453   ** lower 30 bits of a 32-bit signed integer.
  1315    454   */
  1316    455   static int strlen30(const char *z){
  1317    456     const char *z2 = z;
  1318    457     while( *z2 ){ z2++; }
  1319    458     return 0x3fffffff & (int)(z2 - z);
  1320    459   }
          460  +
          461  +/*
          462  +** A callback for the sqlite3_log() interface.
          463  +*/
          464  +static void shellLog(void *pArg, int iErrCode, const char *zMsg){
          465  +  struct callback_data *p = (struct callback_data*)pArg;
          466  +  if( p->pLog==0 ) return;
          467  +  fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
          468  +  fflush(p->pLog);
          469  +}
  1321    470   
  1322    471   /*
  1323    472   ** Output the given string as a hex-encoded blob (eg. X'1234' )
  1324    473   */
  1325    474   static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
  1326    475     int i;
  1327    476     char *zBlob = (char *)pBlob;
................................................................................
  2055   1204       sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
  2056   1205       rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
  2057   1206       free(zQ2);
  2058   1207     }
  2059   1208     return rc;
  2060   1209   }
  2061   1210   
  2062         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY)
  2063         -struct GenfkeyCmd {
  2064         -  sqlite3 *db;                   /* Database handle */
  2065         -  struct callback_data *pCb;     /* Callback data */
  2066         -  int isIgnoreErrors;            /* True for --ignore-errors */
  2067         -  int isExec;                    /* True for --exec */
  2068         -  int isNoDrop;                  /* True for --no-drop */
  2069         -  int nErr;                      /* Number of errors seen so far */
  2070         -};
  2071         -typedef struct GenfkeyCmd GenfkeyCmd;
  2072         -
  2073         -static int genfkeyParseArgs(GenfkeyCmd *p, char **azArg, int nArg){
  2074         -  int ii;
  2075         -  memset(p, 0, sizeof(GenfkeyCmd));
  2076         -
  2077         -  for(ii=0; ii<nArg; ii++){
  2078         -    int n = strlen30(azArg[ii]);
  2079         -
  2080         -    if( n>2 && n<10 && 0==strncmp(azArg[ii], "--no-drop", n) ){
  2081         -      p->isNoDrop = 1;
  2082         -    }else if( n>2 && n<16 && 0==strncmp(azArg[ii], "--ignore-errors", n) ){
  2083         -      p->isIgnoreErrors = 1;
  2084         -    }else if( n>2 && n<7 && 0==strncmp(azArg[ii], "--exec", n) ){
  2085         -      p->isExec = 1;
  2086         -    }else{
  2087         -      fprintf(stderr, "unknown option: %s\n", azArg[ii]);
  2088         -      return -1;
  2089         -    }
  2090         -  }
  2091         -
  2092         -  return SQLITE_OK;
  2093         -}
  2094         -
  2095         -static int genfkeyCmdCb(void *pCtx, int eType, const char *z){
  2096         -  GenfkeyCmd *p = (GenfkeyCmd *)pCtx;
  2097         -  if( eType==GENFKEY_ERROR && !p->isIgnoreErrors ){
  2098         -    p->nErr++;
  2099         -    fprintf(stderr, "%s\n", z);
  2100         -  } 
  2101         -
  2102         -  if( p->nErr==0 && (
  2103         -        (eType==GENFKEY_CREATETRIGGER)
  2104         -     || (eType==GENFKEY_DROPTRIGGER && !p->isNoDrop)
  2105         -  )){
  2106         -    if( p->isExec ){
  2107         -      sqlite3_exec(p->db, z, 0, 0, 0);
  2108         -    }else{
  2109         -      char *zCol = "sql";
  2110         -      callback((void *)p->pCb, 1, (char **)&z, (char **)&zCol);
  2111         -    }
  2112         -  }
  2113         -
  2114         -  return SQLITE_OK;
  2115         -}
  2116         -#endif
  2117         -
  2118   1211   /*
  2119   1212   ** Text of a help message
  2120   1213   */
  2121   1214   static char zHelp[] =
  2122   1215     ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
  2123   1216     ".bail ON|OFF           Stop after hitting an error.  Default OFF\n"
  2124   1217     ".databases             List names and files of attached databases\n"
................................................................................
  2125   1218     ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
  2126   1219     "                         If TABLE specified, only dump tables matching\n"
  2127   1220     "                         LIKE pattern TABLE.\n"
  2128   1221     ".echo ON|OFF           Turn command echo on or off\n"
  2129   1222     ".exit                  Exit this program\n"
  2130   1223     ".explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.\n"
  2131   1224     "                         With no args, it turns EXPLAIN on.\n"
  2132         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY)
  2133         -  ".genfkey ?OPTIONS?     Options are:\n"
  2134         -  "                         --no-drop: Do not drop old fkey triggers.\n"
  2135         -  "                         --ignore-errors: Ignore tables with fkey errors\n"
  2136         -  "                         --exec: Execute generated SQL immediately\n"
  2137         -  "                       See file tool/genfkey.README in the source \n"
  2138         -  "                       distribution for further information.\n"
  2139         -#endif
  2140   1225     ".header(s) ON|OFF      Turn display of headers on or off\n"
  2141   1226     ".help                  Show this message\n"
  2142   1227     ".import FILE TABLE     Import data from FILE into TABLE\n"
  2143   1228     ".indices ?TABLE?       Show names of all indices\n"
  2144   1229     "                         If TABLE specified, only show indices for tables\n"
  2145   1230     "                         matching LIKE pattern TABLE.\n"
  2146   1231   #ifdef SQLITE_ENABLE_IOTRACE
  2147   1232     ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
  2148   1233   #endif
  2149   1234   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  2150   1235     ".load FILE ?ENTRY?     Load an extension library\n"
  2151   1236   #endif
         1237  +  ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
  2152   1238     ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
  2153   1239     "                         csv      Comma-separated values\n"
  2154   1240     "                         column   Left-aligned columns.  (See .width)\n"
  2155   1241     "                         html     HTML <table> code\n"
  2156   1242     "                         insert   SQL insert statements for TABLE\n"
  2157   1243     "                         line     One value per line\n"
  2158   1244     "                         list     Values delimited by .separator string\n"
................................................................................
  2452   1538         p->explainPrev.valid = 0;
  2453   1539         p->mode = p->explainPrev.mode;
  2454   1540         p->showHeader = p->explainPrev.showHeader;
  2455   1541         memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
  2456   1542       }
  2457   1543     }else
  2458   1544   
  2459         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY)
  2460         -  if( c=='g' && strncmp(azArg[0], "genfkey", n)==0 ){
  2461         -    GenfkeyCmd cmd;
  2462         -    if( 0==genfkeyParseArgs(&cmd, &azArg[1], nArg-1) ){
  2463         -      cmd.db = p->db;
  2464         -      cmd.pCb = p;
  2465         -      genfkey_create_triggers(p->db, "main", (void *)&cmd, genfkeyCmdCb);
  2466         -    }
  2467         -  }else
  2468         -#endif
  2469         -
  2470   1545     if( c=='h' && (strncmp(azArg[0], "header", n)==0 ||
  2471   1546                    strncmp(azArg[0], "headers", n)==0) && nArg>1 && nArg<3 ){
  2472   1547       p->showHeader = booleanValue(azArg[1]);
  2473   1548     }else
  2474   1549   
  2475   1550     if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
  2476   1551       fprintf(stderr,"%s",zHelp);
................................................................................
  2670   1745       if( rc!=SQLITE_OK ){
  2671   1746         fprintf(stderr, "Error: %s\n", zErrMsg);
  2672   1747         sqlite3_free(zErrMsg);
  2673   1748         rc = 1;
  2674   1749       }
  2675   1750     }else
  2676   1751   #endif
         1752  +
         1753  +  if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=1 ){
         1754  +    const char *zFile = azArg[1];
         1755  +    if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){
         1756  +      fclose(p->pLog);
         1757  +      p->pLog = 0;
         1758  +    }
         1759  +    if( strcmp(zFile,"stdout")==0 ){
         1760  +      p->pLog = stdout;
         1761  +    }else if( strcmp(zFile, "stderr")==0 ){
         1762  +      p->pLog = stderr;
         1763  +    }else if( strcmp(zFile, "off")==0 ){
         1764  +      p->pLog = 0;
         1765  +    }else{
         1766  +      p->pLog = fopen(zFile, "w");
         1767  +      if( p->pLog==0 ){
         1768  +        fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
         1769  +      }
         1770  +    }
         1771  +  }else
  2677   1772   
  2678   1773     if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
  2679   1774       int n2 = strlen30(azArg[1]);
  2680   1775       if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
  2681   1776           ||
  2682   1777           (n2==5 && strncmp(azArg[1],"lines",n2)==0) ){
  2683   1778         p->mode = MODE_Line;
................................................................................
  3316   2411   ** Initialize the state information in data
  3317   2412   */
  3318   2413   static void main_init(struct callback_data *data) {
  3319   2414     memset(data, 0, sizeof(*data));
  3320   2415     data->mode = MODE_List;
  3321   2416     memcpy(data->separator,"|", 2);
  3322   2417     data->showHeader = 0;
         2418  +  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
  3323   2419     sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
  3324   2420     sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
         2421  +  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  3325   2422   }
  3326   2423   
  3327   2424   int main(int argc, char **argv){
  3328   2425     char *zErrMsg = 0;
  3329   2426     struct callback_data data;
  3330   2427     const char *zInitFile = 0;
  3331   2428     char *zFirstCmd = 0;

Changes to src/sqlite.h.in.

   104    104   */
   105    105   #define SQLITE_VERSION        "--VERS--"
   106    106   #define SQLITE_VERSION_NUMBER --VERSION-NUMBER--
   107    107   #define SQLITE_SOURCE_ID      "--SOURCE-ID--"
   108    108   
   109    109   /*
   110    110   ** CAPI3REF: Run-Time Library Version Numbers
   111         -** KEYWORDS: sqlite3_version
          111  +** KEYWORDS: sqlite3_version, sqlite3_sourceid
   112    112   **
   113    113   ** These interfaces provide the same information as the [SQLITE_VERSION],
   114    114   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
   115    115   ** but are associated with the library instead of the header file.  ^(Cautious
   116    116   ** programmers might include assert() statements in their application to
   117    117   ** verify that values returned by these interfaces match the macros in
   118    118   ** the header, and thus insure that the application is
................................................................................
   126    126   **
   127    127   ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
   128    128   ** macro.  ^The sqlite3_libversion() function returns a pointer to the
   129    129   ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
   130    130   ** function is provided for use in DLLs since DLL users usually do not have
   131    131   ** direct access to string constants within the DLL.  ^The
   132    132   ** sqlite3_libversion_number() function returns an integer equal to
   133         -** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function a pointer
   134         -** to a string constant whose value is the same as the [SQLITE_SOURCE_ID]
   135         -** C preprocessor macro.
          133  +** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
          134  +** a pointer to a string constant whose value is the same as the 
          135  +** [SQLITE_SOURCE_ID] C preprocessor macro.
   136    136   **
   137    137   ** See also: [sqlite_version()] and [sqlite_source_id()].
   138    138   */
   139    139   SQLITE_EXTERN const char sqlite3_version[];
   140    140   const char *sqlite3_libversion(void);
   141    141   const char *sqlite3_sourceid(void);
   142    142   int sqlite3_libversion_number(void);
   143    143   
          144  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
          145  +/*
          146  +** CAPI3REF: Run-Time Library Compilation Options Diagnostics
          147  +**
          148  +** ^The sqlite3_compileoption_used() function returns 0 or 1 
          149  +** indicating whether the specified option was defined at 
          150  +** compile time.  ^The SQLITE_ prefix may be omitted from the 
          151  +** option name passed to sqlite3_compileoption_used().  
          152  +**
          153  +** ^The sqlite3_compileoption_get() function allows interating
          154  +** over the list of options that were defined at compile time by
          155  +** returning the N-th compile time option string.  ^If N is out of range,
          156  +** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_ 
          157  +** prefix is omitted from any strings returned by 
          158  +** sqlite3_compileoption_get().
          159  +**
          160  +** ^Support for the diagnostic functions sqlite3_compileoption_used()
          161  +** and sqlite3_compileoption_get() may be omitted by specifing the 
          162  +** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
          163  +**
          164  +** See also: SQL functions [sqlite_compileoption_used()] and
          165  +** [sqlite_compileoption_get()] and the [compile_options pragma].
          166  +*/
          167  +int sqlite3_compileoption_used(const char *zOptName);
          168  +const char *sqlite3_compileoption_get(int N);
          169  +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
          170  +
   144    171   /*
   145    172   ** CAPI3REF: Test To See If The Library Is Threadsafe
   146    173   **
   147    174   ** ^The sqlite3_threadsafe() function returns zero if and only if
   148    175   ** SQLite was compiled mutexing code omitted due to the
   149    176   ** [SQLITE_THREADSAFE] compile-time option being set to 0.
   150    177   **
................................................................................
   639    666   ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
   640    667   ** opcode causes the xFileControl method to write the current state of
   641    668   ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
   642    669   ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
   643    670   ** into an integer that the pArg argument points to. This capability
   644    671   ** is used during testing and only needs to be supported when SQLITE_TEST
   645    672   ** is defined.
          673  +**
          674  +** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
          675  +** layer a hint of how large the database file will grow to be during the
          676  +** current transaction.  This hint is not guaranteed to be accurate but it
          677  +** is often close.  The underlying VFS might choose to preallocate database
          678  +** file space based on this hint in order to help writes to the database
          679  +** file run faster.
   646    680   */
   647    681   #define SQLITE_FCNTL_LOCKSTATE        1
   648    682   #define SQLITE_GET_LOCKPROXYFILE      2
   649    683   #define SQLITE_SET_LOCKPROXYFILE      3
   650    684   #define SQLITE_LAST_ERRNO             4
          685  +#define SQLITE_FCNTL_SIZE_HINT        5
   651    686   
   652    687   /*
   653    688   ** CAPI3REF: Mutex Handle
   654    689   **
   655    690   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
   656    691   ** abstract type for a mutex object.  The SQLite core never looks
   657    692   ** at the internal representation of an [sqlite3_mutex].  It only
................................................................................
   910    945   int sqlite3_initialize(void);
   911    946   int sqlite3_shutdown(void);
   912    947   int sqlite3_os_init(void);
   913    948   int sqlite3_os_end(void);
   914    949   
   915    950   /*
   916    951   ** CAPI3REF: Configuring The SQLite Library
   917         -** EXPERIMENTAL
   918    952   **
   919    953   ** The sqlite3_config() interface is used to make global configuration
   920    954   ** changes to SQLite in order to tune SQLite to the specific needs of
   921    955   ** the application.  The default configuration is recommended for most
   922    956   ** applications and so this routine is usually not necessary.  It is
   923    957   ** provided to support rare applications with unusual needs.
   924    958   **
................................................................................
  1251   1285   #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
  1252   1286   #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
  1253   1287   #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
  1254   1288   /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
  1255   1289   #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1256   1290   #define SQLITE_CONFIG_PCACHE       14  /* sqlite3_pcache_methods* */
  1257   1291   #define SQLITE_CONFIG_GETPCACHE    15  /* sqlite3_pcache_methods* */
         1292  +#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1258   1293   
  1259   1294   /*
  1260   1295   ** CAPI3REF: Configuration Options
  1261   1296   ** EXPERIMENTAL
  1262   1297   **
  1263   1298   ** These constants are the available integer configuration options that
  1264   1299   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
................................................................................
  5659   5694   ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
  5660   5695   ** compare the contents of two buffers containing UTF-8 strings in a
  5661   5696   ** case-indendent fashion, using the same definition of case independence 
  5662   5697   ** that SQLite uses internally when comparing identifiers.
  5663   5698   */
  5664   5699   int sqlite3_strnicmp(const char *, const char *, int);
  5665   5700   
         5701  +/*
         5702  +** CAPI3REF: Error Logging Interface
         5703  +** EXPERIMENTAL
         5704  +**
         5705  +** ^The [sqlite3_log()] interface writes a message into the error log
         5706  +** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
         5707  +** ^If logging is enabled, the zFormat string and subsequent arguments are
         5708  +** passed through to [sqlite3_vmprintf()] to generate the final output string.
         5709  +**
         5710  +** The sqlite3_log() interface is intended for use by extensions such as
         5711  +** virtual tables, collating functions, and SQL functions.  While there is
         5712  +** nothing to prevent an application from calling sqlite3_log(), doing so
         5713  +** is considered bad form.
         5714  +**
         5715  +** The zFormat string must not be NULL.
         5716  +**
         5717  +** To avoid deadlocks and other threading problems, the sqlite3_log() routine
         5718  +** will not use dynamically allocated memory.  The log message is stored in
         5719  +** a fixed-length buffer on the stack.  If the log message is longer than
         5720  +** a few hundred characters, it will be truncated to the length of the
         5721  +** buffer.
         5722  +*/
         5723  +void sqlite3_log(int iErrCode, const char *zFormat, ...);
         5724  +
  5666   5725   /*
  5667   5726   ** Undo the hack that converts floating point types to integer for
  5668   5727   ** builds on processors without floating point support.
  5669   5728   */
  5670   5729   #ifdef SQLITE_OMIT_FLOATING_POINT
  5671   5730   # undef double
  5672   5731   #endif
  5673   5732   
  5674   5733   #ifdef __cplusplus
  5675   5734   }  /* End of the 'extern "C"' block */
  5676   5735   #endif
  5677   5736   #endif

Changes to src/sqliteInt.h.

    80     80   ** The number of samples of an index that SQLite takes in order to 
    81     81   ** construct a histogram of the table content when running ANALYZE
    82     82   ** and with SQLITE_ENABLE_STAT2
    83     83   */
    84     84   #define SQLITE_INDEX_SAMPLES 10
    85     85   
    86     86   /*
    87         -** This macro is used to "hide" some ugliness in casting an int
    88         -** value to a ptr value under the MSVC 64-bit compiler.   Casting
    89         -** non 64-bit values to ptr types results in a "hard" error with 
    90         -** the MSVC 64-bit compiler which this attempts to avoid.  
           87  +** The following macros are used to cast pointers to integers and
           88  +** integers to pointers.  The way you do this varies from one compiler
           89  +** to the next, so we have developed the following set of #if statements
           90  +** to generate appropriate macros for a wide range of compilers.
    91     91   **
    92         -** A simple compiler pragma or casting sequence could not be found
    93         -** to correct this in all situations, so this macro was introduced.
    94         -**
    95         -** It could be argued that the intptr_t type could be used in this
    96         -** case, but that type is not available on all compilers, or 
    97         -** requires the #include of specific headers which differs between
    98         -** platforms.
           92  +** The correct "ANSI" way to do this is to use the intptr_t type. 
           93  +** Unfortunately, that typedef is not available on all compilers, or
           94  +** if it is available, it requires an #include of specific headers
           95  +** that very from one machine to the next.
    99     96   **
   100     97   ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
   101     98   ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
   102     99   ** So we have to define the macros in different ways depending on the
   103    100   ** compiler.
   104    101   */
   105         -#if defined(__GNUC__)
   106         -# if defined(HAVE_STDINT_H)
   107         -#   define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
   108         -#   define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
   109         -# else
   110         -#   define SQLITE_INT_TO_PTR(X)  ((void*)(X))
   111         -#   define SQLITE_PTR_TO_INT(X)  ((int)(X))
   112         -# endif
   113         -#else
   114         -# define SQLITE_INT_TO_PTR(X)   ((void*)&((char*)0)[X])
   115         -# define SQLITE_PTR_TO_INT(X)   ((int)(((char*)X)-(char*)0))
          102  +#if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
          103  +# define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
          104  +# define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
          105  +#elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
          106  +# define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
          107  +# define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
          108  +#elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
          109  +# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
          110  +# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
          111  +#else                          /* Generates a warning - but it always works */
          112  +# define SQLITE_INT_TO_PTR(X)  ((void*)(X))
          113  +# define SQLITE_PTR_TO_INT(X)  ((int)(X))
   116    114   #endif
   117         -
   118    115   
   119    116   /*
   120    117   ** The SQLITE_THREADSAFE macro must be defined as either 0 or 1.
   121    118   ** Older versions of SQLite used an optional THREADSAFE macro.
   122    119   ** We support that for legacy
   123    120   */
   124    121   #if !defined(SQLITE_THREADSAFE)
................................................................................
   790    787     int errMask;                  /* & result codes with this before returning */
   791    788     u8 autoCommit;                /* The auto-commit flag. */
   792    789     u8 temp_store;                /* 1: file 2: memory 0: default */
   793    790     u8 mallocFailed;              /* True if we have seen a malloc failure */
   794    791     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
   795    792     u8 dfltJournalMode;           /* Default journal mode for attached dbs */
   796    793     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
          794  +  u8 suppressErr;               /* Do not issue error messages if true */
   797    795     int nextPagesize;             /* Pagesize after VACUUM if >0 */
   798    796     int nTable;                   /* Number of tables in the database */
   799    797     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
   800    798     i64 lastRowid;                /* ROWID of most recent insert (see above) */
   801    799     u32 magic;                    /* Magic number for detect library misuse */
   802    800     int nChange;                  /* Value returned by sqlite3_changes() */
   803    801     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
................................................................................
  2366   2364     int isInit;                       /* True after initialization has finished */
  2367   2365     int inProgress;                   /* True while initialization in progress */
  2368   2366     int isMutexInit;                  /* True after mutexes are initialized */
  2369   2367     int isMallocInit;                 /* True after malloc is initialized */
  2370   2368     int isPCacheInit;                 /* True after malloc is initialized */
  2371   2369     sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
  2372   2370     int nRefInitMutex;                /* Number of users of pInitMutex */
         2371  +  void (*xLog)(void*,int,const char*); /* Function for logging */
         2372  +  void *pLogArg;                       /* First argument to xLog() */
  2373   2373   };
  2374   2374   
  2375   2375   /*
  2376   2376   ** Context pointer passed down through the tree-walk.
  2377   2377   */
  2378   2378   struct Walker {
  2379   2379     int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
................................................................................
  2407   2407   #define SQLITE_SKIP_UTF8(zIn) {                        \
  2408   2408     if( (*(zIn++))>=0xc0 ){                              \
  2409   2409       while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
  2410   2410     }                                                    \
  2411   2411   }
  2412   2412   
  2413   2413   /*
  2414         -** The SQLITE_CORRUPT_BKPT macro can be either a constant (for production
  2415         -** builds) or a function call (for debugging).  If it is a function call,
  2416         -** it allows the operator to set a breakpoint at the spot where database
  2417         -** corruption is first detected.
         2414  +** The SQLITE_*_BKPT macros are substitutes for the error codes with
         2415  +** the same name but without the _BKPT suffix.  These macros invoke
         2416  +** routines that report the line-number on which the error originated
         2417  +** using sqlite3_log().  The routines also provide a convenient place
         2418  +** to set a debugger breakpoint.
  2418   2419   */
  2419         -#ifdef SQLITE_DEBUG
  2420         -  int sqlite3Corrupt(void);
  2421         -# define SQLITE_CORRUPT_BKPT sqlite3Corrupt()
  2422         -#else
  2423         -# define SQLITE_CORRUPT_BKPT SQLITE_CORRUPT
  2424         -#endif
         2420  +int sqlite3CorruptError(int);
         2421  +int sqlite3MisuseError(int);
         2422  +int sqlite3CantopenError(int);
         2423  +#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
         2424  +#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
         2425  +#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
         2426  +
  2425   2427   
  2426   2428   /*
  2427   2429   ** FTS4 is really an extension for FTS3.  It is enabled using the
  2428   2430   ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
  2429   2431   ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
  2430   2432   */
  2431   2433   #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
................................................................................
  2548   2550     void sqlite3DebugPrintf(const char*, ...);
  2549   2551   #endif
  2550   2552   #if defined(SQLITE_TEST)
  2551   2553     void *sqlite3TestTextToPtr(const char*);
  2552   2554   #endif
  2553   2555   void sqlite3SetString(char **, sqlite3*, const char*, ...);
  2554   2556   void sqlite3ErrorMsg(Parse*, const char*, ...);
  2555         -void sqlite3ErrorClear(Parse*);
  2556   2557   int sqlite3Dequote(char*);
  2557   2558   int sqlite3KeywordCode(const unsigned char*, int);
  2558   2559   int sqlite3RunParser(Parse*, const char*, char **);
  2559   2560   void sqlite3FinishCoding(Parse*);
  2560   2561   int sqlite3GetTempReg(Parse*);
  2561   2562   void sqlite3ReleaseTempReg(Parse*,int);
  2562   2563   int sqlite3GetTempRange(Parse*,int);
................................................................................
  2718   2719   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  2719   2720   Select *sqlite3SelectDup(sqlite3*,Select*,int);
  2720   2721   void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  2721   2722   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
  2722   2723   void sqlite3RegisterBuiltinFunctions(sqlite3*);
  2723   2724   void sqlite3RegisterDateTimeFunctions(void);
  2724   2725   void sqlite3RegisterGlobalFunctions(void);
  2725         -#ifdef SQLITE_DEBUG
  2726         -  int sqlite3SafetyOn(sqlite3*);
  2727         -  int sqlite3SafetyOff(sqlite3*);
  2728         -#else
  2729         -# define sqlite3SafetyOn(A) 0
  2730         -# define sqlite3SafetyOff(A) 0
  2731         -#endif
  2732   2726   int sqlite3SafetyCheckOk(sqlite3*);
  2733   2727   int sqlite3SafetyCheckSickOrOk(sqlite3*);
  2734   2728   void sqlite3ChangeCookie(Parse*, int);
  2735   2729   
  2736   2730   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
  2737   2731   void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
  2738   2732   #endif
................................................................................
  2860   2854   
  2861   2855   const void *sqlite3ValueText(sqlite3_value*, u8);
  2862   2856   int sqlite3ValueBytes(sqlite3_value*, u8);
  2863   2857   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  2864   2858                           void(*)(void*));
  2865   2859   void sqlite3ValueFree(sqlite3_value*);
  2866   2860   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  2867         -char *sqlite3Utf16to8(sqlite3 *, const void*, int);
         2861  +char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  2868   2862   #ifdef SQLITE_ENABLE_STAT2
  2869   2863   char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
  2870   2864   #endif
  2871   2865   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  2872   2866   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  2873   2867   #ifndef SQLITE_AMALGAMATION
  2874   2868   extern const unsigned char sqlite3OpcodeProperty[];

Changes to src/status.c.

    79     79   ** This implementation assumes that reading or writing an aligned
    80     80   ** 32-bit integer is an atomic operation.  If that assumption is not true,
    81     81   ** then this routine is not threadsafe.
    82     82   */
    83     83   int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
    84     84     wsdStatInit;
    85     85     if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
    86         -    return SQLITE_MISUSE;
           86  +    return SQLITE_MISUSE_BKPT;
    87     87     }
    88     88     *pCurrent = wsdStat.nowValue[op];
    89     89     *pHighwater = wsdStat.mxValue[op];
    90     90     if( resetFlag ){
    91     91       wsdStat.mxValue[op] = wsdStat.nowValue[op];
    92     92     }
    93     93     return SQLITE_OK;

Changes to src/test1.c.

  4906   4906   int Sqlitetest1_Init(Tcl_Interp *interp){
  4907   4907     extern int sqlite3_search_count;
  4908   4908     extern int sqlite3_found_count;
  4909   4909     extern int sqlite3_interrupt_count;
  4910   4910     extern int sqlite3_open_file_count;
  4911   4911     extern int sqlite3_sort_count;
  4912   4912     extern int sqlite3_current_time;
  4913         -#if SQLITE_OS_UNIX && defined(__APPLE__)
         4913  +#if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  4914   4914     extern int sqlite3_hostid_num;
  4915   4915   #endif
  4916   4916     extern int sqlite3_max_blobsize;
  4917   4917     extern int sqlite3BtreeSharedCacheReport(void*,
  4918   4918                                             Tcl_Interp*,int,Tcl_Obj*CONST*);
  4919   4919     static struct {
  4920   4920        char *zName;
................................................................................
  5137   5137         (char*)&sqlite3_like_count, TCL_LINK_INT);
  5138   5138     Tcl_LinkVar(interp, "sqlite_interrupt_count", 
  5139   5139         (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
  5140   5140     Tcl_LinkVar(interp, "sqlite_open_file_count", 
  5141   5141         (char*)&sqlite3_open_file_count, TCL_LINK_INT);
  5142   5142     Tcl_LinkVar(interp, "sqlite_current_time", 
  5143   5143         (char*)&sqlite3_current_time, TCL_LINK_INT);
  5144         -#if SQLITE_OS_UNIX && defined(__APPLE__)
         5144  +#if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  5145   5145     Tcl_LinkVar(interp, "sqlite_hostid_num", 
  5146   5146         (char*)&sqlite3_hostid_num, TCL_LINK_INT);
  5147   5147   #endif
  5148   5148     Tcl_LinkVar(interp, "sqlite3_xferopt_count",
  5149   5149         (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
  5150   5150     Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
  5151   5151         (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);

Changes to src/test_config.c.

   179    179     Tcl_SetVar2(interp, "sqlite_options", "oversize_cell_check", "1",
   180    180                 TCL_GLOBAL_ONLY);
   181    181   #else
   182    182     Tcl_SetVar2(interp, "sqlite_options", "oversize_cell_check", "0",
   183    183                 TCL_GLOBAL_ONLY);
   184    184   #endif
   185    185   
   186         -
          186  +#ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
          187  +  Tcl_SetVar2(interp, "sqlite_options", "compileoption_diags", "0", TCL_GLOBAL_ONLY);
          188  +#else
          189  +  Tcl_SetVar2(interp, "sqlite_options", "compileoption_diags", "1", TCL_GLOBAL_ONLY);
          190  +#endif
   187    191   
   188    192   #ifdef SQLITE_OMIT_COMPLETE
   189    193     Tcl_SetVar2(interp, "sqlite_options", "complete", "0", TCL_GLOBAL_ONLY);
   190    194   #else
   191    195     Tcl_SetVar2(interp, "sqlite_options", "complete", "1", TCL_GLOBAL_ONLY);
   192    196   #endif
   193    197   

Changes to src/tokenize.c.

   476    476     }
   477    477     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
   478    478       sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
   479    479     }
   480    480     assert( pzErrMsg!=0 );
   481    481     if( pParse->zErrMsg ){
   482    482       *pzErrMsg = pParse->zErrMsg;
          483  +    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
   483    484       pParse->zErrMsg = 0;
   484    485       nErr++;
   485    486     }
   486    487     if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
   487    488       sqlite3VdbeDelete(pParse->pVdbe);
   488    489       pParse->pVdbe = 0;
   489    490     }

Changes to src/utf.c.

   433    433   /*
   434    434   ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
   435    435   ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
   436    436   ** be freed by the calling function.
   437    437   **
   438    438   ** NULL is returned if there is an allocation error.
   439    439   */
   440         -char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
          440  +char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
   441    441     Mem m;
   442    442     memset(&m, 0, sizeof(m));
   443    443     m.db = db;
   444         -  sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
          444  +  sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
   445    445     sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   446    446     if( db->mallocFailed ){
   447    447       sqlite3VdbeMemRelease(&m);
   448    448       m.z = 0;
   449    449     }
   450    450     assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
   451    451     assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
   452         -  return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
          452  +  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
          453  +  assert( m.z || db->mallocFailed );
          454  +  return m.z;
   453    455   }
   454    456   
   455    457   /*
   456    458   ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
   457    459   ** enc. A pointer to the new string is returned, and the value of *pnOut
   458    460   ** is set to the length of the returned string in bytes. The call should
   459    461   ** arrange to call sqlite3DbFree() on the returned pointer when it is

Changes to src/util.c.

   144    144   ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
   145    145   ** last thing the sqlite3_prepare() function does is copy the error
   146    146   ** stored by this function into the database handle using sqlite3Error().
   147    147   ** Function sqlite3Error() should be used during statement execution
   148    148   ** (sqlite3_step() etc.).
   149    149   */
   150    150   void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
          151  +  char *zMsg;
   151    152     va_list ap;
   152    153     sqlite3 *db = pParse->db;
   153         -  pParse->nErr++;
   154         -  sqlite3DbFree(db, pParse->zErrMsg);
   155    154     va_start(ap, zFormat);
   156         -  pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
          155  +  zMsg = sqlite3VMPrintf(db, zFormat, ap);
   157    156     va_end(ap);
   158         -  pParse->rc = SQLITE_ERROR;
   159         -}
   160         -
   161         -/*
   162         -** Clear the error message in pParse, if any
   163         -*/
   164         -void sqlite3ErrorClear(Parse *pParse){
   165         -  sqlite3DbFree(pParse->db, pParse->zErrMsg);
   166         -  pParse->zErrMsg = 0;
   167         -  pParse->nErr = 0;
          157  +  if( db->suppressErr ){
          158  +    sqlite3DbFree(db, zMsg);
          159  +  }else{
          160  +    pParse->nErr++;
          161  +    sqlite3DbFree(db, pParse->zErrMsg);
          162  +    pParse->zErrMsg = zMsg;
          163  +    pParse->rc = SQLITE_ERROR;
          164  +  }
   168    165   }
   169    166   
   170    167   /*
   171    168   ** Convert an SQL-style quoted string into a normal string by removing
   172    169   ** the quote characters.  The conversion is done in-place.  If the
   173    170   ** input does not begin with a quote character, then this routine
   174    171   ** is a no-op.
................................................................................
   648    645     if( (v & ~0x3fff)==0 ){
   649    646       p[0] = (u8)((v>>7) | 0x80);
   650    647       p[1] = (u8)(v & 0x7f);
   651    648       return 2;
   652    649     }
   653    650     return sqlite3PutVarint(p, v);
   654    651   }
          652  +
          653  +/*
          654  +** Bitmasks used by sqlite3GetVarint().  These precomputed constants
          655  +** are defined here rather than simply putting the constant expressions
          656  +** inline in order to work around bugs in the RVT compiler.
          657  +**
          658  +** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
          659  +**
          660  +** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
          661  +*/
          662  +#define SLOT_2_0     0x001fc07f
          663  +#define SLOT_4_2_0   0xf01fc07f
          664  +
   655    665   
   656    666   /*
   657    667   ** Read a 64-bit variable-length integer from memory starting at p[0].
   658    668   ** Return the number of bytes read.  The value is stored in *v.
   659    669   */
   660    670   u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
   661    671     u32 a,b,s;
................................................................................
   676    686       a &= 0x7f;
   677    687       a = a<<7;
   678    688       a |= b;
   679    689       *v = a;
   680    690       return 2;
   681    691     }
   682    692   
          693  +  /* Verify that constants are precomputed correctly */
          694  +  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
          695  +  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
          696  +
   683    697     p++;
   684    698     a = a<<14;
   685    699     a |= *p;
   686    700     /* a: p0<<14 | p2 (unmasked) */
   687    701     if (!(a&0x80))
   688    702     {
   689         -    a &= (0x7f<<14)|(0x7f);
          703  +    a &= SLOT_2_0;
   690    704       b &= 0x7f;
   691    705       b = b<<7;
   692    706       a |= b;
   693    707       *v = a;
   694    708       return 3;
   695    709     }
   696    710   
   697    711     /* CSE1 from below */
   698         -  a &= (0x7f<<14)|(0x7f);
          712  +  a &= SLOT_2_0;
   699    713     p++;
   700    714     b = b<<14;
   701    715     b |= *p;
   702    716     /* b: p1<<14 | p3 (unmasked) */
   703    717     if (!(b&0x80))
   704    718     {
   705         -    b &= (0x7f<<14)|(0x7f);
          719  +    b &= SLOT_2_0;
   706    720       /* moved CSE1 up */
   707    721       /* a &= (0x7f<<14)|(0x7f); */
   708    722       a = a<<7;
   709    723       a |= b;
   710    724       *v = a;
   711    725       return 4;
   712    726     }
   713    727   
   714    728     /* a: p0<<14 | p2 (masked) */
   715    729     /* b: p1<<14 | p3 (unmasked) */
   716    730     /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   717    731     /* moved CSE1 up */
   718    732     /* a &= (0x7f<<14)|(0x7f); */
   719         -  b &= (0x7f<<14)|(0x7f);
          733  +  b &= SLOT_2_0;
   720    734     s = a;
   721    735     /* s: p0<<14 | p2 (masked) */
   722    736   
   723    737     p++;
   724    738     a = a<<14;
   725    739     a |= *p;
   726    740     /* a: p0<<28 | p2<<14 | p4 (unmasked) */
................................................................................
   745    759     b = b<<14;
   746    760     b |= *p;
   747    761     /* b: p1<<28 | p3<<14 | p5 (unmasked) */
   748    762     if (!(b&0x80))
   749    763     {
   750    764       /* we can skip this cause it was (effectively) done above in calc'ing s */
   751    765       /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
   752         -    a &= (0x7f<<14)|(0x7f);
          766  +    a &= SLOT_2_0;
   753    767       a = a<<7;
   754    768       a |= b;
   755    769       s = s>>18;
   756    770       *v = ((u64)s)<<32 | a;
   757    771       return 6;
   758    772     }
   759    773   
   760    774     p++;
   761    775     a = a<<14;
   762    776     a |= *p;
   763    777     /* a: p2<<28 | p4<<14 | p6 (unmasked) */
   764    778     if (!(a&0x80))
   765    779     {
   766         -    a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
   767         -    b &= (0x7f<<14)|(0x7f);
          780  +    a &= SLOT_4_2_0;
          781  +    b &= SLOT_2_0;
   768    782       b = b<<7;
   769    783       a |= b;
   770    784       s = s>>11;
   771    785       *v = ((u64)s)<<32 | a;
   772    786       return 7;
   773    787     }
   774    788   
   775    789     /* CSE2 from below */
   776         -  a &= (0x7f<<14)|(0x7f);
          790  +  a &= SLOT_2_0;
   777    791     p++;
   778    792     b = b<<14;
   779    793     b |= *p;
   780    794     /* b: p3<<28 | p5<<14 | p7 (unmasked) */
   781    795     if (!(b&0x80))
   782    796     {
   783         -    b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
          797  +    b &= SLOT_4_2_0;
   784    798       /* moved CSE2 up */
   785    799       /* a &= (0x7f<<14)|(0x7f); */
   786    800       a = a<<7;
   787    801       a |= b;
   788    802       s = s>>4;
   789    803       *v = ((u64)s)<<32 | a;
   790    804       return 8;
................................................................................
   793    807     p++;
   794    808     a = a<<15;
   795    809     a |= *p;
   796    810     /* a: p4<<29 | p6<<15 | p8 (unmasked) */
   797    811   
   798    812     /* moved CSE2 up */
   799    813     /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
   800         -  b &= (0x7f<<14)|(0x7f);
          814  +  b &= SLOT_2_0;
   801    815     b = b<<8;
   802    816     a |= b;
   803    817   
   804    818     s = s<<4;
   805    819     b = p[-4];
   806    820     b &= 0x7f;
   807    821     b = b>>3;
................................................................................
   913    927   
   914    928     p++;
   915    929     a = a<<14;
   916    930     a |= *p;
   917    931     /* a: p0<<28 | p2<<14 | p4 (unmasked) */
   918    932     if (!(a&0x80))
   919    933     {
   920         -    /* Walues  between 268435456 and 34359738367 */
   921         -    a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
   922         -    b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
          934  +    /* Values  between 268435456 and 34359738367 */
          935  +    a &= SLOT_4_2_0;
          936  +    b &= SLOT_4_2_0;
   923    937       b = b<<7;
   924    938       *v = a | b;
   925    939       return 5;
   926    940     }
   927    941   
   928    942     /* We can only reach this point when reading a corrupt database
   929    943     ** file.  In that case we are not in any hurry.  Use the (relatively
................................................................................
  1008   1022       }
  1009   1023       zBlob[i/2] = 0;
  1010   1024     }
  1011   1025     return zBlob;
  1012   1026   }
  1013   1027   #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
  1014   1028   
  1015         -
  1016   1029   /*
  1017         -** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
  1018         -** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
  1019         -** when this routine is called.
  1020         -**
  1021         -** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
  1022         -** value indicates that the database connection passed into the API is
  1023         -** open and is not being used by another thread.  By changing the value
  1024         -** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
  1025         -** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
  1026         -** when the API exits. 
  1027         -**
  1028         -** This routine is a attempt to detect if two threads use the
  1029         -** same sqlite* pointer at the same time.  There is a race 
  1030         -** condition so it is possible that the error is not detected.
  1031         -** But usually the problem will be seen.  The result will be an
  1032         -** error which can be used to debug the application that is
  1033         -** using SQLite incorrectly.
  1034         -**
  1035         -** Ticket #202:  If db->magic is not a valid open value, take care not
  1036         -** to modify the db structure at all.  It could be that db is a stale
  1037         -** pointer.  In other words, it could be that there has been a prior
  1038         -** call to sqlite3_close(db) and db has been deallocated.  And we do
  1039         -** not want to write into deallocated memory.
         1030  +** Log an error that is an API call on a connection pointer that should
         1031  +** not have been used.  The "type" of connection pointer is given as the
         1032  +** argument.  The zType is a word like "NULL" or "closed" or "invalid".
  1040   1033   */
  1041         -#ifdef SQLITE_DEBUG
  1042         -int sqlite3SafetyOn(sqlite3 *db){
  1043         -  if( db->magic==SQLITE_MAGIC_OPEN ){
  1044         -    db->magic = SQLITE_MAGIC_BUSY;
  1045         -    assert( sqlite3_mutex_held(db->mutex) );
  1046         -    return 0;
  1047         -  }else if( db->magic==SQLITE_MAGIC_BUSY ){
  1048         -    db->magic = SQLITE_MAGIC_ERROR;
  1049         -    db->u1.isInterrupted = 1;
  1050         -  }
  1051         -  return 1;
         1034  +static void logBadConnection(const char *zType){
         1035  +  sqlite3_log(SQLITE_MISUSE, 
         1036  +     "API call with %s database connection pointer",
         1037  +     zType
         1038  +  );
  1052   1039   }
  1053         -#endif
  1054         -
  1055         -/*
  1056         -** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
  1057         -** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
  1058         -** when this routine is called.
  1059         -*/
  1060         -#ifdef SQLITE_DEBUG
  1061         -int sqlite3SafetyOff(sqlite3 *db){
  1062         -  if( db->magic==SQLITE_MAGIC_BUSY ){
  1063         -    db->magic = SQLITE_MAGIC_OPEN;
  1064         -    assert( sqlite3_mutex_held(db->mutex) );
  1065         -    return 0;
  1066         -  }else{
  1067         -    db->magic = SQLITE_MAGIC_ERROR;
  1068         -    db->u1.isInterrupted = 1;
  1069         -    return 1;
  1070         -  }
  1071         -}
  1072         -#endif
  1073   1040   
  1074   1041   /*
  1075   1042   ** Check to make sure we have a valid db pointer.  This test is not
  1076   1043   ** foolproof but it does provide some measure of protection against
  1077   1044   ** misuse of the interface such as passing in db pointers that are
  1078   1045   ** NULL or which have been previously closed.  If this routine returns
  1079   1046   ** 1 it means that the db pointer is valid and 0 if it should not be
................................................................................
  1083   1050   ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
  1084   1051   ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
  1085   1052   ** open properly and is not fit for general use but which can be
  1086   1053   ** used as an argument to sqlite3_errmsg() or sqlite3_close().
  1087   1054   */
  1088   1055   int sqlite3SafetyCheckOk(sqlite3 *db){
  1089   1056     u32 magic;
  1090         -  if( db==0 ) return 0;
         1057  +  if( db==0 ){
         1058  +    logBadConnection("NULL");
         1059  +    return 0;
         1060  +  }
  1091   1061     magic = db->magic;
  1092         -  if( magic!=SQLITE_MAGIC_OPEN 
  1093         -#ifdef SQLITE_DEBUG
  1094         -     && magic!=SQLITE_MAGIC_BUSY
  1095         -#endif
  1096         -  ){
         1062  +  if( magic!=SQLITE_MAGIC_OPEN ){
         1063  +    if( sqlite3SafetyCheckSickOrOk(db) ){
         1064  +      testcase( sqlite3GlobalConfig.xLog!=0 );
         1065  +      logBadConnection("unopened");
         1066  +    }
  1097   1067       return 0;
  1098   1068     }else{
  1099   1069       return 1;
  1100   1070     }
  1101   1071   }
  1102   1072   int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
  1103   1073     u32 magic;
  1104   1074     magic = db->magic;
  1105   1075     if( magic!=SQLITE_MAGIC_SICK &&
  1106   1076         magic!=SQLITE_MAGIC_OPEN &&
  1107         -      magic!=SQLITE_MAGIC_BUSY ) return 0;
  1108         -  return 1;
         1077  +      magic!=SQLITE_MAGIC_BUSY ){
         1078  +    testcase( sqlite3GlobalConfig.xLog!=0 );
         1079  +    logBadConnection("invalid");
         1080  +    return 0;
         1081  +  }else{
         1082  +    return 1;
         1083  +  }
  1109   1084   }

Changes to src/vacuum.c.

    14     14   ** Most of the code in this file may be omitted by defining the
    15     15   ** SQLITE_OMIT_VACUUM macro.
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include "vdbeInt.h"
    19     19   
    20     20   #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
           21  +/*
           22  +** Finalize a prepared statement.  If there was an error, store the
           23  +** text of the error message in *pzErrMsg.  Return the result code.
           24  +*/
           25  +static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
           26  +  int rc;
           27  +  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
           28  +  if( rc ){
           29  +    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
           30  +  }
           31  +  return rc;
           32  +}
           33  +
    21     34   /*
    22     35   ** Execute zSql on database db. Return an error code.
    23     36   */
    24         -static int execSql(sqlite3 *db, const char *zSql){
           37  +static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
    25     38     sqlite3_stmt *pStmt;
    26     39     VVA_ONLY( int rc; )
    27     40     if( !zSql ){
    28     41       return SQLITE_NOMEM;
    29     42     }
    30     43     if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
           44  +    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
    31     45       return sqlite3_errcode(db);
    32     46     }
    33     47     VVA_ONLY( rc = ) sqlite3_step(pStmt);
    34     48     assert( rc!=SQLITE_ROW );
    35         -  return sqlite3_finalize(pStmt);
           49  +  return vacuumFinalize(db, pStmt, pzErrMsg);
    36     50   }
    37     51   
    38     52   /*
    39     53   ** Execute zSql on database db. The statement returns exactly
    40     54   ** one column. Execute this as SQL on the same database.
    41     55   */
    42         -static int execExecSql(sqlite3 *db, const char *zSql){
           56  +static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
    43     57     sqlite3_stmt *pStmt;
    44     58     int rc;
    45     59   
    46     60     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
    47     61     if( rc!=SQLITE_OK ) return rc;
    48     62   
    49     63     while( SQLITE_ROW==sqlite3_step(pStmt) ){
    50         -    rc = execSql(db, (char*)sqlite3_column_text(pStmt, 0));
           64  +    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
    51     65       if( rc!=SQLITE_OK ){
    52         -      sqlite3_finalize(pStmt);
           66  +      vacuumFinalize(db, pStmt, pzErrMsg);
    53     67         return rc;
    54     68       }
    55     69     }
    56     70   
    57         -  return sqlite3_finalize(pStmt);
           71  +  return vacuumFinalize(db, pStmt, pzErrMsg);
    58     72   }
    59     73   
    60     74   /*
    61     75   ** The non-standard VACUUM command is used to clean up the database,
    62     76   ** collapse free space, etc.  It is modelled after the VACUUM command
    63     77   ** in PostgreSQL.
    64     78   **
................................................................................
   120    134     ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
   121    135     ** that actually made the VACUUM run slower.  Very little journalling
   122    136     ** actually occurs when doing a vacuum since the vacuum_db is initially
   123    137     ** empty.  Only the journal header is written.  Apparently it takes more
   124    138     ** time to parse and run the PRAGMA to turn journalling off than it does
   125    139     ** to write the journal header file.
   126    140     */
   127         -  zSql = "ATTACH '' AS vacuum_db;";
   128         -  rc = execSql(db, zSql);
          141  +  if( sqlite3TempInMemory(db) ){
          142  +    zSql = "ATTACH ':memory:' AS vacuum_db;";
          143  +  }else{
          144  +    zSql = "ATTACH '' AS vacuum_db;";
          145  +  }
          146  +  rc = execSql(db, pzErrMsg, zSql);
   129    147     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   130    148     pDb = &db->aDb[db->nDb-1];
   131    149     assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
   132    150     pTemp = db->aDb[db->nDb-1].pBt;
   133    151   
   134    152     /* The call to execSql() to attach the temp database has left the file
   135    153     ** locked (as there was more than one active statement when the transaction
................................................................................
   153    171     if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
   154    172      || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
   155    173      || NEVER(db->mallocFailed)
   156    174     ){
   157    175       rc = SQLITE_NOMEM;
   158    176       goto end_of_vacuum;
   159    177     }
   160         -  rc = execSql(db, "PRAGMA vacuum_db.synchronous=OFF");
          178  +  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
   161    179     if( rc!=SQLITE_OK ){
   162    180       goto end_of_vacuum;
   163    181     }
   164    182   
   165    183   #ifndef SQLITE_OMIT_AUTOVACUUM
   166    184     sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
   167    185                                              sqlite3BtreeGetAutoVacuum(pMain));
   168    186   #endif
   169    187   
   170    188     /* Begin a transaction */
   171         -  rc = execSql(db, "BEGIN EXCLUSIVE;");
          189  +  rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
   172    190     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   173    191   
   174    192     /* Query the schema of the main database. Create a mirror schema
   175    193     ** in the temporary database.
   176    194     */
   177         -  rc = execExecSql(db, 
          195  +  rc = execExecSql(db, pzErrMsg,
   178    196         "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
   179    197         "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
   180    198         "   AND rootpage>0"
   181    199     );
   182    200     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   183         -  rc = execExecSql(db, 
          201  +  rc = execExecSql(db, pzErrMsg,
   184    202         "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
   185    203         "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
   186    204     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   187         -  rc = execExecSql(db, 
          205  +  rc = execExecSql(db, pzErrMsg,
   188    206         "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
   189    207         "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
   190    208     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   191    209   
   192    210     /* Loop through the tables in the main database. For each, do
   193    211     ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
   194    212     ** the contents to the temporary database.
   195    213     */
   196         -  rc = execExecSql(db, 
          214  +  rc = execExecSql(db, pzErrMsg,
   197    215         "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
   198    216         "|| ' SELECT * FROM main.' || quote(name) || ';'"
   199    217         "FROM main.sqlite_master "
   200    218         "WHERE type = 'table' AND name!='sqlite_sequence' "
   201    219         "  AND rootpage>0"
   202         -
   203    220     );
   204    221     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   205    222   
   206    223     /* Copy over the sequence table
   207    224     */
   208         -  rc = execExecSql(db, 
          225  +  rc = execExecSql(db, pzErrMsg,
   209    226         "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
   210    227         "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
   211    228     );
   212    229     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   213         -  rc = execExecSql(db, 
          230  +  rc = execExecSql(db, pzErrMsg,
   214    231         "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
   215    232         "|| ' SELECT * FROM main.' || quote(name) || ';' "
   216    233         "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
   217    234     );
   218    235     if( rc!=SQLITE_OK ) goto end_of_vacuum;
   219    236   
   220    237   
   221    238     /* Copy the triggers, views, and virtual tables from the main database
   222    239     ** over to the temporary database.  None of these objects has any
   223    240     ** associated storage, so all we have to do is copy their entries
   224    241     ** from the SQLITE_MASTER table.
   225    242     */
   226         -  rc = execSql(db,
          243  +  rc = execSql(db, pzErrMsg,
   227    244         "INSERT INTO vacuum_db.sqlite_master "
   228    245         "  SELECT type, name, tbl_name, rootpage, sql"
   229    246         "    FROM main.sqlite_master"
   230    247         "   WHERE type='view' OR type='trigger'"
   231    248         "      OR (type='table' AND rootpage=0)"
   232    249     );
   233    250     if( rc ) goto end_of_vacuum;

Changes to src/vdbe.c.

   235    235   ** do so without loss of information.  In other words, if the string
   236    236   ** looks like a number, convert it into a number.  If it does not
   237    237   ** look like a number, leave it alone.
   238    238   */
   239    239   static void applyNumericAffinity(Mem *pRec){
   240    240     if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
   241    241       int realnum;
          242  +    u8 enc = pRec->enc;
   242    243       sqlite3VdbeMemNulTerminate(pRec);
   243         -    if( (pRec->flags&MEM_Str)
   244         -         && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
          244  +    if( (pRec->flags&MEM_Str) && sqlite3IsNumber(pRec->z, &realnum, enc) ){
   245    245         i64 value;
   246         -      sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
   247         -      if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
          246  +      char *zUtf8 = pRec->z;
          247  +#ifndef SQLITE_OMIT_UTF16
          248  +      if( enc!=SQLITE_UTF8 ){
          249  +        assert( pRec->db );
          250  +        zUtf8 = sqlite3Utf16to8(pRec->db, pRec->z, pRec->n, enc);
          251  +        if( !zUtf8 ) return;
          252  +      }
          253  +#endif
          254  +      if( !realnum && sqlite3Atoi64(zUtf8, &value) ){
   248    255           pRec->u.i = value;
   249    256           MemSetTypeFlag(pRec, MEM_Int);
   250    257         }else{
   251         -        sqlite3VdbeMemRealify(pRec);
          258  +        sqlite3AtoF(zUtf8, &pRec->r);
          259  +        MemSetTypeFlag(pRec, MEM_Real);
   252    260         }
          261  +#ifndef SQLITE_OMIT_UTF16
          262  +      if( enc!=SQLITE_UTF8 ){
          263  +        sqlite3DbFree(pRec->db, zUtf8);
          264  +      }
          265  +#endif
   253    266       }
   254    267     }
   255    268   }
   256    269   
   257    270   /*
   258    271   ** Processing is determine by the affinity parameter:
   259    272   **
................................................................................
   534    547   **
   535    548   ** After this routine has finished, sqlite3VdbeFinalize() should be
   536    549   ** used to clean up the mess that was left behind.
   537    550   */
   538    551   int sqlite3VdbeExec(
   539    552     Vdbe *p                    /* The VDBE */
   540    553   ){
   541         -  int pc;                    /* The program counter */
          554  +  int pc=0;                  /* The program counter */
   542    555     Op *aOp = p->aOp;          /* Copy of p->aOp */
   543    556     Op *pOp;                   /* Current operation */
   544    557     int rc = SQLITE_OK;        /* Value to return */
   545    558     sqlite3 *db = p->db;       /* The database */
   546    559     u8 resetSchemaOnFault = 0; /* Reset schema after an error if true */
   547    560     u8 encoding = ENC(db);     /* The database encoding */
   548    561   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
................................................................................
   559    572   #ifdef VDBE_PROFILE
   560    573     u64 start;                 /* CPU clock count at start of opcode */
   561    574     int origPc;                /* Program counter at start of opcode */
   562    575   #endif
   563    576     /*** INSERT STACK UNION HERE ***/
   564    577   
   565    578     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
   566         -  assert( db->magic==SQLITE_MAGIC_BUSY );
   567    579     sqlite3VdbeMutexArrayEnter(p);
   568    580     if( p->rc==SQLITE_NOMEM ){
   569    581       /* This happens if a malloc() inside a call to sqlite3_column_text() or
   570    582       ** sqlite3_column_text16() failed.  */
   571    583       goto no_mem;
   572    584     }
   573    585     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
................................................................................
   644    656       ** sqlite3VdbeExec() or since last time the progress callback was called).
   645    657       ** If the progress callback returns non-zero, exit the virtual machine with
   646    658       ** a return code SQLITE_ABORT.
   647    659       */
   648    660       if( checkProgress ){
   649    661         if( db->nProgressOps==nProgressOps ){
   650    662           int prc;
   651         -        if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
   652         -        prc =db->xProgress(db->pProgressArg);
   653         -        if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
          663  +        prc = db->xProgress(db->pProgressArg);
   654    664           if( prc!=0 ){
   655    665             rc = SQLITE_INTERRUPT;
   656    666             goto vdbe_error_halt;
   657    667           }
   658    668           nProgressOps = 0;
   659    669         }
   660    670         nProgressOps++;
................................................................................
   846    856       break;
   847    857     }
   848    858   
   849    859     p->rc = pOp->p1;
   850    860     p->errorAction = (u8)pOp->p2;
   851    861     p->pc = pc;
   852    862     if( pOp->p4.z ){
          863  +    assert( p->rc!=SQLITE_OK );
   853    864       sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
          865  +    testcase( sqlite3GlobalConfig.xLog!=0 );
          866  +    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
          867  +  }else if( p->rc ){
          868  +    testcase( sqlite3GlobalConfig.xLog!=0 );
          869  +    sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
   854    870     }
   855    871     rc = sqlite3VdbeHalt(p);
   856    872     assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
   857    873     if( rc==SQLITE_BUSY ){
   858    874       p->rc = rc = SQLITE_BUSY;
   859    875     }else{
   860    876       assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
................................................................................
  1396   1412     ctx.isError = 0;
  1397   1413     if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
  1398   1414       assert( pOp>aOp );
  1399   1415       assert( pOp[-1].p4type==P4_COLLSEQ );
  1400   1416       assert( pOp[-1].opcode==OP_CollSeq );
  1401   1417       ctx.pColl = pOp[-1].p4.pColl;
  1402   1418     }
  1403         -  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  1404   1419     (*ctx.pFunc->xFunc)(&ctx, n, apVal);
  1405         -  if( sqlite3SafetyOn(db) ){
  1406         -    sqlite3VdbeMemRelease(&ctx.s);
  1407         -    goto abort_due_to_misuse;
  1408         -  }
  1409   1420     if( db->mallocFailed ){
  1410   1421       /* Even though a malloc() has failed, the implementation of the
  1411   1422       ** user function may have called an sqlite3_result_XXX() function
  1412   1423       ** to return a value. The following call releases any resources
  1413   1424       ** associated with such a value.
  1414         -    **
  1415         -    ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
  1416         -    ** fails also (the if(...) statement above). But if people are
  1417         -    ** misusing sqlite, they have bigger problems than a leaked value.
  1418   1425       */
  1419   1426       sqlite3VdbeMemRelease(&ctx.s);
  1420   1427       goto no_mem;
  1421   1428     }
  1422   1429   
  1423   1430     /* If any auxiliary data functions have been called by this user function,
  1424   1431     ** immediately call the destructor for any non-static values.
................................................................................
  1733   1740   case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
  1734   1741   case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
  1735   1742   case OP_Le:               /* same as TK_LE, jump, in1, in3 */
  1736   1743   case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
  1737   1744   case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
  1738   1745     int res;            /* Result of the comparison of pIn1 against pIn3 */
  1739   1746     char affinity;      /* Affinity to use for comparison */
         1747  +  u16 flags1;         /* Copy of initial value of pIn1->flags */
         1748  +  u16 flags3;         /* Copy of initial value of pIn3->flags */
  1740   1749   
  1741   1750     pIn1 = &aMem[pOp->p1];
  1742   1751     pIn3 = &aMem[pOp->p3];
         1752  +  flags1 = pIn1->flags;
         1753  +  flags3 = pIn3->flags;
  1743   1754     if( (pIn1->flags | pIn3->flags)&MEM_Null ){
  1744   1755       /* One or both operands are NULL */
  1745   1756       if( pOp->p5 & SQLITE_NULLEQ ){
  1746   1757         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
  1747   1758         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
  1748   1759         ** or not both operands are null.
  1749   1760         */
................................................................................
  1790   1801       pOut = &aMem[pOp->p2];
  1791   1802       MemSetTypeFlag(pOut, MEM_Int);
  1792   1803       pOut->u.i = res;
  1793   1804       REGISTER_TRACE(pOp->p2, pOut);
  1794   1805     }else if( res ){
  1795   1806       pc = pOp->p2-1;
  1796   1807     }
         1808  +
         1809  +  /* Undo any changes made by applyAffinity() to the input registers. */
         1810  +  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
         1811  +  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
  1797   1812     break;
  1798   1813   }
  1799   1814   
  1800   1815   /* Opcode: Permutation * * * P4 *
  1801   1816   **
  1802   1817   ** Set the permutation used by the OP_Compare operator to be the array
  1803   1818   ** of integers in P4.
................................................................................
  4045   4060     }else if( pC->deferredMoveto ){
  4046   4061       v = pC->movetoTarget;
  4047   4062   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4048   4063     }else if( pC->pVtabCursor ){
  4049   4064       pVtab = pC->pVtabCursor->pVtab;
  4050   4065       pModule = pVtab->pModule;
  4051   4066       assert( pModule->xRowid );
  4052         -    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  4053   4067       rc = pModule->xRowid(pC->pVtabCursor, &v);
  4054   4068       sqlite3DbFree(db, p->zErrMsg);
  4055   4069       p->zErrMsg = pVtab->zErrMsg;
  4056   4070       pVtab->zErrMsg = 0;
  4057         -    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  4058   4071   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  4059   4072     }else{
  4060   4073       assert( pC->pCursor!=0 );
  4061   4074       rc = sqlite3VdbeCursorMoveto(pC);
  4062   4075       if( rc ) goto abort_due_to_error;
  4063   4076       if( pC->rowidIsValid ){
  4064   4077         v = pC->lastRowid;
................................................................................
  4586   4599       initData.pzErrMsg = &p->zErrMsg;
  4587   4600       zSql = sqlite3MPrintf(db,
  4588   4601          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
  4589   4602          db->aDb[iDb].zName, zMaster, pOp->p4.z);
  4590   4603       if( zSql==0 ){
  4591   4604         rc = SQLITE_NOMEM;
  4592   4605       }else{
  4593         -      (void)sqlite3SafetyOff(db);
  4594   4606         assert( db->init.busy==0 );
  4595   4607         db->init.busy = 1;
  4596   4608         initData.rc = SQLITE_OK;
  4597   4609         assert( !db->mallocFailed );
  4598   4610         rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
  4599   4611         if( rc==SQLITE_OK ) rc = initData.rc;
  4600   4612         sqlite3DbFree(db, zSql);
  4601   4613         db->init.busy = 0;
  4602         -      (void)sqlite3SafetyOn(db);
  4603   4614       }
  4604   4615     }
  4605   4616     sqlite3BtreeLeaveAll(db);
  4606   4617     if( rc==SQLITE_NOMEM ){
  4607   4618       goto no_mem;
  4608   4619     }
  4609   4620     break;  
................................................................................
  5165   5176   /* Opcode: Vacuum * * * * *
  5166   5177   **
  5167   5178   ** Vacuum the entire database.  This opcode will cause other virtual
  5168   5179   ** machines to be created and run.  It may not be called from within
  5169   5180   ** a transaction.
  5170   5181   */
  5171   5182   case OP_Vacuum: {
  5172         -  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; 
  5173   5183     rc = sqlite3RunVacuum(&p->zErrMsg, db);
  5174         -  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  5175   5184     break;
  5176   5185   }
  5177   5186   #endif
  5178   5187   
  5179   5188   #if !defined(SQLITE_OMIT_AUTOVACUUM)
  5180   5189   /* Opcode: IncrVacuum P1 P2 * * *
  5181   5190   **
................................................................................
  5311   5320     sqlite3_module *pModule;
  5312   5321   
  5313   5322     pCur = 0;
  5314   5323     pVtabCursor = 0;
  5315   5324     pVtab = pOp->p4.pVtab->pVtab;
  5316   5325     pModule = (sqlite3_module *)pVtab->pModule;
  5317   5326     assert(pVtab && pModule);
  5318         -  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5319   5327     rc = pModule->xOpen(pVtab, &pVtabCursor);
  5320   5328     sqlite3DbFree(db, p->zErrMsg);
  5321   5329     p->zErrMsg = pVtab->zErrMsg;
  5322   5330     pVtab->zErrMsg = 0;
  5323         -  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  5324   5331     if( SQLITE_OK==rc ){
  5325   5332       /* Initialize sqlite3_vtab_cursor base class */
  5326   5333       pVtabCursor->pVtab = pVtab;
  5327   5334   
  5328   5335       /* Initialise vdbe cursor object */
  5329   5336       pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
  5330   5337       if( pCur ){
................................................................................
  5390   5397       res = 0;
  5391   5398       apArg = p->apArg;
  5392   5399       for(i = 0; i<nArg; i++){
  5393   5400         apArg[i] = &pArgc[i+1];
  5394   5401         sqlite3VdbeMemStoreType(apArg[i]);
  5395   5402       }
  5396   5403   
  5397         -    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5398   5404       p->inVtabMethod = 1;
  5399   5405       rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  5400   5406       p->inVtabMethod = 0;
  5401   5407       sqlite3DbFree(db, p->zErrMsg);
  5402   5408       p->zErrMsg = pVtab->zErrMsg;
  5403   5409       pVtab->zErrMsg = 0;
  5404   5410       if( rc==SQLITE_OK ){
  5405   5411         res = pModule->xEof(pVtabCursor);
  5406   5412       }
  5407         -    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  5408   5413   
  5409   5414       if( res ){
  5410   5415         pc = pOp->p2 - 1;
  5411   5416       }
  5412   5417     }
  5413   5418     pCur->nullRow = 0;
  5414   5419   
................................................................................
  5446   5451     ** the current contents to sContext.s so in case the user-function 
  5447   5452     ** can use the already allocated buffer instead of allocating a 
  5448   5453     ** new one.
  5449   5454     */
  5450   5455     sqlite3VdbeMemMove(&sContext.s, pDest);
  5451   5456     MemSetTypeFlag(&sContext.s, MEM_Null);
  5452   5457   
  5453         -  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5454   5458     rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
  5455   5459     sqlite3DbFree(db, p->zErrMsg);
  5456   5460     p->zErrMsg = pVtab->zErrMsg;
  5457   5461     pVtab->zErrMsg = 0;
  5458   5462     if( sContext.isError ){
  5459   5463       rc = sContext.isError;
  5460   5464     }
................................................................................
  5464   5468     ** dynamic allocation in sContext.s (a Mem struct) is  released.
  5465   5469     */
  5466   5470     sqlite3VdbeChangeEncoding(&sContext.s, encoding);
  5467   5471     sqlite3VdbeMemMove(pDest, &sContext.s);
  5468   5472     REGISTER_TRACE(pOp->p3, pDest);
  5469   5473     UPDATE_MAX_BLOBSIZE(pDest);
  5470   5474   
  5471         -  if( sqlite3SafetyOn(db) ){
  5472         -    goto abort_due_to_misuse;
  5473         -  }
  5474   5475     if( sqlite3VdbeMemTooBig(pDest) ){
  5475   5476       goto too_big;
  5476   5477     }
  5477   5478     break;
  5478   5479   }
  5479   5480   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5480   5481   
................................................................................
  5503   5504   
  5504   5505     /* Invoke the xNext() method of the module. There is no way for the
  5505   5506     ** underlying implementation to return an error if one occurs during
  5506   5507     ** xNext(). Instead, if an error occurs, true is returned (indicating that 
  5507   5508     ** data is available) and the error code returned when xColumn or
  5508   5509     ** some other method is next invoked on the save virtual table cursor.
  5509   5510     */
  5510         -  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5511   5511     p->inVtabMethod = 1;
  5512   5512     rc = pModule->xNext(pCur->pVtabCursor);
  5513   5513     p->inVtabMethod = 0;
  5514   5514     sqlite3DbFree(db, p->zErrMsg);
  5515   5515     p->zErrMsg = pVtab->zErrMsg;
  5516   5516     pVtab->zErrMsg = 0;
  5517   5517     if( rc==SQLITE_OK ){
  5518   5518       res = pModule->xEof(pCur->pVtabCursor);
  5519   5519     }
  5520         -  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  5521   5520   
  5522   5521     if( !res ){
  5523   5522       /* If there is data, jump to P2 */
  5524   5523       pc = pOp->p2 - 1;
  5525   5524     }
  5526   5525     break;
  5527   5526   }
................................................................................
  5539   5538     Mem *pName;
  5540   5539   
  5541   5540     pVtab = pOp->p4.pVtab->pVtab;
  5542   5541     pName = &aMem[pOp->p1];
  5543   5542     assert( pVtab->pModule->xRename );
  5544   5543     REGISTER_TRACE(pOp->p1, pName);
  5545   5544     assert( pName->flags & MEM_Str );
  5546         -  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5547   5545     rc = pVtab->pModule->xRename(pVtab, pName->z);
  5548   5546     sqlite3DbFree(db, p->zErrMsg);
  5549   5547     p->zErrMsg = pVtab->zErrMsg;
  5550   5548     pVtab->zErrMsg = 0;
  5551         -  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  5552   5549   
  5553   5550     break;
  5554   5551   }
  5555   5552   #endif
  5556   5553   
  5557   5554   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5558   5555   /* Opcode: VUpdate P1 P2 P3 P4 *
................................................................................
  5595   5592       apArg = p->apArg;
  5596   5593       pX = &aMem[pOp->p3];
  5597   5594       for(i=0; i<nArg; i++){
  5598   5595         sqlite3VdbeMemStoreType(pX);
  5599   5596         apArg[i] = pX;
  5600   5597         pX++;
  5601   5598       }
  5602         -    if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5603   5599       rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
  5604   5600       sqlite3DbFree(db, p->zErrMsg);
  5605   5601       p->zErrMsg = pVtab->zErrMsg;
  5606   5602       pVtab->zErrMsg = 0;
  5607         -    if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  5608   5603       if( rc==SQLITE_OK && pOp->p1 ){
  5609   5604         assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
  5610   5605         db->lastRowid = rowid;
  5611   5606       }
  5612   5607       p->nChange++;
  5613   5608     }
  5614   5609     break;
................................................................................
  5725   5720   
  5726   5721     /* If we reach this point, it means that execution is finished with
  5727   5722     ** an error of some kind.
  5728   5723     */
  5729   5724   vdbe_error_halt:
  5730   5725     assert( rc );
  5731   5726     p->rc = rc;
         5727  +  testcase( sqlite3GlobalConfig.xLog!=0 );
         5728  +  sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
         5729  +                   pc, p->zSql, p->zErrMsg);
  5732   5730     sqlite3VdbeHalt(p);
  5733   5731     if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
  5734   5732     rc = SQLITE_ERROR;
  5735   5733     if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
  5736   5734   
  5737   5735     /* This is the only way out of this procedure.  We have to
  5738   5736     ** release the mutexes on btrees that were acquired at the
................................................................................
  5753   5751     */
  5754   5752   no_mem:
  5755   5753     db->mallocFailed = 1;
  5756   5754     sqlite3SetString(&p->zErrMsg, db, "out of memory");
  5757   5755     rc = SQLITE_NOMEM;
  5758   5756     goto vdbe_error_halt;
  5759   5757   
  5760         -  /* Jump to here for an SQLITE_MISUSE error.
  5761         -  */
  5762         -abort_due_to_misuse:
  5763         -  rc = SQLITE_MISUSE;
  5764         -  /* Fall thru into abort_due_to_error */
  5765         -
  5766   5758     /* Jump to here for any other kind of fatal error.  The "rc" variable
  5767   5759     ** should hold the error number.
  5768   5760     */
  5769   5761   abort_due_to_error:
  5770   5762     assert( p->zErrMsg==0 );
  5771   5763     if( db->mallocFailed ) rc = SQLITE_NOMEM;
  5772   5764     if( rc!=SQLITE_IOERR_NOMEM ){

Changes to src/vdbe.h.

   178    178   void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   179    179   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   180    180   void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
   181    181   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
   182    182   void sqlite3VdbeUsesBtree(Vdbe*, int);
   183    183   VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   184    184   int sqlite3VdbeMakeLabel(Vdbe*);
          185  +void sqlite3VdbeRunOnlyOnce(Vdbe*);
   185    186   void sqlite3VdbeDelete(Vdbe*);
   186    187   void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
   187    188   int sqlite3VdbeFinalize(Vdbe*);
   188    189   void sqlite3VdbeResolveLabel(Vdbe*, int);
   189    190   int sqlite3VdbeCurrentAddr(Vdbe*);
   190    191   #ifdef SQLITE_DEBUG
   191    192     int sqlite3VdbeAssertMayAbort(Vdbe *, int);

Changes to src/vdbeInt.h.

   297    297     u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   298    298     int pc;                 /* The program counter */
   299    299     int rc;                 /* Value to return */
   300    300     char *zErrMsg;          /* Error message written here */
   301    301     u8 explain;             /* True if EXPLAIN present on SQL command */
   302    302     u8 changeCntOn;         /* True to update the change-counter */
   303    303     u8 expired;             /* True if the VM needs to be recompiled */
          304  +  u8 runOnlyOnce;         /* Automatically expire on reset */
   304    305     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   305    306     u8 inVtabMethod;        /* See comments above */
   306    307     u8 usesStmtJournal;     /* True if uses a statement journal */
   307    308     u8 readOnly;            /* True for read-only statements */
   308    309     u8 isPrepareV2;         /* True if prepared with prepare_v2() */
   309    310     int nChange;            /* Number of db changes made since last reset */
   310    311     int btreeMask;          /* Bitmask of db->aDb[] entries referenced */

Changes to src/vdbeapi.c.

    29     29   ** added or changed.
    30     30   */
    31     31   int sqlite3_expired(sqlite3_stmt *pStmt){
    32     32     Vdbe *p = (Vdbe*)pStmt;
    33     33     return p==0 || p->expired;
    34     34   }
    35     35   #endif
           36  +
           37  +/*
           38  +** Check on a Vdbe to make sure it has not been finalized.  Log
           39  +** an error and return true if it has been finalized (or is otherwise
           40  +** invalid).  Return false if it is ok.
           41  +*/
           42  +static int vdbeSafety(Vdbe *p){
           43  +  if( p->db==0 ){
           44  +    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
           45  +    return 1;
           46  +  }else{
           47  +    return 0;
           48  +  }
           49  +}
           50  +static int vdbeSafetyNotNull(Vdbe *p){
           51  +  if( p==0 ){
           52  +    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
           53  +    return 1;
           54  +  }else{
           55  +    return vdbeSafety(p);
           56  +  }
           57  +}
    36     58   
    37     59   /*
    38     60   ** The following routine destroys a virtual machine that is created by
    39     61   ** the sqlite3_compile() routine. The integer returned is an SQLITE_
    40     62   ** success/failure code that describes the result of executing the virtual
    41     63   ** machine.
    42     64   **
................................................................................
    52     74   #ifdef SQLITE_ENABLE_SQLRR
    53     75       SRRecFinalize(pStmt);
    54     76   #endif
    55     77       sqlite3 *db = v->db;
    56     78   #if SQLITE_THREADSAFE
    57     79       sqlite3_mutex *mutex;
    58     80   #endif
    59         -    if (!sqlite3SafetyCheckOk(db)) {
    60         -      return SQLITE_MISUSE;
    61         -    }
           81  +    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
    62     82   #if SQLITE_THREADSAFE
    63     83       mutex = v->db->mutex;
    64     84   #endif
    65     85       sqlite3_mutex_enter(mutex);
    66     86       rc = sqlite3VdbeFinalize(v);
    67     87       rc = sqlite3ApiExit(db, rc);
    68     88       sqlite3_mutex_leave(mutex);
................................................................................
   313    333   */
   314    334   static int sqlite3Step(Vdbe *p){
   315    335     sqlite3 *db;
   316    336     int rc;
   317    337   
   318    338     assert(p);
   319    339     if( p->magic!=VDBE_MAGIC_RUN ){
   320         -    return SQLITE_MISUSE;
          340  +    sqlite3_log(SQLITE_MISUSE, 
          341  +          "attempt to step a halted statement: [%s]", p->zSql);
          342  +    return SQLITE_MISUSE_BKPT;
   321    343     }
   322    344   
   323    345     /* Check that malloc() has not failed. If it has, return early. */
   324    346     db = p->db;
   325    347     if( db->mallocFailed ){
   326    348       p->rc = SQLITE_NOMEM;
   327    349       return SQLITE_NOMEM;
   328    350     }
   329    351   
   330    352     if( p->pc<=0 && p->expired ){
   331         -    if( ALWAYS(p->rc==SQLITE_OK || p->rc==SQLITE_SCHEMA) ){
   332         -      p->rc = SQLITE_SCHEMA;
   333         -    }
          353  +    p->rc = SQLITE_SCHEMA;
   334    354       rc = SQLITE_ERROR;
   335    355       goto end_of_step;
   336    356     }
   337         -  if( sqlite3SafetyOn(db) ){
   338         -    p->rc = SQLITE_MISUSE;
   339         -    return SQLITE_MISUSE;
   340         -  }
   341    357     if( p->pc<0 ){
   342    358       /* If there are no other statements currently running, then
   343    359       ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
   344    360       ** from interrupting a statement that has not yet started.
   345    361       */
   346    362       if( db->activeVdbeCnt==0 ){
   347    363         db->u1.isInterrupted = 0;
................................................................................
   366    382       rc = sqlite3VdbeList(p);
   367    383     }else
   368    384   #endif /* SQLITE_OMIT_EXPLAIN */
   369    385     {
   370    386       rc = sqlite3VdbeExec(p);
   371    387     }
   372    388   
   373         -  if( sqlite3SafetyOff(db) ){
   374         -    rc = SQLITE_MISUSE;
   375         -  }
   376         -
   377    389   #ifndef SQLITE_OMIT_TRACE
   378    390     /* Invoke the profile callback if there is one
   379    391     */
   380    392     if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
   381    393       double rNow;
   382    394       u64 elapseTime;
   383    395   
................................................................................
   416    428   
   417    429   /*
   418    430   ** This is the top-level implementation of sqlite3_step().  Call
   419    431   ** sqlite3Step() to do most of the work.  If a schema error occurs,
   420    432   ** call sqlite3Reprepare() and try again.
   421    433   */
   422    434   int sqlite3_step(sqlite3_stmt *pStmt){
   423         -  int rc = SQLITE_MISUSE;
   424         -  Vdbe *v = (Vdbe*)pStmt;
   425         -  sqlite3 *db;
   426         -	
   427         -  if( v && (sqlite3SafetyCheckOk(db = v->db))){
   428         -    int cnt = 0;
   429         -#ifdef SQLITE_ENABLE_SQLRR
   430         -    SRRecStep(pStmt);
   431         -#endif
   432         -    sqlite3_mutex_enter(db->mutex);
   433         -    while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   434         -           && cnt++ < 5
   435         -           && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
   436         -      sqlite3_reset(pStmt);
   437         -      v->expired = 0;
   438         -    }
   439         -    if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
   440         -      /* This case occurs after failing to recompile an sql statement. 
   441         -      ** The error message from the SQL compiler has already been loaded 
   442         -      ** into the database handle. This block copies the error message 
   443         -      ** from the database handle into the statement and sets the statement
   444         -      ** program counter to 0 to ensure that when the statement is 
   445         -      ** finalized or reset the parser error message is available via
   446         -      ** sqlite3_errmsg() and sqlite3_errcode().
   447         -      */
   448         -      const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
   449         -      sqlite3DbFree(db, v->zErrMsg);
   450         -      if( !db->mallocFailed ){
   451         -        v->zErrMsg = sqlite3DbStrDup(db, zErr);
   452         -      } else {
   453         -        v->zErrMsg = 0;
   454         -        v->rc = SQLITE_NOMEM;
   455         -      }
   456         -    }
   457         -    rc = sqlite3ApiExit(db, rc);
   458         -    sqlite3_mutex_leave(db->mutex);
   459         -#ifdef SQLITE_ENABLE_SQLRR
   460         -    SRRecStepEnd(pStmt);
   461         -#endif
   462         -  }
          435  +  int rc = SQLITE_OK;      /* Result from sqlite3Step() */
          436  +  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
          437  +  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
          438  +  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
          439  +  sqlite3 *db;             /* The database connection */
          440  +
          441  +  if( vdbeSafetyNotNull(v) ){
          442  +    return SQLITE_MISUSE_BKPT;
          443  +  }
          444  +  db = v->db;
          445  +  sqlite3_mutex_enter(db->mutex);
          446  +  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
          447  +         && cnt++ < 5
          448  +         && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
          449  +    sqlite3_reset(pStmt);
          450  +    v->expired = 0;
          451  +  }
          452  +  if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
          453  +    /* This case occurs after failing to recompile an sql statement. 
          454  +    ** The error message from the SQL compiler has already been loaded 
          455  +    ** into the database handle. This block copies the error message 
          456  +    ** from the database handle into the statement and sets the statement
          457  +    ** program counter to 0 to ensure that when the statement is 
          458  +    ** finalized or reset the parser error message is available via
          459  +    ** sqlite3_errmsg() and sqlite3_errcode().
          460  +    */
          461  +    const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
          462  +    sqlite3DbFree(db, v->zErrMsg);
          463  +    if( !db->mallocFailed ){
          464  +      v->zErrMsg = sqlite3DbStrDup(db, zErr);
          465  +      v->rc = rc2;
          466  +    } else {
          467  +      v->zErrMsg = 0;
          468  +      v->rc = rc = SQLITE_NOMEM;
          469  +    }
          470  +  }
          471  +  rc = sqlite3ApiExit(db, rc);
          472  +  sqlite3_mutex_leave(db->mutex);
   463    473     return rc;
   464    474   }
   465    475   
   466    476   /*
   467    477   ** Extract the user data from a sqlite3_context structure and return a
   468    478   ** pointer to it.
   469    479   */
................................................................................
   925    935   ** the mutex is released if any kind of error occurs.
   926    936   **
   927    937   ** The error code stored in database p->db is overwritten with the return
   928    938   ** value in any case.
   929    939   */
   930    940   static int vdbeUnbind(Vdbe *p, int i){
   931    941     Mem *pVar;
   932         -  if( p==0 ) return SQLITE_MISUSE;
          942  +  if( vdbeSafetyNotNull(p) ){
          943  +    return SQLITE_MISUSE_BKPT;
          944  +  }
   933    945     sqlite3_mutex_enter(p->db->mutex);
   934    946     if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
   935    947       sqlite3Error(p->db, SQLITE_MISUSE, 0);
   936    948       sqlite3_mutex_leave(p->db->mutex);
   937         -    return SQLITE_MISUSE;
          949  +    sqlite3_log(SQLITE_MISUSE, 
          950  +        "bind on a busy prepared statement: [%s]", p->zSql);
          951  +    return SQLITE_MISUSE_BKPT;
   938    952     }
   939    953     if( i<1 || i>p->nVar ){
   940    954       sqlite3Error(p->db, SQLITE_RANGE, 0);
   941    955       sqlite3_mutex_leave(p->db->mutex);
   942    956       return SQLITE_RANGE;
   943    957     }
   944    958     i--;

Changes to src/vdbeaux.c.

   250    250     int j = -1-x;
   251    251     assert( p->magic==VDBE_MAGIC_INIT );
   252    252     assert( j>=0 && j<p->nLabel );
   253    253     if( p->aLabel ){
   254    254       p->aLabel[j] = p->nOp;
   255    255     }
   256    256   }
          257  +
          258  +/*
          259  +** Mark the VDBE as one that can only be run one time.
          260  +*/
          261  +void sqlite3VdbeRunOnlyOnce(Vdbe *p){
          262  +  p->runOnlyOnce = 1;
          263  +}
   257    264   
   258    265   #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
   259    266   
   260    267   /*
   261    268   ** The following type and function are used to iterate through all opcodes
   262    269   ** in a Vdbe main program and each of the sub-programs (triggers) it may 
   263    270   ** invoke directly or indirectly. It should be used as follows:
................................................................................
  1055   1062     sqlite3 *db = p->db;                 /* The database connection */
  1056   1063     int i;                               /* Loop counter */
  1057   1064     int rc = SQLITE_OK;                  /* Return code */
  1058   1065     Mem *pMem = p->pResultSet = &p->aMem[1];  /* First Mem of result set */
  1059   1066   
  1060   1067     assert( p->explain );
  1061   1068     assert( p->magic==VDBE_MAGIC_RUN );
  1062         -  assert( db->magic==SQLITE_MAGIC_BUSY );
  1063   1069     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
  1064   1070   
  1065   1071     /* Even though this opcode does not use dynamic strings for
  1066   1072     ** the result, result columns may become dynamic if the user calls
  1067   1073     ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
  1068   1074     */
  1069   1075     releaseMemArray(pMem, 8);
................................................................................
  1470   1476       sqlite3BtreeCloseCursor(pCx->pCursor);
  1471   1477     }
  1472   1478   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1473   1479     if( pCx->pVtabCursor ){
  1474   1480       sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
  1475   1481       const sqlite3_module *pModule = pCx->pModule;
  1476   1482       p->inVtabMethod = 1;
  1477         -    (void)sqlite3SafetyOff(p->db);
  1478   1483       pModule->xClose(pVtabCursor);
  1479         -    (void)sqlite3SafetyOn(p->db);
  1480   1484       p->inVtabMethod = 0;
  1481   1485     }
  1482   1486   #endif
  1483   1487   }
  1484   1488   
  1485   1489   /*
  1486   1490   ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
................................................................................
  1653   1657         needXcommit = 1;
  1654   1658         if( i!=1 ) nTrans++;
  1655   1659       }
  1656   1660     }
  1657   1661   
  1658   1662     /* If there are any write-transactions at all, invoke the commit hook */
  1659   1663     if( needXcommit && db->xCommitCallback ){
  1660         -    (void)sqlite3SafetyOff(db);
  1661   1664       rc = db->xCommitCallback(db->pCommitArg);
  1662         -    (void)sqlite3SafetyOn(db);
  1663   1665       if( rc ){
  1664   1666         return SQLITE_CONSTRAINT;
  1665   1667       }
  1666   1668     }
  1667   1669   
  1668   1670     /* The simple case - no more than one database file (not counting the
  1669   1671     ** TEMP database) has a transaction active.   There is no need for the
................................................................................
  2121   2123           db->autoCommit = 1;
  2122   2124         }
  2123   2125       }
  2124   2126     
  2125   2127       /* If eStatementOp is non-zero, then a statement transaction needs to
  2126   2128       ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
  2127   2129       ** do so. If this operation returns an error, and the current statement
  2128         -    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then set the error
  2129         -    ** code to the new value.
         2130  +    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
         2131  +    ** current statement error code.
         2132  +    **
         2133  +    ** Note that sqlite3VdbeCloseStatement() can only fail if eStatementOp
         2134  +    ** is SAVEPOINT_ROLLBACK.  But if p->rc==SQLITE_OK then eStatementOp
         2135  +    ** must be SAVEPOINT_RELEASE.  Hence the NEVER(p->rc==SQLITE_OK) in 
         2136  +    ** the following code.
  2130   2137       */
  2131   2138       if( eStatementOp ){
  2132   2139         rc = sqlite3VdbeCloseStatement(p, eStatementOp);
  2133         -      if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
         2140  +      if( rc && (NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT) ){
  2134   2141           p->rc = rc;
  2135   2142           sqlite3DbFree(db, p->zErrMsg);
  2136   2143           p->zErrMsg = 0;
  2137   2144         }
  2138   2145       }
  2139   2146     
  2140   2147       /* If this was an INSERT, UPDATE or DELETE and no statement transaction
................................................................................
  2209   2216     sqlite3 *db;
  2210   2217     db = p->db;
  2211   2218   
  2212   2219     /* If the VM did not run to completion or if it encountered an
  2213   2220     ** error, then it might not have been halted properly.  So halt
  2214   2221     ** it now.
  2215   2222     */
  2216         -  (void)sqlite3SafetyOn(db);
  2217   2223     sqlite3VdbeHalt(p);
  2218         -  (void)sqlite3SafetyOff(db);
  2219   2224   
  2220   2225     /* If the VDBE has be run even partially, then transfer the error code
  2221   2226     ** and error message from the VDBE into the main database structure.  But
  2222   2227     ** if the VDBE has just been set to run but has not actually executed any
  2223   2228     ** instructions yet, leave the main database error information unchanged.
  2224   2229     */
  2225   2230     if( p->pc>=0 ){
................................................................................
  2231   2236         sqlite3DbFree(db, p->zErrMsg);
  2232   2237         p->zErrMsg = 0;
  2233   2238       }else if( p->rc ){
  2234   2239         sqlite3Error(db, p->rc, 0);
  2235   2240       }else{
  2236   2241         sqlite3Error(db, SQLITE_OK, 0);
  2237   2242       }
         2243  +    if( p->runOnlyOnce ) p->expired = 1;
  2238   2244     }else if( p->rc && p->expired ){
  2239   2245       /* The expired flag was set on the VDBE before the first call
  2240   2246       ** to sqlite3_step(). For consistency (since sqlite3_step() was
  2241   2247       ** called), set the database error in this case as well.
  2242   2248       */
  2243   2249       sqlite3Error(db, p->rc, 0);
  2244   2250       sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
................................................................................
  3013   3019     assert( sqlite3BtreeCursorIsValid(pCur) );
  3014   3020     rc = sqlite3BtreeKeySize(pCur, &nCellKey);
  3015   3021     assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
  3016   3022     /* nCellKey will always be between 0 and 0xffffffff because of the say
  3017   3023     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
  3018   3024     if( nCellKey<=0 || nCellKey>0x7fffffff ){
  3019   3025       *res = 0;
  3020         -    return SQLITE_CORRUPT;
         3026  +    return SQLITE_CORRUPT_BKPT;
  3021   3027     }
  3022   3028     memset(&m, 0, sizeof(m));
  3023   3029     rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
  3024   3030     if( rc ){
  3025   3031       return rc;
  3026   3032     }
  3027   3033     assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );

Changes to src/vdbeblob.c.

    91     91       rc = SQLITE_NOMEM;
    92     92       goto blob_open_out;
    93     93     }
    94     94     do {
    95     95       memset(pParse, 0, sizeof(Parse));
    96     96       pParse->db = db;
    97     97   
    98         -    if( sqlite3SafetyOn(db) ){
    99         -      sqlite3DbFree(db, zErr);
   100         -      sqlite3StackFree(db, pParse);
   101         -      sqlite3_mutex_leave(db->mutex);
   102         -      return SQLITE_MISUSE;
   103         -    }
   104         -
   105     98       sqlite3BtreeEnterAll(db);
   106     99       pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
   107    100       if( pTab && IsVirtual(pTab) ){
   108    101         pTab = 0;
   109    102         sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
   110    103       }
   111    104   #ifndef SQLITE_OMIT_VIEW
................................................................................
   117    110       if( !pTab ){
   118    111         if( pParse->zErrMsg ){
   119    112           sqlite3DbFree(db, zErr);
   120    113           zErr = pParse->zErrMsg;
   121    114           pParse->zErrMsg = 0;
   122    115         }
   123    116         rc = SQLITE_ERROR;
   124         -      (void)sqlite3SafetyOff(db);
   125    117         sqlite3BtreeLeaveAll(db);
   126    118         goto blob_open_out;
   127    119       }
   128    120   
   129    121       /* Now search pTab for the exact column. */
   130    122       for(iCol=0; iCol < pTab->nCol; iCol++) {
   131    123         if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
................................................................................
   132    124           break;
   133    125         }
   134    126       }
   135    127       if( iCol==pTab->nCol ){
   136    128         sqlite3DbFree(db, zErr);
   137    129         zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
   138    130         rc = SQLITE_ERROR;
   139         -      (void)sqlite3SafetyOff(db);
   140    131         sqlite3BtreeLeaveAll(db);
   141    132         goto blob_open_out;
   142    133       }
   143    134   
   144    135       /* If the value is being opened for writing, check that the
   145    136       ** column is not indexed, and that it is not part of a foreign key. 
   146    137       ** It is against the rules to open a column to which either of these
................................................................................
   173    164             }
   174    165           }
   175    166         }
   176    167         if( zFault ){
   177    168           sqlite3DbFree(db, zErr);
   178    169           zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
   179    170           rc = SQLITE_ERROR;
   180         -        (void)sqlite3SafetyOff(db);
   181    171           sqlite3BtreeLeaveAll(db);
   182    172           goto blob_open_out;
   183    173         }
   184    174       }
   185    175   
   186    176       v = sqlite3VdbeCreate(db);
   187    177       if( v ){
................................................................................
   223    213         sqlite3VdbeChangeP2(v, 7, pTab->nCol);
   224    214         if( !db->mallocFailed ){
   225    215           sqlite3VdbeMakeReady(v, 1, 1, 1, 0, 0, 0);
   226    216         }
   227    217       }
   228    218      
   229    219       sqlite3BtreeLeaveAll(db);
   230         -    rc = sqlite3SafetyOff(db);
   231         -    if( NEVER(rc!=SQLITE_OK) || db->mallocFailed ){
          220  +    if( db->mallocFailed ){
   232    221         goto blob_open_out;
   233    222       }
   234    223   
   235    224       sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
   236    225       rc = sqlite3_step((sqlite3_stmt *)v);
   237    226       if( rc!=SQLITE_ROW ){
   238    227         nAttempt++;
................................................................................
   325    314     int (*xCall)(BtCursor*, u32, u32, void*)
   326    315   ){
   327    316     int rc;
   328    317     Incrblob *p = (Incrblob *)pBlob;
   329    318     Vdbe *v;
   330    319     sqlite3 *db;
   331    320   
   332         -  if( p==0 ) return SQLITE_MISUSE;
          321  +  if( p==0 ) return SQLITE_MISUSE_BKPT;
   333    322     db = p->db;
   334    323     sqlite3_mutex_enter(db->mutex);
   335    324     v = (Vdbe*)p->pStmt;
   336    325   
   337    326     if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
   338    327       /* Request is out of range. Return a transient error. */
   339    328       rc = SQLITE_ERROR;

Changes to src/vdbemem.c.

   605    605   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   606    606   */
   607    607   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   608    608     assert( (pFrom->flags & MEM_RowSet)==0 );
   609    609     sqlite3VdbeMemReleaseExternal(pTo);
   610    610     memcpy(pTo, pFrom, MEMCELLSIZE);
   611    611     pTo->xDel = 0;
   612         -  if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){
          612  +  if( (pFrom->flags&MEM_Static)==0 ){
   613    613       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   614    614       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   615    615       pTo->flags |= srcType;
   616    616     }
   617    617   }
   618    618   
   619    619   /*

Changes to src/vtab.c.

   119    119     assert( pVTab->nRef>0 );
   120    120     assert( sqlite3SafetyCheckOk(db) );
   121    121   
   122    122     pVTab->nRef--;
   123    123     if( pVTab->nRef==0 ){
   124    124       sqlite3_vtab *p = pVTab->pVtab;
   125    125       if( p ){
   126         -#ifdef SQLITE_DEBUG
   127         -      if( pVTab->db->magic==SQLITE_MAGIC_BUSY ){
   128         -        (void)sqlite3SafetyOff(db);
   129         -        p->pModule->xDisconnect(p);
   130         -        (void)sqlite3SafetyOn(db);
   131         -      } else
   132         -#endif
   133         -      {
   134         -        p->pModule->xDisconnect(p);
   135         -      }
          126  +      p->pModule->xDisconnect(p);
   136    127       }
   137    128       sqlite3DbFree(db, pVTab);
   138    129     }
   139    130   }
   140    131   
   141    132   /*
   142    133   ** Table p is a virtual table. This function moves all elements in the
................................................................................
   464    455     pVTable->pMod = pMod;
   465    456   
   466    457     assert( !db->pVTab );
   467    458     assert( xConstruct );
   468    459     db->pVTab = pTab;
   469    460   
   470    461     /* Invoke the virtual table constructor */
   471         -  (void)sqlite3SafetyOff(db);
   472    462     rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
   473         -  (void)sqlite3SafetyOn(db);
   474    463     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
   475    464   
   476    465     if( SQLITE_OK!=rc ){
   477    466       if( zErr==0 ){
   478    467         *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
   479    468       }else {
   480    469         *pzErr = sqlite3MPrintf(db, "%s", zErr);
................................................................................
   654    643     char *zErr = 0;
   655    644   
   656    645     sqlite3_mutex_enter(db->mutex);
   657    646     pTab = db->pVTab;
   658    647     if( !pTab ){
   659    648       sqlite3Error(db, SQLITE_MISUSE, 0);
   660    649       sqlite3_mutex_leave(db->mutex);
   661         -    return SQLITE_MISUSE;
          650  +    return SQLITE_MISUSE_BKPT;
   662    651     }
   663    652     assert( (pTab->tabFlags & TF_Virtual)!=0 );
   664    653   
   665    654     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   666    655     if( pParse==0 ){
   667    656       rc = SQLITE_NOMEM;
   668    657     }else{
................................................................................
   713    702     int rc = SQLITE_OK;
   714    703     Table *pTab;
   715    704   
   716    705     pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
   717    706     if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
   718    707       VTable *p = vtabDisconnectAll(db, pTab);
   719    708   
   720         -    rc = sqlite3SafetyOff(db);
   721    709       assert( rc==SQLITE_OK );
   722    710       rc = p->pMod->pModule->xDestroy(p->pVtab);
   723         -    (void)sqlite3SafetyOn(db);
   724    711   
   725    712       /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
   726    713       if( rc==SQLITE_OK ){
   727    714         assert( pTab->pVTable==p && p->pNext==0 );
   728    715         p->pVtab = 0;
   729    716         pTab->pVTable = 0;
   730    717         sqlite3VtabUnlock(p);
................................................................................
   768    755   **
   769    756   ** Set *pzErrmsg to point to a buffer that should be released using 
   770    757   ** sqlite3DbFree() containing an error message, if one is available.
   771    758   */
   772    759   int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
   773    760     int i;
   774    761     int rc = SQLITE_OK;
   775         -  int rcsafety;
   776    762     VTable **aVTrans = db->aVTrans;
   777    763   
   778         -  rc = sqlite3SafetyOff(db);
   779    764     db->aVTrans = 0;
   780    765     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
   781    766       int (*x)(sqlite3_vtab *);
   782    767       sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
   783    768       if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
   784    769         rc = x(pVtab);
   785    770         sqlite3DbFree(db, *pzErrmsg);
   786    771         *pzErrmsg = pVtab->zErrMsg;
   787    772         pVtab->zErrMsg = 0;
   788    773       }
   789    774     }
   790    775     db->aVTrans = aVTrans;
   791         -  rcsafety = sqlite3SafetyOn(db);
   792         -
   793         -  if( rc==SQLITE_OK ){
   794         -    rc = rcsafety;
   795         -  }
   796    776     return rc;
   797    777   }
   798    778   
   799    779   /*
   800    780   ** Invoke the xRollback method of all virtual tables in the 
   801    781   ** sqlite3.aVTrans array. Then clear the array itself.
   802    782   */

Changes to src/where.c.

  1092   1092     int idxTerm               /* Index of the term to be analyzed */
  1093   1093   ){
  1094   1094     WhereTerm *pTerm;                /* The term to be analyzed */
  1095   1095     WhereMaskSet *pMaskSet;          /* Set of table index masks */
  1096   1096     Expr *pExpr;                     /* The expression to be analyzed */
  1097   1097     Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
  1098   1098     Bitmask prereqAll;               /* Prerequesites of pExpr */
  1099         -  Bitmask extraRight = 0;          /* */
         1099  +  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
  1100   1100     Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
  1101   1101     int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
  1102   1102     int noCase = 0;                  /* LIKE/GLOB distinguishes case */
  1103   1103     int op;                          /* Top-level operator.  pExpr->op */
  1104   1104     Parse *pParse = pWC->pParse;     /* Parsing context */
  1105   1105     sqlite3 *db = pParse->db;        /* Database connection */
  1106   1106   
................................................................................
  1164   1164           pDup = pExpr;
  1165   1165           pNew = pTerm;
  1166   1166         }
  1167   1167         exprCommute(pParse, pDup);
  1168   1168         pLeft = pDup->pLeft;
  1169   1169         pNew->leftCursor = pLeft->iTable;
  1170   1170         pNew->u.leftColumn = pLeft->iColumn;
  1171         -      pNew->prereqRight = prereqLeft;
         1171  +      testcase( (prereqLeft | extraRight) != prereqLeft );
         1172  +      pNew->prereqRight = prereqLeft | extraRight;
  1172   1173         pNew->prereqAll = prereqAll;
  1173   1174         pNew->eOperator = operatorMask(pDup->op);
  1174   1175       }
  1175   1176     }
  1176   1177   
  1177   1178   #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
  1178   1179     /* If a term is the BETWEEN operator, create two new virtual terms
................................................................................
  1754   1755   ** that this is required.
  1755   1756   */
  1756   1757   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
  1757   1758     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
  1758   1759     int i;
  1759   1760     int rc;
  1760   1761   
  1761         -  (void)sqlite3SafetyOff(pParse->db);
  1762   1762     WHERETRACE(("xBestIndex for %s\n", pTab->zName));
  1763   1763     TRACE_IDX_INPUTS(p);
  1764   1764     rc = pVtab->pModule->xBestIndex(pVtab, p);
  1765   1765     TRACE_IDX_OUTPUTS(p);
  1766         -  (void)sqlite3SafetyOn(pParse->db);
  1767   1766   
  1768   1767     if( rc!=SQLITE_OK ){
  1769   1768       if( rc==SQLITE_NOMEM ){
  1770   1769         pParse->db->mallocFailed = 1;
  1771   1770       }else if( !pVtab->zErrMsg ){
  1772   1771         sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
  1773   1772       }else{

Changes to test/analyze3.test.

   477    477     sqlite3_step $S
   478    478   } {SQLITE_DONE}
   479    479   do_test analyze3-4.1.2 {
   480    480     sqlite3_reset $S
   481    481     sqlite3_bind_text $S 2 "abc" 3
   482    482     execsql { DROP TABLE t1 }
   483    483     sqlite3_step $S
   484         -} {SQLITE_SCHEMA}
          484  +} {SQLITE_ERROR}
   485    485   do_test analyze3-4.1.3 {
   486    486     sqlite3_finalize $S
   487         -} {SQLITE_SCHEMA}
          487  +} {SQLITE_ERROR}
   488    488   
   489    489   # Check an authorization error.
   490    490   #
   491    491   do_test analyze3-4.2.1 {
   492    492     execsql {
   493    493       BEGIN;
   494    494       CREATE TABLE t1(a, b, c);
................................................................................
   507    507     if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
   508    508     return SQLITE_OK
   509    509   }
   510    510   do_test analyze3-4.2.2 {
   511    511     sqlite3_reset $S
   512    512     sqlite3_bind_text $S 2 "abc" 3
   513    513     sqlite3_step $S
   514         -} {SQLITE_SCHEMA}
          514  +} {SQLITE_AUTH}
   515    515   do_test analyze3-4.2.4 {
   516    516     sqlite3_finalize $S
   517         -} {SQLITE_SCHEMA}
          517  +} {SQLITE_AUTH}
   518    518   
   519    519   # Check the effect of an authorization error that occurs in a re-prepare
   520    520   # performed by sqlite3_step() is the same as one that occurs within
   521    521   # sqlite3Reprepare().
   522    522   #
   523    523   do_test analyze3-4.3.1 {
   524    524     db auth {}
   525    525     set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
   526    526     execsql { CREATE TABLE t2(d, e, f) }
   527    527     db auth auth
   528    528     sqlite3_step $S
   529         -} {SQLITE_SCHEMA}
          529  +} {SQLITE_AUTH}
   530    530   do_test analyze3-4.3.2 {
   531    531     sqlite3_finalize $S
   532         -} {SQLITE_SCHEMA}
          532  +} {SQLITE_AUTH}
   533    533   db auth {}
   534    534   
   535    535   #-------------------------------------------------------------------------
   536    536   # Test that modifying bound variables using the clear_bindings() or
   537    537   # transfer_bindings() APIs works.
   538    538   #
   539    539   #   analyze3-5.1.*: sqlite3_clear_bindings()

Changes to test/attachmalloc.test.

    55     55       CREATE INDEX i1 ON t1(a, b);
    56     56     }
    57     57     db2 close
    58     58   } -sqlbody {
    59     59     CREATE TABLE t1(d, e, f);
    60     60     ATTACH 'test2.db' AS db1;
    61     61   }
           62  +
           63  +set enable_shared_cache [sqlite3_enable_shared_cache 1]
           64  +sqlite3 dbaux test2.db
           65  +dbaux eval {SELECT * FROM sqlite_master}
           66  +do_malloc_test attachmalloc-3 -sqlbody {
           67  +  SELECT * FROM sqlite_master;
           68  +  ATTACH 'test2.db' AS two;
           69  +} -cleanup {
           70  +  db eval { DETACH two }
           71  +}
           72  +dbaux close
           73  +sqlite3_enable_shared_cache $enable_shared_cache
           74  +
    62     75   
    63     76   finish_test

Changes to test/backup.test.

   952    952       execsql { UPDATE t1 SET b = randomblob(200) WHERE a IN (1, 250) }
   953    953     } {}
   954    954   
   955    955     do_test backup-10.$tn.5 {
   956    956       B step 50
   957    957     } $rc
   958    958   
   959         -  do_test backup-10.6 {
          959  +  do_test backup-10.$tn.6 {
   960    960       B finish
   961    961     } {SQLITE_OK}
          962  +
          963  +  db2 close
   962    964   }
   963    965   
   964         -db2 close
   965    966   finish_test

Changes to test/capi3c.test.

  1172   1172   do_test capi3c-19.3 {
  1173   1173     sqlite3_step $STMT
  1174   1174   } SQLITE_DONE
  1175   1175   do_test capi3c-19.4 {
  1176   1176     sqlite3_reset $STMT
  1177   1177     db eval {DROP TABLE t3}
  1178   1178     sqlite3_step $STMT
  1179         -} SQLITE_SCHEMA
         1179  +} SQLITE_ERROR
  1180   1180   do_test capi3c-19.4.1 {
  1181   1181     sqlite3_errmsg $DB
  1182   1182   } {no such table: t3}
  1183   1183   ifcapable deprecated {
  1184   1184     do_test capi3c-19.4.2 {
  1185   1185       sqlite3_expired $STMT
  1186   1186     } 1

Changes to test/corrupt.test.

     1         -# 2004 August 30
            1  +# 2004 August 30 {}
     2      2   #
     3      3   # The author disclaims copyright to this source code.  In place of
     4      4   # a legal notice, here is a blessing:
     5      5   #
     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
................................................................................
   307    307     do_test corrupt-7.3 {
   308    308       catchsql {
   309    309         INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A');
   310    310       }
   311    311     } {1 {database disk image is malformed}}
   312    312   }
   313    313   
          314  +db close
          315  +file delete -force test.db test.db-journal
          316  +do_test corrupt-8.1 {
          317  +  sqlite3 db test.db
          318  +  execsql {
          319  +    PRAGMA page_size = 1024;
          320  +    PRAGMA secure_delete = on;
          321  +    PRAGMA auto_vacuum = 0;
          322  +    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
          323  +    INSERT INTO t1 VALUES(5, randomblob(1900));
          324  +  }
          325  +
          326  +  hexio_write test.db 2044 [hexio_render_int32 2]
          327  +  hexio_write test.db 24   [hexio_render_int32 45]
          328  +
          329  +  catchsql { INSERT OR REPLACE INTO t1 VALUES(5, randomblob(1900)) }
          330  +} {1 {database disk image is malformed}}
          331  +
          332  +db close
          333  +file delete -force test.db test.db-journal
          334  +do_test corrupt-8.2 {
          335  +  sqlite3 db test.db
          336  +  execsql {
          337  +    PRAGMA page_size = 1024;
          338  +    PRAGMA secure_delete = on;
          339  +    PRAGMA auto_vacuum = 0;
          340  +    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
          341  +    INSERT INTO t1 VALUES(5, randomblob(900));
          342  +    INSERT INTO t1 VALUES(6, randomblob(900));
          343  +  }
          344  +
          345  +  hexio_write test.db 2047 FF
          346  +  hexio_write test.db 24   [hexio_render_int32 45]
          347  +
          348  +  catchsql { INSERT INTO t1 VALUES(4, randomblob(1900)) }
          349  +} {1 {database disk image is malformed}}
          350  +
   314    351   finish_test

Changes to test/corrupt7.test.

    85     85   Corruption detected in cell 15 on page 2}}
    86     86     do_test corrupt7-2.2 {
    87     87       db close
    88     88       hexio_write test.db 1062 04
    89     89       sqlite3 db test.db
    90     90       db eval {PRAGMA integrity_check(1)}
    91     91     } {{*** in database main ***
    92         -Corruption detected in cell 15 on page 2}}
           92  +On tree page 2 cell 15: Rowid 0 out of order (previous was 15)}}
    93     93   }
    94     94     
    95     95   # The code path that was causing the buffer overrun that this test
    96     96   # case was checking for was removed.
    97     97   #
    98     98   #do_test corrupt7-3.1 {
    99     99   #  execsql {

Added test/corruptE.test.

            1  +# 2010 February 18
            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  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# This file implements tests to make sure SQLite does not crash or
           14  +# segfault if it sees a corrupt database file.  It specifcally
           15  +# focuses on rowid order corruption.
           16  +#
           17  +# $Id: corruptE.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
           18  +
           19  +catch {file delete -force test.db test.db-journal test.bu}
           20  +
           21  +set testdir [file dirname $argv0]
           22  +source $testdir/tester.tcl
           23  +
           24  +# Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on.
           25  +#
           26  +ifcapable oversize_cell_check {
           27  +  finish_test
           28  +  return
           29  +}
           30  +
           31  +# Construct a compact, dense database for testing.
           32  +#
           33  +do_test corruptE-1.1 {
           34  +  execsql {
           35  +    PRAGMA auto_vacuum = 0;
           36  +    PRAGMA legacy_file_format=1;
           37  +    BEGIN;
           38  +    CREATE TABLE t1(x,y);
           39  +    INSERT INTO t1 VALUES(1,1);
           40  +    INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
           41  +    INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
           42  +    INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
           43  +    INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
           44  +    INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
           45  +    INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
           46  +    INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
           47  +    INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
           48  +    CREATE INDEX t1i1 ON t1(x);
           49  +    CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0;
           50  +    COMMIT;
           51  +  }
           52  +} {}
           53  +
           54  +ifcapable {integrityck} {
           55  +  integrity_check corruptE-1.2
           56  +}
           57  +
           58  +# Copy file $from into $to
           59  +#
           60  +proc copy_file {from to} {
           61  +  file copy -force $from $to
           62  +}
           63  +
           64  +# Setup for the tests.  Make a backup copy of the good database in test.bu.
           65  +#
           66  +db close
           67  +copy_file test.db test.bu
           68  +sqlite3 db test.db
           69  +set fsize [file size test.db]
           70  +
           71  +
           72  +do_test corruptE-2.1 {
           73  +  db close
           74  +  copy_file test.bu test.db
           75  +
           76  +  # insert corrupt byte(s)
           77  +  hexio_write test.db 2041 [format %02x 0x2e]
           78  +
           79  +  sqlite3 db test.db
           80  +
           81  +  set res [ catchsql {PRAGMA integrity_check} ]
           82  +  set ans [lindex $res 1]
           83  +
           84  +  list [regexp {out of order.*previous was} $ans] \
           85  +       [regexp {out of order.*max larger than parent max} $ans]
           86  +} {1 1}
           87  +
           88  +do_test corruptE-2.2 {
           89  +  db close
           90  +  copy_file test.bu test.db
           91  +
           92  +  # insert corrupt byte(s)
           93  +  hexio_write test.db 2047 [format %02x 0x84]
           94  +
           95  +  sqlite3 db test.db
           96  +
           97  +  set res [ catchsql {PRAGMA integrity_check} ]
           98  +  set ans [lindex $res 1]
           99  +
          100  +  list [regexp {out of order.*previous was} $ans] \
          101  +       [regexp {out of order.*min less than parent min} $ans]
          102  +} {1 1}
          103  +
          104  +do_test corruptE-2.3 {
          105  +  db close
          106  +  copy_file test.bu test.db
          107  +
          108  +  # insert corrupt byte(s)
          109  +  hexio_write test.db 7420 [format %02x 0xa8]
          110  +  hexio_write test.db 10459 [format %02x 0x8d]
          111  +
          112  +  sqlite3 db test.db
          113  +
          114  +  set res [ catchsql {PRAGMA integrity_check} ]
          115  +  set ans [lindex $res 1]
          116  +
          117  +  list [regexp {out of order.*max larger than parent min} $ans]
          118  +} {1}
          119  +
          120  +do_test corruptE-2.4 {
          121  +  db close
          122  +  copy_file test.bu test.db
          123  +
          124  +  # insert corrupt byte(s)
          125  +  hexio_write test.db 10233 [format %02x 0xd0]
          126  +
          127  +  sqlite3 db test.db
          128  +
          129  +  set res [ catchsql {PRAGMA integrity_check} ]
          130  +  set ans [lindex $res 1]
          131  +
          132  +  list [regexp {out of order.*min less than parent max} $ans]
          133  +} {1}
          134  +
          135  +
          136  +set tests [list {10233 0xd0} \
          137  +                {941 0x42} \
          138  +                {1028 0x53} \
          139  +                {2041 0xd0} \
          140  +                {2042 0x1f} \
          141  +                {2047 0xaa} \
          142  +                {2263 0x29} \
          143  +                {2274 0x75} \
          144  +                {3267 0xf2} \
          145  +                {4104 0x2c} \
          146  +                {5113 0x36} \
          147  +                {10233 0x84} \
          148  +                {10234 0x74} \
          149  +                {10239 0x41} \
          150  +                {10453 0x11} \
          151  +                {11273 0x28} \
          152  +                {11455 0x11} \
          153  +                {11461 0xe6} \
          154  +                {12281 0x99} \
          155  +                {12296 0x9e} \
          156  +                {12297 0xd7} \
          157  +                {13303 0x53} ]
          158  +
          159  +set tc 1
          160  +foreach test $tests {
          161  +  do_test corruptE-3.$tc {
          162  +    db close
          163  +    copy_file test.bu test.db
          164  +
          165  +    # insert corrupt byte(s)
          166  +    hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]]
          167  +
          168  +    sqlite3 db test.db
          169  +
          170  +    set res [ catchsql {PRAGMA integrity_check} ]
          171  +    set ans [lindex $res 1]
          172  +
          173  +    list [regexp {out of order} $ans]
          174  +  } {1}
          175  +  incr tc 1
          176  +}
          177  +
          178  +finish_test

Changes to test/crash8.test.

   335    335     } {def}
   336    336   
   337    337     do_test crash8-4.10 {
   338    338       file delete $zMasterJournal
   339    339       execsql { SELECT b FROM main.ab WHERE a = 0 }
   340    340     } {jkl}
   341    341   }
          342  +
          343  +for {set i 1} {$i < 10} {incr i} {
          344  +  catch { db close }
          345  +  file delete -force test.db test.db-journal
          346  +  sqlite3 db test.db
          347  +  do_test crash8-5.$i.1 {
          348  +    execsql {
          349  +      CREATE TABLE t1(x PRIMARY KEY);
          350  +      INSERT INTO t1 VALUES(randomblob(900));
          351  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          352  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          353  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          354  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          355  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          356  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;          /* 64 rows */
          357  +    }
          358  +    crashsql -file test.db -delay [expr ($::i%2) + 1] {
          359  +      PRAGMA cache_size = 10;
          360  +      BEGIN;
          361  +        UPDATE t1 SET x = randomblob(900);
          362  +      ROLLBACK;
          363  +      INSERT INTO t1 VALUES(randomblob(900));
          364  +    }
          365  +    execsql { PRAGMA integrity_check }
          366  +  } {ok}
          367  +  
          368  +  catch { db close }
          369  +  file delete -force test.db test.db-journal
          370  +  sqlite3 db test.db
          371  +  do_test crash8-5.$i.2 {
          372  +    execsql {
          373  +      PRAGMA cache_size = 10;
          374  +      CREATE TABLE t1(x PRIMARY KEY);
          375  +      INSERT INTO t1 VALUES(randomblob(900));
          376  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          377  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          378  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          379  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          380  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;
          381  +      INSERT INTO t1 SELECT randomblob(900) FROM t1;          /* 64 rows */
          382  +      BEGIN;
          383  +        UPDATE t1 SET x = randomblob(900);
          384  +    }
          385  +    file delete -force testX.db testX.db-journal
          386  +    copy_file test.db testX.db
          387  +    copy_file test.db-journal testX.db-journal
          388  +    db close
          389  +
          390  +    crashsql -file test.db -delay [expr ($::i%2) + 1] {
          391  +      SELECT * FROM sqlite_master;
          392  +      INSERT INTO t1 VALUES(randomblob(900));
          393  +    }
          394  +
          395  +    sqlite3 db2 testX.db
          396  +    execsql { PRAGMA integrity_check } db2
          397  +  } {ok}
          398  +}
          399  +catch {db2 close}
   342    400   
   343    401   finish_test

Added test/ctime.test.

            1  +# 2009 February 24
            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  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# This file implements tests for the compile time diagnostic 
           14  +# functions.
           15  +#
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +
           20  +# Test organization:
           21  +#
           22  +# ctime-1.*: Test pragma support.
           23  +# ctime-2.*: Test function support.
           24  +#
           25  +
           26  +ifcapable !pragma||!compileoption_diags {
           27  +  finish_test
           28  +  return
           29  +}
           30  +
           31  +#####################
           32  +# ctime-1.*: Test pragma support.
           33  +
           34  +do_test ctime-1.1.1 {
           35  +  catchsql {
           36  +    PRAGMA compile_options();
           37  +  }
           38  +} {1 {near ")": syntax error}}
           39  +do_test ctime-1.1.2 {
           40  +  catchsql {
           41  +    PRAGMA compile_options(NULL);
           42  +  }
           43  +} {1 {near "NULL": syntax error}}
           44  +do_test ctime-1.1.3 {
           45  +  catchsql {
           46  +    PRAGMA compile_options *;
           47  +  }
           48  +} {1 {near "*": syntax error}}
           49  +
           50  +do_test ctime-1.2.1 {
           51  +  set ans [ catchsql {
           52  +    PRAGMA compile_options;
           53  +  } ]
           54  +  list [ lindex $ans 0 ]
           55  +} {0}
           56  +# the results should be in sorted order already
           57  +do_test ctime-1.2.2 {
           58  +  set ans [ catchsql {
           59  +    PRAGMA compile_options;
           60  +  } ]
           61  +  list [ lindex $ans 0 ] [ expr { [lsort [lindex $ans 1]]==[lindex $ans 1] } ]
           62  +} {0 1}
           63  +
           64  +# SQLITE_THREADSAFE should pretty much always be defined
           65  +# one way or the other, and it must have a value of 0 or 1.
           66  +do_test ctime-1.4.1 {
           67  +  catchsql {
           68  +    SELECT sqlite_compileoption_used('SQLITE_THREADSAFE');
           69  +  }
           70  +} {0 1}
           71  +do_test ctime-1.4.2 {
           72  +  catchsql {
           73  +    SELECT sqlite_compileoption_used('THREADSAFE');
           74  +  }
           75  +} {0 1}
           76  +do_test ctime-1.4.3 {
           77  +  catchsql {
           78  +    SELECT sqlite_compileoption_used("THREADSAFE");
           79  +  }
           80  +} {0 1}
           81  +
           82  +do_test ctime-1.5 {
           83  +  set ans1 [ catchsql {
           84  +    SELECT sqlite_compileoption_used('THREADSAFE=0');
           85  +  } ]
           86  +  set ans2 [ catchsql {
           87  +    SELECT sqlite_compileoption_used('THREADSAFE=1');
           88  +  } ]
           89  +  lsort [ list $ans1 $ans2 ]
           90  +} {{0 0} {0 1}}
           91  +
           92  +do_test ctime-1.6 {
           93  +  execsql {
           94  +    SELECT sqlite_compileoption_used('THREADSAFE=');
           95  +  }
           96  +} {0}
           97  +
           98  +do_test ctime-1.7.1 {
           99  +  execsql {
          100  +    SELECT sqlite_compileoption_used('SQLITE_OMIT_COMPILEOPTION_DIAGS');
          101  +  }
          102  +} {0}
          103  +do_test ctime-1.7.2 {
          104  +  execsql {
          105  +    SELECT sqlite_compileoption_used('OMIT_COMPILEOPTION_DIAGS');
          106  +  }
          107  +} {0}
          108  +
          109  +#####################
          110  +# ctime-2.*: Test function support.
          111  +
          112  +do_test ctime-2.1.1 {
          113  +  catchsql {
          114  +    SELECT sqlite_compileoption_used();
          115  +  }
          116  +} {1 {wrong number of arguments to function sqlite_compileoption_used()}}
          117  +do_test ctime-2.1.2 {
          118  +  catchsql {
          119  +    SELECT sqlite_compileoption_used(NULL);
          120  +  }
          121  +} {0 {{}}}
          122  +do_test ctime-2.1.3 {
          123  +  catchsql {
          124  +    SELECT sqlite_compileoption_used("");
          125  +  }
          126  +} {0 0}
          127  +do_test ctime-2.1.4 {
          128  +  catchsql {
          129  +    SELECT sqlite_compileoption_used('');
          130  +  }
          131  +} {0 0}
          132  +do_test ctime-2.1.5 {
          133  +  catchsql {
          134  +    SELECT sqlite_compileoption_used(foo);
          135  +  }
          136  +} {1 {no such column: foo}}
          137  +do_test ctime-2.1.6 {
          138  +  catchsql {
          139  +    SELECT sqlite_compileoption_used('THREADSAFE', 0);
          140  +  }
          141  +} {1 {wrong number of arguments to function sqlite_compileoption_used()}}
          142  +do_test ctime-2.1.7 {
          143  +  catchsql {
          144  +    SELECT sqlite_compileoption_used(0);
          145  +  }
          146  +} {0 0}
          147  +do_test ctime-2.1.8 {
          148  +  catchsql {
          149  +    SELECT sqlite_compileoption_used('0');
          150  +  }
          151  +} {0 0}
          152  +do_test ctime-2.1.9 {
          153  +  catchsql {
          154  +    SELECT sqlite_compileoption_used(1.0);
          155  +  }
          156  +} {0 0}
          157  +
          158  +do_test ctime-2.2.1 {
          159  +  catchsql {
          160  +    SELECT sqlite_compileoption_get();
          161  +  }
          162  +} {1 {wrong number of arguments to function sqlite_compileoption_get()}}
          163  +do_test ctime-2.2.2 {
          164  +  catchsql {
          165  +    SELECT sqlite_compileoption_get(0, 0);
          166  +  }
          167  +} {1 {wrong number of arguments to function sqlite_compileoption_get()}}
          168  +
          169  +# This assumes there is at least 1 compile time option
          170  +# (see SQLITE_THREADSAFE above).
          171  +do_test ctime-2.3 {
          172  +  catchsql {
          173  +    SELECT sqlite_compileoption_used(sqlite_compileoption_get(0));
          174  +  }
          175  +} {0 1}
          176  +
          177  +# This assumes there is at least 1 compile time option
          178  +# (see SQLITE_THREADSAFE above).
          179  +do_test ctime-2.4 {
          180  +  set ans [ catchsql {
          181  +    SELECT sqlite_compileoption_get(0);
          182  +  } ]
          183  +  list [lindex $ans 0]
          184  +} {0}
          185  +
          186  +# Get the list of defines using the pragma,
          187  +# then try querying each one with the functions.
          188  +set ans [ catchsql {
          189  +  PRAGMA compile_options;
          190  +} ]
          191  +set opts [ lindex $ans 1 ]
          192  +set tc 1
          193  +foreach opt $opts {
          194  +  do_test ctime-2.5.$tc {
          195  +    set N [ expr {$tc-1} ]
          196  +    set ans1 [ catchsql {
          197  +      SELECT sqlite_compileoption_get($N);
          198  +    } ]
          199  +    set ans2 [ catchsql {
          200  +      SELECT sqlite_compileoption_used($opt);
          201  +    } ]
          202  +    list [ lindex $ans1 0 ] [ expr { [lindex $ans1 1]==$opt } ] \
          203  +         [ expr { $ans2 } ]
          204  +  } {0 1 {0 1}}
          205  +  incr tc 1
          206  +}
          207  +# test 1 past array bounds
          208  +do_test ctime-2.5.$tc {
          209  +  set N [ expr {$tc-1} ]
          210  +  set ans [ catchsql {
          211  +    SELECT sqlite_compileoption_get($N);
          212  +  } ]
          213  +} {0 {{}}}
          214  +incr tc 1
          215  +# test 1 before array bounds (N=-1)
          216  +do_test ctime-2.5.$tc {
          217  +  set N -1
          218  +  set ans [ catchsql {
          219  +    SELECT sqlite_compileoption_get($N);
          220  +  } ]
          221  +} {0 {{}}}
          222  +
          223  +
          224  +finish_test

Changes to test/fts3ac.test.

  1193   1193     execsql {
  1194   1194       SELECT snippet(email) FROM email
  1195   1195        WHERE email MATCH 'enron OR com questar'
  1196   1196     }
  1197   1197   } {{matt.smith@<b>enron</b>.<b>com</b><b>...</b>31 Keystone Receipts
  1198   1198   15 <b>Questar</b> Pipeline
  1199   1199   40 Rockies<b>...</b>}}
         1200  +
         1201  +#-------------------------------------------------------------------------
         1202  +# Test a problem reported on the mailing list.
         1203  +#
         1204  +do_test fts3ac-6.1 {
         1205  +  execsql {
         1206  +    CREATE VIRTUAL TABLE ft USING fts3(one, two);
         1207  +    INSERT INTO ft VALUES('', 'foo');
         1208  +    INSERT INTO ft VALUES('foo', 'foo');
         1209  +    SELECT offsets(ft) FROM ft WHERE ft MATCH 'foo';
         1210  +  }
         1211  +} {{1 0 0 3} {0 0 0 3 1 0 0 3}}
         1212  +do_test fts3ac-6.2 {
         1213  +  execsql {
         1214  +    DELETE FROM ft WHERE one = 'foo';
         1215  +    SELECT offsets(ft) FROM ft WHERE ft MATCH 'foo';
         1216  +  }
         1217  +} {{1 0 0 3}}
  1200   1218   
  1201   1219   finish_test

Changes to test/fts3ag.test.

    69     69   do_test fts3ag-1.9 {
    70     70     execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'this something'}
    71     71   } {}
    72     72   
    73     73   # No support for all-except queries.
    74     74   do_test fts3ag-1.10 {
    75     75     catchsql {SELECT rowid FROM t1 WHERE t1 MATCH '-this -something'}
    76         -} {1 {SQL logic error or missing database}}
           76  +} {1 {malformed MATCH expression: [-this -something]}}
    77     77   
    78     78   # Test that docListOrMerge() correctly handles reaching the end of one
    79     79   # doclist before it reaches the end of the other.
    80     80   do_test fts3ag-1.11 {
    81     81   breakpoint
    82     82     execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'this OR also'}
    83     83   } {1 2}

Changes to test/fts3expr.test.

   332    332   do_test fts3expr-4.1 {
   333    333     execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b, c) }
   334    334   } {}
   335    335   
   336    336   # Mismatched parenthesis:
   337    337   do_test fts3expr-4.2.1 {
   338    338     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world))' }
   339         -} {1 {SQL logic error or missing database}}
          339  +} {1 {malformed MATCH expression: [example AND (hello OR world))]}}
   340    340   do_test fts3expr-4.2.2 {
   341    341     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world' }
   342         -} {1 {SQL logic error or missing database}}
          342  +} {1 {malformed MATCH expression: [example AND (hello OR world]}}
   343    343   do_test fts3expr-4.2.3 {
   344    344     catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello' }
   345         -} {1 {SQL logic error or missing database}}
          345  +} {1 {malformed MATCH expression: [(hello]}}
   346    346   do_test fts3expr-4.2.4 {
   347    347     catchsql { SELECT * FROM t1 WHERE t1 MATCH '(' }
   348         -} {1 {SQL logic error or missing database}}
          348  +} {1 {malformed MATCH expression: [(]}}
   349    349   do_test fts3expr-4.2.5 {
   350    350     catchsql { SELECT * FROM t1 WHERE t1 MATCH ')' }
   351         -} {1 {SQL logic error or missing database}}
          351  +} {1 {malformed MATCH expression: [)]}}
   352    352   
   353    353   do_test fts3expr-4.2.6 {
   354    354     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example (hello world' }
   355         -} {1 {SQL logic error or missing database}}
          355  +} {1 {malformed MATCH expression: [example (hello world]}}
   356    356   
   357    357   # Unterminated quotation marks:
   358    358   do_test fts3expr-4.3.1 {
   359    359     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR "hello world' }
   360         -} {1 {SQL logic error or missing database}}
          360  +} {1 {malformed MATCH expression: [example OR "hello world]}}
   361    361   do_test fts3expr-4.3.2 {
   362    362     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR hello world"' }
   363         -} {1 {SQL logic error or missing database}}
          363  +} {1 {malformed MATCH expression: [example OR hello world"]}}
   364    364   
   365    365   # Binary operators without the required operands.
   366    366   do_test fts3expr-4.4.1 {
   367    367     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'OR hello world' }
   368         -} {1 {SQL logic error or missing database}}
          368  +} {1 {malformed MATCH expression: [OR hello world]}}
   369    369   do_test fts3expr-4.4.2 {
   370    370     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'hello world OR' }
   371         -} {1 {SQL logic error or missing database}}
          371  +} {1 {malformed MATCH expression: [hello world OR]}}
   372    372   do_test fts3expr-4.4.3 {
   373    373     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (hello world OR) two' }
   374         -} {1 {SQL logic error or missing database}}
          374  +} {1 {malformed MATCH expression: [one (hello world OR) two]}}
   375    375   do_test fts3expr-4.4.4 {
   376    376     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (OR hello world) two' }
   377         -} {1 {SQL logic error or missing database}}
          377  +} {1 {malformed MATCH expression: [one (OR hello world) two]}}
   378    378   
   379    379   # NEAR operators with something other than phrases as arguments.
   380    380   do_test fts3expr-4.5.1 {
   381    381     catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello OR world) NEAR one' }
   382         -} {1 {SQL logic error or missing database}}
          382  +} {1 {malformed MATCH expression: [(hello OR world) NEAR one]}}
   383    383   do_test fts3expr-4.5.2 {
   384    384     catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one NEAR (hello OR world)' }
   385         -} {1 {SQL logic error or missing database}}
          385  +} {1 {malformed MATCH expression: [one NEAR (hello OR world)]}}
   386    386   
   387    387   #------------------------------------------------------------------------
   388    388   # The following OOM tests are designed to cover cases in fts3_expr.c.
   389    389   # 
   390    390   source $testdir/malloc_common.tcl
   391    391   do_malloc_test fts3expr-malloc-1 -sqlbody {
   392    392     SELECT fts3_exprtest('simple', 'a b c "d e f"', 'a', 'b', 'c')

Changes to test/fts3snippet.test.

   338    338       for {set i 1} {$i < 150} {incr i} {
   339    339         set commas [string repeat , $i]
   340    340         execsql {INSERT INTO ft VALUES('one' || $commas || 'two')}
   341    341         lappend testresults "{one}$commas{two}"
   342    342       }
   343    343       execsql COMMIT
   344    344     } {}
   345         -  do_snippet_test $T.7.2 {one two} -1 3 {*}$testresults
          345  +  eval [list do_snippet_test $T.7.2 {one two} -1 3] $testresults
   346    346     
   347    347     ##########################################################################
   348    348     # Test the matchinfo function.
   349    349     #
   350    350     proc mit {blob} {
   351    351       set scan(littleEndian) i*
   352    352       set scan(bigEndian) I*

Changes to test/fts4aa.test.

    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # If SQLITE_ENABLE_FTS3 is defined, omit this file.
    20     20   ifcapable !fts3 {
    21     21     finish_test
    22     22     return
           23  +}
           24  +if {[db eval {SELECT sqlite_compileoption_used('ENABLE_FTS4')}]==0} {
           25  +  finish_test
           26  +  return
    23     27   }
    24     28   
    25     29   do_test fts4aa-1.0 {
    26     30   db eval {
    27     31   CREATE VIRTUAL TABLE t1 USING fts4(words, tokenize porter);
    28     32   BEGIN TRANSACTION;
    29     33   INSERT INTO t1(docid,words) VALUES(1001001,'In the beginning God created the heaven and the earth.');

Changes to test/permutations.test.

   468    468     ioerr.test ioerr2.test ioerr3.test ioerr4.test ioerr5.test
   469    469     vacuum3.test incrblob_err.test diskfull.test backup_ioerr.test
   470    470   
   471    471     # Exclude test scripts that use tcl IO to access journal files or count
   472    472     # the number of fsync() calls.
   473    473     pager.test exclusive.test jrnlmode.test sync.test misc1.test 
   474    474     journal1.test conflict.test crash8.test tkt3457.test io.test
          475  +
          476  +  # Exclude stmt.test, which expects sub-journals to use temporary files.
          477  +  stmt.test
   475    478   }
   476    479   
   477    480   ifcapable mem3 {
   478    481     run_tests "memsys3" -description {
   479    482       Run tests using the allocator in mem3.c.
   480    483     } -exclude {
   481    484       autovacuum.test           delete3.test              manydb.test

Deleted test/safety.test.

     1         -# 2005 January 11
     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         -# This file implements regression tests for SQLite library.  The
    12         -# focus of this file is testing the sqlite3SafetyOn and sqlite3SafetyOff
    13         -# functions.  Those routines are not strictly necessary - they are
    14         -# designed to detect misuse of the library.
    15         -#
    16         -# $Id: safety.test,v 1.4 2008/03/18 13:46:53 drh Exp $
    17         -
    18         -set testdir [file dirname $argv0]
    19         -source $testdir/tester.tcl
    20         -
    21         -ifcapable !debug {
    22         -  puts "Skipping safety tests since SQLITE_DEBUG is off"
    23         -  finish_test
    24         -  return
    25         -}
    26         -
    27         -# Return the UTF-8 representation of the supplied UTF-16 string $str. 
    28         -proc utf8 {str} {
    29         -  # If $str ends in two 0x00 0x00 bytes, knock these off before
    30         -  # converting to UTF-8 using TCL.
    31         -  binary scan $str \c* vals
    32         -  if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
    33         -    set str [binary format \c* [lrange $vals 0 end-2]]
    34         -  }
    35         -
    36         -  set r [encoding convertfrom unicode $str]
    37         -  return $r
    38         -}
    39         -
    40         -
    41         -do_test safety-1.1 {
    42         -  set DB [sqlite3_connection_pointer db]
    43         -  db eval {CREATE TABLE t1(a)}
    44         -  sqlite_set_magic $DB SQLITE_MAGIC_BUSY
    45         -  catchsql {
    46         -    SELECT name FROM sqlite_master;
    47         -  }
    48         -} {1 {library routine called out of sequence}}
    49         -do_test safety-1.2 {
    50         -  sqlite_set_magic $DB SQLITE_MAGIC_OPEN
    51         -  catchsql {
    52         -    SELECT name FROM sqlite_master
    53         -  }
    54         -} {0 t1}
    55         -
    56         -do_test safety-2.1 {
    57         -  proc safety_on {} "sqlite_set_magic $DB SQLITE_MAGIC_BUSY"
    58         -  db function safety_on safety_on
    59         -  catchsql {
    60         -    SELECT safety_on(), name FROM sqlite_master
    61         -  }
    62         -} {1 {library routine called out of sequence}}
    63         -ifcapable {utf16} {
    64         -  do_test safety-2.1.1 {
    65         -    utf8 [sqlite3_errmsg16 db]
    66         -  } {library routine called out of sequence}
    67         -}
    68         -do_test safety-2.2 {
    69         -  catchsql {
    70         -    SELECT 'hello'
    71         -  }
    72         -} {1 {library routine called out of sequence}}
    73         -do_test safety-2.3 {
    74         -  sqlite3_close $DB
    75         -} {SQLITE_MISUSE}
    76         -do_test safety-2.4 {
    77         -  sqlite_set_magic $DB SQLITE_MAGIC_OPEN
    78         -  execsql {
    79         -    SELECT name FROM sqlite_master
    80         -  }
    81         -} {t1}
    82         -
    83         -do_test safety-3.1 {
    84         -  set rc [catch {
    85         -    db eval {SELECT name FROM sqlite_master} {
    86         -      sqlite_set_magic $DB SQLITE_MAGIC_BUSY
    87         -    }
    88         -  } msg]
    89         -  lappend rc $msg
    90         -} {1 {library routine called out of sequence}}
    91         -sqlite_set_magic $DB SQLITE_MAGIC_OPEN
    92         -
    93         -finish_test

Changes to test/schema.test.

   372    372     db function hello {}
   373    373     db auth auth
   374    374     proc auth {args} {
   375    375       if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
   376    376       return SQLITE_OK
   377    377     }
   378    378     sqlite3_step $S
   379         -} {SQLITE_SCHEMA}
          379  +} {SQLITE_AUTH}
   380    380   
   381    381   do_test schema-13.2 {
   382    382     sqlite3_step $S
   383         -} {SQLITE_SCHEMA}
          383  +} {SQLITE_AUTH}
   384    384   
   385    385   do_test schema-13.3 {
   386    386     sqlite3_finalize $S
   387         -} {SQLITE_SCHEMA}
          387  +} {SQLITE_AUTH}
   388    388   
   389    389   }
   390    390   
   391    391   finish_test

Changes to test/select7.test.

   151    151       } [list 0 $result]
   152    152       append sql { UNION ALL SELECT 99999999}
   153    153       do_test select7-6.2 {
   154    154         catchsql $sql
   155    155       } {1 {too many terms in compound SELECT}}
   156    156     }
   157    157   }
          158  +
          159  +# This block of tests verifies that bug aa92c76cd4 is fixed.
          160  +#
          161  +do_test select7-7.1 {
          162  +  execsql {
          163  +    CREATE TABLE t3(a REAL);
          164  +    INSERT INTO t3 VALUES(44.0);
          165  +    INSERT INTO t3 VALUES(56.0);
          166  +  }
          167  +} {}
          168  +do_test select7-7.2 {
          169  +  execsql {
          170  +    pragma vdbe_trace = 0;
          171  +    SELECT (CASE WHEN a=0 THEN 0 ELSE (a + 25) / 50 END) AS categ, count(*)
          172  +    FROM t3 GROUP BY categ
          173  +  }
          174  +} {1.38 1 1.62 1}
          175  +do_test select7-7.3 {
          176  +  execsql {
          177  +    CREATE TABLE t4(a REAL);
          178  +    INSERT INTO t4 VALUES( 2.0 );
          179  +    INSERT INTO t4 VALUES( 3.0 );
          180  +  }
          181  +} {}
          182  +do_test select7-7.4 {
          183  +  execsql {
          184  +    SELECT (CASE WHEN a=0 THEN 'zero' ELSE a/2 END) AS t FROM t4 GROUP BY t;
          185  +  }
          186  +} {1.0 1.5}
          187  +do_test select7-7.5 {
          188  +  execsql { SELECT a=0, typeof(a) FROM t4 }
          189  +} {0 real 0 real}
          190  +do_test select7-7.6 {
          191  +  execsql { SELECT a=0, typeof(a) FROM t4 GROUP BY a }
          192  +} {0 real 0 real}
          193  +
          194  +do_test select7-7.7 {
          195  +  execsql {
          196  +    CREATE TABLE t5(a TEXT, b INT);
          197  +    INSERT INTO t5 VALUES(123, 456);
          198  +    SELECT typeof(a), a FROM t5 GROUP BY a HAVING a<b;
          199  +  }
          200  +} {text 123}
   158    201   
   159    202   finish_test

Added test/stmt.test.

            1  +# 2010 February 18
            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  +# The tests in this file check that SQLite uses (or does not use) a
           13  +# statement journal for various SQL statements.
           14  +#
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +
           19  +do_test stmt-1.1 {
           20  +  execsql { CREATE TABLE t1(a integer primary key, b INTEGER NOT NULL) }
           21  +} {}
           22  +
           23  +# The following tests verify the method used for the tests in this file -
           24  +# that if a statement journal is required by a statement it is opened and
           25  +# remains open until the current transaction is committed or rolled back.
           26  +#
           27  +# This only work if SQLITE_TEMP_STORE!=3
           28  +#
           29  +if {$::TEMP_STORE==3} {
           30  +  finish_test
           31  +  return
           32  +}
           33  +do_test stmt-1.2 {
           34  +  set sqlite_open_file_count
           35  +} {1}
           36  +do_test stmt-1.3 {
           37  +  execsql {
           38  +    BEGIN;
           39  +      INSERT INTO t1 VALUES(1, 1);
           40  +  }
           41  +  set sqlite_open_file_count
           42  +} {2}
           43  +do_test stmt-1.4 {
           44  +  execsql {
           45  +    INSERT INTO t1 SELECT a+1, b+1 FROM t1;
           46  +  }
           47  +  set sqlite_open_file_count
           48  +} {3}
           49  +do_test stmt-1.5 {
           50  +  execsql COMMIT
           51  +  set sqlite_open_file_count
           52  +} {1}
           53  +do_test stmt-1.6 {
           54  +  execsql {
           55  +    BEGIN;
           56  +      INSERT INTO t1 SELECT a+2, b+2 FROM t1;
           57  +  }
           58  +  set sqlite_open_file_count
           59  +} {3}
           60  +do_test stmt-1.7 {
           61  +  execsql COMMIT
           62  +  set sqlite_open_file_count
           63  +} {1}
           64  +
           65  +
           66  +proc filecount {testname sql expected} {
           67  +  uplevel [list do_test $testname [subst -nocommand {
           68  +    execsql BEGIN
           69  +    execsql { $sql }
           70  +    set ret [set sqlite_open_file_count]
           71  +    execsql ROLLBACK
           72  +    set ret
           73  +  }] $expected]
           74  +}
           75  +
           76  +filecount stmt-2.1 { INSERT INTO t1 VALUES(5, 5)  } 2
           77  +filecount stmt-2.2 { REPLACE INTO t1 VALUES(5, 5) } 2
           78  +filecount stmt-2.3 { INSERT INTO t1 SELECT 5, 5   } 3
           79  +
           80  +do_test stmt-2.4 {
           81  +  execsql { CREATE INDEX i1 ON t1(b) }
           82  +} {}
           83  +filecount stmt-2.5 { REPLACE INTO t1 VALUES(5, 5) } 3
           84  +
           85  +finish_test

Added test/vacuum4.test.

            1  +# 2010 February 21
            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  +# This file implements a test of ticket [da1151f97df244a1]:  An
           13  +# assertion fault while VACUUMing an auto_vacuumed database with
           14  +# large schema.
           15  +#
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +
           20  +# If the VACUUM statement is disabled in the current build, skip all
           21  +# the tests in this file.
           22  +#
           23  +ifcapable !vacuum {
           24  +  finish_test
           25  +  return
           26  +}
           27  +
           28  +do_test vacuum4-1.1 {
           29  +  db eval {
           30  +    PRAGMA auto_vacuum=FULL;
           31  +    CREATE TABLE t1(
           32  +      c000, c001, c002, c003, c004, c005, c006, c007, c008, c009,
           33  +      c010, c011, c012, c013, c014, c015, c016, c017, c018, c019,
           34  +      c020, c021, c022, c023, c024, c025, c026, c027, c028, c029,
           35  +      c030, c031, c032, c033, c034, c035, c036, c037, c038, c039,
           36  +      c040, c041, c042, c043, c044, c045, c046, c047, c048, c049,
           37  +      c050, c051, c052, c053, c054, c055, c056, c057, c058, c059,
           38  +      c060, c061, c062, c063, c064, c065, c066, c067, c068, c069,
           39  +      c070, c071, c072, c073, c074, c075, c076, c077, c078, c079,
           40  +      c080, c081, c082, c083, c084, c085, c086, c087, c088, c089,
           41  +      c090, c091, c092, c093, c094, c095, c096, c097, c098, c099,
           42  +      c100, c101, c102, c103, c104, c105, c106, c107, c108, c109,
           43  +      c110, c111, c112, c113, c114, c115, c116, c117, c118, c119,
           44  +      c120, c121, c122, c123, c124, c125, c126, c127, c128, c129,
           45  +      c130, c131, c132, c133, c134, c135, c136, c137, c138, c139,
           46  +      c140, c141, c142, c143, c144, c145, c146, c147, c148, c149
           47  +    );
           48  +    CREATE TABLE t2(
           49  +      c000, c001, c002, c003, c004, c005, c006, c007, c008, c009,
           50  +      c010, c011, c012, c013, c014, c015, c016, c017, c018, c019,
           51  +      c020, c021, c022, c023, c024, c025, c026, c027, c028, c029,
           52  +      c030, c031, c032, c033, c034, c035, c036, c037, c038, c039,
           53  +      c040, c041, c042, c043, c044, c045, c046, c047, c048, c049,
           54  +      c050, c051, c052, c053, c054, c055, c056, c057, c058, c059,
           55  +      c060, c061, c062, c063, c064, c065, c066, c067, c068, c069,
           56  +      c070, c071, c072, c073, c074, c075, c076, c077, c078, c079,
           57  +      c080, c081, c082, c083, c084, c085, c086, c087, c088, c089,
           58  +      c090, c091, c092, c093, c094, c095, c096, c097, c098, c099,
           59  +      c100, c101, c102, c103, c104, c105, c106, c107, c108, c109,
           60  +      c110, c111, c112, c113, c114, c115, c116, c117, c118, c119,
           61  +      c120, c121, c122, c123, c124, c125, c126, c127, c128, c129,
           62  +      c130, c131, c132, c133, c134, c135, c136, c137, c138, c139,
           63  +      c140, c141, c142, c143, c144, c145, c146, c147, c148, c149
           64  +    );
           65  +    VACUUM;
           66  +  }
           67  +} {}

Changes to test/vtabA.test.

   127    127     analyse_parse {(a HiDden, b HIDDEN, c hidden)} {a b c}
   128    128   } {{} {} {} {}}
   129    129   
   130    130   do_test vtabA-2.4 {
   131    131     analyse_parse {(a whatelse can i hidden test, b HIDDEN hidden)} {a b}
   132    132   } {{} {whatelse can i test} hidden}
   133    133   
          134  +
          135  +# Ticket [d2f02d37f52bfe23e421f2c60fbb8586ac76ff01]:
          136  +# assertion failure on an UPDATE involving two virtual tables.
          137  +#
          138  +do_test vtabA-3.1 {
          139  +  db eval {
          140  +    DROP TABLE IF EXISTS t1;
          141  +    DROP TABLE IF EXISTS t2;
          142  +    CREATE TABLE t1(a,b);
          143  +    INSERT INTO t1 VALUES(1,2);
          144  +    CREATE TABLE t2(x,y);
          145  +    INSERT INTO t2 VALUES(3,4);
          146  +    CREATE VIRTUAL TABLE vt1 USING echo(t1);
          147  +    CREATE VIRTUAL TABLE vt2 USING echo(t2);
          148  +    UPDATE vt2 SET x=(SELECT a FROM vt1 WHERE b=2) WHERE y=4;
          149  +    SELECT * FROM t2;
          150  +  }
          151  +} {1 4}
          152  +
   134    153   finish_test

Changes to test/where6.test.

   123    123   } {1 3 1 3}
   124    124   do_test where6-2.14 {
   125    125     execsql {
   126    126       SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
   127    127     }
   128    128   } {1 3 1 3}
   129    129   
          130  +# Ticket [ebdbadade5b]:
          131  +# If the ON close on a LEFT JOIN is of the form x=y where both x and y
          132  +# are indexed columns on tables to left of the join, then do not use that 
          133  +# term with indices to either table.
          134  +#
          135  +do_test where6-3.1 {
          136  +  db eval {
          137  +    CREATE TABLE t4(x UNIQUE);
          138  +    INSERT INTO t4 VALUES('abc');
          139  +    INSERT INTO t4 VALUES('def');
          140  +    INSERT INTO t4 VALUES('ghi');
          141  +    CREATE TABLE t5(a, b, c, PRIMARY KEY(a,b));
          142  +    INSERT INTO t5 VALUES('abc','def',123);
          143  +    INSERT INTO t5 VALUES('def','ghi',456);
          144  +
          145  +    SELECT t4a.x, t4b.x, t5.c, t6.v
          146  +      FROM t4 AS t4a
          147  +           INNER JOIN t4 AS t4b
          148  +           LEFT JOIN t5 ON t5.a=t4a.x AND t5.b=t4b.x
          149  +           LEFT JOIN (SELECT 1 AS v) AS t6 ON t4a.x=t4b.x
          150  +     ORDER BY 1, 2, 3;
          151  +  }
          152  +} {abc abc {} 1 abc def 123 {} abc ghi {} {} def abc {} {} def def {} 1 def ghi 456 {} ghi abc {} {} ghi def {} {} ghi ghi {} 1}
          153  +
   130    154   finish_test

Changes to tool/lemon.c.

    29     29   #define PRIVATE
    30     30   
    31     31   #ifdef TEST
    32     32   #define MAXRHS 5       /* Set low to exercise exception code */
    33     33   #else
    34     34   #define MAXRHS 1000
    35     35   #endif
           36  +
           37  +static const char **made_files = NULL;
           38  +static int made_files_count = 0;
           39  +static int successful_exit = 0;
           40  +static void LemonAtExit(void)
           41  +{
           42  +    /* if we failed, delete (most) files we made, to unconfuse build tools. */
           43  +    int i;
           44  +    for (i = 0; i < made_files_count; i++) {
           45  +        if (!successful_exit) {
           46  +            remove(made_files[i]);
           47  +        }
           48  +    }
           49  +    free(made_files);
           50  +    made_files_count = 0;
           51  +    made_files = NULL;
           52  +}
    36     53   
    37     54   static char *msort(char*,char**,int(*)(const char*,const char*));
    38     55   
    39     56   /*
    40     57   ** Compilers are getting increasingly pedantic about type conversions
    41     58   ** as C evolves ever closer to Ada....  To work around the latest problems
    42     59   ** we have to define the following variant of strlen().
    43     60   */
    44     61   #define lemonStrlen(X)   ((int)strlen(X))
           62  +
           63  +/* a few forward declarations... */
           64  +struct rule;
           65  +struct lemon;
           66  +struct action;
    45     67   
    46     68   static struct action *Action_new(void);
    47     69   static struct action *Action_sort(struct action *);
    48     70   
    49     71   /********** From the file "build.h" ************************************/
    50     72   void FindRulePrecedences();
    51     73   void FindFirstSets();
    52     74   void FindStates();
    53     75   void FindLinks();
    54     76   void FindFollowSets();
    55     77   void FindActions();
    56     78   
    57     79   /********* From the file "configlist.h" *********************************/
    58         -void Configlist_init(/* void */);
    59         -struct config *Configlist_add(/* struct rule *, int */);
    60         -struct config *Configlist_addbasis(/* struct rule *, int */);
    61         -void Configlist_closure(/* void */);
    62         -void Configlist_sort(/* void */);
    63         -void Configlist_sortbasis(/* void */);
    64         -struct config *Configlist_return(/* void */);
    65         -struct config *Configlist_basis(/* void */);
    66         -void Configlist_eat(/* struct config * */);
    67         -void Configlist_reset(/* void */);
           80  +void Configlist_init(void);
           81  +struct config *Configlist_add(struct rule *, int);
           82  +struct config *Configlist_addbasis(struct rule *, int);
           83  +void Configlist_closure(struct lemon *);
           84  +void Configlist_sort(void);
           85  +void Configlist_sortbasis(void);
           86  +struct config *Configlist_return(void);
           87  +struct config *Configlist_basis(void);
           88  +void Configlist_eat(struct config *);
           89  +void Configlist_reset(void);
    68     90   
    69     91   /********* From the file "error.h" ***************************************/
    70     92   void ErrorMsg(const char *, int,const char *, ...);
    71     93   
    72     94   /****** From the file "option.h" ******************************************/
           95  +enum option_type { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
           96  +         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
    73     97   struct s_options {
    74         -  enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
    75         -         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
    76         -  char *label;
           98  +  enum option_type type;
           99  +  const char *label;
    77    100     char *arg;
    78         -  char *message;
          101  +  const char *message;
    79    102   };
    80         -int    OptInit(/* char**,struct s_options*,FILE* */);
    81         -int    OptNArgs(/* void */);
    82         -char  *OptArg(/* int */);
    83         -void   OptErr(/* int */);
    84         -void   OptPrint(/* void */);
          103  +int    OptInit(char**,struct s_options*,FILE*);
          104  +int    OptNArgs(void);
          105  +char  *OptArg(int);
          106  +void   OptErr(int);
          107  +void   OptPrint(void);
    85    108   
    86    109   /******** From the file "parse.h" *****************************************/
    87         -void Parse(/* struct lemon *lemp */);
          110  +void Parse(struct lemon *lemp);
    88    111   
    89    112   /********* From the file "plink.h" ***************************************/
    90         -struct plink *Plink_new(/* void */);
    91         -void Plink_add(/* struct plink **, struct config * */);
    92         -void Plink_copy(/* struct plink **, struct plink * */);
    93         -void Plink_delete(/* struct plink * */);
          113  +struct plink *Plink_new(void);
          114  +void Plink_add(struct plink **, struct config *);
          115  +void Plink_copy(struct plink **, struct plink *);
          116  +void Plink_delete(struct plink *);
    94    117   
    95    118   /********** From the file "report.h" *************************************/
    96         -void Reprint(/* struct lemon * */);
    97         -void ReportOutput(/* struct lemon * */);
    98         -void ReportTable(/* struct lemon * */);
    99         -void ReportHeader(/* struct lemon * */);
   100         -void CompressTables(/* struct lemon * */);
   101         -void ResortStates(/* struct lemon * */);
          119  +void Reprint(struct lemon *);
          120  +void ReportOutput(struct lemon *);
          121  +void ReportTable(struct lemon *, int);
          122  +void ReportHeader(struct lemon *);
          123  +void CompressTables(struct lemon *);
          124  +void ResortStates(struct lemon *);
   102    125   
   103    126   /********** From the file "set.h" ****************************************/
   104         -void  SetSize(/* int N */);             /* All sets will be of size N */
   105         -char *SetNew(/* void */);               /* A new set for element 0..N */
   106         -void  SetFree(/* char* */);             /* Deallocate a set */
          127  +void  SetSize(int);             /* All sets will be of size N */
          128  +char *SetNew(void);               /* A new set for element 0..N */
          129  +void  SetFree(char*);             /* Deallocate a set */
   107    130   
   108         -int SetAdd(/* char*,int */);            /* Add element to a set */
   109         -int SetUnion(/* char *A,char *B */);    /* A <- A U B, thru element N */
   110         -
          131  +char *SetNew(void);               /* A new set for element 0..N */
          132  +int SetAdd(char*,int);            /* Add element to a set */
          133  +int SetUnion(char *,char *);    /* A <- A U B, thru element N */
   111    134   #define SetFind(X,Y) (X[Y])       /* True if Y is in set X */
   112    135   
   113    136   /********** From the file "struct.h" *************************************/
   114    137   /*
   115    138   ** Principal data structures for the LEMON parser generator.
   116    139   */
   117    140   
   118    141   typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
   119    142   
   120    143   /* Symbols (terminals and nonterminals) of the grammar are stored
   121    144   ** in the following: */
   122         -struct symbol {
   123         -  char *name;              /* Name of the symbol */
   124         -  int index;               /* Index number for this symbol */
   125         -  enum {
   126         -    TERMINAL,
   127         -    NONTERMINAL,
   128         -    MULTITERMINAL
   129         -  } type;                  /* Symbols are all either TERMINALS or NTs */
   130         -  struct rule *rule;       /* Linked list of rules of this (if an NT) */
   131         -  struct symbol *fallback; /* fallback token in case this token doesn't parse */
   132         -  int prec;                /* Precedence if defined (-1 otherwise) */
   133         -  enum e_assoc {
          145  +enum symbol_type {
          146  +  TERMINAL,
          147  +  NONTERMINAL,
          148  +  MULTITERMINAL
          149  +};
          150  +enum e_assoc {
   134    151       LEFT,
   135    152       RIGHT,
   136    153       NONE,
   137    154       UNK
   138         -  } assoc;                 /* Associativity if precedence is defined */
          155  +};
          156  +struct symbol {
          157  +  const char *name;        /* Name of the symbol */
          158  +  int index;               /* Index number for this symbol */
          159  +  enum symbol_type type;   /* Symbols are all either TERMINALS or NTs */
          160  +  struct rule *rule;       /* Linked list of rules of this (if an NT) */
          161  +  struct symbol *fallback; /* fallback token in case this token doesn't parse */
          162  +  int prec;                /* Precedence if defined (-1 otherwise) */
          163  +  enum e_assoc assoc;      /* Associativity if precedence is defined */
   139    164     char *firstset;          /* First-set for all rules of this symbol */
   140    165     Boolean lambda;          /* True if NT and can generate an empty string */
   141    166     int useCnt;              /* Number of times used */
   142    167     char *destructor;        /* Code which executes whenever this symbol is
   143    168                              ** popped from the stack during error processing */
   144    169     int destLineno;          /* Line number for start of destructor */
   145    170     char *datatype;          /* The data type of information held by this
................................................................................
   152    177     struct symbol **subsym;  /* Array of constituent symbols */
   153    178   };
   154    179   
   155    180   /* Each production rule in the grammar is stored in the following
   156    181   ** structure.  */
   157    182   struct rule {
   158    183     struct symbol *lhs;      /* Left-hand side of the rule */
   159         -  char *lhsalias;          /* Alias for the LHS (NULL if none) */
          184  +  const char *lhsalias;    /* Alias for the LHS (NULL if none) */
   160    185     int lhsStart;            /* True if left-hand side is the start symbol */
   161    186     int ruleline;            /* Line number for the rule */
   162    187     int nrhs;                /* Number of RHS symbols */
   163    188     struct symbol **rhs;     /* The RHS symbols */
   164         -  char **rhsalias;         /* An alias for each RHS symbol (NULL if none) */
          189  +  const char **rhsalias;   /* An alias for each RHS symbol (NULL if none) */
   165    190     int line;                /* Line number at which code begins */
   166         -  char *code;              /* The code executed when this rule is reduced */
          191  +  const char *code;        /* The code executed when this rule is reduced */
   167    192     struct symbol *precsym;  /* Precedence symbol for this rule */
   168    193     int index;               /* An index number for this rule */
   169    194     Boolean canReduce;       /* True if this rule is ever reduced */
   170    195     struct rule *nextlhs;    /* Next rule with the same LHS */
   171    196     struct rule *next;       /* Next rule in the global list */
   172    197   };
   173    198   
   174    199   /* A configuration is a production rule of the grammar together with
   175    200   ** a mark (dot) showing how much of that rule has been processed so far.
   176    201   ** Configurations also contain a follow-set which is a list of terminal
   177    202   ** symbols which are allowed to immediately follow the end of the rule.
   178    203   ** Every configuration is recorded as an instance of the following: */
          204  +enum cfgstatus {
          205  +  COMPLETE,
          206  +  INCOMPLETE
          207  +};
   179    208   struct config {
   180    209     struct rule *rp;         /* The rule upon which the configuration is based */
   181    210     int dot;                 /* The parse point */
   182    211     char *fws;               /* Follow-set for this configuration only */
   183    212     struct plink *fplp;      /* Follow-set forward propagation links */
   184    213     struct plink *bplp;      /* Follow-set backwards propagation links */
   185    214     struct state *stp;       /* Pointer to state which contains this */
   186         -  enum {
   187         -    COMPLETE,              /* The status is used during followset and */
   188         -    INCOMPLETE             /*    shift computations */
   189         -  } status;
          215  +  enum cfgstatus status;   /* used during followset and shift computations */
   190    216     struct config *next;     /* Next configuration in the state */
   191    217     struct config *bp;       /* The next basis configuration */
   192    218   };
          219  +
          220  +enum e_action {
          221  +  SHIFT,
          222  +  ACCEPT,
          223  +  REDUCE,
          224  +  ERROR,
          225  +  SSCONFLICT,              /* A shift/shift conflict */
          226  +  SRCONFLICT,              /* Was a reduce, but part of a conflict */
          227  +  RRCONFLICT,              /* Was a reduce, but part of a conflict */
          228  +  SH_RESOLVED,             /* Was a shift.  Precedence resolved conflict */
          229  +  RD_RESOLVED,             /* Was reduce.  Precedence resolved conflict */
          230  +  NOT_USED                 /* Deleted by compression */
          231  +};
   193    232   
   194    233   /* Every shift or reduce operation is stored as one of the following */
   195    234   struct action {
   196    235     struct symbol *sp;       /* The look-ahead symbol */
   197         -  enum e_action {
   198         -    SHIFT,
   199         -    ACCEPT,
   200         -    REDUCE,
   201         -    ERROR,
   202         -    SSCONFLICT,              /* A shift/shift conflict */
   203         -    SRCONFLICT,              /* Was a reduce, but part of a conflict */
   204         -    RRCONFLICT,              /* Was a reduce, but part of a conflict */
   205         -    SH_RESOLVED,             /* Was a shift.  Precedence resolved conflict */
   206         -    RD_RESOLVED,             /* Was reduce.  Precedence resolved conflict */
   207         -    NOT_USED                 /* Deleted by compression */
   208         -  } type;
          236  +  enum e_action type;
   209    237     union {
   210    238       struct state *stp;     /* The new state, if a shift */
   211    239       struct rule *rp;       /* The rule, if a reduce */
   212    240     } x;
   213    241     struct action *next;     /* Next action for this state */
   214    242     struct action *collide;  /* Next action with the same hash */
   215    243   };
................................................................................
   288    316   ** by the associative array code building program "aagen".
   289    317   ** Do not edit this file!  Instead, edit the specification
   290    318   ** file, then rerun aagen.
   291    319   */
   292    320   /*
   293    321   ** Code for processing tables in the LEMON parser generator.
   294    322   */
   295         -
   296    323   /* Routines for handling a strings */
   297    324   
   298         -char *Strsafe();
          325  +const char *Strsafe(const char *);
   299    326   
   300         -void Strsafe_init(/* void */);
   301         -int Strsafe_insert(/* char * */);
   302         -char *Strsafe_find(/* char * */);
          327  +void Strsafe_init(void);
          328  +int Strsafe_insert(const char *);
          329  +const char *Strsafe_find(const char *);
   303    330   
   304    331   /* Routines for handling symbols of the grammar */
   305    332   
   306         -struct symbol *Symbol_new();
   307         -int Symbolcmpp(/* struct symbol **, struct symbol ** */);
   308         -void Symbol_init(/* void */);
   309         -int Symbol_insert(/* struct symbol *, char * */);
   310         -struct symbol *Symbol_find(/* char * */);
   311         -struct symbol *Symbol_Nth(/* int */);
   312         -int Symbol_count(/*  */);
   313         -struct symbol **Symbol_arrayof(/*  */);
          333  +struct symbol *Symbol_new(const char *);
          334  +int Symbolcmpp(const void *, const void *);
          335  +void Symbol_init(void);
          336  +int Symbol_insert(struct symbol *, const char *);
          337  +struct symbol *Symbol_find(const char *);
          338  +struct symbol *Symbol_Nth(int);
          339  +int Symbol_count(void);
          340  +struct symbol **Symbol_arrayof(void);
   314    341   
   315    342   /* Routines to manage the state table */
   316    343   
   317         -int Configcmp(/* struct config *, struct config * */);
   318         -struct state *State_new();
   319         -void State_init(/* void */);
   320         -int State_insert(/* struct state *, struct config * */);
   321         -struct state *State_find(/* struct config * */);
          344  +int Configcmp(const char *, const char *);
          345  +struct state *State_new(void);
          346  +void State_init(void);
          347  +int State_insert(struct state *, struct config *);
          348  +struct state *State_find(struct config *);
   322    349   struct state **State_arrayof(/*  */);
   323    350   
   324    351   /* Routines used for efficiency in Configlist_add */
   325    352   
   326         -void Configtable_init(/* void */);
   327         -int Configtable_insert(/* struct config * */);
   328         -struct config *Configtable_find(/* struct config * */);
   329         -void Configtable_clear(/* int(*)(struct config *) */);
          353  +void Configtable_init(void);
          354  +int Configtable_insert(struct config *);
          355  +struct config *Configtable_find(struct config *);
          356  +void Configtable_clear(int(*)(struct config *));
          357  +
   330    358   /****************** From the file "action.c" *******************************/
   331    359   /*
   332    360   ** Routines processing parser actions in the LEMON parser generator.
   333    361   */
   334    362   
   335    363   /* Allocate a new parser action */
   336    364   static struct action *Action_new(void){
   337    365     static struct action *freelist = 0;
   338         -  struct action *new;
          366  +  struct action *newaction;
   339    367   
   340    368     if( freelist==0 ){
   341    369       int i;
   342    370       int amt = 100;
   343    371       freelist = (struct action *)calloc(amt, sizeof(struct action));
   344    372       if( freelist==0 ){
   345    373         fprintf(stderr,"Unable to allocate memory for a new parser action.");
   346    374         exit(1);
   347    375       }
   348    376       for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
   349    377       freelist[amt-1].next = 0;
   350    378     }
   351         -  new = freelist;
          379  +  newaction = freelist;
   352    380     freelist = freelist->next;
   353         -  return new;
          381  +  return newaction;
   354    382   }
   355    383   
   356    384   /* Compare two actions for sorting purposes.  Return negative, zero, or
   357    385   ** positive if the first action is less than, equal to, or greater than
   358    386   ** the first
   359    387   */
   360    388   static int actioncmp(
................................................................................
   366    394     if( rc==0 ){
   367    395       rc = (int)ap1->type - (int)ap2->type;
   368    396     }
   369    397     if( rc==0 && ap1->type==REDUCE ){
   370    398       rc = ap1->x.rp->index - ap2->x.rp->index;
   371    399     }
   372    400     if( rc==0 ){
   373         -    rc = ap2 - ap1;
          401  +    rc = (int) (ap2 - ap1);
   374    402     }
   375    403     return rc;
   376    404   }
   377    405   
   378    406   /* Sort parser actions */
   379    407   static struct action *Action_sort(
   380    408     struct action *ap
   381    409   ){
   382    410     ap = (struct action *)msort((char *)ap,(char **)&ap->next,
   383    411                                 (int(*)(const char*,const char*))actioncmp);
   384    412     return ap;
   385    413   }
   386    414   
   387         -void Action_add(app,type,sp,arg)
   388         -struct action **app;
   389         -enum e_action type;
   390         -struct symbol *sp;
   391         -char *arg;
   392         -{
   393         -  struct action *new;
   394         -  new = Action_new();
   395         -  new->next = *app;
   396         -  *app = new;
   397         -  new->type = type;
   398         -  new->sp = sp;
          415  +void Action_add(
          416  +  struct action **app,
          417  +  enum e_action type,
          418  +  struct symbol *sp,
          419  +  char *arg
          420  +){
          421  +  struct action *newaction;
          422  +  newaction = Action_new();
          423  +  newaction->next = *app;
          424  +  *app = newaction;
          425  +  newaction->type = type;
          426  +  newaction->sp = sp;
   399    427     if( type==SHIFT ){
   400         -    new->x.stp = (struct state *)arg;
          428  +    newaction->x.stp = (struct state *)arg;
   401    429     }else{
   402         -    new->x.rp = (struct rule *)arg;
          430  +    newaction->x.rp = (struct rule *)arg;
   403    431     }
   404    432   }
   405    433   /********************** New code to implement the "acttab" module ***********/
   406    434   /*
   407    435   ** This module implements routines use to construct the yy_action[] table.
   408    436   */
   409    437   
................................................................................
   423    451   ** All actions associated with a single state_number are first entered
   424    452   ** into aLookahead[] using multiple calls to acttab_action().  Then the 
   425    453   ** actions for that single state_number are placed into the aAction[] 
   426    454   ** array with a single call to acttab_insert().  The acttab_insert() call
   427    455   ** also resets the aLookahead[] array in preparation for the next
   428    456   ** state number.
   429    457   */
          458  +struct lookahead_action {
          459  +  int lookahead;             /* Value of the lookahead token */
          460  +  int action;                /* Action to take on the given lookahead */
          461  +};
   430    462   typedef struct acttab acttab;
   431    463   struct acttab {
   432    464     int nAction;                 /* Number of used slots in aAction[] */
   433    465     int nActionAlloc;            /* Slots allocated for aAction[] */
   434         -  struct {
   435         -    int lookahead;             /* Value of the lookahead token */
   436         -    int action;                /* Action to take on the given lookahead */
   437         -  } *aAction,                  /* The yy_action[] table under construction */
          466  +  struct lookahead_action
          467  +    *aAction,                  /* The yy_action[] table under construction */
   438    468       *aLookahead;               /* A single new transaction set */
   439    469     int mnLookahead;             /* Minimum aLookahead[].lookahead */
   440    470     int mnAction;                /* Action associated with mnLookahead */
   441    471     int mxLookahead;             /* Maximum aLookahead[].lookahead */
   442    472     int nLookahead;              /* Used slots in aLookahead[] */
   443    473     int nLookaheadAlloc;         /* Slots allocated in aLookahead[] */
   444    474   };
................................................................................
   457    487     free( p->aAction );
   458    488     free( p->aLookahead );
   459    489     free( p );
   460    490   }
   461    491   
   462    492   /* Allocate a new acttab structure */
   463    493   acttab *acttab_alloc(void){
   464         -  acttab *p = calloc( 1, sizeof(*p) );
          494  +  acttab *p = (acttab *) calloc( 1, sizeof(*p) );
   465    495     if( p==0 ){
   466    496       fprintf(stderr,"Unable to allocate memory for a new acttab.");
   467    497       exit(1);
   468    498     }
   469    499     memset(p, 0, sizeof(*p));
   470    500     return p;
   471    501   }
................................................................................
   474    504   **
   475    505   ** This routine is called once for each lookahead for a particular
   476    506   ** state.
   477    507   */
   478    508   void acttab_action(acttab *p, int lookahead, int action){
   479    509     if( p->nLookahead>=p->nLookaheadAlloc ){
   480    510       p->nLookaheadAlloc += 25;
   481         -    p->aLookahead = realloc( p->aLookahead,
          511  +    p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
   482    512                                sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
   483    513       if( p->aLookahead==0 ){
   484    514         fprintf(stderr,"malloc failed\n");
   485    515         exit(1);
   486    516       }
   487    517     }
   488    518     if( p->nLookahead==0 ){
................................................................................
   516    546     ** in the worst case.  The worst case occurs if the transaction set
   517    547     ** must be appended to the current action table
   518    548     */
   519    549     n = p->mxLookahead + 1;
   520    550     if( p->nAction + n >= p->nActionAlloc ){
   521    551       int oldAlloc = p->nActionAlloc;
   522    552       p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
   523         -    p->aAction = realloc( p->aAction,
          553  +    p->aAction = (struct lookahead_action *) realloc( p->aAction,
   524    554                             sizeof(p->aAction[0])*p->nActionAlloc);
   525    555       if( p->aAction==0 ){
   526    556         fprintf(stderr,"malloc failed\n");
   527    557         exit(1);
   528    558       }
   529    559       for(i=oldAlloc; i<p->nActionAlloc; i++){
   530    560         p->aAction[i].lookahead = -1;
................................................................................
   614    644   ** Those rules which have a precedence symbol coded in the input
   615    645   ** grammar using the "[symbol]" construct will already have the
   616    646   ** rp->precsym field filled.  Other rules take as their precedence
   617    647   ** symbol the first RHS symbol with a defined precedence.  If there
   618    648   ** are not RHS symbols with a defined precedence, the precedence
   619    649   ** symbol field is left blank.
   620    650   */
   621         -void FindRulePrecedences(xp)
   622         -struct lemon *xp;
          651  +void FindRulePrecedences(struct lemon *xp)
   623    652   {
   624    653     struct rule *rp;
   625    654     for(rp=xp->rule; rp; rp=rp->next){
   626    655       if( rp->precsym==0 ){
   627    656         int i, j;
   628    657         for(i=0; i<rp->nrhs && rp->precsym==0; i++){
   629    658           struct symbol *sp = rp->rhs[i];
................................................................................
   644    673   }
   645    674   
   646    675   /* Find all nonterminals which will generate the empty string.
   647    676   ** Then go back and compute the first sets of every nonterminal.
   648    677   ** The first set is the set of all terminal symbols which can begin
   649    678   ** a string generated by that nonterminal.
   650    679   */
   651         -void FindFirstSets(lemp)
   652         -struct lemon *lemp;
          680  +void FindFirstSets(struct lemon *lemp)
   653    681   {
   654    682     int i, j;
   655    683     struct rule *rp;
   656    684     int progress;
   657    685   
   658    686     for(i=0; i<lemp->nsymbol; i++){
   659    687       lemp->symbols[i]->lambda = LEMON_FALSE;
................................................................................
   706    734     return;
   707    735   }
   708    736   
   709    737   /* Compute all LR(0) states for the grammar.  Links
   710    738   ** are added to between some states so that the LR(1) follow sets
   711    739   ** can be computed later.
   712    740   */
   713         -PRIVATE struct state *getstate(/* struct lemon * */);  /* forward reference */
   714         -void FindStates(lemp)
   715         -struct lemon *lemp;
          741  +PRIVATE struct state *getstate(struct lemon *);  /* forward reference */
          742  +void FindStates(struct lemon *lemp)
   716    743   {
   717    744     struct symbol *sp;
   718    745     struct rule *rp;
   719    746   
   720    747     Configlist_init();
   721    748   
   722    749     /* Find the start symbol */
................................................................................
   766    793     (void)getstate(lemp);
   767    794     return;
   768    795   }
   769    796   
   770    797   /* Return a pointer to a state which is described by the configuration
   771    798   ** list which has been built from calls to Configlist_add.
   772    799   */
   773         -PRIVATE void buildshifts(/* struct lemon *, struct state * */); /* Forwd ref */
   774         -PRIVATE struct state *getstate(lemp)
   775         -struct lemon *lemp;
          800  +PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
          801  +PRIVATE struct state *getstate(struct lemon *lemp)
   776    802   {
   777    803     struct config *cfp, *bp;
   778    804     struct state *stp;
   779    805   
   780    806     /* Extract the sorted basis of the new state.  The basis was constructed
   781    807     ** by prior calls to "Configlist_addbasis()". */
   782    808     Configlist_sortbasis();
................................................................................
   812    838     }
   813    839     return stp;
   814    840   }
   815    841   
   816    842   /*
   817    843   ** Return true if two symbols are the same.
   818    844   */
   819         -int same_symbol(a,b)
   820         -struct symbol *a;
   821         -struct symbol *b;
          845  +int same_symbol(struct symbol *a, struct symbol *b)
   822    846   {
   823    847     int i;
   824    848     if( a==b ) return 1;
   825    849     if( a->type!=MULTITERMINAL ) return 0;
   826    850     if( b->type!=MULTITERMINAL ) return 0;
   827    851     if( a->nsubsym!=b->nsubsym ) return 0;
   828    852     for(i=0; i<a->nsubsym; i++){
................................................................................
   830    854     }
   831    855     return 1;
   832    856   }
   833    857   
   834    858   /* Construct all successor states to the given state.  A "successor"
   835    859   ** state is any state which can be reached by a shift action.
   836    860   */
   837         -PRIVATE void buildshifts(lemp,stp)
   838         -struct lemon *lemp;
   839         -struct state *stp;     /* The state from which successors are computed */
          861  +PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
   840    862   {
   841    863     struct config *cfp;  /* For looping thru the config closure of "stp" */
   842    864     struct config *bcfp; /* For the inner loop on config closure of "stp" */
   843         -  struct config *new;  /* */
          865  +  struct config *newcfg;  /* */
   844    866     struct symbol *sp;   /* Symbol following the dot in configuration "cfp" */
   845    867     struct symbol *bsp;  /* Symbol following the dot in configuration "bcfp" */
   846    868     struct state *newstp; /* A pointer to a successor state */
   847    869   
   848    870     /* Each configuration becomes complete after it contibutes to a successor
   849    871     ** state.  Initially, all configurations are incomplete */
   850    872     for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
................................................................................
   861    883       ** construction but with the dot shifted one symbol to the right. */
   862    884       for(bcfp=cfp; bcfp; bcfp=bcfp->next){
   863    885         if( bcfp->status==COMPLETE ) continue;    /* Already used */
   864    886         if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
   865    887         bsp = bcfp->rp->rhs[bcfp->dot];           /* Get symbol after dot */
   866    888         if( !same_symbol(bsp,sp) ) continue;      /* Must be same as for "cfp" */
   867    889         bcfp->status = COMPLETE;                  /* Mark this config as used */
   868         -      new = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
   869         -      Plink_add(&new->bplp,bcfp);
          890  +      newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
          891  +      Plink_add(&newcfg->bplp,bcfp);
   870    892       }
   871    893   
   872    894       /* Get a pointer to the state described by the basis configuration set
   873    895       ** constructed in the preceding loop */
   874    896       newstp = getstate(lemp);
   875    897   
   876    898       /* The state "newstp" is reached from the state "stp" by a shift action
................................................................................
   885    907       }
   886    908     }
   887    909   }
   888    910   
   889    911   /*
   890    912   ** Construct the propagation links
   891    913   */
   892         -void FindLinks(lemp)
   893         -struct lemon *lemp;
          914  +void FindLinks(struct lemon *lemp)
   894    915   {
   895    916     int i;
   896    917     struct config *cfp, *other;
   897    918     struct state *stp;
   898    919     struct plink *plp;
   899    920   
   900    921     /* Housekeeping detail:
................................................................................
   921    942   }
   922    943   
   923    944   /* Compute all followsets.
   924    945   **
   925    946   ** A followset is the set of all symbols which can come immediately
   926    947   ** after a configuration.
   927    948   */
   928         -void FindFollowSets(lemp)
   929         -struct lemon *lemp;
          949  +void FindFollowSets(struct lemon *lemp)
   930    950   {
   931    951     int i;
   932    952     struct config *cfp;
   933    953     struct plink *plp;
   934    954     int progress;
   935    955     int change;
   936    956   
................................................................................
   954    974   	}
   955    975           cfp->status = COMPLETE;
   956    976         }
   957    977       }
   958    978     }while( progress );
   959    979   }
   960    980   
   961         -static int resolve_conflict();
          981  +static int resolve_conflict(struct action *,struct action *, struct symbol *);
   962    982   
   963    983   /* Compute the reduce actions, and resolve conflicts.
   964    984   */
   965         -void FindActions(lemp)
   966         -struct lemon *lemp;
          985  +void FindActions(struct lemon *lemp)
   967    986   {
   968    987     int i,j;
   969    988     struct config *cfp;
   970    989     struct state *stp;
   971    990     struct symbol *sp;
   972    991     struct rule *rp;
   973    992   
................................................................................
  1042   1061   **   is not associated with the error rule.  If neither or both
  1043   1062   **   actions are associated with an error rule, then try to
  1044   1063   **   use precedence to resolve the conflict.
  1045   1064   **
  1046   1065   ** If either action is a SHIFT, then it must be apx.  This
  1047   1066   ** function won't work if apx->type==REDUCE and apy->type==SHIFT.
  1048   1067   */
  1049         -static int resolve_conflict(apx,apy,errsym)
  1050         -struct action *apx;
  1051         -struct action *apy;
  1052         -struct symbol *errsym;   /* The error symbol (if defined.  NULL otherwise) */
  1053         -{
         1068  +static int resolve_conflict(
         1069  +  struct action *apx,
         1070  +  struct action *apy,
         1071  +  struct symbol *errsym   /* The error symbol (if defined.  NULL otherwise) */
         1072  +){
  1054   1073     struct symbol *spx, *spy;
  1055   1074     int errcnt = 0;
  1056   1075     assert( apx->sp==apy->sp );  /* Otherwise there would be no conflict */
  1057   1076     if( apx->type==SHIFT && apy->type==SHIFT ){
  1058   1077       apy->type = SSCONFLICT;
  1059   1078       errcnt++;
  1060   1079     }
................................................................................
  1119   1138   static struct config *current = 0;       /* Top of list of configurations */
  1120   1139   static struct config **currentend = 0;   /* Last on list of configs */
  1121   1140   static struct config *basis = 0;         /* Top of list of basis configs */
  1122   1141   static struct config **basisend = 0;     /* End of list of basis configs */
  1123   1142   
  1124   1143   /* Return a pointer to a new configuration */
  1125   1144   PRIVATE struct config *newconfig(){
  1126         -  struct config *new;
         1145  +  struct config *newcfg;
  1127   1146     if( freelist==0 ){
  1128   1147       int i;
  1129   1148       int amt = 3;
  1130   1149       freelist = (struct config *)calloc( amt, sizeof(struct config) );
  1131   1150       if( freelist==0 ){
  1132   1151         fprintf(stderr,"Unable to allocate memory for a new configuration.");
  1133   1152         exit(1);
  1134   1153       }
  1135   1154       for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
  1136   1155       freelist[amt-1].next = 0;
  1137   1156     }
  1138         -  new = freelist;
         1157  +  newcfg = freelist;
  1139   1158     freelist = freelist->next;
  1140         -  return new;
         1159  +  return newcfg;
  1141   1160   }
  1142   1161   
  1143   1162   /* The configuration "old" is no longer used */
  1144         -PRIVATE void deleteconfig(old)
  1145         -struct config *old;
         1163  +PRIVATE void deleteconfig(struct config *old)
  1146   1164   {
  1147   1165     old->next = freelist;
  1148   1166     freelist = old;
  1149   1167   }
  1150   1168   
  1151   1169   /* Initialized the configuration list builder */
  1152   1170   void Configlist_init(){
................................................................................
  1165   1183     basis = 0;
  1166   1184     basisend = &basis;
  1167   1185     Configtable_clear(0);
  1168   1186     return;
  1169   1187   }
  1170   1188   
  1171   1189   /* Add another configuration to the configuration list */
  1172         -struct config *Configlist_add(rp,dot)
  1173         -struct rule *rp;    /* The rule */
  1174         -int dot;            /* Index into the RHS of the rule where the dot goes */
  1175         -{
         1190  +struct config *Configlist_add(
         1191  +  struct rule *rp,    /* The rule */
         1192  +  int dot             /* Index into the RHS of the rule where the dot goes */
         1193  +){
  1176   1194     struct config *cfp, model;
  1177   1195   
  1178   1196     assert( currentend!=0 );
  1179   1197     model.rp = rp;
  1180   1198     model.dot = dot;
  1181   1199     cfp = Configtable_find(&model);
  1182   1200     if( cfp==0 ){
................................................................................
  1192   1210       currentend = &cfp->next;
  1193   1211       Configtable_insert(cfp);
  1194   1212     }
  1195   1213     return cfp;
  1196   1214   }
  1197   1215   
  1198   1216   /* Add a basis configuration to the configuration list */
  1199         -struct config *Configlist_addbasis(rp,dot)
  1200         -struct rule *rp;
  1201         -int dot;
         1217  +struct config *Configlist_addbasis(struct rule *rp, int dot)
  1202   1218   {
  1203   1219     struct config *cfp, model;
  1204   1220   
  1205   1221     assert( basisend!=0 );
  1206   1222     assert( currentend!=0 );
  1207   1223     model.rp = rp;
  1208   1224     model.dot = dot;
................................................................................
  1222   1238       basisend = &cfp->bp;
  1223   1239       Configtable_insert(cfp);
  1224   1240     }
  1225   1241     return cfp;
  1226   1242   }
  1227   1243   
  1228   1244   /* Compute the closure of the configuration list */
  1229         -void Configlist_closure(lemp)
  1230         -struct lemon *lemp;
         1245  +void Configlist_closure(struct lemon *lemp)
  1231   1246   {
  1232   1247     struct config *cfp, *newcfp;
  1233   1248     struct rule *rp, *newrp;
  1234   1249     struct symbol *sp, *xsp;
  1235   1250     int i, dot;
  1236   1251   
  1237   1252     assert( currentend!=0 );
................................................................................
  1302   1317     old = basis;
  1303   1318     basis = 0;
  1304   1319     basisend = 0;
  1305   1320     return old;
  1306   1321   }
  1307   1322   
  1308   1323   /* Free all elements of the given configuration list */
  1309         -void Configlist_eat(cfp)
  1310         -struct config *cfp;
         1324  +void Configlist_eat(struct config *cfp)
  1311   1325   {
  1312   1326     struct config *nextcfp;
  1313   1327     for(; cfp; cfp=nextcfp){
  1314   1328       nextcfp = cfp->next;
  1315   1329       assert( cfp->fplp==0 );
  1316   1330       assert( cfp->bplp==0 );
  1317   1331       if( cfp->fws ) SetFree(cfp->fws);
................................................................................
  1320   1334     return;
  1321   1335   }
  1322   1336   /***************** From the file "error.c" *********************************/
  1323   1337   /*
  1324   1338   ** Code for printing error message.
  1325   1339   */
  1326   1340   
  1327         -/* Find a good place to break "msg" so that its length is at least "min"
  1328         -** but no more than "max".  Make the point as close to max as possible.
  1329         -*/
  1330         -static int findbreak(msg,min,max)
  1331         -char *msg;
  1332         -int min;
  1333         -int max;
  1334         -{
  1335         -  int i,spot;
  1336         -  char c;
  1337         -  for(i=spot=min; i<=max; i++){
  1338         -    c = msg[i];
  1339         -    if( c=='\t' ) msg[i] = ' ';
  1340         -    if( c=='\n' ){ msg[i] = ' '; spot = i; break; }
  1341         -    if( c==0 ){ spot = i; break; }
  1342         -    if( c=='-' && i<max-1 ) spot = i+1;
  1343         -    if( c==' ' ) spot = i;
  1344         -  }
  1345         -  return spot;
  1346         -}
  1347         -
  1348         -/*
  1349         -** The error message is split across multiple lines if necessary.  The
  1350         -** splits occur at a space, if there is a space available near the end
  1351         -** of the line.
  1352         -*/
  1353         -#define ERRMSGSIZE  10000 /* Hope this is big enough.  No way to error check */
  1354         -#define LINEWIDTH      79 /* Max width of any output line */
  1355         -#define PREFIXLIMIT    30 /* Max width of the prefix on each line */
  1356   1341   void ErrorMsg(const char *filename, int lineno, const char *format, ...){
  1357         -  char errmsg[ERRMSGSIZE];
  1358         -  char prefix[PREFIXLIMIT+10];
  1359         -  int errmsgsize;
  1360         -  int prefixsize;
  1361         -  int availablewidth;
  1362   1342     va_list ap;
  1363         -  int end, restart, base;
  1364         -
         1343  +  fprintf(stderr, "%s:%d: ", filename, lineno);
  1365   1344     va_start(ap, format);
  1366         -  /* Prepare a prefix to be prepended to every output line */
  1367         -  if( lineno>0 ){
  1368         -    sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
  1369         -  }else{
  1370         -    sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
  1371         -  }
  1372         -  prefixsize = lemonStrlen(prefix);
  1373         -  availablewidth = LINEWIDTH - prefixsize;
  1374         -
  1375         -  /* Generate the error message */
  1376         -  vsprintf(errmsg,format,ap);
         1345  +  vfprintf(stderr,format,ap);
  1377   1346     va_end(ap);
  1378         -  errmsgsize = lemonStrlen(errmsg);
  1379         -  /* Remove trailing '\n's from the error message. */
  1380         -  while( errmsgsize>0 && errmsg[errmsgsize-1]=='\n' ){
  1381         -     errmsg[--errmsgsize] = 0;
  1382         -  }
  1383         -
  1384         -  /* Print the error message */
  1385         -  base = 0;
  1386         -  while( errmsg[base]!=0 ){
  1387         -    end = restart = findbreak(&errmsg[base],0,availablewidth);
  1388         -    restart += base;
  1389         -    while( errmsg[restart]==' ' ) restart++;
  1390         -    fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
  1391         -    base = restart;
  1392         -  }
         1347  +  fprintf(stderr, "\n");
  1393   1348   }
  1394   1349   /**************** From the file "main.c" ************************************/
  1395   1350   /*
  1396   1351   ** Main program file for the LEMON parser generator.
  1397   1352   */
  1398   1353   
  1399   1354   /* Report an out-of-memory condition and abort.  This function
................................................................................
  1409   1364   
  1410   1365   /* This routine is called with the argument to each -D command-line option.
  1411   1366   ** Add the macro defined to the azDefine array.
  1412   1367   */
  1413   1368   static void handle_D_option(char *z){
  1414   1369     char **paz;
  1415   1370     nDefine++;
  1416         -  azDefine = realloc(azDefine, sizeof(azDefine[0])*nDefine);
         1371  +  azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
  1417   1372     if( azDefine==0 ){
  1418   1373       fprintf(stderr,"out of memory\n");
  1419   1374       exit(1);
  1420   1375     }
  1421   1376     paz = &azDefine[nDefine-1];
  1422         -  *paz = malloc( lemonStrlen(z)+1 );
         1377  +  *paz = (char *) malloc( lemonStrlen(z)+1 );
  1423   1378     if( *paz==0 ){
  1424   1379       fprintf(stderr,"out of memory\n");
  1425   1380       exit(1);
  1426   1381     }
  1427   1382     strcpy(*paz, z);
  1428   1383     for(z=*paz; *z && *z!='='; z++){}
  1429   1384     *z = 0;
  1430   1385   }
  1431   1386   
         1387  +static char *user_templatename = NULL;
         1388  +static void handle_T_option(char *z){
         1389  +  user_templatename = (char *) malloc( lemonStrlen(z)+1 );
         1390  +  if( user_templatename==0 ){
         1391  +    memory_error();
         1392  +  }
         1393  +  strcpy(user_templatename, z);
         1394  +}
  1432   1395   
  1433   1396   /* The main program.  Parse the command line and do it... */
  1434         -int main(argc,argv)
  1435         -int argc;
  1436         -char **argv;
         1397  +int main(int argc, char **argv)
  1437   1398   {
  1438   1399     static int version = 0;
  1439   1400     static int rpflag = 0;
  1440   1401     static int basisflag = 0;
  1441   1402     static int compress = 0;
  1442   1403     static int quiet = 0;
  1443   1404     static int statistics = 0;
  1444   1405     static int mhflag = 0;
  1445   1406     static int nolinenosflag = 0;
  1446   1407     static struct s_options options[] = {
  1447   1408       {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
  1448   1409       {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
  1449   1410       {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
         1411  +    {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
  1450   1412       {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
  1451   1413       {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
  1452   1414       {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
  1453   1415       {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
  1454   1416       {OPT_FLAG, "s", (char*)&statistics,
  1455   1417                                      "Print parser stats to standard output."},
  1456   1418       {OPT_FLAG, "x", (char*)&version, "Print the version number."},
  1457   1419       {OPT_FLAG,0,0,0}
  1458   1420     };
  1459   1421     int i;
         1422  +  int exitcode;
  1460   1423     struct lemon lem;
         1424  +
         1425  +  atexit(LemonAtExit);
  1461   1426   
  1462   1427     OptInit(argv,options,stderr);
  1463   1428     if( version ){
  1464   1429        printf("Lemon version 1.0\n");
  1465   1430        exit(0); 
  1466   1431     }
  1467   1432     if( OptNArgs()!=1 ){
................................................................................
  1492   1457     }
  1493   1458   
  1494   1459     /* Count and index the symbols of the grammar */
  1495   1460     lem.nsymbol = Symbol_count();
  1496   1461     Symbol_new("{default}");
  1497   1462     lem.symbols = Symbol_arrayof();
  1498   1463     for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
  1499         -  qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*),
  1500         -        (int(*)())Symbolcmpp);
         1464  +  qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*), Symbolcmpp);
  1501   1465     for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
  1502   1466     for(i=1; isupper(lem.symbols[i]->name[0]); i++);
  1503   1467     lem.nterminal = i;
  1504   1468   
  1505   1469     /* Generate a reprint of the grammar, if requested on the command line */
  1506   1470     if( rpflag ){
  1507   1471       Reprint(&lem);
................................................................................
  1551   1515     }
  1552   1516     if( statistics ){
  1553   1517       printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
  1554   1518         lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
  1555   1519       printf("                   %d states, %d parser table entries, %d conflicts\n",
  1556   1520         lem.nstate, lem.tablesize, lem.nconflict);
  1557   1521     }
  1558         -  if( lem.nconflict ){
         1522  +  if( lem.nconflict > 0 ){
  1559   1523       fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
  1560   1524     }
  1561         -  exit(lem.errorcnt + lem.nconflict);
  1562         -  return (lem.errorcnt + lem.nconflict);
         1525  +
         1526  +  /* return 0 on success, 1 on failure. */
         1527  +  exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
         1528  +  successful_exit = (exitcode == 0);
         1529  +  exit(exitcode);
         1530  +  return (exitcode);
  1563   1531   }
  1564   1532   /******************** From the file "msort.c" *******************************/
  1565   1533   /*
  1566   1534   ** A generic merge-sort program.
  1567   1535   **
  1568   1536   ** USAGE:
  1569   1537   ** Let "ptr" be a pointer to some structure which is at the head of
................................................................................
  1689   1657   
  1690   1658   #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
  1691   1659   
  1692   1660   /*
  1693   1661   ** Print the command line with a carrot pointing to the k-th character
  1694   1662   ** of the n-th field.
  1695   1663   */
  1696         -static void errline(n,k,err)
  1697         -int n;
  1698         -int k;
  1699         -FILE *err;
         1664  +static void errline(int n, int k, FILE *err)
  1700   1665   {
  1701   1666     int spcnt, i;
  1702   1667     if( argv[0] ) fprintf(err,"%s",argv[0]);
  1703   1668     spcnt = lemonStrlen(argv[0]) + 1;
  1704   1669     for(i=1; i<n && argv[i]; i++){
  1705   1670       fprintf(err," %s",argv[i]);
  1706   1671       spcnt += lemonStrlen(argv[i])+1;
................................................................................
  1714   1679     }
  1715   1680   }
  1716   1681   
  1717   1682   /*
  1718   1683   ** Return the index of the N-th non-switch argument.  Return -1
  1719   1684   ** if N is out of range.
  1720   1685   */
  1721         -static int argindex(n)
  1722         -int n;
         1686  +static int argindex(int n)
  1723   1687   {
  1724   1688     int i;
  1725   1689     int dashdash = 0;
  1726   1690     if( argv!=0 && *argv!=0 ){
  1727   1691       for(i=1; argv[i]; i++){
  1728   1692         if( dashdash || !ISOPT(argv[i]) ){
  1729   1693           if( n==0 ) return i;
................................................................................
  1736   1700   }
  1737   1701   
  1738   1702   static char emsg[] = "Command line syntax error: ";
  1739   1703   
  1740   1704   /*
  1741   1705   ** Process a flag command line argument.
  1742   1706   */
  1743         -static int handleflags(i,err)
  1744         -int i;
  1745         -FILE *err;
         1707  +static int handleflags(int i, FILE *err)
  1746   1708   {
  1747   1709     int v;
  1748   1710     int errcnt = 0;
  1749   1711     int j;
  1750   1712     for(j=0; op[j].label; j++){
  1751   1713       if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
  1752   1714     }
................................................................................
  1756   1718         fprintf(err,"%sundefined option.\n",emsg);
  1757   1719         errline(i,1,err);
  1758   1720       }
  1759   1721       errcnt++;
  1760   1722     }else if( op[j].type==OPT_FLAG ){
  1761   1723       *((int*)op[j].arg) = v;
  1762   1724     }else if( op[j].type==OPT_FFLAG ){
  1763         -    (*(void(*)())(op[j].arg))(v);
         1725  +    (*(void(*)(int))(op[j].arg))(v);
  1764   1726     }else if( op[j].type==OPT_FSTR ){
  1765         -    (*(void(*)())(op[j].arg))(&argv[i][2]);
         1727  +    (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
  1766   1728     }else{
  1767   1729       if( err ){
  1768   1730         fprintf(err,"%smissing argument on switch.\n",emsg);
  1769   1731         errline(i,1,err);
  1770   1732       }
  1771   1733       errcnt++;
  1772   1734     }
  1773   1735     return errcnt;
  1774   1736   }
  1775   1737   
  1776   1738   /*
  1777   1739   ** Process a command line switch which has an argument.
  1778   1740   */
  1779         -static int handleswitch(i,err)
  1780         -int i;
  1781         -FILE *err;
         1741  +static int handleswitch(int i, FILE *err)
  1782   1742   {
  1783   1743     int lv = 0;
  1784   1744     double dv = 0.0;
  1785   1745     char *sv = 0, *end;
  1786   1746     char *cp;
  1787   1747     int j;
  1788   1748     int errcnt = 0;
................................................................................
  1841   1801         case OPT_FLAG:
  1842   1802         case OPT_FFLAG:
  1843   1803           break;
  1844   1804         case OPT_DBL:
  1845   1805           *(double*)(op[j].arg) = dv;
  1846   1806           break;
  1847   1807         case OPT_FDBL:
  1848         -        (*(void(*)())(op[j].arg))(dv);
         1808  +        (*(void(*)(double))(op[j].arg))(dv);
  1849   1809           break;
  1850   1810         case OPT_INT:
  1851   1811           *(int*)(op[j].arg) = lv;
  1852   1812           break;
  1853   1813         case OPT_FINT:
  1854         -        (*(void(*)())(op[j].arg))((int)lv);
         1814  +        (*(void(*)(int))(op[j].arg))((int)lv);
  1855   1815           break;
  1856   1816         case OPT_STR:
  1857   1817           *(char**)(op[j].arg) = sv;
  1858   1818           break;
  1859   1819         case OPT_FSTR:
  1860         -        (*(void(*)())(op[j].arg))(sv);
         1820  +        (*(void(*)(char *))(op[j].arg))(sv);
  1861   1821           break;
  1862   1822       }
  1863   1823     }
  1864   1824     return errcnt;
  1865   1825   }
  1866   1826   
  1867         -int OptInit(a,o,err)
  1868         -char **a;
  1869         -struct s_options *o;
  1870         -FILE *err;
         1827  +int OptInit(char **a, struct s_options *o, FILE *err)
  1871   1828   {
  1872   1829     int errcnt = 0;
  1873   1830     argv = a;
  1874   1831     op = o;
  1875   1832     errstream = err;
  1876   1833     if( argv && *argv && op ){
  1877   1834       int i;
................................................................................
  1900   1857         if( dashdash || !ISOPT(argv[i]) ) cnt++;
  1901   1858         if( strcmp(argv[i],"--")==0 ) dashdash = 1;
  1902   1859       }
  1903   1860     }
  1904   1861     return cnt;
  1905   1862   }
  1906   1863   
  1907         -char *OptArg(n)
  1908         -int n;
         1864  +char *OptArg(int n)
  1909   1865   {
  1910   1866     int i;
  1911   1867     i = argindex(n);
  1912   1868     return i>=0 ? argv[i] : 0;
  1913   1869   }
  1914   1870   
  1915         -void OptErr(n)
  1916         -int n;
         1871  +void OptErr(int n)
  1917   1872   {
  1918   1873     int i;
  1919   1874     i = argindex(n);
  1920   1875     if( i>=0 ) errline(i,0,errstream);
  1921   1876   }
  1922   1877   
  1923   1878   void OptPrint(){
................................................................................
  1971   1926   }
  1972   1927   /*********************** From the file "parse.c" ****************************/
  1973   1928   /*
  1974   1929   ** Input file parser for the LEMON parser generator.
  1975   1930   */
  1976   1931   
  1977   1932   /* The state of the parser */
         1933  +enum e_state {
         1934  +  INITIALIZE,
         1935  +  WAITING_FOR_DECL_OR_RULE,
         1936  +  WAITING_FOR_DECL_KEYWORD,
         1937  +  WAITING_FOR_DECL_ARG,
         1938  +  WAITING_FOR_PRECEDENCE_SYMBOL,
         1939  +  WAITING_FOR_ARROW,
         1940  +  IN_RHS,
         1941  +  LHS_ALIAS_1,
         1942  +  LHS_ALIAS_2,
         1943  +  LHS_ALIAS_3,
         1944  +  RHS_ALIAS_1,
         1945  +  RHS_ALIAS_2,
         1946  +  PRECEDENCE_MARK_1,
         1947  +  PRECEDENCE_MARK_2,
         1948  +  RESYNC_AFTER_RULE_ERROR,
         1949  +  RESYNC_AFTER_DECL_ERROR,
         1950  +  WAITING_FOR_DESTRUCTOR_SYMBOL,
         1951  +  WAITING_FOR_DATATYPE_SYMBOL,
         1952  +  WAITING_FOR_FALLBACK_ID,
         1953  +  WAITING_FOR_WILDCARD_ID
         1954  +};
  1978   1955   struct pstate {
  1979   1956     char *filename;       /* Name of the input file */
  1980   1957     int tokenlineno;      /* Linenumber at which current token starts */
  1981   1958     int errorcnt;         /* Number of errors so far */
  1982   1959     char *tokenstart;     /* Text of current token */
  1983   1960     struct lemon *gp;     /* Global state vector */
  1984         -  enum e_state {
  1985         -    INITIALIZE,
  1986         -    WAITING_FOR_DECL_OR_RULE,
  1987         -    WAITING_FOR_DECL_KEYWORD,
  1988         -    WAITING_FOR_DECL_ARG,
  1989         -    WAITING_FOR_PRECEDENCE_SYMBOL,
  1990         -    WAITING_FOR_ARROW,
  1991         -    IN_RHS,
  1992         -    LHS_ALIAS_1,
  1993         -    LHS_ALIAS_2,
  1994         -    LHS_ALIAS_3,
  1995         -    RHS_ALIAS_1,
  1996         -    RHS_ALIAS_2,
  1997         -    PRECEDENCE_MARK_1,
  1998         -    PRECEDENCE_MARK_2,
  1999         -    RESYNC_AFTER_RULE_ERROR,
  2000         -    RESYNC_AFTER_DECL_ERROR,
  2001         -    WAITING_FOR_DESTRUCTOR_SYMBOL,
  2002         -    WAITING_FOR_DATATYPE_SYMBOL,
  2003         -    WAITING_FOR_FALLBACK_ID,
  2004         -    WAITING_FOR_WILDCARD_ID
  2005         -  } state;                   /* The state of the parser */
         1961  +  enum e_state state;        /* The state of the parser */
  2006   1962     struct symbol *fallback;   /* The fallback token */
  2007   1963     struct symbol *lhs;        /* Left-hand side of current rule */
  2008         -  char *lhsalias;            /* Alias for the LHS */
         1964  +  const char *lhsalias;      /* Alias for the LHS */
  2009   1965     int nrhs;                  /* Number of right-hand side symbols seen */
  2010   1966     struct symbol *rhs[MAXRHS];  /* RHS symbols */
  2011         -  char *alias[MAXRHS];       /* Aliases for each RHS symbol (or NULL) */
         1967  +  const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
  2012   1968     struct rule *prevrule;     /* Previous rule parsed */
  2013         -  char *declkeyword;         /* Keyword of a declaration */
         1969  +  const char *declkeyword;   /* Keyword of a declaration */
  2014   1970     char **declargslot;        /* Where the declaration argument should be put */
  2015   1971     int insertLineMacro;       /* Add #line before declaration insert */
  2016   1972     int *decllinenoslot;       /* Where to write declaration line number */
  2017   1973     enum e_assoc declassoc;    /* Assign this association to decl arguments */
  2018   1974     int preccounter;           /* Assign this precedence to decl arguments */
  2019   1975     struct rule *firstrule;    /* Pointer to first rule in the grammar */
  2020   1976     struct rule *lastrule;     /* Pointer to the most recently parsed rule */
  2021   1977   };
  2022   1978   
  2023   1979   /* Parse a single token */
  2024         -static void parseonetoken(psp)
  2025         -struct pstate *psp;
         1980  +static void parseonetoken(struct pstate *psp)
  2026   1981   {
  2027         -  char *x;
         1982  +  const char *x;
  2028   1983     x = Strsafe(psp->tokenstart);     /* Save the token permanently */
  2029   1984   #if 0
  2030   1985     printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
  2031   1986       x,psp->state);
  2032   1987   #endif
  2033   1988     switch( psp->state ){
  2034   1989       case INITIALIZE:
................................................................................
  2152   2107               "Can't allocate enough memory for this rule.");
  2153   2108             psp->errorcnt++;
  2154   2109             psp->prevrule = 0;
  2155   2110   	}else{
  2156   2111             int i;
  2157   2112             rp->ruleline = psp->tokenlineno;
  2158   2113             rp->rhs = (struct symbol**)&rp[1];
  2159         -          rp->rhsalias = (char**)&(rp->rhs[psp->nrhs]);
         2114  +          rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
  2160   2115             for(i=0; i<psp->nrhs; i++){
  2161   2116               rp->rhs[i] = psp->rhs[i];
  2162   2117               rp->rhsalias[i] = psp->alias[i];
  2163   2118   	  }
  2164   2119             rp->lhs = psp->lhs;
  2165   2120             rp->lhsalias = psp->lhsalias;
  2166   2121             rp->nrhs = psp->nrhs;
................................................................................
  2191   2146             psp->alias[psp->nrhs] = 0;
  2192   2147             psp->nrhs++;
  2193   2148   	}
  2194   2149         }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
  2195   2150           struct symbol *msp = psp->rhs[psp->nrhs-1];
  2196   2151           if( msp->type!=MULTITERMINAL ){
  2197   2152             struct symbol *origsp = msp;
  2198         -          msp = calloc(1,sizeof(*msp));
         2153  +          msp = (struct symbol *) calloc(1,sizeof(*msp));
  2199   2154             memset(msp, 0, sizeof(*msp));
  2200   2155             msp->type = MULTITERMINAL;
  2201   2156             msp->nsubsym = 1;
  2202         -          msp->subsym = calloc(1,sizeof(struct symbol*));
         2157  +          msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
  2203   2158             msp->subsym[0] = origsp;
  2204   2159             msp->name = origsp->name;
  2205   2160             psp->rhs[psp->nrhs-1] = msp;
  2206   2161           }
  2207   2162           msp->nsubsym++;
  2208         -        msp->subsym = realloc(msp->subsym, sizeof(struct symbol*)*msp->nsubsym);
         2163  +        msp->subsym = (struct symbol **) realloc(msp->subsym,
         2164  +          sizeof(struct symbol*)*msp->nsubsym);
  2209   2165           msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
  2210   2166           if( islower(x[1]) || islower(msp->subsym[0]->name[0]) ){
  2211   2167             ErrorMsg(psp->filename,psp->tokenlineno,
  2212   2168               "Cannot form a compound containing a non-terminal");
  2213   2169             psp->errorcnt++;
  2214   2170           }
  2215   2171         }else if( x[0]=='(' && psp->nrhs>0 ){
................................................................................
  2320   2276           psp->errorcnt++;
  2321   2277           psp->state = RESYNC_AFTER_DECL_ERROR;
  2322   2278         }
  2323   2279         break;
  2324   2280       case WAITING_FOR_DESTRUCTOR_SYMBOL:
  2325   2281         if( !isalpha(x[0]) ){
  2326   2282           ErrorMsg(psp->filename,psp->tokenlineno,
  2327         -          "Symbol name missing after %destructor keyword");
         2283  +          "Symbol name missing after %%destructor keyword");
  2328   2284           psp->errorcnt++;
  2329   2285           psp->state = RESYNC_AFTER_DECL_ERROR;
  2330   2286         }else{
  2331   2287           struct symbol *sp = Symbol_new(x);
  2332   2288           psp->declargslot = &sp->destructor;
  2333   2289           psp->decllinenoslot = &sp->destLineno;
  2334   2290           psp->insertLineMacro = 1;
  2335   2291           psp->state = WAITING_FOR_DECL_ARG;
  2336   2292         }
  2337   2293         break;
  2338   2294       case WAITING_FOR_DATATYPE_SYMBOL:
  2339   2295         if( !isalpha(x[0]) ){
  2340   2296           ErrorMsg(psp->filename,psp->tokenlineno,
  2341         -          "Symbol name missing after %destructor keyword");
         2297  +          "Symbol name missing after %%type keyword");
  2342   2298           psp->errorcnt++;
  2343   2299           psp->state = RESYNC_AFTER_DECL_ERROR;
  2344   2300         }else{
  2345         -        struct symbol *sp = Symbol_new(x);
  2346         -        psp->declargslot = &sp->datatype;
  2347         -        psp->insertLineMacro = 0;
  2348         -        psp->state = WAITING_FOR_DECL_ARG;
         2301  +        struct symbol *sp = Symbol_find(x);
         2302  +        if((sp) && (sp->datatype)){
         2303  +          ErrorMsg(psp->filename,psp->tokenlineno,
         2304  +            "Symbol %%type \"%s\" already defined", x);
         2305  +          psp->errorcnt++;
         2306  +          psp->state = RESYNC_AFTER_DECL_ERROR;
         2307  +        }else{
         2308  +          if (!sp){
         2309  +            sp = Symbol_new(x);
         2310  +          }
         2311  +          psp->declargslot = &sp->datatype;
         2312  +          psp->insertLineMacro = 0;
         2313  +          psp->state = WAITING_FOR_DECL_ARG;
         2314  +        }
  2349   2315         }
  2350   2316         break;
  2351   2317       case WAITING_FOR_PRECEDENCE_SYMBOL:
  2352   2318         if( x[0]=='.' ){
  2353   2319           psp->state = WAITING_FOR_DECL_OR_RULE;
  2354   2320         }else if( isupper(x[0]) ){
  2355   2321           struct symbol *sp;
................................................................................
  2366   2332           ErrorMsg(psp->filename,psp->tokenlineno,
  2367   2333             "Can't assign a precedence to \"%s\".",x);
  2368   2334           psp->errorcnt++;
  2369   2335         }
  2370   2336         break;
  2371   2337       case WAITING_FOR_DECL_ARG:
  2372   2338         if( x[0]=='{' || x[0]=='\"' || isalnum(x[0]) ){
  2373         -        char *zOld, *zNew, *zBuf, *z;
         2339  +        const char *zOld, *zNew;
         2340  +        char *zBuf, *z;
  2374   2341           int nOld, n, nLine, nNew, nBack;
  2375   2342           int addLineMacro;
  2376   2343           char zLine[50];
  2377   2344           zNew = x;
  2378   2345           if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
  2379   2346           nNew = lemonStrlen(zNew);
  2380   2347           if( *psp->declargslot ){
................................................................................
  2390   2357             for(z=psp->filename, nBack=0; *z; z++){
  2391   2358               if( *z=='\\' ) nBack++;
  2392   2359             }
  2393   2360             sprintf(zLine, "#line %d ", psp->tokenlineno);
  2394   2361             nLine = lemonStrlen(zLine);
  2395   2362             n += nLine + lemonStrlen(psp->filename) + nBack;
  2396   2363           }
  2397         -        *psp->declargslot = zBuf = realloc(*psp->declargslot, n);
  2398         -        zBuf += nOld;
         2364  +        *psp->declargslot = (char *) realloc(*psp->declargslot, n);
         2365  +        zBuf = *psp->declargslot + nOld;
  2399   2366           if( addLineMacro ){
  2400   2367             if( nOld && zBuf[-1]!='\n' ){
  2401   2368               *(zBuf++) = '\n';
  2402   2369             }
  2403   2370             memcpy(zBuf, zLine, nLine);
  2404   2371             zBuf += nLine;
  2405   2372             *(zBuf++) = '"';
................................................................................
  2527   2494   }
  2528   2495   
  2529   2496   /* In spite of its name, this function is really a scanner.  It read
  2530   2497   ** in the entire input file (all at once) then tokenizes it.  Each
  2531   2498   ** token is passed to the function "parseonetoken" which builds all
  2532   2499   ** the appropriate data structures in the global state vector "gp".
  2533   2500   */
  2534         -void Parse(gp)
  2535         -struct lemon *gp;
         2501  +void Parse(struct lemon *gp)
  2536   2502   {
  2537   2503     struct pstate ps;
  2538   2504     FILE *fp;
  2539   2505     char *filebuf;
  2540   2506     int filesize;
  2541   2507     int lineno;
  2542   2508     int c;
................................................................................
  2682   2648   ** Routines processing configuration follow-set propagation links
  2683   2649   ** in the LEMON parser generator.
  2684   2650   */
  2685   2651   static struct plink *plink_freelist = 0;
  2686   2652   
  2687   2653   /* Allocate a new plink */
  2688   2654   struct plink *Plink_new(){
  2689         -  struct plink *new;
         2655  +  struct plink *newlink;
  2690   2656   
  2691   2657     if( plink_freelist==0 ){
  2692   2658       int i;
  2693   2659       int amt = 100;
  2694   2660       plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
  2695   2661       if( plink_freelist==0 ){
  2696   2662         fprintf(stderr,
  2697   2663         "Unable to allocate memory for a new follow-set propagation link.\n");
  2698   2664         exit(1);
  2699   2665       }
  2700   2666       for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
  2701   2667       plink_freelist[amt-1].next = 0;
  2702   2668     }
  2703         -  new = plink_freelist;
         2669  +  newlink = plink_freelist;
  2704   2670     plink_freelist = plink_freelist->next;
  2705         -  return new;
         2671  +  return newlink;
  2706   2672   }
  2707   2673   
  2708   2674   /* Add a plink to a plink list */
  2709         -void Plink_add(plpp,cfp)
  2710         -struct plink **plpp;
  2711         -struct config *cfp;
         2675  +void Plink_add(struct plink **plpp, struct config *cfp)
  2712   2676   {
  2713         -  struct plink *new;
  2714         -  new = Plink_new();
  2715         -  new->next = *plpp;
  2716         -  *plpp = new;
  2717         -  new->cfp = cfp;
         2677  +  struct plink *newlink;
         2678  +  newlink = Plink_new();
         2679  +  newlink->next = *plpp;
         2680  +  *plpp = newlink;
         2681  +  newlink->cfp = cfp;
  2718   2682   }
  2719   2683   
  2720   2684   /* Transfer every plink on the list "from" to the list "to" */
  2721         -void Plink_copy(to,from)
  2722         -struct plink **to;
  2723         -struct plink *from;
         2685  +void Plink_copy(struct plink **to, struct plink *from)
  2724   2686   {
  2725   2687     struct plink *nextpl;
  2726   2688     while( from ){
  2727   2689       nextpl = from->next;
  2728   2690       from->next = *to;
  2729   2691       *to = from;
  2730   2692       from = nextpl;
  2731   2693     }
  2732   2694   }
  2733   2695   
  2734   2696   /* Delete every plink on the list */
  2735         -void Plink_delete(plp)
  2736         -struct plink *plp;
         2697  +void Plink_delete(struct plink *plp)
  2737   2698   {
  2738   2699     struct plink *nextpl;
  2739   2700   
  2740   2701     while( plp ){
  2741   2702       nextpl = plp->next;
  2742   2703       plp->next = plink_freelist;
  2743   2704       plink_freelist = plp;
................................................................................
  2749   2710   ** Procedures for generating reports and tables in the LEMON parser generator.
  2750   2711   */
  2751   2712   
  2752   2713   /* Generate a filename with the given suffix.  Space to hold the
  2753   2714   ** name comes from malloc() and must be freed by the calling
  2754   2715   ** function.
  2755   2716   */
  2756         -PRIVATE char *file_makename(lemp,suffix)
  2757         -struct lemon *lemp;
  2758         -char *suffix;
         2717  +PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
  2759   2718   {
  2760   2719     char *name;
  2761   2720     char *cp;
  2762   2721   
  2763         -  name = malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
         2722  +  name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
  2764   2723     if( name==0 ){
  2765   2724       fprintf(stderr,"Can't allocate space for a filename.\n");
  2766   2725       exit(1);
  2767   2726     }
  2768   2727     strcpy(name,lemp->filename);
  2769   2728     cp = strrchr(name,'.');
  2770   2729     if( cp ) *cp = 0;
................................................................................
  2771   2730     strcat(name,suffix);
  2772   2731     return name;
  2773   2732   }
  2774   2733   
  2775   2734   /* Open a file with a name based on the name of the input file,
  2776   2735   ** but with a different (specified) suffix, and return a pointer
  2777   2736   ** to the stream */
  2778         -PRIVATE FILE *file_open(lemp,suffix,mode)
  2779         -struct lemon *lemp;
  2780         -char *suffix;
  2781         -char *mode;
  2782         -{
         2737  +PRIVATE FILE *file_open(
         2738  +  struct lemon *lemp,
         2739  +  const char *suffix,
         2740  +  const char *mode
         2741  +){
  2783   2742     FILE *fp;
  2784   2743   
  2785   2744     if( lemp->outname ) free(lemp->outname);
  2786   2745     lemp->outname = file_makename(lemp, suffix);
  2787   2746     fp = fopen(lemp->outname,mode);
  2788   2747     if( fp==0 && *mode=='w' ){
  2789   2748       fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
  2790   2749       lemp->errorcnt++;
  2791   2750       return 0;
  2792   2751     }
         2752  +
         2753  +  /* Add files we create to a list, so we can delete them if we fail. This
         2754  +  ** is to keep makefiles from getting confused. We don't include .out files,
         2755  +  ** though: this is debug information, and you don't want it deleted if there
         2756  +  ** was an error you need to track down.
         2757  +  */
         2758  +  if(( *mode=='w' ) && (strcmp(suffix, ".out") != 0)){
         2759  +    const char **ptr = (const char **)
         2760  +        realloc(made_files, sizeof (const char **) * (made_files_count + 1));
         2761  +    const char *fname = Strsafe(lemp->outname);
         2762  +    if ((ptr == NULL) || (fname == NULL)) {
         2763  +        free(ptr);
         2764  +        memory_error();
         2765  +    }
         2766  +    made_files = ptr;
         2767  +    made_files[made_files_count++] = fname;
         2768  +  }
  2793   2769     return fp;
  2794   2770   }
  2795   2771   
  2796   2772   /* Duplicate the input file without comments and without actions 
  2797   2773   ** on rules */
  2798         -void Reprint(lemp)
  2799         -struct lemon *lemp;
         2774  +void Reprint(struct lemon *lemp)
  2800   2775   {
  2801   2776     struct rule *rp;
  2802   2777     struct symbol *sp;
  2803   2778     int i, j, maxlen, len, ncolumns, skip;
  2804   2779     printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
  2805   2780     maxlen = 10;
  2806   2781     for(i=0; i<lemp->nsymbol; i++){
................................................................................
  2837   2812       printf(".");
  2838   2813       if( rp->precsym ) printf(" [%s]",rp->precsym->name);
  2839   2814       /* if( rp->code ) printf("\n    %s",rp->code); */
  2840   2815       printf("\n");
  2841   2816     }
  2842   2817   }
  2843   2818   
  2844         -void ConfigPrint(fp,cfp)
  2845         -FILE *fp;
  2846         -struct config *cfp;
         2819  +void ConfigPrint(FILE *fp, struct config *cfp)
  2847   2820   {
  2848   2821     struct rule *rp;
  2849   2822     struct symbol *sp;
  2850   2823     int i, j;
  2851   2824     rp = cfp->rp;
  2852   2825     fprintf(fp,"%s ::=",rp->lhs->name);
  2853   2826     for(i=0; i<=rp->nrhs; i++){
................................................................................
  2932   2905         result = 0;
  2933   2906         break;
  2934   2907     }
  2935   2908     return result;
  2936   2909   }
  2937   2910   
  2938   2911   /* Generate the "y.output" log file */
  2939         -void ReportOutput(lemp)
  2940         -struct lemon *lemp;
         2912  +void ReportOutput(struct lemon *lemp)
  2941   2913   {
  2942   2914     int i;
  2943   2915     struct state *stp;
  2944   2916     struct config *cfp;
  2945   2917     struct action *ap;
  2946   2918     FILE *fp;
  2947   2919   
................................................................................
  2999   2971     }
  3000   2972     fclose(fp);
  3001   2973     return;
  3002   2974   }
  3003   2975   
  3004   2976   /* Search for the file "name" which is in the same directory as
  3005   2977   ** the exacutable */
  3006         -PRIVATE char *pathsearch(argv0,name,modemask)
  3007         -char *argv0;
  3008         -char *name;
  3009         -int modemask;
         2978  +PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
  3010   2979   {
  3011         -  char *pathlist;
         2980  +  const char *pathlist;
         2981  +  char *pathbufptr;
         2982  +  char *pathbuf;
  3012   2983     char *path,*cp;
  3013   2984     char c;
  3014   2985   
  3015   2986   #ifdef __WIN32__
  3016   2987     cp = strrchr(argv0,'\\');
  3017   2988   #else
  3018   2989     cp = strrchr(argv0,'/');
................................................................................
  3020   2991     if( cp ){
  3021   2992       c = *cp;
  3022   2993       *cp = 0;
  3023   2994       path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
  3024   2995       if( path ) sprintf(path,"%s/%s",argv0,name);
  3025   2996       *cp = c;
  3026   2997     }else{
  3027         -    extern char *getenv();
  3028   2998       pathlist = getenv("PATH");
  3029   2999       if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
         3000  +    pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
  3030   3001       path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
  3031         -    if( path!=0 ){
  3032         -      while( *pathlist ){
  3033         -        cp = strchr(pathlist,':');
  3034         -        if( cp==0 ) cp = &pathlist[lemonStrlen(pathlist)];
         3002  +    if( (pathbuf != 0) && (path!=0) ){
         3003  +      pathbufptr = pathbuf;
         3004  +      strcpy(pathbuf, pathlist);
         3005  +      while( *pathbuf ){
         3006  +        cp = strchr(pathbuf,':');
         3007  +        if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
  3035   3008           c = *cp;
  3036   3009           *cp = 0;
  3037         -        sprintf(path,"%s/%s",pathlist,name);
         3010  +        sprintf(path,"%s/%s",pathbuf,name);
  3038   3011           *cp = c;
  3039         -        if( c==0 ) pathlist = "";
  3040         -        else pathlist = &cp[1];
         3012  +        if( c==0 ) pathbuf[0] = 0;
         3013  +        else pathbuf = &cp[1];
  3041   3014           if( access(path,modemask)==0 ) break;
  3042   3015         }
         3016  +      free(pathbufptr);
  3043   3017       }
  3044   3018     }
  3045   3019     return path;
  3046   3020   }
  3047   3021   
  3048   3022   /* Given an action, compute the integer value for that action
  3049   3023   ** which is to be put in the action table of the generated machine.
  3050   3024   ** Return negative if no action should be generated.
  3051   3025   */
  3052         -PRIVATE int compute_action(lemp,ap)
  3053         -struct lemon *lemp;
  3054         -struct action *ap;
         3026  +PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
  3055   3027   {
  3056   3028     int act;
  3057   3029     switch( ap->type ){
  3058   3030       case SHIFT:  act = ap->x.stp->statenum;            break;
  3059   3031       case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
  3060   3032       case ERROR:  act = lemp->nstate + lemp->nrule;     break;
  3061   3033       case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
................................................................................
  3070   3042   /* The first function transfers data from "in" to "out" until
  3071   3043   ** a line is seen which begins with "%%".  The line number is
  3072   3044   ** tracked.
  3073   3045   **
  3074   3046   ** if name!=0, then any word that begin with "Parse" is changed to
  3075   3047   ** begin with *name instead.
  3076   3048   */
  3077         -PRIVATE void tplt_xfer(name,in,out,lineno)
  3078         -char *name;
  3079         -FILE *in;
  3080         -FILE *out;
  3081         -int *lineno;
         3049  +PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
  3082   3050   {
  3083   3051     int i, iStart;
  3084   3052     char line[LINESIZE];
  3085   3053     while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
  3086   3054       (*lineno)++;
  3087   3055       iStart = 0;
  3088   3056       if( name ){
................................................................................
  3099   3067       }
  3100   3068       fprintf(out,"%s",&line[iStart]);
  3101   3069     }
  3102   3070   }
  3103   3071   
  3104   3072   /* The next function finds the template file and opens it, returning
  3105   3073   ** a pointer to the opened file. */
  3106         -PRIVATE FILE *tplt_open(lemp)
  3107         -struct lemon *lemp;
         3074  +PRIVATE FILE *tplt_open(struct lemon *lemp)
  3108   3075   {
  3109   3076     static char templatename[] = "lempar.c";
  3110   3077     char buf[1000];
  3111   3078     FILE *in;
  3112   3079     char *tpltname;
  3113   3080     char *cp;
         3081  +
         3082  +  /* first, see if user specified a template filename on the command line. */
         3083  +  if (user_templatename != 0) {
         3084  +    if( access(user_templatename,004)==-1 ){
         3085  +      fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
         3086  +        user_templatename);
         3087  +      lemp->errorcnt++;
         3088  +      return 0;
         3089  +    }
         3090  +    in = fopen(user_templatename,"rb");
         3091  +    if( in==0 ){
         3092  +      fprintf(stderr,"Can't open the template file \"%s\".\n",user_templatename);
         3093  +      lemp->errorcnt++;
         3094  +      return 0;
         3095  +    }
         3096  +    return in;
         3097  +  }
  3114   3098   
  3115   3099     cp = strrchr(lemp->filename,'.');
  3116   3100     if( cp ){
  3117   3101       sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
  3118   3102     }else{
  3119   3103       sprintf(buf,"%s.lt",lemp->filename);
  3120   3104     }
................................................................................
  3137   3121       lemp->errorcnt++;
  3138   3122       return 0;
  3139   3123     }
  3140   3124     return in;
  3141   3125   }
  3142   3126   
  3143   3127   /* Print a #line directive line to the output file. */
  3144         -PRIVATE void tplt_linedir(out,lineno,filename)
  3145         -FILE *out;
  3146         -int lineno;
  3147         -char *filename;
         3128  +PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
  3148   3129   {
  3149   3130     fprintf(out,"#line %d \"",lineno);
  3150   3131     while( *filename ){
  3151   3132       if( *filename == '\\' ) putc('\\',out);
  3152   3133       putc(*filename,out);
  3153   3134       filename++;
  3154   3135     }
  3155   3136     fprintf(out,"\"\n");
  3156   3137   }
  3157   3138   
  3158   3139   /* Print a string to the file and keep the linenumber up to date */
  3159         -PRIVATE void tplt_print(out,lemp,str,lineno)
  3160         -FILE *out;
  3161         -struct lemon *lemp;
  3162         -char *str;
  3163         -int *lineno;
         3140  +PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
  3164   3141   {
  3165   3142     if( str==0 ) return;
  3166   3143     while( *str ){
  3167   3144       putc(*str,out);
  3168   3145       if( *str=='\n' ) (*lineno)++;
  3169   3146       str++;
  3170   3147     }
................................................................................
  3178   3155     return;
  3179   3156   }
  3180   3157   
  3181   3158   /*
  3182   3159   ** The following routine emits code for the destructor for the
  3183   3160   ** symbol sp
  3184   3161   */
  3185         -void emit_destructor_code(out,sp,lemp,lineno)
  3186         -FILE *out;
  3187         -struct symbol *sp;
  3188         -struct lemon *lemp;
  3189         -int *lineno;
  3190         -{
         3162  +void emit_destructor_code(
         3163  +  FILE *out,
         3164  +  struct symbol *sp,
         3165  +  struct lemon *lemp,
         3166  +  int *lineno
         3167  +){
  3191   3168    char *cp = 0;
  3192   3169   
  3193   3170    if( sp->type==TERMINAL ){
  3194   3171      cp = lemp->tokendest;
  3195   3172      if( cp==0 ) return;
  3196   3173      fprintf(out,"{\n"); (*lineno)++;
  3197   3174    }else if( sp->destructor ){
................................................................................
  3221   3198    fprintf(out,"}\n"); (*lineno)++;
  3222   3199    return;
  3223   3200   }
  3224   3201   
  3225   3202   /*
  3226   3203   ** Return TRUE (non-zero) if the given symbol has a destructor.
  3227   3204   */
  3228         -int has_destructor(sp, lemp)
  3229         -struct symbol *sp;
  3230         -struct lemon *lemp;
         3205  +int has_destructor(struct symbol *sp, struct lemon *lemp)
  3231   3206   {
  3232   3207     int ret;
  3233   3208     if( sp->type==TERMINAL ){
  3234   3209       ret = lemp->tokendest!=0;
  3235   3210     }else{
  3236   3211       ret = lemp->vardest!=0 || sp->destructor!=0;
  3237   3212     }
................................................................................
  3246   3221   ** n bytes of zText are stored.  If n==0 then all of zText up to the first
  3247   3222   ** \000 terminator is stored.  zText can contain up to two instances of
  3248   3223   ** %d.  The values of p1 and p2 are written into the first and second
  3249   3224   ** %d.
  3250   3225   **
  3251   3226   ** If n==-1, then the previous character is overwritten.
  3252   3227   */
  3253         -PRIVATE char *append_str(char *zText, int n, int p1, int p2){
         3228  +PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
         3229  +  static char empty[1] = { 0 };
  3254   3230     static char *z = 0;
  3255   3231     static int alloced = 0;
  3256   3232     static int used = 0;
  3257   3233     int c;
  3258   3234     char zInt[40];
  3259         -
  3260   3235     if( zText==0 ){
  3261   3236       used = 0;
  3262   3237       return z;
  3263   3238     }
  3264   3239     if( n<=0 ){
  3265   3240       if( n<0 ){
  3266   3241         used += n;
  3267   3242         assert( used>=0 );
  3268   3243       }
  3269   3244       n = lemonStrlen(zText);
  3270   3245     }
  3271   3246     if( n+sizeof(zInt)*2+used >= alloced ){
  3272   3247       alloced = n + sizeof(zInt)*2 + used + 200;
  3273         -    z = realloc(z,  alloced);
         3248  +    z = (char *) realloc(z,  alloced);
  3274   3249     }
  3275         -  if( z==0 ) return "";
         3250  +  if( z==0 ) return empty;
  3276   3251     while( n-- > 0 ){
  3277   3252       c = *(zText++);
  3278   3253       if( c=='%' && n>0 && zText[0]=='d' ){
  3279   3254         sprintf(zInt, "%d", p1);
  3280   3255         p1 = p2;
  3281   3256         strcpy(&z[used], zInt);
  3282   3257         used += lemonStrlen(&z[used]);
................................................................................
  3301   3276     char lhsused = 0;    /* True if the LHS element has been used */
  3302   3277     char used[MAXRHS];   /* True for each RHS element which is used */
  3303   3278   
  3304   3279     for(i=0; i<rp->nrhs; i++) used[i] = 0;
  3305   3280     lhsused = 0;
  3306   3281   
  3307   3282     if( rp->code==0 ){
  3308         -    rp->code = "\n";
         3283  +    static char newlinestr[2] = { '\n', '\0' };
         3284  +    rp->code = newlinestr;
  3309   3285       rp->line = rp->ruleline;
  3310   3286     }
  3311   3287   
  3312   3288     append_str(0,0,0,0);
  3313         -  for(cp=rp->code; *cp; cp++){
         3289  +
         3290  +  /* This const cast is wrong but harmless, if we're careful. */
         3291  +  for(cp=(char *)rp->code; *cp; cp++){
  3314   3292       if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){
  3315   3293         char saved;
  3316   3294         for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++);
  3317   3295         saved = *xp;
  3318   3296         *xp = 0;
  3319   3297         if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
  3320   3298           append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
................................................................................
  3379   3357     }
  3380   3358   }
  3381   3359   
  3382   3360   /* 
  3383   3361   ** Generate code which executes when the rule "rp" is reduced.  Write
  3384   3362   ** the code to "out".  Make sure lineno stays up-to-date.
  3385   3363   */
  3386         -PRIVATE void emit_code(out,rp,lemp,lineno)
  3387         -FILE *out;
  3388         -struct rule *rp;
  3389         -struct lemon *lemp;
  3390         -int *lineno;
  3391         -{
  3392         - char *cp;
         3364  +PRIVATE void emit_code(
         3365  +  FILE *out,
         3366  +  struct rule *rp,
         3367  +  struct lemon *lemp,
         3368  +  int *lineno
         3369  +){
         3370  + const char *cp;
  3393   3371   
  3394   3372    /* Generate code to do the reduce action */
  3395   3373    if( rp->code ){
  3396   3374      if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,rp->line,lemp->filename); }
  3397   3375      fprintf(out,"{%s",rp->code);
  3398   3376      for(cp=rp->code; *cp; cp++){
  3399   3377        if( *cp=='\n' ) (*lineno)++;
................................................................................
  3408   3386   /*
  3409   3387   ** Print the definition of the union used for the parser's data stack.
  3410   3388   ** This union contains fields for every possible data type for tokens
  3411   3389   ** and nonterminals.  In the process of computing and printing this
  3412   3390   ** union, also set the ".dtnum" field of every terminal and nonterminal
  3413   3391   ** symbol.
  3414   3392   */
  3415         -void print_stack_union(out,lemp,plineno,mhflag)
  3416         -FILE *out;                  /* The output stream */
  3417         -struct lemon *lemp;         /* The main info structure for this parser */
  3418         -int *plineno;               /* Pointer to the line number */
  3419         -int mhflag;                 /* True if generating makeheaders output */
  3420         -{
         3393  +void print_stack_union(
         3394  +  FILE *out,                  /* The output stream */
         3395  +  struct lemon *lemp,         /* The main info structure for this parser */
         3396  +  int *plineno,               /* Pointer to the line number */
         3397  +  int mhflag                  /* True if generating makeheaders output */
         3398  +){
  3421   3399     int lineno = *plineno;    /* The line number of the output */
  3422   3400     char **types;             /* A hash table of datatypes */
  3423   3401     int arraysize;            /* Size of the "types" array */
  3424   3402     int maxdtlength;          /* Maximum length of any ".datatype" field. */
  3425   3403     char *stddt;              /* Standardized name for a datatype */
  3426   3404     int i,j;                  /* Loop counters */
  3427   3405     int hash;                 /* For hashing the name of a type */
  3428         -  char *name;               /* Name of the parser */
         3406  +  const char *name;         /* Name of the parser */
  3429   3407   
  3430   3408     /* Allocate and initialize types[] and allocate stddt[] */
  3431   3409     arraysize = lemp->nsymbol * 2;
  3432   3410     types = (char**)calloc( arraysize, sizeof(char*) );
  3433   3411     for(i=0; i<arraysize; i++) types[i] = 0;
  3434   3412     maxdtlength = 0;
  3435   3413     if( lemp->vartype ){
................................................................................
  3590   3568         }
  3591   3569       }
  3592   3570     }
  3593   3571   }
  3594   3572   
  3595   3573   
  3596   3574   /* Generate C source code for the parser */
  3597         -void ReportTable(lemp, mhflag)
  3598         -struct lemon *lemp;
  3599         -int mhflag;     /* Output in makeheaders format if true */
  3600         -{
         3575  +void ReportTable(
         3576  +  struct lemon *lemp,
         3577  +  int mhflag     /* Output in makeheaders format if true */
         3578  +){
  3601   3579     FILE *out, *in;
  3602   3580     char line[LINESIZE];
  3603   3581     int  lineno;
  3604   3582     struct state *stp;
  3605   3583     struct action *ap;
  3606   3584     struct rule *rp;
  3607   3585     struct acttab *pActtab;
  3608         -  int i, j, k, n;
  3609         -  char *name;
         3586  +  int i, j, n;
         3587  +  const char *name;
  3610   3588     int mnTknOfst, mxTknOfst;
  3611   3589     int mnNtOfst, mxNtOfst;
  3612   3590     struct axset *ax;
  3613   3591   
  3614   3592     in = tplt_open(lemp);
  3615   3593     if( in==0 ) return;
  3616   3594     out = file_open(lemp,".c","wb");
................................................................................
  3628   3606       fprintf(out,"#include \"%s\"\n", name); lineno++;
  3629   3607       free(name);
  3630   3608     }
  3631   3609     tplt_xfer(lemp->name,in,out,&lineno);
  3632   3610   
  3633   3611     /* Generate #defines for all tokens */
  3634   3612     if( mhflag ){
  3635         -    char *prefix;
         3613  +    const char *prefix;
  3636   3614       fprintf(out,"#if INTERFACE\n"); lineno++;
  3637   3615       if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  3638   3616       else                    prefix = "";
  3639   3617       for(i=1; i<lemp->nterminal; i++){
  3640   3618         fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
  3641   3619         lineno++;
  3642   3620       }
................................................................................
  3706   3684     **                     shifting terminals.
  3707   3685     **  yy_reduce_ofst[]   For each state, the offset into yy_action for
  3708   3686     **                     shifting non-terminals after a reduce.
  3709   3687     **  yy_default[]       Default action for each state.
  3710   3688     */
  3711   3689   
  3712   3690     /* Compute the actions on all states and count them up */
  3713         -  ax = calloc(lemp->nstate*2, sizeof(ax[0]));
         3691  +  ax = (struct axset *) calloc(lemp->nstate*2, sizeof(ax[0]));
  3714   3692     if( ax==0 ){
  3715   3693       fprintf(stderr,"malloc failed\n");
  3716   3694       exit(1);
  3717   3695     }
  3718   3696     for(i=0; i<lemp->nstate; i++){
  3719   3697       stp = lemp->sorted[i];
  3720   3698       ax[i*2].stp = stp;
................................................................................
  4034   4012   
  4035   4013     fclose(in);
  4036   4014     fclose(out);
  4037   4015     return;
  4038   4016   }
  4039   4017   
  4040   4018   /* Generate a header file for the parser */
  4041         -void ReportHeader(lemp)
  4042         -struct lemon *lemp;
         4019  +void ReportHeader(struct lemon *lemp)
  4043   4020   {
  4044   4021     FILE *out, *in;
  4045         -  char *prefix;
         4022  +  const char *prefix;
  4046   4023     char line[LINESIZE];
  4047   4024     char pattern[LINESIZE];
  4048   4025     int i;
  4049   4026   
  4050   4027     if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  4051   4028     else                    prefix = "";
  4052   4029     in = file_open(lemp,".h","rb");
................................................................................
  4074   4051   /* Reduce the size of the action tables, if possible, by making use
  4075   4052   ** of defaults.
  4076   4053   **
  4077   4054   ** In this version, we take the most frequent REDUCE action and make
  4078   4055   ** it the default.  Except, there is no default if the wildcard token
  4079   4056   ** is a possible look-ahead.
  4080   4057   */
  4081         -void CompressTables(lemp)
  4082         -struct lemon *lemp;
         4058  +void CompressTables(struct lemon *lemp)
  4083   4059   {
  4084   4060     struct state *stp;
  4085   4061     struct action *ap, *ap2;
  4086   4062     struct rule *rp, *rp2, *rbest;
  4087   4063     int nbest, n;
  4088   4064     int i;
  4089   4065     int usesWildcard;
................................................................................
  4159   4135   }
  4160   4136   
  4161   4137   
  4162   4138   /*
  4163   4139   ** Renumber and resort states so that states with fewer choices
  4164   4140   ** occur at the end.  Except, keep state 0 as the first state.
  4165   4141   */
  4166         -void ResortStates(lemp)
  4167         -struct lemon *lemp;
         4142  +void ResortStates(struct lemon *lemp)
  4168   4143   {
  4169   4144     int i;
  4170   4145     struct state *stp;
  4171   4146     struct action *ap;
  4172   4147   
  4173   4148     for(i=0; i<lemp->nstate; i++){
  4174   4149       stp = lemp->sorted[i];
................................................................................
  4200   4175   /*
  4201   4176   ** Set manipulation routines for the LEMON parser generator.
  4202   4177   */
  4203   4178   
  4204   4179   static int size = 0;
  4205   4180   
  4206   4181   /* Set the set size */
  4207         -void SetSize(n)
  4208         -int n;
         4182  +void SetSize(int n)
  4209   4183   {
  4210   4184     size = n+1;
  4211   4185   }
  4212   4186   
  4213   4187   /* Allocate a new set */
  4214   4188   char *SetNew(){
  4215   4189     char *s;
................................................................................
  4218   4192       extern void memory_error();
  4219   4193       memory_error();
  4220   4194     }
  4221   4195     return s;
  4222   4196   }
  4223   4197   
  4224   4198   /* Deallocate a set */
  4225         -void SetFree(s)
  4226         -char *s;
         4199  +void SetFree(char *s)
  4227   4200   {
  4228   4201     free(s);
  4229   4202   }
  4230   4203   
  4231   4204   /* Add a new element to the set.  Return TRUE if the element was added
  4232   4205   ** and FALSE if it was already there. */
  4233         -int SetAdd(s,e)
  4234         -char *s;
  4235         -int e;
         4206  +int SetAdd(char *s, int e)
  4236   4207   {
  4237   4208     int rv;
  4238   4209     assert( e>=0 && e<size );
  4239   4210     rv = s[e];
  4240   4211     s[e] = 1;
  4241   4212     return !rv;
  4242   4213   }
  4243   4214   
  4244   4215   /* Add every element of s2 to s1.  Return TRUE if s1 changes. */
  4245         -int SetUnion(s1,s2)
  4246         -char *s1;
  4247         -char *s2;
         4216  +int SetUnion(char *s1, char *s2)
  4248   4217   {
  4249   4218     int i, progress;
  4250   4219     progress = 0;
  4251   4220     for(i=0; i<size; i++){
  4252   4221       if( s2[i]==0 ) continue;
  4253   4222       if( s1[i]==0 ){
  4254   4223         progress = 1;
................................................................................
  4266   4235   ** Do not edit this file!  Instead, edit the specification
  4267   4236   ** file, then rerun aagen.
  4268   4237   */
  4269   4238   /*
  4270   4239   ** Code for processing tables in the LEMON parser generator.
  4271   4240   */
  4272   4241   
  4273         -PRIVATE int strhash(x)
  4274         -char *x;
         4242  +PRIVATE int strhash(const char *x)
  4275   4243   {
  4276   4244     int h = 0;
  4277   4245     while( *x) h = h*13 + *(x++);
  4278   4246     return h;
  4279   4247   }
  4280   4248   
  4281   4249   /* Works like strdup, sort of.  Save a string in malloced memory, but
  4282   4250   ** keep strings in a table so that the same string is not in more
  4283   4251   ** than one place.
  4284   4252   */
  4285         -char *Strsafe(y)
  4286         -char *y;
         4253  +const char *Strsafe(const char *y)
  4287   4254   {
  4288         -  char *z;
         4255  +  const char *z;
         4256  +  char *cpy;
  4289   4257   
  4290   4258     if( y==0 ) return 0;
  4291   4259     z = Strsafe_find(y);
  4292         -  if( z==0 && (z=malloc( lemonStrlen(y)+1 ))!=0 ){
  4293         -    strcpy(z,y);
         4260  +  if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
         4261  +    strcpy(cpy,y);
         4262  +    z = cpy;
  4294   4263       Strsafe_insert(z);
  4295   4264     }
  4296   4265     MemoryCheck(z);
  4297   4266     return z;
  4298   4267   }
  4299   4268   
  4300   4269   /* There is one instance of the following structure for each
................................................................................
  4309   4278     struct s_x1node **ht;  /* Hash table for lookups */
  4310   4279   };
  4311   4280   
  4312   4281   /* There is one instance of this structure for every data element
  4313   4282   ** in an associative array of type "x1".
  4314   4283   */
  4315   4284   typedef struct s_x1node {
  4316         -  char *data;                  /* The data */
         4285  +  const char *data;        /* The data */
  4317   4286     struct s_x1node *next;   /* Next entry with the same hash */
  4318   4287     struct s_x1node **from;  /* Previous link */
  4319   4288   } x1node;
  4320   4289   
  4321   4290   /* There is only one instance of the array, which is the following */
  4322   4291   static struct s_x1 *x1a;
  4323   4292   
................................................................................
  4338   4307         x1a->ht = (x1node**)&(x1a->tbl[1024]);
  4339   4308         for(i=0; i<1024; i++) x1a->ht[i] = 0;
  4340   4309       }
  4341   4310     }
  4342   4311   }
  4343   4312   /* Insert a new record into the array.  Return TRUE if successful.
  4344   4313   ** Prior data with the same key is NOT overwritten */
  4345         -int Strsafe_insert(data)
  4346         -char *data;
         4314  +int Strsafe_insert(const char *data)
  4347   4315   {
  4348   4316     x1node *np;
  4349   4317     int h;
  4350   4318     int ph;
  4351   4319   
  4352   4320     if( x1a==0 ) return 0;
  4353   4321     ph = strhash(data);
................................................................................
  4395   4363     x1a->ht[h] = np;
  4396   4364     np->from = &(x1a->ht[h]);
  4397   4365     return 1;
  4398   4366   }
  4399   4367   
  4400   4368   /* Return a pointer to data assigned to the given key.  Return NULL
  4401   4369   ** if no such key. */
  4402         -char *Strsafe_find(key)
  4403         -char *key;
         4370  +const char *Strsafe_find(const char *key)
  4404   4371   {
  4405   4372     int h;
  4406   4373     x1node *np;
  4407   4374   
  4408   4375     if( x1a==0 ) return 0;
  4409   4376     h = strhash(key) & (x1a->size-1);
  4410   4377     np = x1a->ht[h];
................................................................................
  4414   4381     }
  4415   4382     return np ? np->data : 0;
  4416   4383   }
  4417   4384   
  4418   4385   /* Return a pointer to the (terminal or nonterminal) symbol "x".
  4419   4386   ** Create a new symbol if this is the first time "x" has been seen.
  4420   4387   */
  4421         -struct symbol *Symbol_new(x)
  4422         -char *x;
         4388  +struct symbol *Symbol_new(const char *x)
  4423   4389   {
  4424   4390     struct symbol *sp;
  4425   4391   
  4426   4392     sp = Symbol_find(x);
  4427   4393     if( sp==0 ){
  4428   4394       sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
  4429   4395       MemoryCheck(sp);
................................................................................
  4451   4417   ** must sort before symbols that begin with lower case letters
  4452   4418   ** (non-terminals).  Other than that, the order does not matter.
  4453   4419   **
  4454   4420   ** We find experimentally that leaving the symbols in their original
  4455   4421   ** order (the order they appeared in the grammar file) gives the
  4456   4422   ** smallest parser tables in SQLite.
  4457   4423   */
  4458         -int Symbolcmpp(struct symbol **a, struct symbol **b){
         4424  +int Symbolcmpp(const void *_a, const void *_b)
         4425  +{
         4426  +  const struct symbol **a = (const struct symbol **) _a;
         4427  +  const struct symbol **b = (const struct symbol **) _b;
  4459   4428     int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
  4460   4429     int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
  4461   4430     assert( i1!=i2 || strcmp((**a).name,(**b).name)==0 );
  4462   4431     return i1-i2;
  4463   4432   }
  4464   4433   
  4465   4434   /* There is one instance of the following structure for each
................................................................................
  4474   4443     struct s_x2node **ht;  /* Hash table for lookups */
  4475   4444   };
  4476   4445   
  4477   4446   /* There is one instance of this structure for every data element
  4478   4447   ** in an associative array of type "x2".
  4479   4448   */
  4480   4449   typedef struct s_x2node {
  4481         -  struct symbol *data;                  /* The data */
  4482         -  char *key;                   /* The key */
         4450  +  struct symbol *data;     /* The data */
         4451  +  const char *key;         /* The key */
  4483   4452     struct s_x2node *next;   /* Next entry with the same hash */
  4484   4453     struct s_x2node **from;  /* Previous link */
  4485   4454   } x2node;
  4486   4455   
  4487   4456   /* There is only one instance of the array, which is the following */
  4488   4457   static struct s_x2 *x2a;
  4489   4458   
................................................................................
  4504   4473         x2a->ht = (x2node**)&(x2a->tbl[128]);
  4505   4474         for(i=0; i<128; i++) x2a->ht[i] = 0;
  4506   4475       }
  4507   4476     }
  4508   4477   }
  4509   4478   /* Insert a new record into the array.  Return TRUE if successful.
  4510   4479   ** Prior data with the same key is NOT overwritten */
  4511         -int Symbol_insert(data,key)
  4512         -struct symbol *data;
  4513         -char *key;
         4480  +int Symbol_insert(struct symbol *data, const char *key)
  4514   4481   {
  4515   4482     x2node *np;
  4516   4483     int h;
  4517   4484     int ph;
  4518   4485   
  4519   4486     if( x2a==0 ) return 0;
  4520   4487     ph = strhash(key);
................................................................................
  4564   4531     x2a->ht[h] = np;
  4565   4532     np->from = &(x2a->ht[h]);
  4566   4533     return 1;
  4567   4534   }
  4568   4535   
  4569   4536   /* Return a pointer to data assigned to the given key.  Return NULL
  4570   4537   ** if no such key. */
  4571         -struct symbol *Symbol_find(key)
  4572         -char *key;
         4538  +struct symbol *Symbol_find(const char *key)
  4573   4539   {
  4574   4540     int h;
  4575   4541     x2node *np;
  4576   4542   
  4577   4543     if( x2a==0 ) return 0;
  4578   4544     h = strhash(key) & (x2a->size-1);
  4579   4545     np = x2a->ht[h];
................................................................................
  4581   4547       if( strcmp(np->key,key)==0 ) break;
  4582   4548       np = np->next;
  4583   4549     }
  4584   4550     return np ? np->data : 0;
  4585   4551   }
  4586   4552   
  4587   4553   /* Return the n-th data.  Return NULL if n is out of range. */
  4588         -struct symbol *Symbol_Nth(n)
  4589         -int n;
         4554  +struct symbol *Symbol_Nth(int n)
  4590   4555   {
  4591   4556     struct symbol *data;
  4592   4557     if( x2a && n>0 && n<=x2a->count ){
  4593   4558       data = x2a->tbl[n-1].data;
  4594   4559     }else{
  4595   4560       data = 0;
  4596   4561     }
................................................................................
  4616   4581     if( array ){
  4617   4582       for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
  4618   4583     }
  4619   4584     return array;
  4620   4585   }
  4621   4586   
  4622   4587   /* Compare two configurations */
  4623         -int Configcmp(a,b)
  4624         -struct config *a;
  4625         -struct config *b;
         4588  +int Configcmp(const char *_a,const char *_b)
  4626   4589   {
         4590  +  const struct config *a = (struct config *) _a;
         4591  +  const struct config *b = (struct config *) _b;
  4627   4592     int x;
  4628   4593     x = a->rp->index - b->rp->index;
  4629   4594     if( x==0 ) x = a->dot - b->dot;
  4630   4595     return x;
  4631   4596   }
  4632   4597   
  4633   4598   /* Compare two states */
  4634         -PRIVATE int statecmp(a,b)
  4635         -struct config *a;
  4636         -struct config *b;
         4599  +PRIVATE int statecmp(struct config *a, struct config *b)
  4637   4600   {
  4638   4601     int rc;
  4639   4602     for(rc=0; rc==0 && a && b;  a=a->bp, b=b->bp){
  4640   4603       rc = a->rp->index - b->rp->index;
  4641   4604       if( rc==0 ) rc = a->dot - b->dot;
  4642   4605     }
  4643   4606     if( rc==0 ){
................................................................................
  4644   4607       if( a ) rc = 1;
  4645   4608       if( b ) rc = -1;
  4646   4609     }
  4647   4610     return rc;
  4648   4611   }
  4649   4612   
  4650   4613   /* Hash a state */
  4651         -PRIVATE int statehash(a)
  4652         -struct config *a;
         4614  +PRIVATE int statehash(struct config *a)
  4653   4615   {
  4654   4616     int h=0;
  4655   4617     while( a ){
  4656   4618       h = h*571 + a->rp->index*37 + a->dot;
  4657   4619       a = a->bp;
  4658   4620     }
  4659   4621     return h;
  4660   4622   }
  4661   4623   
  4662   4624   /* Allocate a new state structure */
  4663   4625   struct state *State_new()
  4664   4626   {
  4665         -  struct state *new;
  4666         -  new = (struct state *)calloc(1, sizeof(struct state) );
  4667         -  MemoryCheck(new);
  4668         -  return new;
         4627  +  struct state *newstate;
         4628  +  newstate = (struct state *)calloc(1, sizeof(struct state) );
         4629  +  MemoryCheck(newstate);
         4630  +  return newstate;
  4669   4631   }
  4670   4632   
  4671   4633   /* There is one instance of the following structure for each
  4672   4634   ** associative array of type "x3".
  4673   4635   */
  4674   4636   struct s_x3 {
  4675   4637     int size;               /* The number of available slots. */
................................................................................
  4710   4672         x3a->ht = (x3node**)&(x3a->tbl[128]);
  4711   4673         for(i=0; i<128; i++) x3a->ht[i] = 0;
  4712   4674       }
  4713   4675     }
  4714   4676   }
  4715   4677   /* Insert a new record into the array.  Return TRUE if successful.
  4716   4678   ** Prior data with the same key is NOT overwritten */
  4717         -int State_insert(data,key)
  4718         -struct state *data;
  4719         -struct config *key;
         4679  +int State_insert(struct state *data, struct config *key)
  4720   4680   {
  4721   4681     x3node *np;
  4722   4682     int h;
  4723   4683     int ph;
  4724   4684   
  4725   4685     if( x3a==0 ) return 0;
  4726   4686     ph = statehash(key);
................................................................................
  4770   4730     x3a->ht[h] = np;
  4771   4731     np->from = &(x3a->ht[h]);
  4772   4732     return 1;
  4773   4733   }
  4774   4734   
  4775   4735   /* Return a pointer to data assigned to the given key.  Return NULL
  4776   4736   ** if no such key. */
  4777         -struct state *State_find(key)
  4778         -struct config *key;
         4737  +struct state *State_find(struct config *key)
  4779   4738   {
  4780   4739     int h;
  4781   4740     x3node *np;
  4782   4741   
  4783   4742     if( x3a==0 ) return 0;
  4784   4743     h = statehash(key) & (x3a->size-1);
  4785   4744     np = x3a->ht[h];
................................................................................
  4803   4762     if( array ){
  4804   4763       for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
  4805   4764     }
  4806   4765     return array;
  4807   4766   }
  4808   4767   
  4809   4768   /* Hash a configuration */
  4810         -PRIVATE int confighash(a)
  4811         -struct config *a;
         4769  +PRIVATE int confighash(struct config *a)
  4812   4770   {
  4813   4771     int h=0;
  4814   4772     h = h*571 + a->rp->index*37 + a->dot;
  4815   4773     return h;
  4816   4774   }
  4817   4775   
  4818   4776   /* There is one instance of the following structure for each
................................................................................
  4856   4814         x4a->ht = (x4node**)&(x4a->tbl[64]);
  4857   4815         for(i=0; i<64; i++) x4a->ht[i] = 0;
  4858   4816       }
  4859   4817     }
  4860   4818   }
  4861   4819   /* Insert a new record into the array.  Return TRUE if successful.
  4862   4820   ** Prior data with the same key is NOT overwritten */
  4863         -int Configtable_insert(data)
  4864         -struct config *data;
         4821  +int Configtable_insert(struct config *data)
  4865   4822   {
  4866   4823     x4node *np;
  4867   4824     int h;
  4868   4825     int ph;
  4869   4826   
  4870   4827     if( x4a==0 ) return 0;
  4871   4828     ph = confighash(data);
  4872   4829     h = ph & (x4a->size-1);
  4873   4830     np = x4a->ht[h];
  4874   4831     while( np ){
  4875         -    if( Configcmp(np->data,data)==0 ){
         4832  +    if( Configcmp((const char *) np->data,(const char *) data)==0 ){
  4876   4833         /* An existing entry with the same key is found. */
  4877   4834         /* Fail because overwrite is not allows. */
  4878   4835         return 0;
  4879   4836       }
  4880   4837       np = np->next;
  4881   4838     }
  4882   4839     if( x4a->count>=x4a->size ){
................................................................................
  4913   4870     x4a->ht[h] = np;
  4914   4871     np->from = &(x4a->ht[h]);
  4915   4872     return 1;
  4916   4873   }
  4917   4874   
  4918   4875   /* Return a pointer to data assigned to the given key.  Return NULL
  4919   4876   ** if no such key. */
  4920         -struct config *Configtable_find(key)
  4921         -struct config *key;
         4877  +struct config *Configtable_find(struct config *key)
  4922   4878   {
  4923   4879     int h;
  4924   4880     x4node *np;
  4925   4881   
  4926   4882     if( x4a==0 ) return 0;
  4927   4883     h = confighash(key) & (x4a->size-1);
  4928   4884     np = x4a->ht[h];
  4929   4885     while( np ){
  4930         -    if( Configcmp(np->data,key)==0 ) break;
         4886  +    if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
  4931   4887       np = np->next;
  4932   4888     }
  4933   4889     return np ? np->data : 0;
  4934   4890   }
  4935   4891   
  4936   4892   /* Remove all data from the table.  Pass each data to the function "f"
  4937   4893   ** as it is removed.  ("f" may be null to avoid this step.) */
  4938         -void Configtable_clear(f)
  4939         -int(*f)(/* struct config * */);
         4894  +void Configtable_clear(int(*f)(struct config *))
  4940   4895   {
  4941   4896     int i;
  4942   4897     if( x4a==0 || x4a->count==0 ) return;
  4943   4898     if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
  4944   4899     for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
  4945   4900     x4a->count = 0;
  4946   4901     return;
  4947   4902   }

Changes to tool/mksqlite3c.tcl.

   208    208   # used subroutines first in order to help the compiler find
   209    209   # inlining opportunities.
   210    210   #
   211    211   foreach file {
   212    212      sqliteInt.h
   213    213   
   214    214      global.c
          215  +   ctime.c
   215    216      status.c
   216    217      date.c
   217    218      os.c
   218    219   
   219    220      fault.c
   220    221      mem0.c
   221    222      mem1.c