/ Check-in [de7083cf]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Merge all trunk enhancements and fixes into the sessions branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: de7083cfe2bb00b689bec6bcc75e994f564ceda6
User & Date: drh 2015-05-05 17:12:27
Context
2015-05-07
12:29
Merge in the 3.8.10 release changes. check-in: 0404ef88 user: drh tags: sessions
2015-05-05
17:12
Merge all trunk enhancements and fixes into the sessions branch. check-in: de7083cf user: drh tags: sessions
16:57
Fix a minor coding inefficiency found during pre-release inspection. check-in: cc50883d user: drh tags: trunk
2015-04-23
17:22
Modify the sqlite3session_diff() API so that tables with no PRIMARY KEYs are ignored. This matches the other sessions APIs. Also change sqlite3session_diff() so that it returns SQLITE_SCHEMA, instead of SQLITE_ERROR, if the tables being compared do not have compatible schemas. check-in: aada0ad0 user: dan tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to Makefile.in.

   165    165   
   166    166   USE_AMALGAMATION = @USE_AMALGAMATION@
   167    167   
   168    168   # Object files for the SQLite library (non-amalgamation).
   169    169   #
   170    170   LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
   171    171            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
   172         -         callback.lo complete.lo ctime.lo date.lo delete.lo \
          172  +         callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \
   173    173            expr.lo fault.lo fkey.lo \
   174    174            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   175    175            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   176    176            fts3_tokenize_vtab.lo \
   177    177            fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   178    178            func.lo global.lo hash.lo \
   179    179            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
................................................................................
   212    212     $(TOP)/src/btree.h \
   213    213     $(TOP)/src/btreeInt.h \
   214    214     $(TOP)/src/build.c \
   215    215     $(TOP)/src/callback.c \
   216    216     $(TOP)/src/complete.c \
   217    217     $(TOP)/src/ctime.c \
   218    218     $(TOP)/src/date.c \
          219  +  $(TOP)/src/dbstat.c \
   219    220     $(TOP)/src/delete.c \
   220    221     $(TOP)/src/expr.c \
   221    222     $(TOP)/src/fault.c \
   222    223     $(TOP)/src/fkey.c \
   223    224     $(TOP)/src/func.c \
   224    225     $(TOP)/src/global.c \
   225    226     $(TOP)/src/hash.c \
................................................................................
   392    393     $(TOP)/src/test_pcache.c \
   393    394     $(TOP)/src/test_quota.c \
   394    395     $(TOP)/src/test_rtree.c \
   395    396     $(TOP)/src/test_schema.c \
   396    397     $(TOP)/src/test_server.c \
   397    398     $(TOP)/src/test_superlock.c \
   398    399     $(TOP)/src/test_syscall.c \
   399         -  $(TOP)/src/test_stat.c \
   400    400     $(TOP)/src/test_tclvar.c \
   401    401     $(TOP)/src/test_thread.c \
   402    402     $(TOP)/src/test_vfs.c \
   403    403     $(TOP)/src/test_wsd.c       \
   404    404     $(TOP)/ext/fts3/fts3_term.c \
   405    405     $(TOP)/ext/fts3/fts3_test.c  \
   406    406     $(TOP)/ext/session/test_session.c
................................................................................
   427    427     $(TOP)/src/attach.c \
   428    428     $(TOP)/src/backup.c \
   429    429     $(TOP)/src/bitvec.c \
   430    430     $(TOP)/src/btree.c \
   431    431     $(TOP)/src/build.c \
   432    432     $(TOP)/src/ctime.c \
   433    433     $(TOP)/src/date.c \
          434  +  $(TOP)/src/dbstat.c \
   434    435     $(TOP)/src/expr.c \
   435    436     $(TOP)/src/func.c \
   436    437     $(TOP)/src/insert.c \
   437    438     $(TOP)/src/wal.c \
   438    439     $(TOP)/src/main.c \
   439    440     $(TOP)/src/mem5.c \
   440    441     $(TOP)/src/os.c \
................................................................................
   541    542   		-avoid-version
   542    543   
   543    544   sqlite3$(TEXE):	$(TOP)/src/shell.c libsqlite3.la sqlite3.h
   544    545   	$(LTLINK) $(READLINE_FLAGS) \
   545    546   		-o $@ $(TOP)/src/shell.c libsqlite3.la \
   546    547   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   547    548   
   548         -sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          549  +sqldiff$(TEXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
   549    550   	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS)
   550    551   
   551         -fuzzershell$(EXE):	$(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h
          552  +fuzzershell$(TEXE):	$(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h
   552    553   	$(LTLINK) -o $@ $(TOP)/tool/fuzzershell.c	sqlite3.c $(TLIBS)
   553    554   
   554         -mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
          555  +mptester$(TEXE):	sqlite3.c $(TOP)/mptest/mptest.c
   555    556   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   556    557   		$(TLIBS) -rpath "$(libdir)"
   557    558   
   558         -MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   559         -MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
   560         -mptest:	mptester$(EXE)
          559  +MPTEST1=./mptester$(TEXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
          560  +MPTEST2=./mptester$(TEXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
          561  +mptest:	mptester$(TEXE)
   561    562   	rm -f mptest.db
   562    563   	$(MPTEST1) --journalmode DELETE
   563    564   	$(MPTEST2) --journalmode WAL
   564    565   	$(MPTEST1) --journalmode WAL
   565    566   	$(MPTEST2) --journalmode PERSIST
   566    567   	$(MPTEST1) --journalmode PERSIST
   567    568   	$(MPTEST2) --journalmode TRUNCATE
................................................................................
   658    659   
   659    660   ctime.lo:	$(TOP)/src/ctime.c $(HDR)
   660    661   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/ctime.c
   661    662   
   662    663   date.lo:	$(TOP)/src/date.c $(HDR)
   663    664   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/date.c
   664    665   
          666  +dbstat.lo:	$(TOP)/src/dbstat.c $(HDR)
          667  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/dbstat.c
          668  +
   665    669   delete.lo:	$(TOP)/src/delete.c $(HDR)
   666    670   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/delete.c
   667    671   
   668    672   expr.lo:	$(TOP)/src/expr.c $(HDR)
   669    673   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/expr.c
   670    674   
   671    675   fault.lo:	$(TOP)/src/fault.c $(HDR)
................................................................................
   960    964   TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
   961    965   
   962    966   testfixture$(TEXE):	$(TESTFIXTURE_SRC)
   963    967   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \
   964    968   		-o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS)
   965    969   
   966    970   # A very detailed test running most or all test cases
   967         -fulltest:	testfixture$(TEXE) sqlite3$(TEXE)
          971  +fulltest:	testfixture$(TEXE) sqlite3$(TEXE) fuzztest
   968    972   	./testfixture$(TEXE) $(TOP)/test/all.test
   969    973   
   970    974   # Really really long testing
   971         -soaktest:	testfixture$(TEXE) sqlite3$(TEXE)
          975  +soaktest:	testfixture$(TEXE) sqlite3$(TEXE) fuzzoomtest
   972    976   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
   973    977   
   974    978   # Do extra testing but not aeverything.
   975    979   fulltestonly:	testfixture$(TEXE) sqlite3$(TEXE)
   976    980   	./testfixture$(TEXE) $(TOP)/test/full.test
   977    981   
          982  +# Fuzz testing
          983  +fuzztest:	fuzzershell$(TEXE)
          984  +	./fuzzershell$(TEXE) $(TOP)/test/fuzzdata1.txt $(TOP)/test/fuzzdata2.txt
          985  +
          986  +fuzzoomtest:	fuzzershell$(TEXE)
          987  +	./fuzzershell$(TEXE) -f $(TOP)/test/fuzzdata1.txt --oom
          988  +
   978    989   # This is the common case.  Run many tests but not those that take
   979    990   # a really long time.
   980    991   #
   981         -test:	testfixture$(TEXE) sqlite3$(TEXE)
          992  +test:	testfixture$(TEXE) sqlite3$(TEXE) fuzztest
   982    993   	./testfixture$(TEXE) $(TOP)/test/veryquick.test
   983    994   
   984    995   # Run a test using valgrind.  This can take a really long time
   985    996   # because valgrind is so much slower than a native machine.
   986    997   #
   987         -valgrindtest:	testfixture$(TEXE) sqlite3$(TEXE)
          998  +valgrindtest:	testfixture$(TEXE) sqlite3$(TEXE) fuzzershell$(TEXE)
          999  +	valgrind -v ./fuzzershell$(TEXE) -f $(TOP)/test/fuzzdata1.txt
   988   1000   	OMIT_MISUSE=1 valgrind -v ./testfixture$(TEXE) $(TOP)/test/permutations.test valgrind
   989   1001   
   990   1002   # A very fast test that checks basic sanity.  The name comes from
   991   1003   # the 60s-era electronics testing:  "Turn it on and see if smoke
   992   1004   # comes out."
   993   1005   #
   994         -smoketest:	testfixture$(TEXE)
         1006  +smoketest:	testfixture$(TEXE) fuzzershell$(TEXE)
   995   1007   	./testfixture$(TEXE) $(TOP)/test/main.test
   996   1008   
   997         -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
         1009  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
   998   1010   	echo "#define TCLSH 2" > $@
   999         -	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@
         1011  +	echo "#define SQLITE_ENABLE_DBSTAT_VTAB" >> $@
         1012  +	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
  1000   1013   	echo "static const char *tclsh_main_loop(void){" >> $@
  1001   1014   	echo "static const char *zMainloop = " >> $@
  1002   1015   	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
  1003   1016   	echo "; return zMainloop; }" >> $@
  1004   1017   
  1005   1018   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
  1006   1019   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)

Changes to Makefile.msc.

   143    143   !ENDIF
   144    144   
   145    145   # Set this to non-0 to use the Win32 native heap subsystem.
   146    146   #
   147    147   !IFNDEF WIN32HEAP
   148    148   WIN32HEAP = 0
   149    149   !ENDIF
          150  +
          151  +# Set this to non-0 to enable OSTRACE() macros, which can be useful when
          152  +# debugging.
          153  +#
          154  +!IFNDEF OSTRACE
          155  +OSTRACE = 0
          156  +!ENDIF
   150    157   
   151    158   # Set this to one of the following values to enable various debugging
   152    159   # features.  Each level includes the debugging options from the previous
   153    160   # levels.  Currently, the recognized values for DEBUG are:
   154    161   #
   155    162   # 0 == NDEBUG: Disables assert() and other runtime diagnostics.
   156    163   # 1 == SQLITE_ENABLE_API_ARMOR: extra attempts to detect misuse of the API.
................................................................................
   492    499   !ENDIF
   493    500   
   494    501   !IF $(DEBUG)>2
   495    502   TCC = $(TCC) -DSQLITE_DEBUG
   496    503   RCC = $(RCC) -DSQLITE_DEBUG
   497    504   !ENDIF
   498    505   
   499         -!IF $(DEBUG)>4
   500         -TCC = $(TCC) -DSQLITE_DEBUG_OS_TRACE=1
   501         -RCC = $(RCC) -DSQLITE_DEBUG_OS_TRACE=1
          506  +!IF $(DEBUG)>4 || $(OSTRACE)!=0
          507  +TCC = $(TCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
          508  +RCC = $(RCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   502    509   !ENDIF
   503    510   
   504    511   !IF $(DEBUG)>5
   505    512   TCC = $(TCC) -DSQLITE_ENABLE_IOTRACE
   506    513   RCC = $(RCC) -DSQLITE_ENABLE_IOTRACE
   507    514   !ENDIF
   508    515   
................................................................................
   814    821   # You should not have to change anything below this line
   815    822   ###############################################################################
   816    823   
   817    824   # Object files for the SQLite library (non-amalgamation).
   818    825   #
   819    826   LIBOBJS0 = vdbe.lo parse.lo alter.lo analyze.lo attach.lo auth.lo \
   820    827            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
   821         -         callback.lo complete.lo ctime.lo date.lo delete.lo \
          828  +         callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \
   822    829            expr.lo fault.lo fkey.lo \
   823    830            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   824    831            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   825    832            fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   826    833            func.lo global.lo hash.lo \
   827    834            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   828    835            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
................................................................................
   871    878     $(TOP)\src\btree.h \
   872    879     $(TOP)\src\btreeInt.h \
   873    880     $(TOP)\src\build.c \
   874    881     $(TOP)\src\callback.c \
   875    882     $(TOP)\src\complete.c \
   876    883     $(TOP)\src\ctime.c \
   877    884     $(TOP)\src\date.c \
          885  +  $(TOP)\src\dbstat.c \
   878    886     $(TOP)\src\delete.c \
   879    887     $(TOP)\src\expr.c \
   880    888     $(TOP)\src\fault.c \
   881    889     $(TOP)\src\fkey.c \
   882    890     $(TOP)\src\func.c \
   883    891     $(TOP)\src\global.c \
   884    892     $(TOP)\src\hash.c \
................................................................................
  1050   1058     $(TOP)\src\test_pcache.c \
  1051   1059     $(TOP)\src\test_quota.c \
  1052   1060     $(TOP)\src\test_rtree.c \
  1053   1061     $(TOP)\src\test_schema.c \
  1054   1062     $(TOP)\src\test_server.c \
  1055   1063     $(TOP)\src\test_superlock.c \
  1056   1064     $(TOP)\src\test_syscall.c \
  1057         -  $(TOP)\src\test_stat.c \
  1058   1065     $(TOP)\src\test_tclvar.c \
  1059   1066     $(TOP)\src\test_thread.c \
  1060   1067     $(TOP)\src\test_vfs.c \
  1061   1068     $(TOP)\src\test_wsd.c \
  1062   1069     $(TOP)\ext\fts3\fts3_term.c \
  1063   1070     $(TOP)\ext\fts3\fts3_test.c \
  1064   1071     $(TOP)\ext\session\test_session.c
................................................................................
  1087   1094     $(TOP)\src\attach.c \
  1088   1095     $(TOP)\src\backup.c \
  1089   1096     $(TOP)\src\bitvec.c \
  1090   1097     $(TOP)\src\btree.c \
  1091   1098     $(TOP)\src\build.c \
  1092   1099     $(TOP)\src\ctime.c \
  1093   1100     $(TOP)\src\date.c \
         1101  +  $(TOP)\src\dbstat.c \
  1094   1102     $(TOP)\src\expr.c \
  1095   1103     $(TOP)\src\func.c \
  1096   1104     $(TOP)\src\insert.c \
  1097   1105     $(TOP)\src\wal.c \
  1098   1106     $(TOP)\src\main.c \
  1099   1107     $(TOP)\src\mem5.c \
  1100   1108     $(TOP)\src\os.c \
................................................................................
  1340   1348   
  1341   1349   ctime.lo:	$(TOP)\src\ctime.c $(HDR)
  1342   1350   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\ctime.c
  1343   1351   
  1344   1352   date.lo:	$(TOP)\src\date.c $(HDR)
  1345   1353   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\date.c
  1346   1354   
         1355  +dbstat.lo:	$(TOP)\src\date.c $(HDR)
         1356  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\dbstat.c
         1357  +
  1347   1358   delete.lo:	$(TOP)\src\delete.c $(HDR)
  1348   1359   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\delete.c
  1349   1360   
  1350   1361   expr.lo:	$(TOP)\src\expr.c $(HDR)
  1351   1362   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\expr.c
  1352   1363   
  1353   1364   fault.lo:	$(TOP)\src\fault.c $(HDR)
................................................................................
  1648   1659   		-DBUILD_sqlite -I$(TCLINCDIR) \
  1649   1660   		$(TESTFIXTURE_SRC) \
  1650   1661   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1651   1662   
  1652   1663   extensiontest: testfixture.exe testloadext.dll
  1653   1664   	.\testfixture.exe $(TOP)\test\loadext.test
  1654   1665   
  1655         -fulltest:	testfixture.exe sqlite3.exe
         1666  +fulltest:	testfixture.exe sqlite3.exe fuzztest
  1656   1667   	.\testfixture.exe $(TOP)\test\all.test
  1657   1668   
  1658         -soaktest:	testfixture.exe sqlite3.exe
         1669  +soaktest:	testfixture.exe sqlite3.exe fuzzoomtest
  1659   1670   	.\testfixture.exe $(TOP)\test\all.test -soak=1
  1660   1671   
  1661         -fulltestonly:	testfixture.exe sqlite3.exe
         1672  +fulltestonly:	testfixture.exe sqlite3.exe fuzztest
  1662   1673   	.\testfixture.exe $(TOP)\test\full.test
  1663   1674   
  1664   1675   queryplantest:	testfixture.exe sqlite3.exe
  1665   1676   	.\testfixture.exe $(TOP)\test\permutations.test queryplanner
  1666   1677   
  1667         -test:	testfixture.exe sqlite3.exe
         1678  +fuzztest:	fuzzershell.exe
         1679  +	.\fuzzershell.exe $(TOP)\test\fuzzdata1.txt $(TOP)\test\fuzzdata2.txt
         1680  +
         1681  +fuzzoomtest:	fuzzershell.exe
         1682  +	.\fuzzershell.exe -f $(TOP)\test\fuzzdata1.txt --oom
         1683  +
         1684  +test:	testfixture.exe sqlite3.exe fuzztest
  1668   1685   	.\testfixture.exe $(TOP)\test\veryquick.test
  1669   1686   
  1670   1687   smoketest:	testfixture.exe
  1671   1688   	.\testfixture.exe $(TOP)\test\main.test
  1672   1689   
  1673         -sqlite3_analyzer.c: $(SQLITE3C) $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
  1674         -	copy $(SQLITE3C) + $(TOP)\src\test_stat.c + $(TOP)\src\tclsqlite.c $@
         1690  +sqlite3_analyzer.c: $(SQLITE3C) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
         1691  +	copy $(SQLITE3C) + $(TOP)\src\tclsqlite.c $@
  1675   1692   	echo static const char *tclsh_main_loop(void){ >> $@
  1676   1693   	echo static const char *zMainloop = >> $@
  1677   1694   	$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
  1678   1695   	echo ; return zMainloop; } >> $@
  1679   1696   
  1680   1697   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  1681         -	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
         1698  +	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -DSQLITE_ENABLE_DBSTAT_VTAB -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
  1682   1699   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1683   1700   
  1684   1701   testloadext.lo:	$(TOP)\src\test_loadext.c
  1685   1702   	$(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c
  1686   1703   
  1687   1704   testloadext.dll: testloadext.lo
  1688   1705   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /OUT:$@ testloadext.lo

Changes to ext/fts3/fts3.c.

   503    503   
   504    504     /* Invoke the tokenizer destructor to free the tokenizer. */
   505    505     p->pTokenizer->pModule->xDestroy(p->pTokenizer);
   506    506   
   507    507     sqlite3_free(p);
   508    508     return SQLITE_OK;
   509    509   }
          510  +
          511  +/*
          512  +** Write an error message into *pzErr
          513  +*/
          514  +void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
          515  +  va_list ap;
          516  +  sqlite3_free(*pzErr);
          517  +  va_start(ap, zFormat);
          518  +  *pzErr = sqlite3_vmprintf(zFormat, ap);
          519  +  va_end(ap);
          520  +}
   510    521   
   511    522   /*
   512    523   ** Construct one or more SQL statements from the format string given
   513    524   ** and then evaluate those statements. The success code is written
   514    525   ** into *pRc.
   515    526   **
   516    527   ** If *pRc is initially non-zero then this routine is a no-op.
................................................................................
  1035   1046   
  1036   1047     zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
  1037   1048     if( !zSql ){
  1038   1049       rc = SQLITE_NOMEM;
  1039   1050     }else{
  1040   1051       rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  1041   1052       if( rc!=SQLITE_OK ){
  1042         -      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
         1053  +      sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
  1043   1054       }
  1044   1055     }
  1045   1056     sqlite3_free(zSql);
  1046   1057   
  1047   1058     if( rc==SQLITE_OK ){
  1048   1059       const char **azCol;           /* Output array */
  1049   1060       int nStr = 0;                 /* Size of all column names (incl. 0x00) */
................................................................................
  1203   1214           for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
  1204   1215             struct Fts4Option *pOp = &aFts4Opt[iOpt];
  1205   1216             if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
  1206   1217               break;
  1207   1218             }
  1208   1219           }
  1209   1220           if( iOpt==SizeofArray(aFts4Opt) ){
  1210         -          *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
         1221  +          sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
  1211   1222             rc = SQLITE_ERROR;
  1212   1223           }else{
  1213   1224             switch( iOpt ){
  1214   1225               case 0:               /* MATCHINFO */
  1215   1226                 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
  1216         -                *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
         1227  +                sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
  1217   1228                   rc = SQLITE_ERROR;
  1218   1229                 }
  1219   1230                 bNoDocsize = 1;
  1220   1231                 break;
  1221   1232   
  1222   1233               case 1:               /* PREFIX */
  1223   1234                 sqlite3_free(zPrefix);
................................................................................
  1237   1248                 zVal = 0;
  1238   1249                 break;
  1239   1250   
  1240   1251               case 4:               /* ORDER */
  1241   1252                 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
  1242   1253                  && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
  1243   1254                 ){
  1244         -                *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
         1255  +                sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
  1245   1256                   rc = SQLITE_ERROR;
  1246   1257                 }
  1247   1258                 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
  1248   1259                 break;
  1249   1260   
  1250   1261               case 5:              /* CONTENT */
  1251   1262                 sqlite3_free(zContent);
................................................................................
  1323   1334       if( rc!=SQLITE_OK ) goto fts3_init_out;
  1324   1335     }
  1325   1336     assert( pTokenizer );
  1326   1337   
  1327   1338     rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
  1328   1339     if( rc==SQLITE_ERROR ){
  1329   1340       assert( zPrefix );
  1330         -    *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
         1341  +    sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
  1331   1342     }
  1332   1343     if( rc!=SQLITE_OK ) goto fts3_init_out;
  1333   1344   
  1334   1345     /* Allocate and populate the Fts3Table structure. */
  1335   1346     nByte = sizeof(Fts3Table) +                  /* Fts3Table */
  1336   1347             nCol * sizeof(char *) +              /* azColumn */
  1337   1348             nIndex * sizeof(struct Fts3Index) +  /* aIndex */
................................................................................
  1405   1416           sqlite3_free(zNot);
  1406   1417           azNotindexed[i] = 0;
  1407   1418         }
  1408   1419       }
  1409   1420     }
  1410   1421     for(i=0; i<nNotindexed; i++){
  1411   1422       if( azNotindexed[i] ){
  1412         -      *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]);
         1423  +      sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
  1413   1424         rc = SQLITE_ERROR;
  1414   1425       }
  1415   1426     }
  1416   1427   
  1417   1428     if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
  1418   1429       char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
  1419   1430       rc = SQLITE_ERROR;
  1420         -    *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
         1431  +    sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
  1421   1432     }
  1422   1433     p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
  1423   1434     p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
  1424   1435     if( rc!=SQLITE_OK ) goto fts3_init_out;
  1425   1436   
  1426   1437     /* If this is an xCreate call, create the underlying tables in the 
  1427   1438     ** database. TODO: For xConnect(), it could verify that said tables exist.

Changes to ext/fts3/fts3Int.h.

   535    535   int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
   536    536   
   537    537   #define fts3GetVarint32(p, piVal) (                                           \
   538    538     (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
   539    539   )
   540    540   
   541    541   /* fts3.c */
          542  +void sqlite3Fts3ErrMsg(char**,const char*,...);
   542    543   int sqlite3Fts3PutVarint(char *, sqlite3_int64);
   543    544   int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
   544    545   int sqlite3Fts3GetVarint32(const char *, int *);
   545    546   int sqlite3Fts3VarintLen(sqlite3_uint64);
   546    547   void sqlite3Fts3Dequote(char *);
   547    548   void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
   548    549   int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);

Changes to ext/fts3/fts3_aux.c.

   112    112     memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
   113    113     sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
   114    114   
   115    115     *ppVtab = (sqlite3_vtab *)p;
   116    116     return SQLITE_OK;
   117    117   
   118    118    bad_args:
   119         -  *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
          119  +  sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
   120    120     return SQLITE_ERROR;
   121    121   }
   122    122   
   123    123   /*
   124    124   ** This function does the work for both the xDisconnect and xDestroy methods.
   125    125   ** These tables have no persistent representation of their own, so xDisconnect
   126    126   ** and xDestroy are identical operations.

Changes to ext/fts3/fts3_expr.c.

  1018   1018       }
  1019   1019     }
  1020   1020   
  1021   1021     if( rc!=SQLITE_OK ){
  1022   1022       sqlite3Fts3ExprFree(*ppExpr);
  1023   1023       *ppExpr = 0;
  1024   1024       if( rc==SQLITE_TOOBIG ){
  1025         -      *pzErr = sqlite3_mprintf(
         1025  +      sqlite3Fts3ErrMsg(pzErr,
  1026   1026             "FTS expression tree is too large (maximum depth %d)", 
  1027   1027             SQLITE_FTS3_MAX_EXPR_DEPTH
  1028   1028         );
  1029   1029         rc = SQLITE_ERROR;
  1030   1030       }else if( rc==SQLITE_ERROR ){
  1031         -      *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
         1031  +      sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
  1032   1032       }
  1033   1033     }
  1034   1034   
  1035   1035     return rc;
  1036   1036   }
  1037   1037   
  1038   1038   /*

Changes to ext/fts3/fts3_snippet.c.

    23     23   #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
    24     24   #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
    25     25   #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
    26     26   #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
    27     27   #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
    28     28   #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
    29     29   #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
           30  +#define FTS3_MATCHINFO_LHITS     'y'        /* nCol*nPhrase values */
    30     31   
    31     32   /*
    32     33   ** The default value for the second argument to matchinfo(). 
    33     34   */
    34     35   #define FTS3_MATCHINFO_DEFAULT   "pcx"
    35     36   
    36     37   
................................................................................
   804    805       }else{
   805    806         p->aMatchinfo[iStart+i*3] = 0;
   806    807       }
   807    808     }
   808    809   
   809    810     return rc;
   810    811   }
          812  +
          813  +/*
          814  +** fts3ExprIterate() callback used to gather information for the matchinfo
          815  +** directive 'y'.
          816  +*/
          817  +static int fts3ExprLHitsCb(
          818  +  Fts3Expr *pExpr,                /* Phrase expression node */
          819  +  int iPhrase,                    /* Phrase number */
          820  +  void *pCtx                      /* Pointer to MatchInfo structure */
          821  +){
          822  +  MatchInfo *p = (MatchInfo *)pCtx;
          823  +  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
          824  +  int rc = SQLITE_OK;
          825  +  int iStart = iPhrase * p->nCol;
          826  +  Fts3Expr *pEof;                 /* Ancestor node already at EOF */
          827  +  
          828  +  /* This must be a phrase */
          829  +  assert( pExpr->pPhrase );
          830  +
          831  +  /* Initialize all output integers to zero. */
          832  +  memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol);
          833  +
          834  +  /* Check if this or any parent node is at EOF. If so, then all output
          835  +  ** values are zero.  */
          836  +  for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent);
          837  +
          838  +  if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
          839  +    Fts3Phrase *pPhrase = pExpr->pPhrase;
          840  +    char *pIter = pPhrase->doclist.pList;
          841  +    int iCol = 0;
          842  +
          843  +    while( 1 ){
          844  +      int nHit = fts3ColumnlistCount(&pIter);
          845  +      if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
          846  +        p->aMatchinfo[iStart + iCol] = (u32)nHit;
          847  +      }
          848  +      assert( *pIter==0x00 || *pIter==0x01 );
          849  +      if( *pIter!=0x01 ) break;
          850  +      pIter++;
          851  +      pIter += fts3GetVarint32(pIter, &iCol);
          852  +    }
          853  +  }
          854  +
          855  +  return rc;
          856  +}
   811    857   
   812    858   static int fts3MatchinfoCheck(
   813    859     Fts3Table *pTab, 
   814    860     char cArg,
   815    861     char **pzErr
   816    862   ){
   817    863     if( (cArg==FTS3_MATCHINFO_NPHRASE)
   818    864      || (cArg==FTS3_MATCHINFO_NCOL)
   819    865      || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
   820    866      || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
   821    867      || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
   822    868      || (cArg==FTS3_MATCHINFO_LCS)
   823    869      || (cArg==FTS3_MATCHINFO_HITS)
          870  +   || (cArg==FTS3_MATCHINFO_LHITS)
   824    871     ){
   825    872       return SQLITE_OK;
   826    873     }
   827         -  *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
          874  +  sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
   828    875     return SQLITE_ERROR;
   829    876   }
   830    877   
   831    878   static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
   832    879     int nVal;                       /* Number of integers output by cArg */
   833    880   
   834    881     switch( cArg ){
................................................................................
   839    886         break;
   840    887   
   841    888       case FTS3_MATCHINFO_AVGLENGTH:
   842    889       case FTS3_MATCHINFO_LENGTH:
   843    890       case FTS3_MATCHINFO_LCS:
   844    891         nVal = pInfo->nCol;
   845    892         break;
          893  +
          894  +    case FTS3_MATCHINFO_LHITS:
          895  +      nVal = pInfo->nCol * pInfo->nPhrase;
          896  +      break;
   846    897   
   847    898       default:
   848    899         assert( cArg==FTS3_MATCHINFO_HITS );
   849    900         nVal = pInfo->nCol * pInfo->nPhrase * 3;
   850    901         break;
   851    902     }
   852    903   
................................................................................
  1093   1144   
  1094   1145         case FTS3_MATCHINFO_LCS:
  1095   1146           rc = fts3ExprLoadDoclists(pCsr, 0, 0);
  1096   1147           if( rc==SQLITE_OK ){
  1097   1148             rc = fts3MatchinfoLcs(pCsr, pInfo);
  1098   1149           }
  1099   1150           break;
         1151  +
         1152  +      case FTS3_MATCHINFO_LHITS:
         1153  +        (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLHitsCb, (void*)pInfo);
         1154  +        break;
  1100   1155   
  1101   1156         default: {
  1102   1157           Fts3Expr *pExpr;
  1103   1158           assert( zArg[i]==FTS3_MATCHINFO_HITS );
  1104   1159           pExpr = pCsr->pExpr;
  1105   1160           rc = fts3ExprLoadDoclists(pCsr, 0, 0);
  1106   1161           if( rc!=SQLITE_OK ) break;

Changes to ext/fts3/fts3_term.c.

    77     77     if( argc==5 ){
    78     78       iIndex = atoi(argv[4]);
    79     79       argc--;
    80     80     }
    81     81   
    82     82     /* The user should specify a single argument - the name of an fts3 table. */
    83     83     if( argc!=4 ){
    84         -    *pzErr = sqlite3_mprintf(
           84  +    sqlite3Fts3ErrMsg(pzErr,
    85     85           "wrong number of arguments to fts4term constructor"
    86     86       );
    87     87       return SQLITE_ERROR;
    88     88     }
    89     89   
    90     90     zDb = argv[1]; 
    91     91     nDb = (int)strlen(zDb);

Changes to ext/fts3/fts3_tokenize_vtab.c.

    81     81     char **pzErr
    82     82   ){
    83     83     sqlite3_tokenizer_module *p;
    84     84     int nName = (int)strlen(zName);
    85     85   
    86     86     p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
    87     87     if( !p ){
    88         -    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
           88  +    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
    89     89       return SQLITE_ERROR;
    90     90     }
    91     91   
    92     92     *pp = p;
    93     93     return SQLITE_OK;
    94     94   }
    95     95   

Changes to ext/fts3/fts3_tokenizer.c.

   168    168       z = zCopy;
   169    169     }
   170    170     z[n] = '\0';
   171    171     sqlite3Fts3Dequote(z);
   172    172   
   173    173     m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
   174    174     if( !m ){
   175         -    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
          175  +    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
   176    176       rc = SQLITE_ERROR;
   177    177     }else{
   178    178       char const **aArg = 0;
   179    179       int iArg = 0;
   180    180       z = &z[n+1];
   181    181       while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
   182    182         int nNew = sizeof(char *)*(iArg+1);
................................................................................
   191    191         z[n] = '\0';
   192    192         sqlite3Fts3Dequote(z);
   193    193         z = &z[n+1];
   194    194       }
   195    195       rc = m->xCreate(iArg, aArg, ppTok);
   196    196       assert( rc!=SQLITE_OK || *ppTok );
   197    197       if( rc!=SQLITE_OK ){
   198         -      *pzErr = sqlite3_mprintf("unknown tokenizer");
          198  +      sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
   199    199       }else{
   200    200         (*ppTok)->pModule = m; 
   201    201       }
   202    202       sqlite3_free((void *)aArg);
   203    203     }
   204    204   
   205    205     sqlite3_free(zCopy);

Changes to ext/rtree/rtree.c.

  2816   2816     ** the conflicting row can be removed before proceeding. In the second
  2817   2817     ** case, SQLITE_CONSTRAINT must be returned regardless of the
  2818   2818     ** conflict-handling mode specified by the user.
  2819   2819     */
  2820   2820     if( nData>1 ){
  2821   2821       int ii;
  2822   2822   
  2823         -    /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
  2824         -    assert( nData==(pRtree->nDim*2 + 3) );
         2823  +    /* Populate the cell.aCoord[] array. The first coordinate is azData[3].
         2824  +    **
         2825  +    ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
         2826  +    ** with "column" that are interpreted as table constraints.
         2827  +    ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
         2828  +    ** This problem was discovered after years of use, so we silently ignore
         2829  +    ** these kinds of misdeclared tables to avoid breaking any legacy.
         2830  +    */
         2831  +    assert( nData<=(pRtree->nDim*2 + 3) );
         2832  +
  2825   2833   #ifndef SQLITE_RTREE_INT_ONLY
  2826   2834       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  2827         -      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
         2835  +      for(ii=0; ii<nData-4; ii+=2){
  2828   2836           cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
  2829   2837           cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
  2830   2838           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
  2831   2839             rc = SQLITE_CONSTRAINT;
  2832   2840             goto constraint;
  2833   2841           }
  2834   2842         }
  2835   2843       }else
  2836   2844   #endif
  2837   2845       {
  2838         -      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
         2846  +      for(ii=0; ii<nData-4; ii+=2){
  2839   2847           cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
  2840   2848           cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
  2841   2849           if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
  2842   2850             rc = SQLITE_CONSTRAINT;
  2843   2851             goto constraint;
  2844   2852           }
  2845   2853         }

Changes to main.mk.

    50     50   TCCX += -I$(TOP)/ext/session
    51     51   
    52     52   # Object files for the SQLite library.
    53     53   #
    54     54   LIBOBJ+= vdbe.o parse.o \
    55     55            alter.o analyze.o attach.o auth.o \
    56     56            backup.o bitvec.o btmutex.o btree.o build.o \
    57         -         callback.o complete.o ctime.o date.o delete.o expr.o fault.o fkey.o \
           57  +         callback.o complete.o ctime.o date.o dbstat.o delete.o expr.o fault.o fkey.o \
    58     58            fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    59     59            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
    60     60            fts3_tokenize_vtab.o \
    61     61   	 fts3_unicode.o fts3_unicode2.o \
    62     62            fts3_write.o func.o global.o hash.o \
    63     63            icu.o insert.o journal.o legacy.o loadext.o \
    64     64            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
................................................................................
    90     90     $(TOP)/src/btree.h \
    91     91     $(TOP)/src/btreeInt.h \
    92     92     $(TOP)/src/build.c \
    93     93     $(TOP)/src/callback.c \
    94     94     $(TOP)/src/complete.c \
    95     95     $(TOP)/src/ctime.c \
    96     96     $(TOP)/src/date.c \
           97  +  $(TOP)/src/dbstat.c \
    97     98     $(TOP)/src/delete.c \
    98     99     $(TOP)/src/expr.c \
    99    100     $(TOP)/src/fault.c \
   100    101     $(TOP)/src/fkey.c \
   101    102     $(TOP)/src/func.c \
   102    103     $(TOP)/src/global.c \
   103    104     $(TOP)/src/hash.c \
................................................................................
   272    273     $(TOP)/src/test_onefile.c \
   273    274     $(TOP)/src/test_osinst.c \
   274    275     $(TOP)/src/test_pcache.c \
   275    276     $(TOP)/src/test_quota.c \
   276    277     $(TOP)/src/test_rtree.c \
   277    278     $(TOP)/src/test_schema.c \
   278    279     $(TOP)/src/test_server.c \
   279         -  $(TOP)/src/test_stat.c \
   280    280     $(TOP)/src/test_sqllog.c \
   281    281     $(TOP)/src/test_superlock.c \
   282    282     $(TOP)/src/test_syscall.c \
   283    283     $(TOP)/src/test_tclvar.c \
   284    284     $(TOP)/src/test_thread.c \
   285    285     $(TOP)/src/test_vfs.c \
   286    286     $(TOP)/src/test_wsd.c
................................................................................
   308    308   
   309    309   TESTSRC2 = \
   310    310     $(TOP)/src/attach.c \
   311    311     $(TOP)/src/backup.c \
   312    312     $(TOP)/src/btree.c \
   313    313     $(TOP)/src/build.c \
   314    314     $(TOP)/src/date.c \
          315  +  $(TOP)/src/dbstat.c \
   315    316     $(TOP)/src/expr.c \
   316    317     $(TOP)/src/func.c \
   317    318     $(TOP)/src/insert.c \
   318    319     $(TOP)/src/wal.c \
   319    320     $(TOP)/src/main.c \
   320    321     $(TOP)/src/mem5.c \
   321    322     $(TOP)/src/os.c \
................................................................................
   609    610   
   610    611   # Rules for building test programs and for running tests
   611    612   #
   612    613   tclsqlite3:	$(TOP)/src/tclsqlite.c libsqlite3.a
   613    614   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite3 \
   614    615   		$(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB)
   615    616   
   616         -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c \
   617         -                    $(TOP)/tool/spaceanal.tcl
          617  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
   618    618   	echo "#define TCLSH 2" > $@
   619         -	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@
          619  +	echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@
          620  +	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
   620    621   	echo "static const char *tclsh_main_loop(void){" >> $@
   621    622   	echo "static const char *zMainloop = " >> $@
   622    623   	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
   623    624   	echo "; return zMainloop; }" >> $@
   624    625   
   625    626   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   626    627   	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
................................................................................
   644    645   
   645    646   fts3-testfixture$(EXE): sqlite3.c fts3amal.c $(TESTSRC) $(TOP)/src/tclsqlite.c
   646    647   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS)                  \
   647    648   	-DSQLITE_ENABLE_FTS3=1                                               \
   648    649   		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c fts3amal.c       \
   649    650   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   650    651   
   651         -fulltest:	testfixture$(EXE) sqlite3$(EXE)
          652  +fulltest:	testfixture$(EXE) sqlite3$(EXE) fuzztest
   652    653   	./testfixture$(EXE) $(TOP)/test/all.test
   653    654   
   654         -soaktest:	testfixture$(EXE) sqlite3$(EXE)
          655  +soaktest:	testfixture$(EXE) sqlite3$(EXE) fuzzoomtest
   655    656   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
   656    657   
   657         -fulltestonly:	testfixture$(EXE) sqlite3$(EXE)
          658  +fulltestonly:	testfixture$(EXE) sqlite3$(EXE) fuzztest
   658    659   	./testfixture$(EXE) $(TOP)/test/full.test
   659    660   
   660    661   queryplantest:	testfixture$(EXE) sqlite3$(EXE)
   661    662   	./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner
   662    663   
   663         -test:	testfixture$(EXE) sqlite3$(EXE)
          664  +fuzztest:	fuzzershell$(EXE)
          665  +	./fuzzershell$(EXE) $(TOP)/test/fuzzdata1.txt $(TOP)/test/fuzzdata2.txt
          666  +
          667  +fuzzoomtest:	fuzzershell$(EXE)
          668  +	./fuzzershell$(EXE) -f $(TOP)/test/fuzzdata1.txt --oom
          669  +
          670  +test:	testfixture$(EXE) sqlite3$(EXE) fuzztest
   664    671   	./testfixture$(EXE) $(TOP)/test/veryquick.test
   665    672   
          673  +# Run a test using valgrind.  This can take a really long time
          674  +# because valgrind is so much slower than a native machine.
          675  +#
          676  +valgrindtest:	testfixture$(EXE) sqlite3$(EXE) fuzzershell$(EXE)
          677  +	valgrind -v ./fuzzershell$(EXE) -f $(TOP)/test/fuzzdata1.txt
          678  +	OMIT_MISUSE=1 valgrind -v ./testfixture$(EXE) $(TOP)/test/permutations.test valgrind
          679  +
   666    680   # The next two rules are used to support the "threadtest" target. Building
   667    681   # threadtest runs a few thread-safety tests that are implemented in C. This
   668    682   # target is invoked by the releasetest.tcl script.
   669    683   # 
   670    684   THREADTEST3_SRC = $(TOP)/test/threadtest3.c    \
   671    685                     $(TOP)/test/tt3_checkpoint.c \
   672    686                     $(TOP)/test/tt3_index.c      \

Changes to src/alter.c.

   122    122       if( token==TK_REFERENCES ){
   123    123         char *zParent;
   124    124         do {
   125    125           z += n;
   126    126           n = sqlite3GetToken(z, &token);
   127    127         }while( token==TK_SPACE );
   128    128   
          129  +      if( token==TK_ILLEGAL ) break;
   129    130         zParent = sqlite3DbStrNDup(db, (const char *)z, n);
   130    131         if( zParent==0 ) break;
   131    132         sqlite3Dequote(zParent);
   132    133         if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
   133    134           char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
   134    135               (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
   135    136           );

Changes to src/bitvec.c.

   337    337     int i, nx, pc, op;
   338    338     void *pTmpSpace;
   339    339   
   340    340     /* Allocate the Bitvec to be tested and a linear array of
   341    341     ** bits to act as the reference */
   342    342     pBitvec = sqlite3BitvecCreate( sz );
   343    343     pV = sqlite3MallocZero( (sz+7)/8 + 1 );
   344         -  pTmpSpace = sqlite3_malloc(BITVEC_SZ);
          344  +  pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
   345    345     if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
   346    346   
   347    347     /* NULL pBitvec tests */
   348    348     sqlite3BitvecSet(0, 1);
   349    349     sqlite3BitvecClear(0, 1, pTmpSpace);
   350    350   
   351    351     /* Run the program */

Changes to src/btree.c.

  2425   2425     if( nReserve<0 ){
  2426   2426       nReserve = pBt->pageSize - pBt->usableSize;
  2427   2427     }
  2428   2428     assert( nReserve>=0 && nReserve<=255 );
  2429   2429     if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
  2430   2430           ((pageSize-1)&pageSize)==0 ){
  2431   2431       assert( (pageSize & 7)==0 );
  2432         -    assert( !pBt->pPage1 && !pBt->pCursor );
         2432  +    assert( !pBt->pCursor );
  2433   2433       pBt->pageSize = (u32)pageSize;
  2434   2434       freeTempSpace(pBt);
  2435   2435     }
  2436   2436     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
  2437   2437     pBt->usableSize = pBt->pageSize - (u16)nReserve;
  2438   2438     if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
  2439   2439     sqlite3BtreeLeave(p);
................................................................................
  8533   8533   **
  8534   8534   ** aHeap[0] is the number of elements on the heap.  aHeap[1] is the
  8535   8535   ** root element.  The daughter nodes of aHeap[N] are aHeap[N*2]
  8536   8536   ** and aHeap[N*2+1].
  8537   8537   **
  8538   8538   ** The heap property is this:  Every node is less than or equal to both
  8539   8539   ** of its daughter nodes.  A consequence of the heap property is that the
  8540         -** root node aHeap[1] is always the minimum value current in the heap.
         8540  +** root node aHeap[1] is always the minimum value currently in the heap.
  8541   8541   **
  8542   8542   ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
  8543   8543   ** the heap, preserving the heap property.  The btreeHeapPull() routine
  8544   8544   ** removes the root element from the heap (the minimum value in the heap)
  8545         -** and then move other nodes around as necessary to preserve the heap
         8545  +** and then moves other nodes around as necessary to preserve the heap
  8546   8546   ** property.
  8547   8547   **
  8548   8548   ** This heap is used for cell overlap and coverage testing.  Each u32
  8549   8549   ** entry represents the span of a cell or freeblock on a btree page.  
  8550   8550   ** The upper 16 bits are the index of the first byte of a range and the
  8551   8551   ** lower 16 bits are the index of the last byte of that range.
  8552   8552   */
................................................................................
  8895   8895     if( !sCheck.aPgRef ){
  8896   8896       *pnErr = 1;
  8897   8897       sqlite3BtreeLeave(p);
  8898   8898       return 0;
  8899   8899     }
  8900   8900     i = PENDING_BYTE_PAGE(pBt);
  8901   8901     if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
  8902         -  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
  8903         -  sCheck.errMsg.useMalloc = 2;
         8902  +  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
  8904   8903   
  8905   8904     /* Check the integrity of the freelist
  8906   8905     */
  8907   8906     sCheck.zPfx = "Main freelist: ";
  8908   8907     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
  8909   8908               get4byte(&pBt->pPage1->aData[36]));
  8910   8909     sCheck.zPfx = 0;

Changes to src/build.c.

  4020   4020     Index *pIdx       /* The index that triggers the constraint */
  4021   4021   ){
  4022   4022     char *zErr;
  4023   4023     int j;
  4024   4024     StrAccum errMsg;
  4025   4025     Table *pTab = pIdx->pTable;
  4026   4026   
  4027         -  sqlite3StrAccumInit(&errMsg, 0, 0, 200);
  4028         -  errMsg.db = pParse->db;
         4027  +  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
  4029   4028     for(j=0; j<pIdx->nKeyCol; j++){
  4030   4029       char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
  4031   4030       if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
  4032   4031       sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
  4033   4032       sqlite3StrAccumAppend(&errMsg, ".", 1);
  4034   4033       sqlite3StrAccumAppendAll(&errMsg, zCol);
  4035   4034     }

Name change from src/test_stat.c to src/dbstat.c.

    14     14   **
    15     15   ** The dbstat virtual table is used to extract low-level formatting
    16     16   ** information from an SQLite database in order to implement the
    17     17   ** "sqlite3_analyzer" utility.  See the ../tool/spaceanal.tcl script
    18     18   ** for an example implementation.
    19     19   */
    20     20   
    21         -#ifndef SQLITE_AMALGAMATION
    22         -# include "sqliteInt.h"
    23         -#endif
    24         -
    25         -#ifndef SQLITE_OMIT_VIRTUALTABLE
           21  +#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
           22  +    && !defined(SQLITE_OMIT_VIRTUALTABLE)
           23  +#include "sqliteInt.h"   /* Requires access to internal data structures */
    26     24   
    27     25   /*
    28     26   ** Page paths:
    29     27   ** 
    30     28   **   The value of the 'path' column describes the path taken from the 
    31     29   **   root-node of the b-tree structure to each page. The value of the 
    32     30   **   root-node path is '/'.
................................................................................
   136    134   static int statConnect(
   137    135     sqlite3 *db,
   138    136     void *pAux,
   139    137     int argc, const char *const*argv,
   140    138     sqlite3_vtab **ppVtab,
   141    139     char **pzErr
   142    140   ){
   143         -  StatTable *pTab;
          141  +  StatTable *pTab = 0;
          142  +  int rc = SQLITE_OK;
   144    143   
   145         -  pTab = (StatTable *)sqlite3_malloc(sizeof(StatTable));
          144  +  rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
          145  +  if( rc==SQLITE_OK ){
          146  +    pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
          147  +    if( pTab==0 ) rc = SQLITE_NOMEM;
          148  +  }
          149  +
          150  +  assert( rc==SQLITE_OK || pTab==0 );
          151  +  if( rc==SQLITE_OK ){
   146    152     memset(pTab, 0, sizeof(StatTable));
   147    153     pTab->db = db;
          154  +  }
   148    155   
   149         -  sqlite3_declare_vtab(db, VTAB_SCHEMA);
   150         -  *ppVtab = &pTab->base;
   151         -  return SQLITE_OK;
          156  +  *ppVtab = (sqlite3_vtab*)pTab;
          157  +  return rc;
   152    158   }
   153    159   
   154    160   /*
   155    161   ** Disconnect from or destroy a statvfs virtual table.
   156    162   */
   157    163   static int statDisconnect(sqlite3_vtab *pVtab){
   158    164     sqlite3_free(pVtab);
................................................................................
   191    197   ** Open a new statvfs cursor.
   192    198   */
   193    199   static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
   194    200     StatTable *pTab = (StatTable *)pVTab;
   195    201     StatCursor *pCsr;
   196    202     int rc;
   197    203   
   198         -  pCsr = (StatCursor *)sqlite3_malloc(sizeof(StatCursor));
          204  +  pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
          205  +  if( pCsr==0 ){
          206  +    rc = SQLITE_NOMEM;
          207  +  }else{
   199    208     memset(pCsr, 0, sizeof(StatCursor));
   200    209     pCsr->base.pVtab = pVTab;
   201    210   
   202    211     rc = sqlite3_prepare_v2(pTab->db, 
   203    212         "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
   204    213         "  UNION ALL  "
   205    214         "SELECT name, rootpage, type FROM sqlite_master WHERE rootpage!=0"
   206    215         "  ORDER BY name", -1,
   207    216         &pCsr->pStmt, 0
   208    217     );
   209    218     if( rc!=SQLITE_OK ){
   210    219       sqlite3_free(pCsr);
   211         -    return rc;
          220  +      pCsr = 0;
          221  +    }
   212    222     }
   213    223   
   214    224     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
   215         -  return SQLITE_OK;
          225  +  return rc;
   216    226   }
   217    227   
   218    228   static void statClearPage(StatPage *p){
   219    229     int i;
          230  +  if( p->aCell ){
   220    231     for(i=0; i<p->nCell; i++){
   221    232       sqlite3_free(p->aCell[i].aOvfl);
   222    233     }
   223         -  sqlite3PagerUnref(p->pPg);
   224    234     sqlite3_free(p->aCell);
          235  +  }
          236  +  sqlite3PagerUnref(p->pPg);
   225    237     sqlite3_free(p->zPath);
   226    238     memset(p, 0, sizeof(StatPage));
   227    239   }
   228    240   
   229    241   static void statResetCsr(StatCursor *pCsr){
   230    242     int i;
   231    243     sqlite3_reset(pCsr->pStmt);
................................................................................
   302    314     if( p->nCell ){
   303    315       int i;                        /* Used to iterate through cells */
   304    316       int nUsable;                  /* Usable bytes per page */
   305    317   
   306    318       sqlite3BtreeEnter(pBt);
   307    319       nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
   308    320       sqlite3BtreeLeave(pBt);
   309         -    p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell));
          321  +    p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
          322  +    if( p->aCell==0 ) return SQLITE_NOMEM;
   310    323       memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
   311    324   
   312    325       for(i=0; i<p->nCell; i++){
   313    326         StatCell *pCell = &p->aCell[i];
   314    327   
   315    328         iOff = get2byte(&aData[nHdr+i*2]);
   316    329         if( !isLeaf ){
................................................................................
   334    347           assert( nPayload>=(u32)nLocal );
   335    348           assert( nLocal<=(nUsable-35) );
   336    349           if( nPayload>(u32)nLocal ){
   337    350             int j;
   338    351             int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
   339    352             pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
   340    353             pCell->nOvfl = nOvfl;
   341         -          pCell->aOvfl = sqlite3_malloc(sizeof(u32)*nOvfl);
          354  +          pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
          355  +          if( pCell->aOvfl==0 ) return SQLITE_NOMEM;
   342    356             pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
   343    357             for(j=1; j<nOvfl; j++){
   344    358               int rc;
   345    359               u32 iPrev = pCell->aOvfl[j-1];
   346    360               DbPage *pPg = 0;
   347    361               rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
   348    362               if( rc!=SQLITE_OK ){
................................................................................
   482    496     */
   483    497     if( rc==SQLITE_OK ){
   484    498       int i;
   485    499       StatPage *p = &pCsr->aPage[pCsr->iPage];
   486    500       pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
   487    501       pCsr->iPageno = p->iPgno;
   488    502   
   489         -    statDecodePage(pBt, p);
          503  +    rc = statDecodePage(pBt, p);
          504  +    if( rc==SQLITE_OK ){
   490    505       statSizeAndOffset(pCsr);
   491    506   
   492    507       switch( p->flags ){
   493    508         case 0x05:             /* table internal */
   494    509         case 0x02:             /* index internal */
   495    510           pCsr->zPagetype = "internal";
   496    511           break;
................................................................................
   508    523       pCsr->zPath = sqlite3_mprintf("%s", p->zPath);
   509    524       nPayload = 0;
   510    525       for(i=0; i<p->nCell; i++){
   511    526         nPayload += p->aCell[i].nLocal;
   512    527       }
   513    528       pCsr->nPayload = nPayload;
   514    529     }
          530  +  }
   515    531   
   516    532     return rc;
   517    533   }
   518    534   
   519    535   static int statEof(sqlite3_vtab_cursor *pCursor){
   520    536     StatCursor *pCsr = (StatCursor *)pCursor;
   521    537     return pCsr->isEof;
................................................................................
   575    591   
   576    592   static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
   577    593     StatCursor *pCsr = (StatCursor *)pCursor;
   578    594     *pRowid = pCsr->iPageno;
   579    595     return SQLITE_OK;
   580    596   }
   581    597   
          598  +/*
          599  +** Invoke this routine to register the "dbstat" virtual table module
          600  +*/
   582    601   int sqlite3_dbstat_register(sqlite3 *db){
   583    602     static sqlite3_module dbstat_module = {
   584    603       0,                            /* iVersion */
   585    604       statConnect,                  /* xCreate */
   586    605       statConnect,                  /* xConnect */
   587    606       statBestIndex,                /* xBestIndex */
   588    607       statDisconnect,               /* xDisconnect */
................................................................................
   598    617       0,                            /* xBegin */
   599    618       0,                            /* xSync */
   600    619       0,                            /* xCommit */
   601    620       0,                            /* xRollback */
   602    621       0,                            /* xFindMethod */
   603    622       0,                            /* xRename */
   604    623     };
   605         -  sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
   606         -  return SQLITE_OK;
          624  +  return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
   607    625   }
   608         -
   609         -#endif
   610         -
   611         -#if defined(SQLITE_TEST) || TCLSH==2
   612         -#include <tcl.h>
   613         -
   614         -static int test_dbstat(
   615         -  void *clientData,
   616         -  Tcl_Interp *interp,
   617         -  int objc,
   618         -  Tcl_Obj *CONST objv[]
   619         -){
   620         -#ifdef SQLITE_OMIT_VIRTUALTABLE
   621         -  Tcl_AppendResult(interp, "dbstat not available because of "
   622         -                           "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
   623         -  return TCL_ERROR;
   624         -#else
   625         -  struct SqliteDb { sqlite3 *db; };
   626         -  char *zDb;
   627         -  Tcl_CmdInfo cmdInfo;
   628         -
   629         -  if( objc!=2 ){
   630         -    Tcl_WrongNumArgs(interp, 1, objv, "DB");
   631         -    return TCL_ERROR;
   632         -  }
   633         -
   634         -  zDb = Tcl_GetString(objv[1]);
   635         -  if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
   636         -    sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
   637         -    sqlite3_dbstat_register(db);
   638         -  }
   639         -  return TCL_OK;
   640         -#endif
   641         -}
   642         -
   643         -int SqlitetestStat_Init(Tcl_Interp *interp){
   644         -  Tcl_CreateObjCommand(interp, "register_dbstat_vtab", test_dbstat, 0, 0);
   645         -  return TCL_OK;
   646         -}
   647         -#endif /* if defined(SQLITE_TEST) || TCLSH==2 */
          626  +#endif /* SQLITE_ENABLE_DBSTAT_VTAB */

Changes to src/expr.c.

  1248   1248   */
  1249   1249   u32 sqlite3ExprListFlags(const ExprList *pList){
  1250   1250     int i;
  1251   1251     u32 m = 0;
  1252   1252     if( pList ){
  1253   1253       for(i=0; i<pList->nExpr; i++){
  1254   1254          Expr *pExpr = pList->a[i].pExpr;
  1255         -       if( ALWAYS(pExpr) ) m |= pList->a[i].pExpr->flags;
         1255  +       if( ALWAYS(pExpr) ) m |= pExpr->flags;
  1256   1256       }
  1257   1257     }
  1258   1258     return m;
  1259   1259   }
  1260   1260   
  1261   1261   /*
  1262   1262   ** These routines are Walker callbacks used to check expressions to

Changes to src/func.c.

   228    228     int argc,
   229    229     sqlite3_value **argv
   230    230   ){
   231    231     PrintfArguments x;
   232    232     StrAccum str;
   233    233     const char *zFormat;
   234    234     int n;
          235  +  sqlite3 *db = sqlite3_context_db_handle(context);
   235    236   
   236    237     if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
   237    238       x.nArg = argc-1;
   238    239       x.nUsed = 0;
   239    240       x.apArg = argv+1;
   240         -    sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH);
   241         -    str.db = sqlite3_context_db_handle(context);
          241  +    sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
   242    242       sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
   243    243       n = str.nChar;
   244    244       sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
   245    245                           SQLITE_DYNAMIC);
   246    246     }
   247    247   }
   248    248   
................................................................................
   384    384       sqlite3_free(zBuf);
   385    385     }
   386    386     sqlite3_result_double(context, r);
   387    387   }
   388    388   #endif
   389    389   
   390    390   /*
   391         -** Allocate nByte bytes of space using sqlite3_malloc(). If the
          391  +** Allocate nByte bytes of space using sqlite3Malloc(). If the
   392    392   ** allocation fails, call sqlite3_result_error_nomem() to notify
   393    393   ** the database handle that malloc() has failed and return NULL.
   394    394   ** If nByte is larger than the maximum string or blob length, then
   395    395   ** raise an SQLITE_TOOBIG exception and return NULL.
   396    396   */
   397    397   static void *contextMalloc(sqlite3_context *context, i64 nByte){
   398    398     char *z;
................................................................................
  1053   1053   static void charFunc(
  1054   1054     sqlite3_context *context,
  1055   1055     int argc,
  1056   1056     sqlite3_value **argv
  1057   1057   ){
  1058   1058     unsigned char *z, *zOut;
  1059   1059     int i;
  1060         -  zOut = z = sqlite3_malloc( argc*4+1 );
         1060  +  zOut = z = sqlite3_malloc64( argc*4+1 );
  1061   1061     if( z==0 ){
  1062   1062       sqlite3_result_error_nomem(context);
  1063   1063       return;
  1064   1064     }
  1065   1065     for(i=0; i<argc; i++){
  1066   1066       sqlite3_int64 x;
  1067   1067       unsigned c;
................................................................................
  1201   1201         testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
  1202   1202         if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1203   1203           sqlite3_result_error_toobig(context);
  1204   1204           sqlite3_free(zOut);
  1205   1205           return;
  1206   1206         }
  1207   1207         zOld = zOut;
  1208         -      zOut = sqlite3_realloc(zOut, (int)nOut);
         1208  +      zOut = sqlite3_realloc64(zOut, (int)nOut);
  1209   1209         if( zOut==0 ){
  1210   1210           sqlite3_result_error_nomem(context);
  1211   1211           sqlite3_free(zOld);
  1212   1212           return;
  1213   1213         }
  1214   1214         memcpy(&zOut[j], zRep, nRep);
  1215   1215         j += nRep;
................................................................................
  1563   1563     int nVal, nSep;
  1564   1564     assert( argc==1 || argc==2 );
  1565   1565     if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  1566   1566     pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
  1567   1567   
  1568   1568     if( pAccum ){
  1569   1569       sqlite3 *db = sqlite3_context_db_handle(context);
  1570         -    int firstTerm = pAccum->useMalloc==0;
  1571         -    pAccum->useMalloc = 2;
         1570  +    int firstTerm = pAccum->mxAlloc==0;
  1572   1571       pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
  1573   1572       if( !firstTerm ){
  1574   1573         if( argc==2 ){
  1575   1574           zSep = (char*)sqlite3_value_text(argv[1]);
  1576   1575           nSep = sqlite3_value_bytes(argv[1]);
  1577   1576         }else{
  1578   1577           zSep = ",";

Changes to src/loadext.c.

   426    426     sqlite3_vfs *pVfs = db->pVfs;
   427    427     void *handle;
   428    428     int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   429    429     char *zErrmsg = 0;
   430    430     const char *zEntry;
   431    431     char *zAltEntry = 0;
   432    432     void **aHandle;
   433         -  int nMsg = 300 + sqlite3Strlen30(zFile);
          433  +  u64 nMsg = 300 + sqlite3Strlen30(zFile);
   434    434     int ii;
   435    435   
   436    436     /* Shared library endings to try if zFile cannot be loaded as written */
   437    437     static const char *azEndings[] = {
   438    438   #if SQLITE_OS_WIN
   439    439        "dll"   
   440    440   #elif defined(__APPLE__)
................................................................................
   469    469       if( zAltFile==0 ) return SQLITE_NOMEM;
   470    470       handle = sqlite3OsDlOpen(pVfs, zAltFile);
   471    471       sqlite3_free(zAltFile);
   472    472     }
   473    473   #endif
   474    474     if( handle==0 ){
   475    475       if( pzErrMsg ){
   476         -      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
          476  +      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
   477    477         if( zErrmsg ){
   478    478           sqlite3_snprintf(nMsg, zErrmsg, 
   479    479               "unable to open shared library [%s]", zFile);
   480    480           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
   481    481         }
   482    482       }
   483    483       return SQLITE_ERROR;
................................................................................
   495    495     **
   496    496     **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
   497    497     **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
   498    498     */
   499    499     if( xInit==0 && zProc==0 ){
   500    500       int iFile, iEntry, c;
   501    501       int ncFile = sqlite3Strlen30(zFile);
   502         -    zAltEntry = sqlite3_malloc(ncFile+30);
          502  +    zAltEntry = sqlite3_malloc64(ncFile+30);
   503    503       if( zAltEntry==0 ){
   504    504         sqlite3OsDlClose(pVfs, handle);
   505    505         return SQLITE_NOMEM;
   506    506       }
   507    507       memcpy(zAltEntry, "sqlite3_", 8);
   508    508       for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
   509    509       iFile++;
................................................................................
   517    517       zEntry = zAltEntry;
   518    518       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   519    519                        sqlite3OsDlSym(pVfs, handle, zEntry);
   520    520     }
   521    521     if( xInit==0 ){
   522    522       if( pzErrMsg ){
   523    523         nMsg += sqlite3Strlen30(zEntry);
   524         -      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
          524  +      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
   525    525         if( zErrmsg ){
   526    526           sqlite3_snprintf(nMsg, zErrmsg,
   527    527               "no entry point [%s] in shared library [%s]", zEntry, zFile);
   528    528           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
   529    529         }
   530    530       }
   531    531       sqlite3OsDlClose(pVfs, handle);
................................................................................
   616    616   ** extensions.
   617    617   **
   618    618   ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
   619    619   ** mutex must be held while accessing this list.
   620    620   */
   621    621   typedef struct sqlite3AutoExtList sqlite3AutoExtList;
   622    622   static SQLITE_WSD struct sqlite3AutoExtList {
   623         -  int nExt;              /* Number of entries in aExt[] */          
          623  +  u32 nExt;              /* Number of entries in aExt[] */          
   624    624     void (**aExt)(void);   /* Pointers to the extension init functions */
   625    625   } sqlite3Autoext = { 0, 0 };
   626    626   
   627    627   /* The "wsdAutoext" macro will resolve to the autoextension
   628    628   ** state vector.  If writable static data is unsupported on the target,
   629    629   ** we have to locate the state vector at run-time.  In the more common
   630    630   ** case where writable static data is supported, wsdStat can refer directly
................................................................................
   649    649   #ifndef SQLITE_OMIT_AUTOINIT
   650    650     rc = sqlite3_initialize();
   651    651     if( rc ){
   652    652       return rc;
   653    653     }else
   654    654   #endif
   655    655     {
   656         -    int i;
          656  +    u32 i;
   657    657   #if SQLITE_THREADSAFE
   658    658       sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   659    659   #endif
   660    660       wsdAutoextInit;
   661    661       sqlite3_mutex_enter(mutex);
   662    662       for(i=0; i<wsdAutoext.nExt; i++){
   663    663         if( wsdAutoext.aExt[i]==xInit ) break;
   664    664       }
   665    665       if( i==wsdAutoext.nExt ){
   666         -      int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
          666  +      u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
   667    667         void (**aNew)(void);
   668         -      aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
          668  +      aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
   669    669         if( aNew==0 ){
   670    670           rc = SQLITE_NOMEM;
   671    671         }else{
   672    672           wsdAutoext.aExt = aNew;
   673    673           wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
   674    674           wsdAutoext.nExt++;
   675    675         }
................................................................................
   693    693   #if SQLITE_THREADSAFE
   694    694     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   695    695   #endif
   696    696     int i;
   697    697     int n = 0;
   698    698     wsdAutoextInit;
   699    699     sqlite3_mutex_enter(mutex);
   700         -  for(i=wsdAutoext.nExt-1; i>=0; i--){
          700  +  for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
   701    701       if( wsdAutoext.aExt[i]==xInit ){
   702    702         wsdAutoext.nExt--;
   703    703         wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
   704    704         n++;
   705    705         break;
   706    706       }
   707    707     }
................................................................................
   731    731   
   732    732   /*
   733    733   ** Load all automatic extensions.
   734    734   **
   735    735   ** If anything goes wrong, set an error in the database connection.
   736    736   */
   737    737   void sqlite3AutoLoadExtensions(sqlite3 *db){
   738         -  int i;
          738  +  u32 i;
   739    739     int go = 1;
   740    740     int rc;
   741    741     int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   742    742   
   743    743     wsdAutoextInit;
   744    744     if( wsdAutoext.nExt==0 ){
   745    745       /* Common case: early out without every having to acquire a mutex */

Changes to src/main.c.

    51     51   
    52     52   /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
    53     53   ** zero if and only if SQLite was compiled with mutexing code omitted due to
    54     54   ** the SQLITE_THREADSAFE compile-time option being set to 0.
    55     55   */
    56     56   int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
    57     57   
           58  +/*
           59  +** When compiling the test fixture or with debugging enabled (on Win32),
           60  +** this variable being set to non-zero will cause OSTRACE macros to emit
           61  +** extra diagnostic information.
           62  +*/
           63  +#ifdef SQLITE_HAVE_OS_TRACE
           64  +# ifndef SQLITE_DEBUG_OS_TRACE
           65  +#   define SQLITE_DEBUG_OS_TRACE 0
           66  +# endif
           67  +  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
           68  +#endif
           69  +
    58     70   #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
    59     71   /*
    60     72   ** If the following function pointer is not NULL and if
    61     73   ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
    62     74   ** I/O active are written using this function.  These messages
    63     75   ** are intended for debugging activity only.
    64     76   */
................................................................................
  1190   1202     }
  1191   1203   }
  1192   1204   
  1193   1205   /*
  1194   1206   ** Return a static string containing the name corresponding to the error code
  1195   1207   ** specified in the argument.
  1196   1208   */
  1197         -#if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST)
         1209  +#if defined(SQLITE_NEED_ERR_NAME)
  1198   1210   const char *sqlite3ErrName(int rc){
  1199   1211     const char *zName = 0;
  1200   1212     int i, origRc = rc;
  1201   1213     for(i=0; i<2 && zName==0; i++, rc &= 0xff){
  1202   1214       switch( rc ){
  1203   1215         case SQLITE_OK:                 zName = "SQLITE_OK";                break;
  1204   1216         case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
................................................................................
  2436   2448               || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
  2437   2449      && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
  2438   2450     ){
  2439   2451       char *zOpt;
  2440   2452       int eState;                   /* Parser state when parsing URI */
  2441   2453       int iIn;                      /* Input character index */
  2442   2454       int iOut = 0;                 /* Output character index */
  2443         -    int nByte = nUri+2;           /* Bytes of space to allocate */
         2455  +    u64 nByte = nUri+2;           /* Bytes of space to allocate */
  2444   2456   
  2445   2457       /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
  2446   2458       ** method that there may be extra parameters following the file-name.  */
  2447   2459       flags |= SQLITE_OPEN_URI;
  2448   2460   
  2449   2461       for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
  2450         -    zFile = sqlite3_malloc(nByte);
         2462  +    zFile = sqlite3_malloc64(nByte);
  2451   2463       if( !zFile ) return SQLITE_NOMEM;
  2452   2464   
  2453   2465       iIn = 5;
  2454   2466   #ifdef SQLITE_ALLOW_URI_AUTHORITY
  2455   2467       if( strncmp(zUri+5, "///", 3)==0 ){
  2456   2468         iIn = 7;
  2457   2469         /* The following condition causes URIs with five leading / characters
................................................................................
  2609   2621           }
  2610   2622         }
  2611   2623   
  2612   2624         zOpt = &zVal[nVal+1];
  2613   2625       }
  2614   2626   
  2615   2627     }else{
  2616         -    zFile = sqlite3_malloc(nUri+2);
         2628  +    zFile = sqlite3_malloc64(nUri+2);
  2617   2629       if( !zFile ) return SQLITE_NOMEM;
  2618   2630       memcpy(zFile, zUri, nUri);
  2619   2631       zFile[nUri] = '\0';
  2620   2632       zFile[nUri+1] = '\0';
  2621   2633       flags &= ~SQLITE_OPEN_URI;
  2622   2634     }
  2623   2635   
................................................................................
  2880   2892   #endif
  2881   2893   
  2882   2894   #ifdef SQLITE_ENABLE_RTREE
  2883   2895     if( !db->mallocFailed && rc==SQLITE_OK){
  2884   2896       rc = sqlite3RtreeInit(db);
  2885   2897     }
  2886   2898   #endif
         2899  +
         2900  +#ifdef SQLITE_ENABLE_DBSTAT_VTAB
         2901  +  if( !db->mallocFailed && rc==SQLITE_OK){
         2902  +    int sqlite3_dbstat_register(sqlite3*);
         2903  +    rc = sqlite3_dbstat_register(db);
         2904  +  }
         2905  +#endif
  2887   2906   
  2888   2907     /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  2889   2908     ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  2890   2909     ** mode.  Doing nothing at all also makes NORMAL the default.
  2891   2910     */
  2892   2911   #ifdef SQLITE_DEFAULT_LOCKING_MODE
  2893   2912     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;

Changes to src/os_common.h.

    25     25   ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
    26     26   ** switch.  The following code should catch this problem at compile-time.
    27     27   */
    28     28   #ifdef MEMORY_DEBUG
    29     29   # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
    30     30   #endif
    31     31   
    32         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
    33         -# ifndef SQLITE_DEBUG_OS_TRACE
    34         -#   define SQLITE_DEBUG_OS_TRACE 0
    35         -# endif
    36         -  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
    37         -# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
    38         -#else
    39         -# define OSTRACE(X)
    40         -#endif
    41         -
    42     32   /*
    43     33   ** Macros for performance tracing.  Normally turned off.  Only works
    44     34   ** on i486 hardware.
    45     35   */
    46     36   #ifdef SQLITE_PERFORMANCE_TRACE
    47     37   
    48     38   /* 

Changes to src/os_unix.c.

   637    637   #ifdef SQLITE_DEBUG
   638    638   static int unixMutexHeld(void) {
   639    639     return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   640    640   }
   641    641   #endif
   642    642   
   643    643   
   644         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
          644  +#ifdef SQLITE_HAVE_OS_TRACE
   645    645   /*
   646    646   ** Helper function for printing out trace information from debugging
   647    647   ** binaries. This returns the string representation of the supplied
   648    648   ** integer lock-type.
   649    649   */
   650    650   static const char *azFileLock(int eFileLock){
   651    651     switch( eFileLock ){
................................................................................
   900    900   static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
   901    901     struct vxworksFileId *pNew;         /* search key and new file ID */
   902    902     struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
   903    903     int n;                              /* Length of zAbsoluteName string */
   904    904   
   905    905     assert( zAbsoluteName[0]=='/' );
   906    906     n = (int)strlen(zAbsoluteName);
   907         -  pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
          907  +  pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
   908    908     if( pNew==0 ) return 0;
   909    909     pNew->zCanonicalName = (char*)&pNew[1];
   910    910     memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
   911    911     n = vxworksSimplifyName(pNew->zCanonicalName, n);
   912    912   
   913    913     /* Search for an existing entry that matching the canonical name.
   914    914     ** If found, increment the reference count and return a pointer to
................................................................................
  1304   1304     fileId.ino = statbuf.st_ino;
  1305   1305   #endif
  1306   1306     pInode = inodeList;
  1307   1307     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
  1308   1308       pInode = pInode->pNext;
  1309   1309     }
  1310   1310     if( pInode==0 ){
  1311         -    pInode = sqlite3_malloc( sizeof(*pInode) );
         1311  +    pInode = sqlite3_malloc64( sizeof(*pInode) );
  1312   1312       if( pInode==0 ){
  1313   1313         return SQLITE_NOMEM;
  1314   1314       }
  1315   1315       memset(pInode, 0, sizeof(*pInode));
  1316   1316       memcpy(&pInode->fileId, &fileId, sizeof(fileId));
  1317   1317       pInode->nRef = 1;
  1318   1318       pInode->pNext = inodeList;
................................................................................
  3825   3825         return SQLITE_OK;
  3826   3826       }
  3827   3827       case SQLITE_FCNTL_VFSNAME: {
  3828   3828         *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
  3829   3829         return SQLITE_OK;
  3830   3830       }
  3831   3831       case SQLITE_FCNTL_TEMPFILENAME: {
  3832         -      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
         3832  +      char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
  3833   3833         if( zTFile ){
  3834   3834           unixGetTempname(pFile->pVfs->mxPathname, zTFile);
  3835   3835           *(char**)pArg = zTFile;
  3836   3836         }
  3837   3837         return SQLITE_OK;
  3838   3838       }
  3839   3839       case SQLITE_FCNTL_HAS_MOVED: {
................................................................................
  4266   4266     struct unixShmNode *pShmNode;   /* The underlying mmapped file */
  4267   4267     int rc;                         /* Result code */
  4268   4268     unixInodeInfo *pInode;          /* The inode of fd */
  4269   4269     char *zShmFilename;             /* Name of the file used for SHM */
  4270   4270     int nShmFilename;               /* Size of the SHM filename in bytes */
  4271   4271   
  4272   4272     /* Allocate space for the new unixShm object. */
  4273         -  p = sqlite3_malloc( sizeof(*p) );
         4273  +  p = sqlite3_malloc64( sizeof(*p) );
  4274   4274     if( p==0 ) return SQLITE_NOMEM;
  4275   4275     memset(p, 0, sizeof(*p));
  4276   4276     assert( pDbFd->pShm==0 );
  4277   4277   
  4278   4278     /* Check to see if a unixShmNode object already exists. Reuse an existing
  4279   4279     ** one if present. Create a new one if necessary.
  4280   4280     */
................................................................................
  4297   4297       }
  4298   4298   
  4299   4299   #ifdef SQLITE_SHM_DIRECTORY
  4300   4300       nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
  4301   4301   #else
  4302   4302       nShmFilename = 6 + (int)strlen(zBasePath);
  4303   4303   #endif
  4304         -    pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
         4304  +    pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
  4305   4305       if( pShmNode==0 ){
  4306   4306         rc = SQLITE_NOMEM;
  4307   4307         goto shm_open_err;
  4308   4308       }
  4309   4309       memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
  4310   4310       zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
  4311   4311   #ifdef SQLITE_SHM_DIRECTORY
................................................................................
  4507   4507               MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
  4508   4508           );
  4509   4509           if( pMem==MAP_FAILED ){
  4510   4510             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
  4511   4511             goto shmpage_out;
  4512   4512           }
  4513   4513         }else{
  4514         -        pMem = sqlite3_malloc(szRegion);
         4514  +        pMem = sqlite3_malloc64(szRegion);
  4515   4515           if( pMem==0 ){
  4516   4516             rc = SQLITE_NOMEM;
  4517   4517             goto shmpage_out;
  4518   4518           }
  4519   4519           memset(pMem, 0, szRegion);
  4520   4520         }
  4521   4521   
................................................................................
  5344   5344   
  5345   5345   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
  5346   5346     else if( pLockingStyle == &afpIoMethods ){
  5347   5347       /* AFP locking uses the file path so it needs to be included in
  5348   5348       ** the afpLockingContext.
  5349   5349       */
  5350   5350       afpLockingContext *pCtx;
  5351         -    pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
         5351  +    pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
  5352   5352       if( pCtx==0 ){
  5353   5353         rc = SQLITE_NOMEM;
  5354   5354       }else{
  5355   5355         /* NB: zFilename exists and remains valid until the file is closed
  5356   5356         ** according to requirement F11141.  So we do not need to make a
  5357   5357         ** copy of the filename. */
  5358   5358         pCtx->dbPath = zFilename;
................................................................................
  5374   5374       /* Dotfile locking uses the file path so it needs to be included in
  5375   5375       ** the dotlockLockingContext 
  5376   5376       */
  5377   5377       char *zLockFile;
  5378   5378       int nFilename;
  5379   5379       assert( zFilename!=0 );
  5380   5380       nFilename = (int)strlen(zFilename) + 6;
  5381         -    zLockFile = (char *)sqlite3_malloc(nFilename);
         5381  +    zLockFile = (char *)sqlite3_malloc64(nFilename);
  5382   5382       if( zLockFile==0 ){
  5383   5383         rc = SQLITE_NOMEM;
  5384   5384       }else{
  5385   5385         sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
  5386   5386       }
  5387   5387       pNew->lockingContext = zLockFile;
  5388   5388     }
................................................................................
  5751   5751   
  5752   5752     if( eType==SQLITE_OPEN_MAIN_DB ){
  5753   5753       UnixUnusedFd *pUnused;
  5754   5754       pUnused = findReusableFd(zName, flags);
  5755   5755       if( pUnused ){
  5756   5756         fd = pUnused->fd;
  5757   5757       }else{
  5758         -      pUnused = sqlite3_malloc(sizeof(*pUnused));
         5758  +      pUnused = sqlite3_malloc64(sizeof(*pUnused));
  5759   5759         if( !pUnused ){
  5760   5760           return SQLITE_NOMEM;
  5761   5761         }
  5762   5762       }
  5763   5763       p->pUnused = pUnused;
  5764   5764   
  5765   5765       /* Database filenames are double-zero terminated if they are not
................................................................................
  6131   6131     **
  6132   6132     ** When testing, initializing zBuf[] to zero is all we do.  That means
  6133   6133     ** that we always use the same random number sequence.  This makes the
  6134   6134     ** tests repeatable.
  6135   6135     */
  6136   6136     memset(zBuf, 0, nBuf);
  6137   6137     randomnessPid = osGetpid(0);  
  6138         -#if !defined(SQLITE_TEST)
         6138  +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
  6139   6139     {
  6140   6140       int fd, got;
  6141   6141       fd = robust_open("/dev/urandom", O_RDONLY, 0);
  6142   6142       if( fd<0 ){
  6143   6143         time_t t;
  6144   6144         time(&t);
  6145   6145         memcpy(zBuf, &t, sizeof(t));
................................................................................
  6543   6543     ** 3. if that fails, try to open the file read-only
  6544   6544     ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
  6545   6545     */
  6546   6546     pUnused = findReusableFd(path, openFlags);
  6547   6547     if( pUnused ){
  6548   6548       fd = pUnused->fd;
  6549   6549     }else{
  6550         -    pUnused = sqlite3_malloc(sizeof(*pUnused));
         6550  +    pUnused = sqlite3_malloc64(sizeof(*pUnused));
  6551   6551       if( !pUnused ){
  6552   6552         return SQLITE_NOMEM;
  6553   6553       }
  6554   6554     }
  6555   6555     if( fd<0 ){
  6556   6556       fd = robust_open(path, openFlags, 0);
  6557   6557       terrno = errno;
................................................................................
  6576   6576         case EIO: 
  6577   6577           return SQLITE_IOERR_LOCK; /* even though it is the conch */
  6578   6578         default:
  6579   6579           return SQLITE_CANTOPEN_BKPT;
  6580   6580       }
  6581   6581     }
  6582   6582     
  6583         -  pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
         6583  +  pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
  6584   6584     if( pNew==NULL ){
  6585   6585       rc = SQLITE_NOMEM;
  6586   6586       goto end_create_proxy;
  6587   6587     }
  6588   6588     memset(pNew, 0, sizeof(unixFile));
  6589   6589     pNew->openFlags = openFlags;
  6590   6590     memset(&dummyVfs, 0, sizeof(dummyVfs));
................................................................................
  7038   7038     OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
  7039   7039              (rc==SQLITE_OK ? "ok" : "failed")));
  7040   7040     return rc;
  7041   7041   }
  7042   7042   
  7043   7043   /*
  7044   7044   ** Given the name of a database file, compute the name of its conch file.
  7045         -** Store the conch filename in memory obtained from sqlite3_malloc().
         7045  +** Store the conch filename in memory obtained from sqlite3_malloc64().
  7046   7046   ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
  7047   7047   ** or SQLITE_NOMEM if unable to obtain memory.
  7048   7048   **
  7049   7049   ** The caller is responsible for ensuring that the allocated memory
  7050   7050   ** space is eventually freed.
  7051   7051   **
  7052   7052   ** *pConchPath is set to NULL if a memory allocation error occurs.
................................................................................
  7054   7054   static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
  7055   7055     int i;                        /* Loop counter */
  7056   7056     int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
  7057   7057     char *conchPath;              /* buffer in which to construct conch name */
  7058   7058   
  7059   7059     /* Allocate space for the conch filename and initialize the name to
  7060   7060     ** the name of the original database file. */  
  7061         -  *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
         7061  +  *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
  7062   7062     if( conchPath==0 ){
  7063   7063       return SQLITE_NOMEM;
  7064   7064     }
  7065   7065     memcpy(conchPath, dbPath, len+1);
  7066   7066     
  7067   7067     /* now insert a "." before the last / character */
  7068   7068     for( i=(len-1); i>=0; i-- ){
................................................................................
  7170   7170     }else{
  7171   7171       lockPath=(char *)path;
  7172   7172     }
  7173   7173     
  7174   7174     OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
  7175   7175              (lockPath ? lockPath : ":auto:"), osGetpid(0)));
  7176   7176   
  7177         -  pCtx = sqlite3_malloc( sizeof(*pCtx) );
         7177  +  pCtx = sqlite3_malloc64( sizeof(*pCtx) );
  7178   7178     if( pCtx==0 ){
  7179   7179       return SQLITE_NOMEM;
  7180   7180     }
  7181   7181     memset(pCtx, 0, sizeof(*pCtx));
  7182   7182   
  7183   7183     rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
  7184   7184     if( rc==SQLITE_OK ){

Changes to src/os_win.c.

  2753   2753   #ifndef SQLITE_NO_SYNC
  2754   2754     /*
  2755   2755     ** Used only when SQLITE_NO_SYNC is not defined.
  2756   2756      */
  2757   2757     BOOL rc;
  2758   2758   #endif
  2759   2759   #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
  2760         -    (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
         2760  +    defined(SQLITE_HAVE_OS_TRACE)
  2761   2761     /*
  2762   2762     ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
  2763   2763     ** OSTRACE() macros.
  2764   2764      */
  2765   2765     winFile *pFile = (winFile*)id;
  2766   2766   #else
  2767   2767     UNUSED_PARAMETER(id);
................................................................................
  3430   3430       void *pMap;
  3431   3431     } *aRegion;
  3432   3432     DWORD lastErrno;           /* The Windows errno from the last I/O error */
  3433   3433   
  3434   3434     int nRef;                  /* Number of winShm objects pointing to this */
  3435   3435     winShm *pFirst;            /* All winShm objects pointing to this */
  3436   3436     winShmNode *pNext;         /* Next in list of all winShmNode objects */
  3437         -#ifdef SQLITE_DEBUG
         3437  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  3438   3438     u8 nextShmId;              /* Next available winShm.id value */
  3439   3439   #endif
  3440   3440   };
  3441   3441   
  3442   3442   /*
  3443   3443   ** A global array of all winShmNode objects.
  3444   3444   **
................................................................................
  3461   3461   */
  3462   3462   struct winShm {
  3463   3463     winShmNode *pShmNode;      /* The underlying winShmNode object */
  3464   3464     winShm *pNext;             /* Next winShm with the same winShmNode */
  3465   3465     u8 hasMutex;               /* True if holding the winShmNode mutex */
  3466   3466     u16 sharedMask;            /* Mask of shared locks held */
  3467   3467     u16 exclMask;              /* Mask of exclusive locks held */
  3468         -#ifdef SQLITE_DEBUG
         3468  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  3469   3469     u8 id;                     /* Id of this connection with its winShmNode */
  3470   3470   #endif
  3471   3471   };
  3472   3472   
  3473   3473   /*
  3474   3474   ** Constants used for locking
  3475   3475   */
................................................................................
  3652   3652         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
  3653   3653       }
  3654   3654       if( rc ) goto shm_open_err;
  3655   3655     }
  3656   3656   
  3657   3657     /* Make the new connection a child of the winShmNode */
  3658   3658     p->pShmNode = pShmNode;
  3659         -#ifdef SQLITE_DEBUG
         3659  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  3660   3660     p->id = pShmNode->nextShmId++;
  3661   3661   #endif
  3662   3662     pShmNode->nRef++;
  3663   3663     pDbFd->pShm = p;
  3664   3664     winShmLeaveMutex();
  3665   3665   
  3666   3666     /* The reference count on pShmNode has already been incremented under
................................................................................
  3921   3921           rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
  3922   3922                            "winShmMap2", pDbFd->zPath);
  3923   3923           goto shmpage_out;
  3924   3924         }
  3925   3925       }
  3926   3926   
  3927   3927       /* Map the requested memory region into this processes address space. */
  3928         -    apNew = (struct ShmRegion *)sqlite3_realloc(
         3928  +    apNew = (struct ShmRegion *)sqlite3_realloc64(
  3929   3929           pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
  3930   3930       );
  3931   3931       if( !apNew ){
  3932   3932         rc = SQLITE_IOERR_NOMEM;
  3933   3933         goto shmpage_out;
  3934   3934       }
  3935   3935       pShmNode->aRegion = apNew;
................................................................................
  5368   5368   
  5369   5369   /*
  5370   5370   ** Write up to nBuf bytes of randomness into zBuf.
  5371   5371   */
  5372   5372   static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  5373   5373     int n = 0;
  5374   5374     UNUSED_PARAMETER(pVfs);
  5375         -#if defined(SQLITE_TEST)
         5375  +#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
  5376   5376     n = nBuf;
  5377   5377     memset(zBuf, 0, nBuf);
  5378   5378   #else
  5379   5379     if( sizeof(SYSTEMTIME)<=nBuf-n ){
  5380   5380       SYSTEMTIME x;
  5381   5381       osGetSystemTime(&x);
  5382   5382       memcpy(&zBuf[n], &x, sizeof(x));
................................................................................
  5402   5402   #endif
  5403   5403     if( sizeof(LARGE_INTEGER)<=nBuf-n ){
  5404   5404       LARGE_INTEGER i;
  5405   5405       osQueryPerformanceCounter(&i);
  5406   5406       memcpy(&zBuf[n], &i, sizeof(i));
  5407   5407       n += sizeof(i);
  5408   5408     }
  5409         -#endif
  5410   5409   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
  5411   5410     if( sizeof(UUID)<=nBuf-n ){
  5412   5411       UUID id;
  5413   5412       memset(&id, 0, sizeof(UUID));
  5414   5413       osUuidCreate(&id);
  5415   5414       memcpy(zBuf, &id, sizeof(UUID));
  5416   5415       n += sizeof(UUID);
................................................................................
  5419   5418       UUID id;
  5420   5419       memset(&id, 0, sizeof(UUID));
  5421   5420       osUuidCreateSequential(&id);
  5422   5421       memcpy(zBuf, &id, sizeof(UUID));
  5423   5422       n += sizeof(UUID);
  5424   5423     }
  5425   5424   #endif
         5425  +#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
  5426   5426     return n;
  5427   5427   }
  5428   5428   
  5429   5429   
  5430   5430   /*
  5431   5431   ** Sleep for a little while.  Return the amount of time slept.
  5432   5432   */

Changes to src/pager.c.

  7006   7006           if( rc==SQLITE_OK && state==PAGER_READER ){
  7007   7007             pagerUnlockDb(pPager, SHARED_LOCK);
  7008   7008           }else if( state==PAGER_OPEN ){
  7009   7009             pager_unlock(pPager);
  7010   7010           }
  7011   7011           assert( state==pPager->eState );
  7012   7012         }
         7013  +    }else if( eMode==PAGER_JOURNALMODE_OFF ){
         7014  +      sqlite3OsClose(pPager->jfd);
  7013   7015       }
  7014   7016     }
  7015   7017   
  7016   7018     /* Return the new journal mode */
  7017   7019     return (int)pPager->journalMode;
  7018   7020   }
  7019   7021   

Changes to src/printf.c.

   134    134   }
   135    135   #endif /* SQLITE_OMIT_FLOATING_POINT */
   136    136   
   137    137   /*
   138    138   ** Set the StrAccum object to an error mode.
   139    139   */
   140    140   static void setStrAccumError(StrAccum *p, u8 eError){
          141  +  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
   141    142     p->accError = eError;
   142    143     p->nAlloc = 0;
   143    144   }
   144    145   
   145    146   /*
   146    147   ** Extra argument values from a PrintfArguments object
   147    148   */
................................................................................
   751    752     char *zNew;
   752    753     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
   753    754     if( p->accError ){
   754    755       testcase(p->accError==STRACCUM_TOOBIG);
   755    756       testcase(p->accError==STRACCUM_NOMEM);
   756    757       return 0;
   757    758     }
   758         -  if( !p->useMalloc ){
          759  +  if( p->mxAlloc==0 ){
   759    760       N = p->nAlloc - p->nChar - 1;
   760    761       setStrAccumError(p, STRACCUM_TOOBIG);
   761    762       return N;
   762    763     }else{
   763    764       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   764    765       i64 szNew = p->nChar;
   765    766       szNew += N + 1;
................................................................................
   771    772       if( szNew > p->mxAlloc ){
   772    773         sqlite3StrAccumReset(p);
   773    774         setStrAccumError(p, STRACCUM_TOOBIG);
   774    775         return 0;
   775    776       }else{
   776    777         p->nAlloc = (int)szNew;
   777    778       }
   778         -    if( p->useMalloc==1 ){
          779  +    if( p->db ){
   779    780         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   780    781       }else{
   781         -      zNew = sqlite3_realloc(zOld, p->nAlloc);
          782  +      zNew = sqlite3_realloc64(zOld, p->nAlloc);
   782    783       }
   783    784       if( zNew ){
   784    785         assert( p->zText!=0 || p->nChar==0 );
   785    786         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
   786    787         p->zText = zNew;
   787    788         p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
   788    789       }else{
................................................................................
   851    852   ** Finish off a string by making sure it is zero-terminated.
   852    853   ** Return a pointer to the resulting string.  Return a NULL
   853    854   ** pointer if any kind of error was encountered.
   854    855   */
   855    856   char *sqlite3StrAccumFinish(StrAccum *p){
   856    857     if( p->zText ){
   857    858       p->zText[p->nChar] = 0;
   858         -    if( p->useMalloc && p->zText==p->zBase ){
   859         -      if( p->useMalloc==1 ){
          859  +    if( p->mxAlloc>0 && p->zText==p->zBase ){
   860    860           p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   861         -      }else{
   862         -        p->zText = sqlite3_malloc(p->nChar+1);
   863         -      }
   864    861         if( p->zText ){
   865    862           memcpy(p->zText, p->zBase, p->nChar+1);
   866    863         }else{
   867    864           setStrAccumError(p, STRACCUM_NOMEM);
   868    865         }
   869    866       }
   870    867     }
................................................................................
   872    869   }
   873    870   
   874    871   /*
   875    872   ** Reset an StrAccum string.  Reclaim all malloced memory.
   876    873   */
   877    874   void sqlite3StrAccumReset(StrAccum *p){
   878    875     if( p->zText!=p->zBase ){
   879         -    if( p->useMalloc==1 ){
   880    876         sqlite3DbFree(p->db, p->zText);
   881         -    }else{
   882         -      sqlite3_free(p->zText);
   883         -    }
   884    877     }
   885    878     p->zText = 0;
   886    879   }
   887    880   
   888    881   /*
   889         -** Initialize a string accumulator
          882  +** Initialize a string accumulator.
          883  +**
          884  +** p:     The accumulator to be initialized.
          885  +** db:    Pointer to a database connection.  May be NULL.  Lookaside
          886  +**        memory is used if not NULL. db->mallocFailed is set appropriately
          887  +**        when not NULL.
          888  +** zBase: An initial buffer.  May be NULL in which case the initial buffer
          889  +**        is malloced.
          890  +** n:     Size of zBase in bytes.  If total space requirements never exceed
          891  +**        n then no memory allocations ever occur.
          892  +** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
          893  +**        allocations will ever occur.
   890    894   */
   891         -void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
          895  +void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
   892    896     p->zText = p->zBase = zBase;
   893         -  p->db = 0;
          897  +  p->db = db;
   894    898     p->nChar = 0;
   895    899     p->nAlloc = n;
   896    900     p->mxAlloc = mx;
   897         -  p->useMalloc = 1;
   898    901     p->accError = 0;
   899    902   }
   900    903   
   901    904   /*
   902    905   ** Print into memory obtained from sqliteMalloc().  Use the internal
   903    906   ** %-conversion extensions.
   904    907   */
   905    908   char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
   906    909     char *z;
   907    910     char zBase[SQLITE_PRINT_BUF_SIZE];
   908    911     StrAccum acc;
   909    912     assert( db!=0 );
   910         -  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
          913  +  sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
   911    914                         db->aLimit[SQLITE_LIMIT_LENGTH]);
   912         -  acc.db = db;
   913    915     sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
   914    916     z = sqlite3StrAccumFinish(&acc);
   915    917     if( acc.accError==STRACCUM_NOMEM ){
   916    918       db->mallocFailed = 1;
   917    919     }
   918    920     return z;
   919    921   }
................................................................................
   963    965       (void)SQLITE_MISUSE_BKPT;
   964    966       return 0;
   965    967     }
   966    968   #endif
   967    969   #ifndef SQLITE_OMIT_AUTOINIT
   968    970     if( sqlite3_initialize() ) return 0;
   969    971   #endif
   970         -  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
   971         -  acc.useMalloc = 2;
          972  +  sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
   972    973     sqlite3VXPrintf(&acc, 0, zFormat, ap);
   973    974     z = sqlite3StrAccumFinish(&acc);
   974    975     return z;
   975    976   }
   976    977   
   977    978   /*
   978    979   ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
................................................................................
  1009   1010   #ifdef SQLITE_ENABLE_API_ARMOR
  1010   1011     if( zBuf==0 || zFormat==0 ) {
  1011   1012       (void)SQLITE_MISUSE_BKPT;
  1012   1013       if( zBuf ) zBuf[0] = 0;
  1013   1014       return zBuf;
  1014   1015     }
  1015   1016   #endif
  1016         -  sqlite3StrAccumInit(&acc, zBuf, n, 0);
  1017         -  acc.useMalloc = 0;
         1017  +  sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
  1018   1018     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1019   1019     return sqlite3StrAccumFinish(&acc);
  1020   1020   }
  1021   1021   char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
  1022   1022     char *z;
  1023   1023     va_list ap;
  1024   1024     va_start(ap,zFormat);
................................................................................
  1036   1036   ** allocate memory because it might be called while the memory allocator
  1037   1037   ** mutex is held.
  1038   1038   */
  1039   1039   static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
  1040   1040     StrAccum acc;                          /* String accumulator */
  1041   1041     char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
  1042   1042   
  1043         -  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
  1044         -  acc.useMalloc = 0;
         1043  +  sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
  1045   1044     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1046   1045     sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
  1047   1046                              sqlite3StrAccumFinish(&acc));
  1048   1047   }
  1049   1048   
  1050   1049   /*
  1051   1050   ** Format and write a message to the log if logging is enabled.
................................................................................
  1055   1054     if( sqlite3GlobalConfig.xLog ){
  1056   1055       va_start(ap, zFormat);
  1057   1056       renderLogMsg(iErrCode, zFormat, ap);
  1058   1057       va_end(ap);
  1059   1058     }
  1060   1059   }
  1061   1060   
  1062         -#if defined(SQLITE_DEBUG)
         1061  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  1063   1062   /*
  1064   1063   ** A version of printf() that understands %lld.  Used for debugging.
  1065   1064   ** The printf() built into some versions of windows does not understand %lld
  1066   1065   ** and segfaults if you give it a long long int.
  1067   1066   */
  1068   1067   void sqlite3DebugPrintf(const char *zFormat, ...){
  1069   1068     va_list ap;
  1070   1069     StrAccum acc;
  1071   1070     char zBuf[500];
  1072         -  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
  1073         -  acc.useMalloc = 0;
         1071  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
  1074   1072     va_start(ap,zFormat);
  1075   1073     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1076   1074     va_end(ap);
  1077   1075     sqlite3StrAccumFinish(&acc);
  1078   1076     fprintf(stdout,"%s", zBuf);
  1079   1077     fflush(stdout);
  1080   1078   }
................................................................................
  1093   1091   ** a diagram of Expr, ExprList, and Select objects.
  1094   1092   **
  1095   1093   */
  1096   1094   /* Add a new subitem to the tree.  The moreToFollow flag indicates that this
  1097   1095   ** is not the last item in the tree. */
  1098   1096   TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
  1099   1097     if( p==0 ){
  1100         -    p = sqlite3_malloc( sizeof(*p) );
         1098  +    p = sqlite3_malloc64( sizeof(*p) );
  1101   1099       if( p==0 ) return 0;
  1102   1100       memset(p, 0, sizeof(*p));
  1103   1101     }else{
  1104   1102       p->iLevel++;
  1105   1103     }
  1106   1104     assert( moreToFollow==0 || moreToFollow==1 );
  1107   1105     if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
................................................................................
  1116   1114   /* Generate a single line of output for the tree, with a prefix that contains
  1117   1115   ** all the appropriate tree lines */
  1118   1116   void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
  1119   1117     va_list ap;
  1120   1118     int i;
  1121   1119     StrAccum acc;
  1122   1120     char zBuf[500];
  1123         -  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
  1124         -  acc.useMalloc = 0;
         1121  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
  1125   1122     if( p ){
  1126   1123       for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
  1127   1124         sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
  1128   1125       }
  1129   1126       sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
  1130   1127     }
  1131   1128     va_start(ap, zFormat);

Changes to src/select.c.

  2599   2599   #ifndef SQLITE_OMIT_SUBQUERY
  2600   2600       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
  2601   2601       ** then there should be a single item on the stack.  Write this
  2602   2602       ** item into the set table with bogus data.
  2603   2603       */
  2604   2604       case SRT_Set: {
  2605   2605         int r1;
  2606         -      assert( pIn->nSdst==1 );
         2606  +      assert( pIn->nSdst==1 || pParse->nErr>0 );
  2607   2607         pDest->affSdst = 
  2608   2608            sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
  2609   2609         r1 = sqlite3GetTempReg(pParse);
  2610   2610         sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
  2611   2611         sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
  2612   2612         sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
  2613   2613         sqlite3ReleaseTempReg(pParse, r1);
................................................................................
  5535   5535       int i;
  5536   5536       pView = sqlite3TreeViewPush(pView, (n--)>0);
  5537   5537       sqlite3TreeViewLine(pView, "FROM");
  5538   5538       for(i=0; i<p->pSrc->nSrc; i++){
  5539   5539         struct SrcList_item *pItem = &p->pSrc->a[i];
  5540   5540         StrAccum x;
  5541   5541         char zLine[100];
  5542         -      sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0);
  5543         -      x.useMalloc = 0;
         5542  +      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
  5544   5543         sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
  5545   5544         if( pItem->zDatabase ){
  5546   5545           sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
  5547   5546         }else if( pItem->zName ){
  5548   5547           sqlite3XPrintf(&x, 0, " %s", pItem->zName);
  5549   5548         }
  5550   5549         if( pItem->pTab ){

Changes to src/shell.c.

  1003   1003         }
  1004   1004         setTextMode(p->out);
  1005   1005         break;
  1006   1006       }
  1007   1007       case MODE_Insert: {
  1008   1008         p->cnt++;
  1009   1009         if( azArg==0 ) break;
  1010         -      fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
         1010  +      fprintf(p->out,"INSERT INTO %s",p->zDestTable);
         1011  +      if( p->showHeader ){
         1012  +        fprintf(p->out,"(");
         1013  +        for(i=0; i<nArg; i++){
         1014  +          char *zSep = i>0 ? ",": "";
         1015  +          fprintf(p->out, "%s%s", zSep, azCol[i]);
         1016  +        }
         1017  +        fprintf(p->out,")");
         1018  +      }
         1019  +      fprintf(p->out," VALUES(");
  1011   1020         for(i=0; i<nArg; i++){
  1012   1021           char *zSep = i>0 ? ",": "";
  1013   1022           if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
  1014   1023             fprintf(p->out,"%sNULL",zSep);
  1015   1024           }else if( aiType && aiType[i]==SQLITE_TEXT ){
  1016   1025             if( zSep[0] ) fprintf(p->out,"%s",zSep);
  1017   1026             output_quoted_string(p->out, azArg[i]);
................................................................................
  1204   1213   /*
  1205   1214   ** Allocate space and save off current error string.
  1206   1215   */
  1207   1216   static char *save_err_msg(
  1208   1217     sqlite3 *db            /* Database to query */
  1209   1218   ){
  1210   1219     int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
  1211         -  char *zErrMsg = sqlite3_malloc(nErrMsg);
         1220  +  char *zErrMsg = sqlite3_malloc64(nErrMsg);
  1212   1221     if( zErrMsg ){
  1213   1222       memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
  1214   1223     }
  1215   1224     return zErrMsg;
  1216   1225   }
  1217   1226   
  1218   1227   /*
................................................................................
  1441   1450       ** SQL trigger or foreign key.  */
  1442   1451       int p2 = sqlite3_column_int(pSql, 3);
  1443   1452       int p2op = (p2 + (iOp-iAddr));
  1444   1453   
  1445   1454       /* Grow the p->aiIndent array as required */
  1446   1455       if( iOp>=nAlloc ){
  1447   1456         nAlloc += 100;
  1448         -      p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
  1449         -      abYield = (int*)sqlite3_realloc(abYield, nAlloc*sizeof(int));
         1457  +      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
         1458  +      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
  1450   1459       }
  1451   1460       abYield[iOp] = str_in_array(zOp, azYield);
  1452   1461       p->aiIndent[iOp] = 0;
  1453   1462       p->nIndent = iOp+1;
  1454   1463   
  1455   1464       if( str_in_array(zOp, azNext) ){
  1456   1465         for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
................................................................................
  1559   1568         rc = sqlite3_step(pStmt);
  1560   1569         /* if we have a result set... */
  1561   1570         if( SQLITE_ROW == rc ){
  1562   1571           /* if we have a callback... */
  1563   1572           if( xCallback ){
  1564   1573             /* allocate space for col name ptr, value ptr, and type */
  1565   1574             int nCol = sqlite3_column_count(pStmt);
  1566         -          void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
         1575  +          void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
  1567   1576             if( !pData ){
  1568   1577               rc = SQLITE_NOMEM;
  1569   1578             }else{
  1570   1579               char **azCols = (char **)pData;      /* Names of result columns */
  1571   1580               char **azVals = &azCols[nCol];       /* Results */
  1572   1581               int *aiTypes = (int *)&azVals[nCol]; /* Result types */
  1573   1582               int i, x;
................................................................................
  1785   1794   
  1786   1795   /*
  1787   1796   ** Text of a help message
  1788   1797   */
  1789   1798   static char zHelp[] =
  1790   1799     ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
  1791   1800     ".bail on|off           Stop after hitting an error.  Default OFF\n"
         1801  +  ".binary on|off         Turn binary output on or off.  Default OFF\n"
  1792   1802     ".clone NEWDB           Clone data into NEWDB from the existing database\n"
  1793   1803     ".databases             List names and files of attached databases\n"
  1794   1804     ".dbinfo ?DB?           Show status information about the database\n"
  1795   1805     ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
  1796   1806     "                         If TABLE specified, only dump tables matching\n"
  1797   1807     "                         LIKE pattern TABLE.\n"
  1798   1808     ".echo on|off           Turn command echo on or off\n"
................................................................................
  1806   1816     ".import FILE TABLE     Import data from FILE into TABLE\n"
  1807   1817     ".indexes ?TABLE?       Show names of all indexes\n"
  1808   1818     "                         If TABLE specified, only show indexes for tables\n"
  1809   1819     "                         matching LIKE pattern TABLE.\n"
  1810   1820   #ifdef SQLITE_ENABLE_IOTRACE
  1811   1821     ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
  1812   1822   #endif
         1823  +  ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
  1813   1824   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1814   1825     ".load FILE ?ENTRY?     Load an extension library\n"
  1815   1826   #endif
  1816   1827     ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
  1817   1828     ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
  1818   1829     "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
  1819   1830     "                         csv      Comma-separated values\n"
................................................................................
  1902   1913     zName = (const char*)sqlite3_value_text(argv[0]);
  1903   1914     if( zName==0 ) return;
  1904   1915     in = fopen(zName, "rb");
  1905   1916     if( in==0 ) return;
  1906   1917     fseek(in, 0, SEEK_END);
  1907   1918     nIn = ftell(in);
  1908   1919     rewind(in);
  1909         -  pBuf = sqlite3_malloc( nIn );
         1920  +  pBuf = sqlite3_malloc64( nIn );
  1910   1921     if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
  1911   1922       sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
  1912   1923     }else{
  1913   1924       sqlite3_free(pBuf);
  1914   1925     }
  1915   1926     fclose(in);
  1916   1927   }
................................................................................
  2018   2029                               writefileFunc, 0, 0);
  2019   2030     }
  2020   2031   }
  2021   2032   
  2022   2033   /*
  2023   2034   ** Do C-language style dequoting.
  2024   2035   **
         2036  +**    \a    -> alarm
         2037  +**    \b    -> backspace
  2025   2038   **    \t    -> tab
  2026   2039   **    \n    -> newline
         2040  +**    \v    -> vertical tab
         2041  +**    \f    -> form feed
  2027   2042   **    \r    -> carriage return
         2043  +**    \s    -> space
  2028   2044   **    \"    -> "
  2029         -**    \NNN  -> ascii character NNN in octal
         2045  +**    \'    -> '
  2030   2046   **    \\    -> backslash
         2047  +**    \NNN  -> ascii character NNN in octal
  2031   2048   */
  2032   2049   static void resolve_backslashes(char *z){
  2033   2050     int i, j;
  2034   2051     char c;
  2035   2052     while( *z && *z!='\\' ) z++;
  2036   2053     for(i=j=0; (c = z[i])!=0; i++, j++){
  2037   2054       if( c=='\\' && z[i+1]!=0 ){
  2038   2055         c = z[++i];
  2039         -      if( c=='n' ){
  2040         -        c = '\n';
         2056  +      if( c=='a' ){
         2057  +        c = '\a';
         2058  +      }else if( c=='b' ){
         2059  +        c = '\b';
  2041   2060         }else if( c=='t' ){
  2042   2061           c = '\t';
         2062  +      }else if( c=='n' ){
         2063  +        c = '\n';
         2064  +      }else if( c=='v' ){
         2065  +        c = '\v';
         2066  +      }else if( c=='f' ){
         2067  +        c = '\f';
  2043   2068         }else if( c=='r' ){
  2044   2069           c = '\r';
         2070  +      }else if( c=='"' ){
         2071  +        c = '"';
         2072  +      }else if( c=='\'' ){
         2073  +        c = '\'';
  2045   2074         }else if( c=='\\' ){
  2046   2075           c = '\\';
  2047   2076         }else if( c>='0' && c<='7' ){
  2048   2077           c -= '0';
  2049   2078           if( z[i+1]>='0' && z[i+1]<='7' ){
  2050   2079             i++;
  2051   2080             c = (c<<3) + z[i] - '0';
................................................................................
  2207   2236     int cRowSep;        /* The row separator character.  (Usually "\n") */
  2208   2237   };
  2209   2238   
  2210   2239   /* Append a single byte to z[] */
  2211   2240   static void import_append_char(ImportCtx *p, int c){
  2212   2241     if( p->n+1>=p->nAlloc ){
  2213   2242       p->nAlloc += p->nAlloc + 100;
  2214         -    p->z = sqlite3_realloc(p->z, p->nAlloc);
         2243  +    p->z = sqlite3_realloc64(p->z, p->nAlloc);
  2215   2244       if( p->z==0 ){
  2216   2245         fprintf(stderr, "out of memory\n");
  2217   2246         exit(1);
  2218   2247       }
  2219   2248     }
  2220   2249     p->z[p->n++] = (char)c;
  2221   2250   }
  2222   2251   
  2223   2252   /* Read a single field of CSV text.  Compatible with rfc4180 and extended
  2224   2253   ** with the option of having a separator other than ",".
  2225   2254   **
  2226   2255   **   +  Input comes from p->in.
  2227   2256   **   +  Store results in p->z of length p->n.  Space to hold p->z comes
  2228         -**      from sqlite3_malloc().
         2257  +**      from sqlite3_malloc64().
  2229   2258   **   +  Use p->cSep as the column separator.  The default is ",".
  2230   2259   **   +  Use p->rSep as the row separator.  The default is "\n".
  2231   2260   **   +  Keep track of the line number in p->nLine.
  2232   2261   **   +  Store the character that terminates the field in p->cTerm.  Store
  2233   2262   **      EOF on end-of-file.
  2234   2263   **   +  Report syntax errors on stderr
  2235   2264   */
................................................................................
  2295   2324     return p->z;
  2296   2325   }
  2297   2326   
  2298   2327   /* Read a single field of ASCII delimited text.
  2299   2328   **
  2300   2329   **   +  Input comes from p->in.
  2301   2330   **   +  Store results in p->z of length p->n.  Space to hold p->z comes
  2302         -**      from sqlite3_malloc().
         2331  +**      from sqlite3_malloc64().
  2303   2332   **   +  Use p->cSep as the column separator.  The default is "\x1F".
  2304   2333   **   +  Use p->rSep as the row separator.  The default is "\x1E".
  2305   2334   **   +  Keep track of the row number in p->nLine.
  2306   2335   **   +  Store the character that terminates the field in p->cTerm.  Store
  2307   2336   **      EOF on end-of-file.
  2308   2337   **   +  Report syntax errors on stderr
  2309   2338   */
................................................................................
  2355   2384     if( rc ){
  2356   2385       fprintf(stderr, "Error %d: %s on [%s]\n",
  2357   2386               sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
  2358   2387               zQuery);
  2359   2388       goto end_data_xfer;
  2360   2389     }
  2361   2390     n = sqlite3_column_count(pQuery);
  2362         -  zInsert = sqlite3_malloc(200 + nTable + n*3);
         2391  +  zInsert = sqlite3_malloc64(200 + nTable + n*3);
  2363   2392     if( zInsert==0 ){
  2364   2393       fprintf(stderr, "out of memory\n");
  2365   2394       goto end_data_xfer;
  2366   2395     }
  2367   2396     sqlite3_snprintf(200+nTable,zInsert,
  2368   2397                      "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
  2369   2398     i = (int)strlen(zInsert);
................................................................................
  2770   2799       if( nArg==2 ){
  2771   2800         bail_on_error = booleanValue(azArg[1]);
  2772   2801       }else{
  2773   2802         fprintf(stderr, "Usage: .bail on|off\n");
  2774   2803         rc = 1;
  2775   2804       }
  2776   2805     }else
         2806  +
         2807  +  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
         2808  +    if( nArg==2 ){
         2809  +      if( booleanValue(azArg[1]) ){
         2810  +        setBinaryMode(p->out);
         2811  +      }else{
         2812  +        setTextMode(p->out);
         2813  +      }
         2814  +    }else{
         2815  +      fprintf(stderr, "Usage: .binary on|off\n");
         2816  +      rc = 1;
         2817  +    }
         2818  +  }else
  2777   2819   
  2778   2820     /* The undocumented ".breakpoint" command causes a call to the no-op
  2779   2821     ** routine named test_breakpoint().
  2780   2822     */
  2781   2823     if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
  2782   2824       test_breakpoint();
  2783   2825     }else
................................................................................
  3110   3152         xCloser(sCtx.in);
  3111   3153         return 1;
  3112   3154       }
  3113   3155       nCol = sqlite3_column_count(pStmt);
  3114   3156       sqlite3_finalize(pStmt);
  3115   3157       pStmt = 0;
  3116   3158       if( nCol==0 ) return 0; /* no columns, no error */
  3117         -    zSql = sqlite3_malloc( nByte*2 + 20 + nCol*2 );
         3159  +    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
  3118   3160       if( zSql==0 ){
  3119   3161         fprintf(stderr, "Error: out of memory\n");
  3120   3162         xCloser(sCtx.in);
  3121   3163         return 1;
  3122   3164       }
  3123   3165       sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
  3124   3166       j = strlen30(zSql);
................................................................................
  3250   3292           rc = 1;
  3251   3293         }else{
  3252   3294           sqlite3IoTrace = iotracePrintf;
  3253   3295         }
  3254   3296       }
  3255   3297     }else
  3256   3298   #endif
         3299  +  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
         3300  +    static const struct {
         3301  +       const char *zLimitName;   /* Name of a limit */
         3302  +       int limitCode;            /* Integer code for that limit */
         3303  +    } aLimit[] = {
         3304  +      { "length",                SQLITE_LIMIT_LENGTH                    },
         3305  +      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
         3306  +      { "column",                SQLITE_LIMIT_COLUMN                    },
         3307  +      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
         3308  +      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
         3309  +      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
         3310  +      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
         3311  +      { "attached",              SQLITE_LIMIT_ATTACHED                  },
         3312  +      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
         3313  +      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
         3314  +      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
         3315  +      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
         3316  +    };
         3317  +    int i, n2;
         3318  +    open_db(p, 0);
         3319  +    if( nArg==1 ){
         3320  +      for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
         3321  +        printf("%20s %d\n", aLimit[i].zLimitName, 
         3322  +               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
         3323  +      }
         3324  +    }else if( nArg>3 ){
         3325  +      fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
         3326  +      rc = 1;
         3327  +      goto meta_command_exit;
         3328  +    }else{
         3329  +      int iLimit = -1;
         3330  +      n2 = strlen30(azArg[1]);
         3331  +      for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
         3332  +        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
         3333  +          if( iLimit<0 ){
         3334  +            iLimit = i;
         3335  +          }else{
         3336  +            fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
         3337  +            rc = 1;
         3338  +            goto meta_command_exit;
         3339  +          }
         3340  +        }
         3341  +      }
         3342  +      if( iLimit<0 ){
         3343  +        fprintf(stderr, "unknown limit: \"%s\"\n"
         3344  +                        "enter \".limits\" with no arguments for a list.\n",
         3345  +                         azArg[1]);
         3346  +        rc = 1;
         3347  +        goto meta_command_exit;
         3348  +      }
         3349  +      if( nArg==3 ){
         3350  +        sqlite3_limit(p->db, aLimit[iLimit].limitCode, integerValue(azArg[2]));
         3351  +      }
         3352  +      printf("%20s %d\n", aLimit[iLimit].zLimitName,
         3353  +             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
         3354  +    }
         3355  +  }else
  3257   3356   
  3258   3357   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3259   3358     if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
  3260   3359       const char *zFile, *zProc;
  3261   3360       char *zErrMsg = 0;
  3262   3361       if( nArg<2 ){
  3263   3362         fprintf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
................................................................................
  3927   4026       }else{
  3928   4027         sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
  3929   4028       }
  3930   4029       while( sqlite3_step(pStmt)==SQLITE_ROW ){
  3931   4030         if( nRow>=nAlloc ){
  3932   4031           char **azNew;
  3933   4032           int n2 = nAlloc*2 + 10;
  3934         -        azNew = sqlite3_realloc(azResult, sizeof(azResult[0])*n2);
         4033  +        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
  3935   4034           if( azNew==0 ){
  3936   4035             fprintf(stderr, "Error: out of memory\n");
  3937   4036             break;
  3938   4037           }
  3939   4038           nAlloc = n2;
  3940   4039           azResult = azNew;
  3941   4040         }

Changes to src/sqliteInt.h.

   358    358   # define ALWAYS(X)      ((X)?1:(assert(0),0))
   359    359   # define NEVER(X)       ((X)?(assert(0),1):0)
   360    360   #else
   361    361   # define ALWAYS(X)      (X)
   362    362   # define NEVER(X)       (X)
   363    363   #endif
   364    364   
          365  +/*
          366  +** Declarations used for tracing the operating system interfaces.
          367  +*/
          368  +#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
          369  +    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
          370  +  extern int sqlite3OSTrace;
          371  +# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
          372  +# define SQLITE_HAVE_OS_TRACE
          373  +#else
          374  +# define OSTRACE(X)
          375  +# undef  SQLITE_HAVE_OS_TRACE
          376  +#endif
          377  +
          378  +/*
          379  +** Is the sqlite3ErrName() function needed in the build?  Currently,
          380  +** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
          381  +** OSTRACE is enabled), and by several "test*.c" files (which are
          382  +** compiled using SQLITE_TEST).
          383  +*/
          384  +#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
          385  +    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
          386  +# define SQLITE_NEED_ERR_NAME
          387  +#else
          388  +# undef  SQLITE_NEED_ERR_NAME
          389  +#endif
          390  +
   365    391   /*
   366    392   ** Return true (non-zero) if the input is an integer that is too large
   367    393   ** to fit in 32-bits.  This macro is used inside of various testcase()
   368    394   ** macros to verify that we have tested SQLite for large-file support.
   369    395   */
   370    396   #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
   371    397   
................................................................................
  1561   1587     int nRef;                 /* Number of pointers to this structure */
  1562   1588     u8 bConstraint;           /* True if constraints are supported */
  1563   1589     int iSavepoint;           /* Depth of the SAVEPOINT stack */
  1564   1590     VTable *pNext;            /* Next in linked list (see above) */
  1565   1591   };
  1566   1592   
  1567   1593   /*
  1568         -** Each SQL table is represented in memory by an instance of the
  1569         -** following structure.
  1570         -**
  1571         -** Table.zName is the name of the table.  The case of the original
  1572         -** CREATE TABLE statement is stored, but case is not significant for
  1573         -** comparisons.
  1574         -**
  1575         -** Table.nCol is the number of columns in this table.  Table.aCol is a
  1576         -** pointer to an array of Column structures, one for each column.
  1577         -**
  1578         -** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
  1579         -** the column that is that key.   Otherwise Table.iPKey is negative.  Note
  1580         -** that the datatype of the PRIMARY KEY must be INTEGER for this field to
  1581         -** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
  1582         -** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
  1583         -** is generated for each row of the table.  TF_HasPrimaryKey is set if
  1584         -** the table has any PRIMARY KEY, INTEGER or otherwise.
  1585         -**
  1586         -** Table.tnum is the page number for the root BTree page of the table in the
  1587         -** database file.  If Table.iDb is the index of the database table backend
  1588         -** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
  1589         -** holds temporary tables and indices.  If TF_Ephemeral is set
  1590         -** then the table is stored in a file that is automatically deleted
  1591         -** when the VDBE cursor to the table is closed.  In this case Table.tnum 
  1592         -** refers VDBE cursor number that holds the table open, not to the root
  1593         -** page number.  Transient tables are used to hold the results of a
  1594         -** sub-query that appears instead of a real table name in the FROM clause 
  1595         -** of a SELECT statement.
         1594  +** The schema for each SQL table and view is represented in memory
         1595  +** by an instance of the following structure.
  1596   1596   */
  1597   1597   struct Table {
  1598   1598     char *zName;         /* Name of the table or view */
  1599   1599     Column *aCol;        /* Information about each column */
  1600   1600     Index *pIndex;       /* List of SQL indexes on this table. */
  1601   1601     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  1602   1602     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  1603   1603     char *zColAff;       /* String defining the affinity of each column */
  1604   1604   #ifndef SQLITE_OMIT_CHECK
  1605   1605     ExprList *pCheck;    /* All CHECK constraints */
  1606   1606   #endif
  1607         -  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  1608         -  int tnum;            /* Root BTree node for this table (see note above) */
  1609         -  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
         1607  +  int tnum;            /* Root BTree page for this table */
         1608  +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
  1610   1609     i16 nCol;            /* Number of columns in this table */
  1611   1610     u16 nRef;            /* Number of pointers to this Table */
         1611  +  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  1612   1612     LogEst szTabRow;     /* Estimated size of each table row in bytes */
  1613   1613   #ifdef SQLITE_ENABLE_COSTMULT
  1614   1614     LogEst costMult;     /* Cost multiplier for using this table */
  1615   1615   #endif
  1616   1616     u8 tabFlags;         /* Mask of TF_* values */
  1617   1617     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1618   1618   #ifndef SQLITE_OMIT_ALTERTABLE
................................................................................
  2843   2843   */
  2844   2844   struct StrAccum {
  2845   2845     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
  2846   2846     char *zBase;         /* A base allocation.  Not from malloc. */
  2847   2847     char *zText;         /* The string collected so far */
  2848   2848     int  nChar;          /* Length of the string so far */
  2849   2849     int  nAlloc;         /* Amount of space allocated in zText */
  2850         -  int  mxAlloc;        /* Maximum allowed string length */
  2851         -  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
         2850  +  int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
  2852   2851     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
  2853   2852   };
  2854   2853   #define STRACCUM_NOMEM   1
  2855   2854   #define STRACCUM_TOOBIG  2
  2856   2855   
  2857   2856   /*
  2858   2857   ** A pointer to this structure is used to communicate information
................................................................................
  3161   3160   #define SQLITE_PRINTF_INTERNAL 0x01
  3162   3161   #define SQLITE_PRINTF_SQLFUNC  0x02
  3163   3162   void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
  3164   3163   void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
  3165   3164   char *sqlite3MPrintf(sqlite3*,const char*, ...);
  3166   3165   char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  3167   3166   char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
  3168         -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
         3167  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  3169   3168     void sqlite3DebugPrintf(const char*, ...);
  3170   3169   #endif
  3171   3170   #if defined(SQLITE_TEST)
  3172   3171     void *sqlite3TestTextToPtr(const char*);
  3173   3172   #endif
  3174   3173   
  3175   3174   #if defined(SQLITE_DEBUG)
................................................................................
  3508   3507   int sqlite3DecOrHexToI64(const char*, i64*);
  3509   3508   void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
  3510   3509   void sqlite3Error(sqlite3*,int);
  3511   3510   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3512   3511   u8 sqlite3HexToInt(int h);
  3513   3512   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3514   3513   
  3515         -#if defined(SQLITE_TEST) 
         3514  +#if defined(SQLITE_NEED_ERR_NAME)
  3516   3515   const char *sqlite3ErrName(int);
  3517   3516   #endif
  3518   3517   
  3519   3518   const char *sqlite3ErrStr(int);
  3520   3519   int sqlite3ReadSchema(Parse *pParse);
  3521   3520   CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  3522   3521   CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
................................................................................
  3602   3601     void (*)(sqlite3_context*,int,sqlite3_value **),
  3603   3602     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  3604   3603     FuncDestructor *pDestructor
  3605   3604   );
  3606   3605   int sqlite3ApiExit(sqlite3 *db, int);
  3607   3606   int sqlite3OpenTempDatabase(Parse *);
  3608   3607   
  3609         -void sqlite3StrAccumInit(StrAccum*, char*, int, int);
         3608  +void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
  3610   3609   void sqlite3StrAccumAppend(StrAccum*,const char*,int);
  3611   3610   void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  3612   3611   void sqlite3AppendChar(StrAccum*,int,char);
  3613   3612   char *sqlite3StrAccumFinish(StrAccum*);
  3614   3613   void sqlite3StrAccumReset(StrAccum*);
  3615   3614   void sqlite3SelectDestInit(SelectDest*,int,int);
  3616   3615   Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);

Changes to src/table.c.

    86     86     */
    87     87     if( argv!=0 ){
    88     88       for(i=0; i<nCol; i++){
    89     89         if( argv[i]==0 ){
    90     90           z = 0;
    91     91         }else{
    92     92           int n = sqlite3Strlen30(argv[i])+1;
    93         -        z = sqlite3_malloc( n );
           93  +        z = sqlite3_malloc64( n );
    94     94           if( z==0 ) goto malloc_failed;
    95     95           memcpy(z, argv[i], n);
    96     96         }
    97     97         p->azResult[p->nData++] = z;
    98     98       }
    99     99       p->nRow++;
   100    100     }
................................................................................
   135    135     if( pzErrMsg ) *pzErrMsg = 0;
   136    136     res.zErrMsg = 0;
   137    137     res.nRow = 0;
   138    138     res.nColumn = 0;
   139    139     res.nData = 1;
   140    140     res.nAlloc = 20;
   141    141     res.rc = SQLITE_OK;
   142         -  res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
          142  +  res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
   143    143     if( res.azResult==0 ){
   144    144        db->errCode = SQLITE_NOMEM;
   145    145        return SQLITE_NOMEM;
   146    146     }
   147    147     res.azResult[0] = 0;
   148    148     rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
   149    149     assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
................................................................................
   163    163     sqlite3_free(res.zErrMsg);
   164    164     if( rc!=SQLITE_OK ){
   165    165       sqlite3_free_table(&res.azResult[1]);
   166    166       return rc;
   167    167     }
   168    168     if( res.nAlloc>res.nData ){
   169    169       char **azNew;
   170         -    azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
          170  +    azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
   171    171       if( azNew==0 ){
   172    172         sqlite3_free_table(&res.azResult[1]);
   173    173         db->errCode = SQLITE_NOMEM;
   174    174         return SQLITE_NOMEM;
   175    175       }
   176    176       res.azResult = azNew;
   177    177     }

Changes to src/tclsqlite.c.

  3846   3846     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){
  3847   3847       return TCL_ERROR;
  3848   3848     }
  3849   3849     Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
  3850   3850   
  3851   3851     return TCL_OK;
  3852   3852   }
  3853         -#endif
         3853  +#endif /* SQLITE_TEST */
         3854  +
         3855  +#if defined(SQLITE_TEST) || defined(SQLITE_ENABLE_DBSTAT_VTAB)
         3856  +/*
         3857  +** tclcmd:   register_dbstat_vtab DB
         3858  +**
         3859  +** Cause the dbstat virtual table to be available on the connection DB
         3860  +*/
         3861  +static int sqlite3RegisterDbstatCmd(
         3862  +  void *clientData,
         3863  +  Tcl_Interp *interp,
         3864  +  int objc,
         3865  +  Tcl_Obj *CONST objv[]
         3866  +){
         3867  +#ifdef SQLITE_OMIT_VIRTUALTABLE
         3868  +  Tcl_AppendResult(interp, "dbstat not available because of "
         3869  +                           "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
         3870  +  return TCL_ERROR;
         3871  +#else
         3872  +  struct SqliteDb { sqlite3 *db; };
         3873  +  char *zDb;
         3874  +  Tcl_CmdInfo cmdInfo;
         3875  +
         3876  +  if( objc!=2 ){
         3877  +    Tcl_WrongNumArgs(interp, 1, objv, "DB");
         3878  +    return TCL_ERROR;
         3879  +  }
         3880  +
         3881  +  zDb = Tcl_GetString(objv[1]);
         3882  +  if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
         3883  +    int sqlite3_dbstat_register(sqlite3*);
         3884  +    sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
         3885  +    sqlite3_dbstat_register(db);
         3886  +  }
         3887  +  return TCL_OK;
         3888  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
         3889  +}
         3890  +#endif /* defined(SQLITE_TEST) || defined(SQLITE_ENABLE_DBSTAT_VTAB) */
  3854   3891   
  3855   3892   /*
  3856   3893   ** Configure the interpreter passed as the first argument to have access
  3857   3894   ** to the commands and linked variables that make up:
  3858   3895   **
  3859   3896   **   * the [sqlite3] extension itself, 
  3860   3897   **
................................................................................
  3870   3907     Md5_Init(interp);
  3871   3908   #endif
  3872   3909   
  3873   3910     /* Install the [register_dbstat_vtab] command to access the implementation
  3874   3911     ** of virtual table dbstat (source file test_stat.c). This command is
  3875   3912     ** required for testfixture and sqlite3_analyzer, but not by the production
  3876   3913     ** Tcl extension.  */
  3877         -#if defined(SQLITE_TEST) || TCLSH==2
  3878         -  {
  3879         -    extern int SqlitetestStat_Init(Tcl_Interp*);
  3880         -    SqlitetestStat_Init(interp);
  3881         -  }
         3914  +#if defined(SQLITE_TEST) || defined(SQLITE_ENABLE_DBSTAT_VTAB)
         3915  +  Tcl_CreateObjCommand(
         3916  +      interp, "register_dbstat_vtab", sqlite3RegisterDbstatCmd, 0, 0
         3917  +  );
  3882   3918   #endif
  3883   3919   
  3884   3920   #ifdef SQLITE_TEST
  3885   3921     {
  3886   3922       extern int Sqliteconfig_Init(Tcl_Interp*);
  3887   3923       extern int Sqlitetest1_Init(Tcl_Interp*);
  3888   3924       extern int Sqlitetest2_Init(Tcl_Interp*);

Changes to src/test_blob.c.

    12     12   **
    13     13   */
    14     14   #include "sqliteInt.h"
    15     15   #include "tcl.h"
    16     16   #include <stdlib.h>
    17     17   #include <string.h>
    18     18   #include <assert.h>
           19  +#ifndef SQLITE_OMIT_INCRBLOB
    19     20   
    20     21   /* These functions are implemented in main.c. */
    21     22   extern const char *sqlite3ErrName(int);
    22     23   
    23     24   /* From test1.c: */
    24     25   extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
    25     26   extern void *sqlite3TestTextToPtr(const char *z);
................................................................................
   291    292     rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
   292    293     if( rc!=SQLITE_OK ){
   293    294       Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
   294    295     }
   295    296   
   296    297     return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
   297    298   }
   298         -
          299  +#endif /* SQLITE_OMIT_INCRBLOB */
   299    300   
   300    301   /*
   301    302   ** Register commands with the TCL interpreter.
   302    303   */
   303    304   int Sqlitetest_blob_Init(Tcl_Interp *interp){
          305  +#ifndef SQLITE_OMIT_INCRBLOB
   304    306     static struct {
   305    307        char *zName;
   306    308        Tcl_ObjCmdProc *xProc;
   307    309     } aObjCmd[] = {
   308    310        { "sqlite3_blob_open",            test_blob_open        },
   309    311        { "sqlite3_blob_close",           test_blob_close       },
   310    312        { "sqlite3_blob_bytes",           test_blob_bytes       },
................................................................................
   311    313        { "sqlite3_blob_read",            test_blob_read        },
   312    314        { "sqlite3_blob_write",           test_blob_write       },
   313    315     };
   314    316     int i;
   315    317     for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
   316    318       Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
   317    319     }
          320  +#endif /* SQLITE_OMIT_INCRBLOB */
   318    321     return TCL_OK;
   319    322   }

Changes to src/test_intarray.c.

    81     81     void *pAux,               /* clientdata for the module */
    82     82     int argc,                 /* Number of arguments */
    83     83     const char *const*argv,   /* Value for all arguments */
    84     84     sqlite3_vtab **ppVtab,    /* Write the new virtual table object here */
    85     85     char **pzErr              /* Put error message text here */
    86     86   ){
    87     87     int rc = SQLITE_NOMEM;
    88         -  intarray_vtab *pVtab = sqlite3_malloc(sizeof(intarray_vtab));
           88  +  intarray_vtab *pVtab = sqlite3_malloc64(sizeof(intarray_vtab));
    89     89   
    90     90     if( pVtab ){
    91     91       memset(pVtab, 0, sizeof(intarray_vtab));
    92     92       pVtab->pContent = (sqlite3_intarray*)pAux;
    93     93       rc = sqlite3_declare_vtab(db, "CREATE TABLE x(value INTEGER PRIMARY KEY)");
    94     94     }
    95     95     *ppVtab = (sqlite3_vtab *)pVtab;
................................................................................
    98     98   
    99     99   /*
   100    100   ** Open a new cursor on the intarray table.
   101    101   */
   102    102   static int intarrayOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
   103    103     int rc = SQLITE_NOMEM;
   104    104     intarray_cursor *pCur;
   105         -  pCur = sqlite3_malloc(sizeof(intarray_cursor));
          105  +  pCur = sqlite3_malloc64(sizeof(intarray_cursor));
   106    106     if( pCur ){
   107    107       memset(pCur, 0, sizeof(intarray_cursor));
   108    108       *ppCursor = (sqlite3_vtab_cursor *)pCur;
   109    109       rc = SQLITE_OK;
   110    110     }
   111    111     return rc;
   112    112   }
................................................................................
   221    221     const char *zName,
   222    222     sqlite3_intarray **ppReturn
   223    223   ){
   224    224     int rc = SQLITE_OK;
   225    225   #ifndef SQLITE_OMIT_VIRTUALTABLE
   226    226     sqlite3_intarray *p;
   227    227   
   228         -  *ppReturn = p = sqlite3_malloc( sizeof(*p) );
          228  +  *ppReturn = p = sqlite3_malloc64( sizeof(*p) );
   229    229     if( p==0 ){
   230    230       return SQLITE_NOMEM;
   231    231     }
   232    232     memset(p, 0, sizeof(*p));
   233    233     rc = sqlite3_create_module_v2(db, zName, &intarrayModule, p,
   234    234                                   (void(*)(void*))intarrayFree);
   235    235     if( rc==SQLITE_OK ){
................................................................................
   336    336     if( objc<2 ){
   337    337       Tcl_WrongNumArgs(interp, 1, objv, "INTARRAY");
   338    338       return TCL_ERROR;
   339    339     }
   340    340     pArray = (sqlite3_intarray*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
   341    341     n = objc - 2;
   342    342   #ifndef SQLITE_OMIT_VIRTUALTABLE
   343         -  a = sqlite3_malloc( sizeof(a[0])*n );
          343  +  a = sqlite3_malloc64( sizeof(a[0])*n );
   344    344     if( a==0 ){
   345    345       Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0);
   346    346       return TCL_ERROR;
   347    347     }
   348    348     for(i=0; i<n; i++){
   349    349       Tcl_WideInt x = 0;
   350    350       Tcl_GetWideIntFromObj(0, objv[i+2], &x);

Changes to src/test_multiplex.c.

   282    282   }
   283    283   
   284    284   /* Compute the filename for the iChunk-th chunk
   285    285   */
   286    286   static int multiplexSubFilename(multiplexGroup *pGroup, int iChunk){
   287    287     if( iChunk>=pGroup->nReal ){
   288    288       struct multiplexReal *p;
   289         -    p = sqlite3_realloc(pGroup->aReal, (iChunk+1)*sizeof(*p));
          289  +    p = sqlite3_realloc64(pGroup->aReal, (iChunk+1)*sizeof(*p));
   290    290       if( p==0 ){
   291    291         return SQLITE_NOMEM;
   292    292       }
   293    293       memset(&p[pGroup->nReal], 0, sizeof(p[0])*(iChunk+1-pGroup->nReal));
   294    294       pGroup->aReal = p;
   295    295       pGroup->nReal = iChunk+1;
   296    296     }
   297    297     if( pGroup->zName && pGroup->aReal[iChunk].z==0 ){
   298    298       char *z;
   299    299       int n = pGroup->nName;
   300         -    pGroup->aReal[iChunk].z = z = sqlite3_malloc( n+5 );
          300  +    pGroup->aReal[iChunk].z = z = sqlite3_malloc64( n+5 );
   301    301       if( z==0 ){
   302    302         return SQLITE_NOMEM;
   303    303       }
   304    304       multiplexFilename(pGroup->zName, pGroup->nName, pGroup->flags, iChunk, z);
   305    305     }
   306    306     return SQLITE_OK;
   307    307   }
................................................................................
   353    353             sqlite3_log(*rc, "multiplexor.xAccess failure on %s",
   354    354                         pGroup->aReal[iChunk].z);
   355    355           }
   356    356           return 0;
   357    357         }
   358    358         flags &= ~SQLITE_OPEN_CREATE;
   359    359       }
   360         -    pSubOpen = sqlite3_malloc( pOrigVfs->szOsFile );
          360  +    pSubOpen = sqlite3_malloc64( pOrigVfs->szOsFile );
   361    361       if( pSubOpen==0 ){
   362    362         *rc = SQLITE_IOERR_NOMEM;
   363    363         return 0;
   364    364       }
   365    365       pGroup->aReal[iChunk].p = pSubOpen;
   366    366       *rc = pOrigVfs->xOpen(pOrigVfs, pGroup->aReal[iChunk].z, pSubOpen,
   367    367                             flags, pOutFlags);
................................................................................
   520    520     pMultiplexOpen = (multiplexConn*)pConn;
   521    521   
   522    522     if( rc==SQLITE_OK ){
   523    523       /* allocate space for group */
   524    524       nName = zName ? multiplexStrlen30(zName) : 0;
   525    525       sz = sizeof(multiplexGroup)                             /* multiplexGroup */
   526    526          + nName + 1;                                         /* zName */
   527         -    pGroup = sqlite3_malloc( sz );
          527  +    pGroup = sqlite3_malloc64( sz );
   528    528       if( pGroup==0 ){
   529    529         rc = SQLITE_NOMEM;
   530    530       }
   531    531     }
   532    532   
   533    533     if( rc==SQLITE_OK ){
   534    534       const char *zUri = (flags & SQLITE_OPEN_URI) ? zName : 0;
................................................................................
   651    651     rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
   652    652     if( rc==SQLITE_OK ){
   653    653       /* If the main chunk was deleted successfully, also delete any subsequent
   654    654       ** chunks - starting with the last (highest numbered). 
   655    655       */
   656    656       int nName = (int)strlen(zName);
   657    657       char *z;
   658         -    z = sqlite3_malloc(nName + 5);
          658  +    z = sqlite3_malloc64(nName + 5);
   659    659       if( z==0 ){
   660    660         rc = SQLITE_IOERR_NOMEM;
   661    661       }else{
   662    662         int iChunk = 0;
   663    663         int bExists;
   664    664         do{
   665    665           multiplexFilename(zName, nName, SQLITE_OPEN_MAIN_JOURNAL, ++iChunk, z);

Changes to src/tokenize.c.

   446    446           }
   447    447           break;
   448    448         }
   449    449       }
   450    450     }
   451    451   abort_parse:
   452    452     assert( nErr==0 );
   453         -  if( zSql[i]==0 && pParse->rc==SQLITE_OK ){
          453  +  if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
   454    454       if( lastTokenParsed!=TK_SEMI ){
   455    455         sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
   456    456         pParse->zTail = &zSql[i];
   457    457       }
          458  +    if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
   458    459       sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
   459    460     }
          461  +  }
   460    462   #ifdef YYTRACKMAXSTACKDEPTH
   461    463     sqlite3_mutex_enter(sqlite3MallocMutex());
   462    464     sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
   463    465         sqlite3ParserStackPeak(pEngine)
   464    466     );
   465    467     sqlite3_mutex_leave(sqlite3MallocMutex());
   466    468   #endif /* YYDEBUG */

Changes to src/vdbe.c.

  1211   1211     do{
  1212   1212       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  1213   1213       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  1214   1214       assert( memIsValid(pIn1) );
  1215   1215       memAboutToChange(p, pOut);
  1216   1216       sqlite3VdbeMemMove(pOut, pIn1);
  1217   1217   #ifdef SQLITE_DEBUG
  1218         -    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  1219         -      pOut->pScopyFrom += p1 - pOp->p2;
         1218  +    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
         1219  +      pOut->pScopyFrom += pOp->p2 - p1;
  1220   1220       }
  1221   1221   #endif
         1222  +    Deephemeralize(pOut);
  1222   1223       REGISTER_TRACE(p2++, pOut);
  1223   1224       pIn1++;
  1224   1225       pOut++;
  1225   1226     }while( --n );
  1226   1227     break;
  1227   1228   }
  1228   1229   
................................................................................
  2483   2484          || (offset > pC->payloadSize)
  2484   2485         ){
  2485   2486           rc = SQLITE_CORRUPT_BKPT;
  2486   2487           goto op_column_error;
  2487   2488         }
  2488   2489       }
  2489   2490   
  2490         -    /* If after trying to extra new entries from the header, nHdrParsed is
         2491  +    /* If after trying to extract new entries from the header, nHdrParsed is
  2491   2492       ** still not up to p2, that means that the record has fewer than p2
  2492   2493       ** columns.  So the result will be either the default value or a NULL.
  2493   2494       */
  2494   2495       if( pC->nHdrParsed<=p2 ){
  2495   2496         if( pOp->p4type==P4_MEM ){
  2496   2497           sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
  2497   2498         }else{
................................................................................
  6163   6164   
  6164   6165       /* Initialize vdbe cursor object */
  6165   6166       pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
  6166   6167       if( pCur ){
  6167   6168         pCur->pVtabCursor = pVtabCursor;
  6168   6169         pVtab->nRef++;
  6169   6170       }else{
  6170         -      db->mallocFailed = 1;
         6171  +      assert( db->mallocFailed );
  6171   6172         pModule->xClose(pVtabCursor);
         6173  +      goto no_mem;
  6172   6174       }
  6173   6175     }
  6174   6176     break;
  6175   6177   }
  6176   6178   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6177   6179   
  6178   6180   #ifndef SQLITE_OMIT_VIRTUALTABLE

Changes to src/vdbemem.c.

   196    196     if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
   197    197       if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
   198    198         return SQLITE_NOMEM;
   199    199       }
   200    200       pMem->z[pMem->n] = 0;
   201    201       pMem->z[pMem->n+1] = 0;
   202    202       pMem->flags |= MEM_Term;
          203  +  }
          204  +  pMem->flags &= ~MEM_Ephem;
   203    205   #ifdef SQLITE_DEBUG
   204    206       pMem->pScopyFrom = 0;
   205    207   #endif
   206         -  }
   207    208   
   208    209     return SQLITE_OK;
   209    210   }
   210    211   
   211    212   /*
   212    213   ** If the given Mem* has a zero-filled tail, turn it into an ordinary
   213    214   ** blob stored in dynamically allocated space.

Changes to src/vdbesort.c.

  2059   2059   ** SQLITE_MAX_WORKER_THREADS==0).  The other values are only used
  2060   2060   ** when there exists one or more separate worker threads.
  2061   2061   */
  2062   2062   #define INCRINIT_NORMAL 0
  2063   2063   #define INCRINIT_TASK   1
  2064   2064   #define INCRINIT_ROOT   2
  2065   2065   
  2066         -/* Forward reference.
  2067         -** The vdbeIncrMergeInit() and vdbePmaReaderIncrMergeInit() routines call each
  2068         -** other (when building a merge tree).
         2066  +/* 
         2067  +** Forward reference required as the vdbeIncrMergeInit() and
         2068  +** vdbePmaReaderIncrInit() routines are called mutually recursively when
         2069  +** building a merge tree.
  2069   2070   */
  2070         -static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode);
         2071  +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
  2071   2072   
  2072   2073   /*
  2073   2074   ** Initialize the MergeEngine object passed as the second argument. Once this
  2074   2075   ** function returns, the first key of merged data may be read from the 
  2075   2076   ** MergeEngine object in the usual fashion.
  2076   2077   **
  2077   2078   ** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
................................................................................
  2110   2111         ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
  2111   2112         ** in use it will block the vdbePmaReaderNext() call while it uses
  2112   2113         ** the main thread to fill its buffer. So calling PmaReaderNext()
  2113   2114         ** on this PmaReader before any of the multi-threaded PmaReaders takes
  2114   2115         ** better advantage of multi-processor hardware. */
  2115   2116         rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
  2116   2117       }else{
  2117         -      rc = vdbePmaReaderIncrMergeInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
         2118  +      rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
  2118   2119       }
  2119   2120       if( rc!=SQLITE_OK ) return rc;
  2120   2121     }
  2121   2122   
  2122   2123     for(i=pMerger->nTree-1; i>0; i--){
  2123   2124       vdbeMergeEngineCompare(pMerger, i);
  2124   2125     }
  2125   2126     return pTask->pUnpacked->errCode;
  2126   2127   }
  2127   2128   
  2128   2129   /*
  2129         -** Initialize the IncrMerge field of a PmaReader.
  2130         -**
  2131         -** If the PmaReader passed as the first argument is not an incremental-reader
  2132         -** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it serves
  2133         -** to open and/or initialize the temp file related fields of the IncrMerge
         2130  +** The PmaReader passed as the first argument is guaranteed to be an
         2131  +** incremental-reader (pReadr->pIncr!=0). This function serves to open
         2132  +** and/or initialize the temp file related fields of the IncrMerge
  2134   2133   ** object at (pReadr->pIncr).
  2135   2134   **
  2136   2135   ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
  2137         -** in the sub-tree headed by pReadr are also initialized. Data is then loaded
  2138         -** into the buffers belonging to pReadr and it is set to
  2139         -** point to the first key in its range.
         2136  +** in the sub-tree headed by pReadr are also initialized. Data is then 
         2137  +** loaded into the buffers belonging to pReadr and it is set to point to 
         2138  +** the first key in its range.
  2140   2139   **
  2141   2140   ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
  2142   2141   ** to be a multi-threaded PmaReader and this function is being called in a
  2143   2142   ** background thread. In this case all PmaReaders in the sub-tree are 
  2144   2143   ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
  2145   2144   ** pReadr is populated. However, pReadr itself is not set up to point
  2146   2145   ** to its first key. A call to vdbePmaReaderNext() is still required to do
................................................................................
  2159   2158   ** the current PmaReader set to point to the first key in its range.
  2160   2159   **
  2161   2160   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  2162   2161   */
  2163   2162   static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
  2164   2163     int rc = SQLITE_OK;
  2165   2164     IncrMerger *pIncr = pReadr->pIncr;
         2165  +  SortSubtask *pTask = pIncr->pTask;
         2166  +  sqlite3 *db = pTask->pSorter->db;
  2166   2167   
  2167   2168     /* eMode is always INCRINIT_NORMAL in single-threaded mode */
  2168   2169     assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
  2169   2170   
  2170         -  if( pIncr ){
  2171         -    SortSubtask *pTask = pIncr->pTask;
  2172         -    sqlite3 *db = pTask->pSorter->db;
  2173         -
  2174   2171       rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
  2175   2172   
  2176   2173       /* Set up the required files for pIncr. A multi-theaded IncrMerge object
  2177   2174       ** requires two temp files to itself, whereas a single-threaded object
  2178   2175       ** only requires a region of pTask->file2. */
  2179   2176       if( rc==SQLITE_OK ){
  2180   2177         int mxSz = pIncr->mxSz;
................................................................................
  2199   2196           }
  2200   2197         }
  2201   2198       }
  2202   2199   
  2203   2200   #if SQLITE_MAX_WORKER_THREADS>0
  2204   2201       if( rc==SQLITE_OK && pIncr->bUseThread ){
  2205   2202         /* Use the current thread to populate aFile[1], even though this
  2206         -      ** PmaReader is multi-threaded. The reason being that this function
  2207         -      ** is already running in background thread pIncr->pTask->thread. */
         2203  +    ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
         2204  +    ** then this function is already running in background thread 
         2205  +    ** pIncr->pTask->thread. 
         2206  +    **
         2207  +    ** If this is the INCRINIT_ROOT object, then it is running in the 
         2208  +    ** main VDBE thread. But that is Ok, as that thread cannot return
         2209  +    ** control to the VDBE or proceed with anything useful until the 
         2210  +    ** first results are ready from this merger object anyway.
         2211  +    */
  2208   2212         assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
  2209   2213         rc = vdbeIncrPopulate(pIncr);
  2210   2214       }
  2211   2215   #endif
  2212   2216   
  2213         -    if( rc==SQLITE_OK
  2214         -     && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK)
  2215         -    ){
         2217  +  if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
  2216   2218         rc = vdbePmaReaderNext(pReadr);
  2217   2219       }
  2218         -  }
         2220  +
  2219   2221     return rc;
  2220   2222   }
  2221   2223   
  2222   2224   #if SQLITE_MAX_WORKER_THREADS>0
  2223   2225   /*
  2224   2226   ** The main routine for vdbePmaReaderIncrMergeInit() operations run in 
  2225   2227   ** background threads.
  2226   2228   */
  2227         -static void *vdbePmaReaderBgInit(void *pCtx){
         2229  +static void *vdbePmaReaderBgIncrInit(void *pCtx){
  2228   2230     PmaReader *pReader = (PmaReader*)pCtx;
  2229   2231     void *pRet = SQLITE_INT_TO_PTR(
  2230   2232                     vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
  2231   2233                  );
  2232   2234     pReader->pIncr->pTask->bDone = 1;
  2233   2235     return pRet;
  2234   2236   }
         2237  +#endif
  2235   2238   
  2236   2239   /*
  2237         -** Use a background thread to invoke vdbePmaReaderIncrMergeInit(INCRINIT_TASK) 
  2238         -** on the PmaReader object passed as the first argument.
         2240  +** If the PmaReader passed as the first argument is not an incremental-reader
         2241  +** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
         2242  +** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
         2243  +** this routine to initialize the incremental merge.
  2239   2244   **
  2240         -** This call will initialize the various fields of the pReadr->pIncr 
  2241         -** structure and, if it is a multi-threaded IncrMerger, launch a 
  2242         -** background thread to populate aFile[1].
         2245  +** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), 
         2246  +** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
         2247  +** Or, if the IncrMerger is single threaded, the same function is called
         2248  +** using the current thread.
  2243   2249   */
  2244         -static int vdbePmaReaderBgIncrInit(PmaReader *pReadr){
         2250  +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
         2251  +  IncrMerger *pIncr = pReadr->pIncr;   /* Incremental merger */
         2252  +  int rc = SQLITE_OK;                  /* Return code */
         2253  +  if( pIncr ){
         2254  +#if SQLITE_MAX_WORKER_THREADS>0
         2255  +    assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
         2256  +    if( pIncr->bUseThread ){
  2245   2257     void *pCtx = (void*)pReadr;
  2246         -  return vdbeSorterCreateThread(pReadr->pIncr->pTask, vdbePmaReaderBgInit, pCtx);
  2247         -}
         2258  +      rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
         2259  +    }else
  2248   2260   #endif
         2261  +    {
         2262  +      rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
         2263  +    }
         2264  +  }
         2265  +  return rc;
         2266  +}
  2249   2267   
  2250   2268   /*
  2251   2269   ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
  2252   2270   ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
  2253   2271   ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
  2254   2272   ** to NULL and return an SQLite error code.
  2255   2273   **
................................................................................
  2486   2504               IncrMerger *pIncr;
  2487   2505               if( (pIncr = pMain->aReadr[iTask].pIncr) ){
  2488   2506                 vdbeIncrMergerSetThreads(pIncr);
  2489   2507                 assert( pIncr->pTask!=pLast );
  2490   2508               }
  2491   2509             }
  2492   2510             for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
         2511  +            /* Check that:
         2512  +            **   
         2513  +            **   a) The incremental merge object is configured to use the
         2514  +            **      right task, and
         2515  +            **   b) If it is using task (nTask-1), it is configured to run
         2516  +            **      in single-threaded mode. This is important, as the
         2517  +            **      root merge (INCRINIT_ROOT) will be using the same task
         2518  +            **      object.
         2519  +            */
  2493   2520               PmaReader *p = &pMain->aReadr[iTask];
  2494         -            assert( p->pIncr==0 || p->pIncr->pTask==&pSorter->aTask[iTask] );
  2495         -            if( p->pIncr ){ 
  2496         -              if( iTask==pSorter->nTask-1 ){
  2497         -                rc = vdbePmaReaderIncrMergeInit(p, INCRINIT_TASK);
  2498         -              }else{
  2499         -                rc = vdbePmaReaderBgIncrInit(p);
  2500         -              }
  2501         -            }
         2521  +            assert( p->pIncr==0 || (
         2522  +                (p->pIncr->pTask==&pSorter->aTask[iTask])             /* a */
         2523  +             && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0)  /* b */
         2524  +            ));
         2525  +            rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
  2502   2526             }
  2503   2527           }
  2504   2528           pMain = 0;
  2505   2529         }
  2506   2530         if( rc==SQLITE_OK ){
  2507   2531           rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
  2508   2532         }

Changes to src/vdbetrace.c.

    80     80     int nToken;              /* Length of the parameter token */
    81     81     int i;                   /* Loop counter */
    82     82     Mem *pVar;               /* Value of a host parameter */
    83     83     StrAccum out;            /* Accumulate the output here */
    84     84     char zBase[100];         /* Initial working space */
    85     85   
    86     86     db = p->db;
    87         -  sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
           87  +  sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), 
    88     88                         db->aLimit[SQLITE_LIMIT_LENGTH]);
    89         -  out.db = db;
    90     89     if( db->nVdbeExec>1 ){
    91     90       while( *zRawSql ){
    92     91         const char *zStart = zRawSql;
    93     92         while( *(zRawSql++)!='\n' && *zRawSql );
    94     93         sqlite3StrAccumAppend(&out, "-- ", 3);
    95     94         assert( (zRawSql - zStart) > 0 );
    96     95         sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));

Changes to src/vtab.c.

  1078   1078     Table **apVtabLock;
  1079   1079   
  1080   1080     assert( IsVirtual(pTab) );
  1081   1081     for(i=0; i<pToplevel->nVtabLock; i++){
  1082   1082       if( pTab==pToplevel->apVtabLock[i] ) return;
  1083   1083     }
  1084   1084     n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
  1085         -  apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
         1085  +  apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
  1086   1086     if( apVtabLock ){
  1087   1087       pToplevel->apVtabLock = apVtabLock;
  1088   1088       pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
  1089   1089     }else{
  1090   1090       pToplevel->db->mallocFailed = 1;
  1091   1091     }
  1092   1092   }

Changes to src/wal.c.

   518    518   static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
   519    519     int rc = SQLITE_OK;
   520    520   
   521    521     /* Enlarge the pWal->apWiData[] array if required */
   522    522     if( pWal->nWiData<=iPage ){
   523    523       int nByte = sizeof(u32*)*(iPage+1);
   524    524       volatile u32 **apNew;
   525         -    apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
          525  +    apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
   526    526       if( !apNew ){
   527    527         *ppPage = 0;
   528    528         return SQLITE_NOMEM;
   529    529       }
   530    530       memset((void*)&apNew[pWal->nWiData], 0,
   531    531              sizeof(u32*)*(iPage+1-pWal->nWiData));
   532    532       pWal->apWiData = apNew;
................................................................................
  1143   1143       if( version!=WAL_MAX_VERSION ){
  1144   1144         rc = SQLITE_CANTOPEN_BKPT;
  1145   1145         goto finished;
  1146   1146       }
  1147   1147   
  1148   1148       /* Malloc a buffer to read frames into. */
  1149   1149       szFrame = szPage + WAL_FRAME_HDRSIZE;
  1150         -    aFrame = (u8 *)sqlite3_malloc(szFrame);
         1150  +    aFrame = (u8 *)sqlite3_malloc64(szFrame);
  1151   1151       if( !aFrame ){
  1152   1152         rc = SQLITE_NOMEM;
  1153   1153         goto recovery_error;
  1154   1154       }
  1155   1155       aData = &aFrame[WAL_FRAME_HDRSIZE];
  1156   1156   
  1157   1157       /* Read all frames from the log file. */
................................................................................
  1536   1536     iLast = pWal->hdr.mxFrame;
  1537   1537   
  1538   1538     /* Allocate space for the WalIterator object. */
  1539   1539     nSegment = walFramePage(iLast) + 1;
  1540   1540     nByte = sizeof(WalIterator) 
  1541   1541           + (nSegment-1)*sizeof(struct WalSegment)
  1542   1542           + iLast*sizeof(ht_slot);
  1543         -  p = (WalIterator *)sqlite3_malloc(nByte);
         1543  +  p = (WalIterator *)sqlite3_malloc64(nByte);
  1544   1544     if( !p ){
  1545   1545       return SQLITE_NOMEM;
  1546   1546     }
  1547   1547     memset(p, 0, nByte);
  1548   1548     p->nSegment = nSegment;
  1549   1549   
  1550   1550     /* Allocate temporary space used by the merge-sort routine. This block
  1551   1551     ** of memory will be freed before this function returns.
  1552   1552     */
  1553         -  aTmp = (ht_slot *)sqlite3_malloc(
         1553  +  aTmp = (ht_slot *)sqlite3_malloc64(
  1554   1554         sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
  1555   1555     );
  1556   1556     if( !aTmp ){
  1557   1557       rc = SQLITE_NOMEM;
  1558   1558     }
  1559   1559   
  1560   1560     for(i=0; rc==SQLITE_OK && i<nSegment; i++){

Changes to src/where.c.

  3099   3099       flags = pLoop->wsFlags;
  3100   3100       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
  3101   3101   
  3102   3102       isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
  3103   3103               || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
  3104   3104               || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
  3105   3105   
  3106         -    sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
  3107         -    str.db = db;
         3106  +    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
  3108   3107       sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
  3109   3108       if( pItem->pSelect ){
  3110   3109         sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
  3111   3110       }else{
  3112   3111         sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
  3113   3112       }
  3114   3113   
................................................................................
  4299   4298   }
  4300   4299   
  4301   4300   /*
  4302   4301   ** Free a WhereInfo structure
  4303   4302   */
  4304   4303   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
  4305   4304     if( ALWAYS(pWInfo) ){
         4305  +    int i;
         4306  +    for(i=0; i<pWInfo->nLevel; i++){
         4307  +      WhereLevel *pLevel = &pWInfo->a[i];
         4308  +      if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
         4309  +        sqlite3DbFree(db, pLevel->u.in.aInLoop);
         4310  +      }
         4311  +    }
  4306   4312       whereClauseClear(&pWInfo->sWC);
  4307   4313       while( pWInfo->pLoops ){
  4308   4314         WhereLoop *p = pWInfo->pLoops;
  4309   4315         pWInfo->pLoops = p->pNextLoop;
  4310   4316         whereLoopDelete(db, p);
  4311   4317       }
  4312   4318       sqlite3DbFree(db, pWInfo);
................................................................................
  6895   6901           sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
  6896   6902           sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
  6897   6903           VdbeCoverage(v);
  6898   6904           VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
  6899   6905           VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
  6900   6906           sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
  6901   6907         }
  6902         -      sqlite3DbFree(db, pLevel->u.in.aInLoop);
  6903   6908       }
  6904   6909       sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
  6905   6910       if( pLevel->addrSkip ){
  6906   6911         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
  6907   6912         VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
  6908   6913         sqlite3VdbeJumpHere(v, pLevel->addrSkip);
  6909   6914         sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);

Changes to test/alter.test.

   908    908   do_execsql_test alter-17.9 {
   909    909     SELECT sqlite_rename_parent('CREATE TABLE t1(a REFERENCES "xyzzy")',
   910    910            NULL, 'lmnop');
   911    911   } {{}}
   912    912   do_execsql_test alter-17.10 {
   913    913     SELECT sqlite_rename_parent(NULL,'abc','xyz');
   914    914   } {{}}
          915  +do_execsql_test alter-17.11 {
          916  +  SELECT sqlite_rename_parent('create references ''','abc','xyz');
          917  +} {{create references '}}
          918  +do_execsql_test alter-17.12 {
          919  +  SELECT sqlite_rename_parent('create references "abc"123" ','abc','xyz');
          920  +} {{create references "xyz"123" }}
          921  +do_execsql_test alter-17.13 {
          922  +  SELECT sqlite_rename_parent("references '''",'abc','xyz');
          923  +} {{references '''}}
   915    924   
   916    925   finish_test

Changes to test/fts3matchinfo.test.

   445    445       JOIN (SELECT 1 AS idx UNION SELECT 2 UNION SELECT 3) AS x
   446    446      WHERE t10 MATCH x.idx
   447    447        AND matchinfo(t10) not null
   448    448      GROUP BY docId
   449    449      ORDER BY 1;
   450    450   } {1 1 one 2 2 two 3 3 three}
   451    451     
          452  +#---------------------------------------------------------------------------
          453  +# Test the 'y' matchinfo flag
          454  +#
          455  +set sqlite_fts3_enable_parentheses 1
          456  +reset_db
          457  +do_execsql_test 11.0 {
          458  +  CREATE VIRTUAL TABLE tt USING fts3(x, y);
          459  +  INSERT INTO tt VALUES('c d a c d d', 'e a g b d a');   -- 1
          460  +  INSERT INTO tt VALUES('c c g a e b', 'c g d g e c');   -- 2
          461  +  INSERT INTO tt VALUES('b e f d e g', 'b a c b c g');   -- 3
          462  +  INSERT INTO tt VALUES('a c f f g d', 'd b f d e g');   -- 4
          463  +  INSERT INTO tt VALUES('g a c f c f', 'd g g b c c');   -- 5
          464  +  INSERT INTO tt VALUES('g a c e b b', 'd b f b g g');   -- 6
          465  +  INSERT INTO tt VALUES('f d a a f c', 'e e a d c f');   -- 7
          466  +  INSERT INTO tt VALUES('a c b b g f', 'a b a e d f');   -- 8
          467  +  INSERT INTO tt VALUES('b a f e c c', 'f d b b a b');   -- 9
          468  +  INSERT INTO tt VALUES('f d c e a c', 'f a f a a f');   -- 10
          469  +}
          470  +
          471  +db func mit mit
          472  +foreach {tn expr res} {
          473  +  1 "a" {
          474  +      1 {1 2}   2 {1 0}   3 {0 1}   4 {1 0}   5 {1 0}
          475  +      6 {1 0}   7 {2 1}   8 {1 2}   9 {1 1}  10 {1 3}
          476  +  }
          477  +
          478  +  2 "b" {
          479  +      1 {0 1}   2 {1 0}   3 {1 2}   4 {0 1}   5 {0 1}
          480  +      6 {2 2}             8 {2 1}   9 {1 3}            
          481  +  }
          482  +
          483  +  3 "y:a" {
          484  +      1 {0 2}             3 {0 1}                    
          485  +                7 {0 1}   8 {0 2}   9 {0 1}  10 {0 3}
          486  +  }
          487  +
          488  +  4 "x:a" {
          489  +      1 {1 0}   2 {1 0}             4 {1 0}   5 {1 0}
          490  +      6 {1 0}   7 {2 0}   8 {1 0}   9 {1 0}  10 {1 0}
          491  +  }
          492  +
          493  +  5 "a OR b" {
          494  +      1 {1 2 0 1}   2 {1 0 1 0}   3 {0 1 1 2}   4 {1 0 0 1}   5 {1 0 0 1}
          495  +      6 {1 0 2 2}   7 {2 1 0 0}   8 {1 2 2 1}   9 {1 1 1 3}  10 {1 3 0 0}
          496  +  }
          497  +
          498  +  6 "a AND b" {
          499  +      1 {1 2 0 1}   2 {1 0 1 0}   3 {0 1 1 2}   4 {1 0 0 1}   5 {1 0 0 1}
          500  +      6 {1 0 2 2}                 8 {1 2 2 1}   9 {1 1 1 3}              
          501  +  }
          502  +
          503  +  7 "a OR (a AND b)" {
          504  +      1 {1 2 1 2 0 1}   2 {1 0 1 0 1 0}   3 {0 1 0 1 1 2}   4 {1 0 1 0 0 1}   
          505  +      5 {1 0 1 0 0 1}   6 {1 0 1 0 2 2}   7 {2 1 0 0 0 0}   8 {1 2 1 2 2 1}   
          506  +      9 {1 1 1 1 1 3}  10 {1 3 0 0 0 0}
          507  +  }
          508  +
          509  +} {
          510  +  do_execsql_test 11.1.$tn  {
          511  +    SELECT rowid, mit(matchinfo(tt, 'y')) FROM tt WHERE tt MATCH $expr
          512  +  } $res
          513  +}
          514  +set sqlite_fts3_enable_parentheses 0
   452    515   
   453    516   finish_test

Added test/fuzzdata1.txt.

cannot compute difference between binary files

Added test/fuzzdata2.txt.

cannot compute difference between binary files

Changes to test/jrnlmode.test.

   550    550   do_execsql_test jrnlmode-8.24 { PRAGMA journal_mode=TRUNCATE }  {truncate}
   551    551   do_execsql_test jrnlmode-8.25 { PRAGMA locking_mode=NORMAL }    {normal}
   552    552   do_execsql_test jrnlmode-8.26 { CREATE TABLE t4(w) }            {}
   553    553   do_execsql_test jrnlmode-8.27 { BEGIN IMMEDIATE }               {}
   554    554   do_execsql_test jrnlmode-8.28 { PRAGMA journal_mode=DELETE }    {delete}
   555    555   do_execsql_test jrnlmode-8.29 { COMMIT }                        {}
   556    556   do_execsql_test jrnlmode-8.30 { PRAGMA journal_mode=DELETE }    {delete}
          557  +
          558  +# Assertion fault on 2015-05-01
          559  +do_test jrnlmode-9.1 {
          560  +  forcedelete test2.db
          561  +  sqlite3 db2 test2.db
          562  +  breakpoint
          563  +  db2 eval {CREATE TEMP TABLE t(l); PRAGMA journal_mode=off;}
          564  +  db2 close
          565  +} {}
          566  +do_execsql_test jrnlmode-9.2 {
          567  +  PRAGMA locking_mode = exclusive;
          568  +  CREATE TABLE tx(a);
          569  +  PRAGMA journal_mode = off;
          570  +} {exclusive off}
          571  +
   557    572   
   558    573   finish_test

Changes to test/malloc.test.

   918    918   }
   919    919   do_faultsim_test 41.2 -faults oom* -body {
   920    920     execsql { SELECT * FROM t1 WHERE a = ('abcde' || $::big)}
   921    921   } -test {
   922    922     faultsim_test_result [list 0 "abcde$::big"]
   923    923     faultsim_integrity_check
   924    924   }
          925  +
          926  +reset_db
          927  +do_execsql_test 42.0 {
          928  +  CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z);
          929  +  CREATE TABLE t2(a, b);
          930  +  CREATE VIEW a002 AS SELECT *, sum(b) AS m FROM t2 GROUP BY a;
          931  +}
          932  +faultsim_save_and_close
          933  +do_faultsim_test 42 -faults oom-tran* -prep {
          934  +  faultsim_restore_and_reopen
          935  +  execsql { SELECT * FROM sqlite_master }
          936  +} -body {
          937  +  execsql {
          938  +    SELECT t1.z, a002.m
          939  +    FROM t1 JOIN a002 ON t1.y=a002.m
          940  +    WHERE t1.x IN (1,2,3);
          941  +  }
          942  +} -test {
          943  +  faultsim_test_result {0 {}}
          944  +}
          945  +
   925    946   
   926    947   # Ensure that no file descriptors were leaked.
   927    948   do_test malloc-99.X {
   928    949     catch {db close}
   929    950     set sqlite_open_file_count
   930    951   } {0}
   931    952   
   932    953   puts open-file-count=$sqlite_open_file_count
   933    954   finish_test

Added test/mkfuzzdata1.tcl.

            1  +#!/usr/bin/tclsh
            2  +#
            3  +# Run this script in order to rebuild the fuzzdata1.txt file containing
            4  +# fuzzer data for the fuzzershell utility that is create by afl-fuzz.
            5  +#
            6  +# This script gathers all of the test cases identified by afl-fuzz and
            7  +# runs afl-cmin and afl-tmin over them all to try to generate a mimimum
            8  +# set of tests that cover all observed behavior.
            9  +# 
           10  +# Options:
           11  +#
           12  +#    --afl-bin DIR1             DIR1 contains the AFL binaries
           13  +#    --fuzzershell PATH         Full pathname of instrumented fuzzershell
           14  +#    --afl-data DIR3            DIR3 is the "-o" directory from afl-fuzz
           15  +#    -o FILE                    Write results into FILE
           16  +#
           17  +set AFLBIN {}
           18  +set FUZZERSHELL {}
           19  +set AFLDATA {}
           20  +set OUTFILE {}
           21  +
           22  +proc usage {} {
           23  +  puts stderr "Usage: $::argv0 --afl-bin DIR --fuzzershell PATH\
           24  +                  --afl-data DIR -o FILE"
           25  +  exit 1
           26  +}
           27  +proc cmdlineerr {msg} {
           28  +  puts stderr $msg
           29  +  usage
           30  +}
           31  +
           32  +for {set i 0} {$i<[llength $argv]} {incr i} {
           33  +  set x [lindex $argv $i]
           34  +  if {[string index $x 0]!="-"} {cmdlineerr "illegal argument: $x"}
           35  +  set x [string trimleft $x -]
           36  +  incr i
           37  +  if {$i>=[llength $argv]} {cmdlineerr "no argument on --$x"}
           38  +  set a [lindex $argv $i]
           39  +  switch -- $x {
           40  +     afl-bin {set AFLBIN $a}
           41  +     afl-data {set AFLDATA $a}
           42  +     fuzzershell {set FUZZERSHELL $a}
           43  +     o {set OUTFILE $a}
           44  +     default {cmdlineerr "unknown option: --$x"}
           45  +  }
           46  +}
           47  +proc checkarg {varname option} {
           48  +  set val [set ::$varname]
           49  +  if {$val==""} {cmdlineerr "required option missing: --$option"}
           50  +}
           51  +checkarg AFLBIN afl-bin
           52  +checkarg AFLDATA afl-data
           53  +checkarg FUZZERSHELL fuzzershell
           54  +checkarg OUTFILE o
           55  +proc checkexec {x} {
           56  +  if {![file exec $x]} {cmdlineerr "cannot find $x"}
           57  +}
           58  +checkexec $AFLBIN/afl-cmin
           59  +checkexec $AFLBIN/afl-tmin
           60  +checkexec $FUZZERSHELL
           61  +proc checkdir {x} {
           62  +  if {![file isdir $x]} {cmdlineerr "no such directory: $x"}
           63  +}
           64  +checkdir $AFLDATA/queue
           65  +
           66  +proc progress {msg} {
           67  +  puts "******** $msg"
           68  +  flush stdout
           69  +}
           70  +progress "mkdir tmp1 tmp2"
           71  +file mkdir tmp1 tmp2
           72  +progress "copying test cases from $AFLDATA into tmp1..."
           73  +set n 0
           74  +foreach file [glob -nocomplain $AFLDATA/queue/id:*] {
           75  +  incr n
           76  +  file copy $file tmp1/$n
           77  +}
           78  +foreach file [glob -nocomplain $AFLDATA/crash*/id:*] {
           79  +  incr n
           80  +  file copy $file tmp1/$n
           81  +}
           82  +progress "total $n files copied."
           83  +progress "running: $AFLBIN/afl-cmin -i tmp1 -o tmp2 $FUZZERSHELL"
           84  +exec $AFLBIN/afl-cmin -i tmp1 -o tmp2 $FUZZERSHELL >&@ stdout
           85  +progress "afl-cmin complete."
           86  +#
           87  +# Experiments show that running afl-tmin is too slow for this application.
           88  +# And it doesn't really make the test cases that much smaller.  So let's
           89  +# just skip it.
           90  +#
           91  +# foreach file [glob tmp2/*] {
           92  +#   progress "$AFLBIN/afl-tmin -i $file -o tmp3/[file tail $file] $FUZZERSHELL"
           93  +#   exec $AFLBIN/afl-tmin -i $file -o tmp3/[file tail $file] \
           94  +#       $FUZZERSHELL >&@ stdout
           95  +# }
           96  +progress "generating final output into $OUTFILE"
           97  +set out [open $OUTFILE wb]
           98  +puts $out "# Test data for use with fuzzershell.  Automatically
           99  +# generated using $argv0.  This file contains binary data
          100  +#"
          101  +set n 0
          102  +foreach file [glob tmp2/*] {
          103  +  incr n
          104  +  puts -nonewline $out "/****<$n>****/"
          105  +  set in [open $file rb]
          106  +  puts -nonewline $out [read $in]
          107  +  close $in
          108  +}
          109  +close $out
          110  +progress "done.  $n test cases written to $OUTFILE"
          111  +progress "clean-up..."
          112  +file delete -force tmp1
          113  +progress "culled test cases left in the tmp2 directory"

Changes to test/pagesize.test.

   211    211         CREATE TABLE t1(x);
   212    212         CREATE TEMP TABLE t2(y);
   213    213         PRAGMA main.page_size;
   214    214         PRAGMA temp.page_size;
   215    215       "
   216    216     } [list $PGSZ $PGSZ]
   217    217   }
          218  +
          219  +reset_db
          220  +do_execsql_test pagesize-3.1 {
          221  +  BEGIN;
          222  +  SELECT * FROM sqlite_master;
          223  +  PRAGMA page_size=2048;
          224  +  PRAGMA main.page_size;
          225  +} {1024}
          226  +do_execsql_test pagesize-3.2 {
          227  +  CREATE TABLE t1(x);
          228  +  COMMIT;
          229  +}
          230  +do_execsql_test pagesize-3.3 {
          231  +  BEGIN;
          232  +    PRAGMA page_size = 2048;
          233  +  COMMIT;
          234  +  PRAGMA main.page_size;
          235  +} {1024}
   218    236   
   219    237   finish_test

Changes to test/releasetest.tcl.

    14     14       --config   CONFIGNAME              (Run only CONFIGNAME)
    15     15       --quick                            (Run "veryquick.test" only)
    16     16       --veryquick                        (Run "make smoketest" only)
    17     17       --msvc                             (Use MSVC as the compiler)
    18     18       --buildonly                        (Just build testfixture - do not run)
    19     19       --dryrun                           (Print what would have happened)
    20     20       --info                             (Show diagnostic info)
           21  +    --with-tcl=DIR                     (Use TCL build at DIR)
    21     22   
    22     23   The default value for --srcdir is the parent of the directory holding
    23     24   this script.
    24     25   
    25     26   The script determines the default value for --platform using the
    26     27   $tcl_platform(os) and $tcl_platform(machine) variables.  Supported
    27     28   platforms are "Linux-x86", "Linux-x86_64", "Darwin-i386",
................................................................................
   103    104       -DSQLITE_MUTEX_NOOP=1
   104    105       -DSQLITE_TCL_DEFAULT_FULLMUTEX=1
   105    106       -DSQLITE_ENABLE_FTS3=1
   106    107       -DSQLITE_ENABLE_RTREE=1
   107    108       -DSQLITE_ENABLE_MEMSYS5=1
   108    109       -DSQLITE_ENABLE_MEMSYS3=1
   109    110       -DSQLITE_ENABLE_COLUMN_METADATA=1
          111  +    -DSQLITE_ENABLE_STAT4
          112  +    -DSQLITE_MAX_ATTACHED=125
          113  +  }
          114  +  "Fast-One" {
          115  +    -O6
          116  +    -DSQLITE_ENABLE_FTS4=1
          117  +    -DSQLITE_ENABLE_RTREE=1
   110    118       -DSQLITE_ENABLE_STAT4
   111    119       -DSQLITE_MAX_ATTACHED=125
   112    120     }
   113    121     "Device-One" {
   114    122       -O2
   115    123       -DSQLITE_DEBUG=1
   116    124       -DSQLITE_DEFAULT_AUTOVACUUM=1
................................................................................
   195    203     # different names for them all so that they results appear in separate
   196    204     # subdirectories.
   197    205     #
   198    206     Fail0 {-O0}
   199    207     Fail2 {-O0}
   200    208     Fail3 {-O0}
   201    209     Fail4 {-O0}
          210  +  FuzzFail1 {-O0}
          211  +  FuzzFail2 {-O0}
   202    212   }]
   203    213   
   204    214   array set ::Platforms [strip_comments {
   205    215     Linux-x86_64 {
   206    216       "Check-Symbols"           checksymbols
   207    217       "Debug-One"               "mptest test"
   208    218       "Have-Not"                test
................................................................................
   210    220       "Unlock-Notify"           "QUICKTEST_INCLUDE=notify2.test test"
   211    221       "Update-Delete-Limit"     test
   212    222       "Extra-Robustness"        test
   213    223       "Device-Two"              test
   214    224       "No-lookaside"            test
   215    225       "Devkit"                  test
   216    226       "Sanitize"                {QUICKTEST_OMIT=func4.test,nan.test test}
          227  +    "Fast-One"                fuzzoomtest
   217    228       "Valgrind"                valgrindtest
   218    229       "Default"                 "threadtest fulltest"
   219    230       "Device-One"              fulltest
   220    231     }
   221    232     Linux-i686 {
   222    233       "Devkit"                  test
   223    234       "Have-Not"                test
................................................................................
   251    262     #
   252    263     Failure-Detection {
   253    264       Fail0     "TEST_FAILURE=0 test"
   254    265       Sanitize  "TEST_FAILURE=1 test"
   255    266       Fail2     "TEST_FAILURE=2 valgrindtest"
   256    267       Fail3     "TEST_FAILURE=3 valgrindtest"
   257    268       Fail4     "TEST_FAILURE=4 test"
          269  +    FuzzFail1 "TEST_FAILURE=5 test"
          270  +    FuzzFail2 "TEST_FAILURE=5 valgrindtest"
   258    271     }
   259    272   }]
   260    273   
   261    274   
   262    275   # End of configuration section.
   263    276   #########################################################################
   264    277   #########################################################################
................................................................................
   347    360     # OPTS Makefile variable. Variable $cflags holds the value for
   348    361     # CFLAGS. The makefile will pass OPTS to both gcc and lemon, but
   349    362     # CFLAGS is only passed to gcc.
   350    363     #
   351    364     set cflags [expr {$::MSVC ? "-Zi" : "-g"}]
   352    365     set opts ""
   353    366     set title ${name}($testtarget)
   354         -  set configOpts ""
          367  +  set configOpts $::WITHTCL
   355    368   
   356    369     regsub -all {#[^\n]*\n} $config \n config
   357    370     foreach arg $config {
   358    371       if {[regexp {^-[UD]} $arg]} {
   359    372         lappend opts $arg
   360    373       } elseif {[regexp {^[A-Z]+=} $arg]} {
   361    374         lappend testtarget $arg
................................................................................
   478    491     set ::SRCDIR    [file normalize [file dirname [file dirname $::argv0]]]
   479    492     set ::QUICK     0
   480    493     set ::MSVC      0
   481    494     set ::BUILDONLY 0
   482    495     set ::DRYRUN    0
   483    496     set ::EXEC      exec
   484    497     set ::TRACE     0
          498  +  set ::WITHTCL   {}
   485    499     set config {}
   486    500     set platform $::tcl_platform(os)-$::tcl_platform(machine)
   487    501   
   488    502     for {set i 0} {$i < [llength $argv]} {incr i} {
   489    503       set x [lindex $argv $i]
   490    504       if {[regexp {^--[a-z]} $x]} {set x [string range $x 1 end]}
   491    505       switch -glob -- $x {
................................................................................
   551    565         -g {
   552    566           if {$::MSVC} {
   553    567             lappend ::EXTRACONFIG -Zi
   554    568           } else {
   555    569             lappend ::EXTRACONFIG [lindex $argv $i]
   556    570           }
   557    571         }
          572  +
          573  +      -with-tcl=* {
          574  +        set ::WITHTCL -$x
          575  +      }
   558    576   
   559    577         -D* -
   560    578         -O* -
   561    579         -enable-* -
   562    580         -disable-* -
   563    581         *=* {
   564    582           lappend ::EXTRACONFIG [lindex $argv $i]
................................................................................
   639    657       incr NTEST
   640    658       run_test_suite $zConfig $target $config_options
   641    659   
   642    660       # If the configuration included the SQLITE_DEBUG option, then remove
   643    661       # it and run veryquick.test. If it did not include the SQLITE_DEBUG option
   644    662       # add it and run veryquick.test.
   645    663       if {$target!="checksymbols" && $target!="valgrindtest"
   646         -           && !$::BUILDONLY && $::QUICK<2} {
          664  +           && $target!="fuzzoomtest" && !$::BUILDONLY && $::QUICK<2} {
   647    665         set debug_idx [lsearch -glob $config_options -DSQLITE_DEBUG*]
   648    666         set xtarget $target
   649    667         regsub -all {fulltest[a-z]*} $xtarget test xtarget
          668  +      regsub -all {fuzzoomtest} $xtarget fuzztest xtarget
   650    669         if {$debug_idx < 0} {
   651    670           incr NTEST
   652    671           append config_options " -DSQLITE_DEBUG=1"
   653    672           run_test_suite "${zConfig}_debug" $xtarget $config_options
   654    673         } else {
   655    674           incr NTEST
   656    675           regsub { *-DSQLITE_MEMDEBUG[^ ]* *} $config_options { } config_options

Changes to test/select4.test.

   269    269     set v [catch {execsql {
   270    270       SELECT DISTINCT log FROM t1 ORDER BY log
   271    271       INTERSECT
   272    272       SELECT n FROM t1 WHERE log=3
   273    273       ORDER BY log;
   274    274     }} msg]
   275    275     lappend v $msg
          276  +} {1 {ORDER BY clause should come after INTERSECT not before}}
          277  +do_catchsql_test select4-4.4 {
          278  +  SELECT 3 IN (
          279  +    SELECT 0 ORDER BY 1
          280  +    INTERSECT
          281  +    SELECT 1
          282  +    INTERSECT 
          283  +    SELECT 2
          284  +    ORDER BY 1
          285  +  );
   276    286   } {1 {ORDER BY clause should come after INTERSECT not before}}
   277    287   
   278    288   # Various error messages while processing UNION or INTERSECT
   279    289   #
   280    290   do_test select4-5.1 {
   281    291     set v [catch {execsql {
   282    292       SELECT DISTINCT log FROM t2

Changes to test/shell1.test.

   734    734     db close
   735    735     forcedelete test.db
   736    736     sqlite3 db test.db
   737    737     db eval {
   738    738       PRAGMA encoding=UTF16;
   739    739       CREATE TABLE t1(x);
   740    740       INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
          741  +    CREATE TABLE t3(x,y);
          742  +    INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
          743  +                         (4,2.25), (5,'hello'), (6,x'807f');
   741    744     }
   742    745     catchcmd test.db {.dump}
   743    746   } {0 {PRAGMA foreign_keys=OFF;
   744    747   BEGIN TRANSACTION;
   745    748   CREATE TABLE t1(x);
   746    749   INSERT INTO "t1" VALUES(NULL);
   747    750   INSERT INTO "t1" VALUES('');
   748    751   INSERT INTO "t1" VALUES(1);
   749    752   INSERT INTO "t1" VALUES(2.25);
   750    753   INSERT INTO "t1" VALUES('hello');
   751    754   INSERT INTO "t1" VALUES(X'807F');
          755  +CREATE TABLE t3(x,y);
          756  +INSERT INTO "t3" VALUES(1,NULL);
          757  +INSERT INTO "t3" VALUES(2,'');
          758  +INSERT INTO "t3" VALUES(3,1);
          759  +INSERT INTO "t3" VALUES(4,2.25);
          760  +INSERT INTO "t3" VALUES(5,'hello');
          761  +INSERT INTO "t3" VALUES(6,X'807F');
   752    762   COMMIT;}}
   753    763   
   754    764   # Test the output of ".mode insert"
   755    765   #
   756         -do_test shell1-4.2 {
          766  +do_test shell1-4.2.1 {
   757    767     catchcmd test.db ".mode insert t1\nselect * from t1;"
   758    768   } {0 {INSERT INTO t1 VALUES(NULL);
   759    769   INSERT INTO t1 VALUES('');
   760    770   INSERT INTO t1 VALUES(1);
   761    771   INSERT INTO t1 VALUES(2.25);
   762    772   INSERT INTO t1 VALUES('hello');
   763    773   INSERT INTO t1 VALUES(X'807f');}}
          774  +
          775  +# Test the output of ".mode insert" with headers
          776  +#
          777  +do_test shell1-4.2.2 {
          778  +  catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
          779  +} {0 {INSERT INTO t1(x) VALUES(NULL);
          780  +INSERT INTO t1(x) VALUES('');
          781  +INSERT INTO t1(x) VALUES(1);
          782  +INSERT INTO t1(x) VALUES(2.25);
          783  +INSERT INTO t1(x) VALUES('hello');
          784  +INSERT INTO t1(x) VALUES(X'807f');}}
          785  +
          786  +# Test the output of ".mode insert"
          787  +#
          788  +do_test shell1-4.2.3 {
          789  +  catchcmd test.db ".mode insert t3\nselect * from t3;"
          790  +} {0 {INSERT INTO t3 VALUES(1,NULL);
          791  +INSERT INTO t3 VALUES(2,'');
          792  +INSERT INTO t3 VALUES(3,1);
          793  +INSERT INTO t3 VALUES(4,2.25);
          794  +INSERT INTO t3 VALUES(5,'hello');
          795  +INSERT INTO t3 VALUES(6,X'807f');}}
          796  +
          797  +# Test the output of ".mode insert" with headers
          798  +#
          799  +do_test shell1-4.2.4 {
          800  +  catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
          801  +} {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
          802  +INSERT INTO t3(x,y) VALUES(2,'');
          803  +INSERT INTO t3(x,y) VALUES(3,1);
          804  +INSERT INTO t3(x,y) VALUES(4,2.25);
          805  +INSERT INTO t3(x,y) VALUES(5,'hello');
          806  +INSERT INTO t3(x,y) VALUES(6,X'807f');}}
   764    807   
   765    808   # Test the output of ".mode tcl"
   766    809   #
   767    810   do_test shell1-4.3 {
   768    811     db close
   769    812     forcedelete test.db
   770    813     sqlite3 db test.db
................................................................................
   813    856   } {0 {"\""
   814    857   "["
   815    858   "]"
   816    859   "\\{"
   817    860   "\\}"
   818    861   ";"
   819    862   "$"} 7}
          863  +
          864  +# Test using arbitrary byte data with the shell via standard input/output.
          865  +#
          866  +do_test shell1-5.0 {
          867  +  #
          868  +  # NOTE: Skip NUL byte because it appears to be incompatible with command
          869  +  #       shell argument parsing.
          870  +  #
          871  +  for {set i 1} {$i < 256} {incr i} {
          872  +    #
          873  +    # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
          874  +    #       command channels opened for it as textual ones), the carriage
          875  +    #       return character (and on Windows, the end-of-file character)
          876  +    #       cannot be used here.
          877  +    #
          878  +    if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
          879  +      continue
          880  +    }
          881  +    set hex [format %02X $i]
          882  +    set char [subst \\x$hex]; set oldChar $char
          883  +    set escapes [list]
          884  +    if {$tcl_platform(platform)=="windows"} {
          885  +      #
          886  +      # NOTE: On Windows, we need to escape all the whitespace characters,
          887  +      #       the alarm (\a) character, and those with special meaning to
          888  +      #       the SQLite shell itself.
          889  +      #
          890  +      set escapes [list \
          891  +          \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
          892  +          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
          893  +    } else {
          894  +      #
          895  +      # NOTE: On Unix, we need to escape most of the whitespace characters
          896  +      #       and those with special meaning to the SQLite shell itself.
          897  +      #       The alarm (\a), backspace (\b), and carriage-return (\r)
          898  +      #       characters do not appear to require escaping on Unix.  For
          899  +      #       the alarm and backspace characters, this is probably due to
          900  +      #       differences in the command shell.  For the carriage-return,
          901  +      #       it is probably due to differences in how Tcl handles command
          902  +      #       channel end-of-line translations.
          903  +      #
          904  +      set escapes [list \
          905  +          \t \\t \n \\n \v \\v \f \\f \
          906  +          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
          907  +    }
          908  +    set char [string map $escapes $char]
          909  +    set x [catchcmdex test.db ".print $char\n"]
          910  +    set code [lindex $x 0]
          911  +    set res [lindex $x 1]
          912  +    if {$code ne "0"} {
          913  +      error "failed with error: $res"
          914  +    }
          915  +    if {$res ne "$oldChar\n"} {
          916  +      error "failed with byte $hex mismatch"
          917  +    }
          918  +  }
          919  +} {}
   820    920   
   821    921   finish_test

Added test/statfault.test.

            1  +# 2015 April 28
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +source $testdir/malloc_common.tcl
           16  +set testprefix statfault
           17  +
           18  +ifcapable !vtab||!compound {
           19  +  finish_test
           20  +  return
           21  +}
           22  +
           23  +register_dbstat_vtab db
           24  +do_execsql_test statfault-1 {
           25  +  CREATE TABLE t1(a, b UNIQUE);
           26  +  INSERT INTO t1 VALUES(1, randomblob(500));
           27  +  INSERT INTO t1 VALUES(randomblob(500), 1);
           28  +  INSERT INTO t1 VALUES(2, randomblob(250));
           29  +  INSERT INTO t1 VALUES(randomblob(250), 2);
           30  +  CREATE VIRTUAL TABLE sss USING dbstat;
           31  +} {}
           32  +faultsim_save_and_close
           33  +
           34  +do_faultsim_test 1 -faults * -prep {
           35  +  faultsim_restore_and_reopen
           36  +  register_dbstat_vtab db
           37  +  execsql { SELECT 1 FROM sqlite_master LIMIT 1 }
           38  +} -body {
           39  +  execsql { SELECT count(*) FROM sss }
           40  +} -test {
           41  +  faultsim_test_result {0 8} 
           42  +}
           43  +
           44  +
           45  +finish_test

Changes to test/tester.tcl.

   661    661       }
   662    662     } else {
   663    663       puts " Omitted"
   664    664       omit_test $name "pattern mismatch" 0
   665    665     }
   666    666     flush stdout
   667    667   }
          668  +
          669  +proc dumpbytes {s} {
          670  +  set r ""
          671  +  for {set i 0} {$i < [string length $s]} {incr i} {
          672  +    if {$i > 0} {append r " "}
          673  +    append r [format %02X [scan [string index $s $i] %c]]
          674  +  }
          675  +  return $r
          676  +}
   668    677   
   669    678   proc catchcmd {db {cmd ""}} {
   670    679     global CLI
   671    680     set out [open cmds.txt w]
   672    681     puts $out $cmd
   673    682     close $out
   674    683     set line "exec $CLI $db < cmds.txt"
   675    684     set rc [catch { eval $line } msg]
   676    685     list $rc $msg
   677    686   }
          687  +
          688  +proc catchcmdex {db {cmd ""}} {
          689  +  global CLI
          690  +  set out [open cmds.txt w]
          691  +  fconfigure $out -encoding binary -translation binary
          692  +  puts -nonewline $out $cmd
          693  +  close $out
          694  +  set line "exec -keepnewline -- $CLI $db < cmds.txt"
          695  +  set chans [list stdin stdout stderr]
          696  +  foreach chan $chans {
          697  +    catch {
          698  +      set modes($chan) [fconfigure $chan]
          699  +      fconfigure $chan -encoding binary -translation binary -buffering none
          700  +    }
          701  +  }
          702  +  set rc [catch { eval $line } msg]
          703  +  foreach chan $chans {
          704  +    catch {
          705  +      eval fconfigure [list $chan] $modes($chan)
          706  +    }
          707  +  }
          708  +  # puts [dumpbytes $msg]
          709  +  list $rc $msg
          710  +}
   678    711   
   679    712   proc filepath_normalize {p} {
   680    713     # test cases should be written to assume "unix"-like file paths
   681    714     if {$::tcl_platform(platform)!="unix"} {
   682    715       # lreverse*2 as a hack to remove any unneeded {} after the string map
   683    716       lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
   684    717     } {

Changes to tool/fuzzershell.c.

    28     28   **    (3)  The main in-memory database can be initialized from a template
    29     29   **         disk database so that the fuzzer starts with a database containing
    30     30   **         content.
    31     31   **
    32     32   **    (4)  The eval() SQL function is added, allowing the fuzzer to do 
    33     33   **         interesting recursive operations.
    34     34   **
    35         -** 2015-04-20: The input text can be divided into separate SQL chunks using
    36         -** lines of the form:
           35  +**    (5)  An error is raised if there is a memory leak.
           36  +**
           37  +** The input text can be divided into separate test cases using comments
           38  +** of the form:
    37     39   **
    38     40   **       |****<...>****|
    39     41   **
    40         -** where the "..." is arbitrary text, except the "|" should really be "/".
    41         -** ("|" is used here to avoid compiler warnings about nested comments.)
    42         -** Each such SQL comment is printed as it is encountered.  A separate 
    43         -** in-memory SQLite database is created to run each chunk of SQL.  This
    44         -** feature allows the "queue" of AFL to be captured into a single big
           42  +** where the "..." is arbitrary text. (Except the "|" should really be "/".
           43  +** "|" is used here to avoid compiler errors about nested comments.)
           44  +** A separate in-memory SQLite database is created to run each test case.
           45  +** This feature allows the "queue" of AFL to be captured into a single big
    45     46   ** file using a command like this:
    46     47   **
    47     48   **    (for i in id:*; do echo '|****<'$i'>****|'; cat $i; done) >~/all-queue.txt
    48     49   **
    49     50   ** (Once again, change the "|" to "/") Then all elements of the AFL queue
    50     51   ** can be run in a single go (for regression testing, for example) by typing:
    51     52   **
    52         -**    fuzzershell -f ~/all-queue.txt >out.txt
           53  +**    fuzzershell -f ~/all-queue.txt
    53     54   **
    54     55   ** After running each chunk of SQL, the database connection is closed.  The
    55     56   ** program aborts if the close fails or if there is any unfreed memory after
    56     57   ** the close.
           58  +**
           59  +** New test cases can be appended to all-queue.txt at any time.  If redundant
           60  +** test cases are added, they can be eliminated by running:
           61  +**
           62  +**    fuzzershell -f ~/all-queue.txt --unique-cases ~/unique-cases.txt
    57     63   */
    58     64   #include <stdio.h>
    59     65   #include <stdlib.h>
    60     66   #include <string.h>
    61     67   #include <stdarg.h>
    62     68   #include <ctype.h>
    63     69   #include "sqlite3.h"
    64     70   
    65     71   /*
    66     72   ** All global variables are gathered into the "g" singleton.
    67     73   */
    68     74   struct GlobalVars {
    69     75     const char *zArgv0;         /* Name of program */
           76  +  sqlite3_mem_methods sOrigMem;    /* Original memory methods */
           77  +  sqlite3_mem_methods sOomMem;     /* Memory methods with OOM simulator */
           78  +  int iOomCntdown;                 /* Memory fails on 1 to 0 transition */
           79  +  int nOomFault;                   /* Increments for each OOM fault */
           80  +  int bOomOnce;                    /* Fail just once if true */
           81  +  int bOomEnable;                  /* True to enable OOM simulation */
           82  +  int nOomBrkpt;                   /* Number of calls to oomFault() */
           83  +  char zTestName[100];             /* Name of current test */
    70     84   } g;
    71     85   
           86  +/*
           87  +** Maximum number of iterations for an OOM test
           88  +*/
           89  +#ifndef OOM_MAX
           90  +# define OOM_MAX 625
           91  +#endif
    72     92   
           93  +/*
           94  +** This routine is called when a simulated OOM occurs.  It exists as a
           95  +** convenient place to set a debugger breakpoint.
           96  +*/
           97  +static void oomFault(void){
           98  +  g.nOomBrkpt++; /* Prevent oomFault() from being optimized out */
           99  +}
          100  +
          101  +
          102  +/* Versions of malloc() and realloc() that simulate OOM conditions */
          103  +static void *oomMalloc(int nByte){
          104  +  if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
          105  +    g.iOomCntdown--;
          106  +    if( g.iOomCntdown==0 ){
          107  +      if( g.nOomFault==0 ) oomFault();
          108  +      g.nOomFault++;
          109  +      if( !g.bOomOnce ) g.iOomCntdown = 1;
          110  +      return 0;
          111  +    }
          112  +  }
          113  +  return g.sOrigMem.xMalloc(nByte);
          114  +}
          115  +static void *oomRealloc(void *pOld, int nByte){
          116  +  if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
          117  +    g.iOomCntdown--;
          118  +    if( g.iOomCntdown==0 ){
          119  +      if( g.nOomFault==0 ) oomFault();
          120  +      g.nOomFault++;
          121  +      if( !g.bOomOnce ) g.iOomCntdown = 1;
          122  +      return 0;
          123  +    }
          124  +  }
          125  +  return g.sOrigMem.xRealloc(pOld, nByte);
          126  +}
    73    127   
    74    128   /*
    75    129   ** Print an error message and abort in such a way to indicate to the
    76    130   ** fuzzer that this counts as a crash.
    77    131   */
    78    132   static void abendError(const char *zFormat, ...){
    79    133     va_list ap;
          134  +  if( g.zTestName[0] ){
          135  +    fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
          136  +  }else{
    80    137     fprintf(stderr, "%s: ", g.zArgv0);
          138  +  }
    81    139     va_start(ap, zFormat);
    82    140     vfprintf(stderr, zFormat, ap);
    83    141     va_end(ap);
    84    142     fprintf(stderr, "\n");
    85    143     abort();
    86    144   }
    87    145   /*
    88    146   ** Print an error message and quit, but not in a way that would look
    89    147   ** like a crash.
    90    148   */
    91    149   static void fatalError(const char *zFormat, ...){
    92    150     va_list ap;
          151  +  if( g.zTestName[0] ){
          152  +    fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
          153  +  }else{
    93    154     fprintf(stderr, "%s: ", g.zArgv0);
          155  +  }
    94    156     va_start(ap, zFormat);
    95    157     vfprintf(stderr, zFormat, ap);
    96    158     va_end(ap);
    97    159     fprintf(stderr, "\n");
    98    160     exit(1);
    99    161   }
   100    162   
................................................................................
   115    177   }
   116    178   
   117    179   /*
   118    180   ** This callback is invoked by sqlite3_log().
   119    181   */
   120    182   static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){
   121    183     printf("LOG: (%d) %s\n", iErrCode, zMsg);
          184  +  fflush(stdout);
          185  +}
          186  +static void shellLogNoop(void *pNotUsed, int iErrCode, const char *zMsg){
          187  +  return;
   122    188   }
   123    189   
   124    190   /*
   125    191   ** This callback is invoked by sqlite3_exec() to return query results.
   126    192   */
   127    193   static int execCallback(void *NotUsed, int argc, char **argv, char **colv){
   128    194     int i;
................................................................................
   132    198       printf(" %s=", colv[i]);
   133    199       if( argv[i] ){
   134    200         printf("[%s]\n", argv[i]);
   135    201       }else{
   136    202         printf("NULL\n");
   137    203       }
   138    204     }
          205  +  fflush(stdout);
          206  +  return 0;
          207  +}
          208  +static int execNoop(void *NotUsed, int argc, char **argv, char **colv){
   139    209     return 0;
   140    210   }
   141    211   
          212  +#ifndef SQLITE_OMIT_TRACE
   142    213   /*
   143    214   ** This callback is invoked by sqlite3_trace() as each SQL statement
   144    215   ** starts.
   145    216   */
   146    217   static void traceCallback(void *NotUsed, const char *zMsg){
   147    218     printf("TRACE: %s\n", zMsg);
          219  +  fflush(stdout);
   148    220   }
          221  +static void traceNoop(void *NotUsed, const char *zMsg){
          222  +  return;
          223  +}
          224  +#endif
   149    225   
   150    226   /***************************************************************************
   151    227   ** eval() implementation copied from ../ext/misc/eval.c
   152    228   */
   153    229   /*
   154    230   ** Structure used to accumulate the output
   155    231   */
................................................................................
   235    311   /* End of the eval() implementation
   236    312   ******************************************************************************/
   237    313   
   238    314   /*
   239    315   ** Print sketchy documentation for this utility program
   240    316   */
   241    317   static void showHelp(void){
   242         -  printf("Usage: %s [options]\n", g.zArgv0);
          318  +  printf("Usage: %s [options] ?FILE...?\n", g.zArgv0);
   243    319     printf(
   244         -"Read SQL text from standard input and evaluate it.\n"
          320  +"Read SQL text from FILE... (or from standard input if FILE... is omitted)\n"
          321  +"and then evaluate each block of SQL contained therein.\n"
   245    322   "Options:\n"
   246    323   "  --autovacuum        Enable AUTOVACUUM mode\n"
   247         -"  -f FILE             Read SQL text from FILE instead of standard input\n"
   248    324   "  --heap SZ MIN       Memory allocator uses SZ bytes & min allocation MIN\n"
   249    325   "  --help              Show this help text\n"    
   250         -"  --initdb DBFILE     Initialize the in-memory database using template DBFILE\n"
   251    326   "  --lookaside N SZ    Configure lookaside for N slots of SZ bytes each\n"
          327  +"  --oom                 Run each test multiple times in a simulated OOM loop\n"
   252    328   "  --pagesize N        Set the page size to N\n"
   253    329   "  --pcache N SZ       Configure N pages of pagecache each of size SZ bytes\n"
          330  +"  -q                    Reduced output\n"
          331  +"  --quiet               Reduced output\n"
   254    332   "  --scratch N SZ      Configure scratch memory for N slots of SZ bytes each\n"
          333  +"  --unique-cases FILE   Write all unique test cases to FILE\n"
   255    334   "  --utf16be           Set text encoding to UTF-16BE\n"
   256    335   "  --utf16le           Set text encoding to UTF-16LE\n"
          336  +"  -v                    Increased output\n"
          337  +"  --verbose             Increased output\n"
   257    338     );
   258    339   }
   259    340   
   260    341   /*
   261    342   ** Return the value of a hexadecimal digit.  Return -1 if the input
   262    343   ** is not a hex digit.
   263    344   */
................................................................................
   311    392         break;
   312    393       }
   313    394     }
   314    395     if( v>0x7fffffff ) abendError("parameter too large - max 2147483648");
   315    396     return (int)(isNeg? -v : v);
   316    397   }
   317    398   
   318         -/*
   319         -** Various operating modes
   320         -*/
   321         -#define FZMODE_Generic   1
   322         -#define FZMODE_Strftime  2
   323         -#define FZMODE_Printf    3
   324         -#define FZMODE_Glob      4
   325         -
          399  +/* Return the current wall-clock time */
          400  +static sqlite3_int64 timeOfDay(void){
          401  +  static sqlite3_vfs *clockVfs = 0;
          402  +  sqlite3_int64 t;
          403  +  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
          404  +  if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
          405  +    clockVfs->xCurrentTimeInt64(clockVfs, &t);
          406  +  }else{
          407  +    double r;
          408  +    clockVfs->xCurrentTime(clockVfs, &r);
          409  +    t = (sqlite3_int64)(r*86400000.0);
          410  +  }
          411  +  return t;
          412  +}
   326    413   
   327    414   int main(int argc, char **argv){
   328    415     char *zIn = 0;          /* Input text */
   329    416     int nAlloc = 0;         /* Number of bytes allocated for zIn[] */
   330    417     int nIn = 0;            /* Number of bytes of zIn[] used */
   331    418     size_t got;             /* Bytes read from input */
   332         -  FILE *in = stdin;       /* Where to read SQL text from */
   333    419     int rc = SQLITE_OK;     /* Result codes from API functions */
   334    420     int i;                  /* Loop counter */
   335    421     int iNext;              /* Next block of SQL */
   336    422     sqlite3 *db;            /* Open database */
   337         -  sqlite3 *dbInit = 0;    /* On-disk database used to initialize the in-memory db */
   338         -  const char *zInitDb = 0;/* Name of the initialization database file */
   339    423     char *zErrMsg = 0;      /* Error message returned from sqlite3_exec() */
   340    424     const char *zEncoding = 0;    /* --utf16be or --utf16le */
   341    425     int nHeap = 0, mnHeap = 0;    /* Heap size from --heap */
   342    426     int nLook = 0, szLook = 0;    /* --lookaside configuration */
   343    427     int nPCache = 0, szPCache = 0;/* --pcache configuration */
   344    428     int nScratch = 0, szScratch=0;/* --scratch configuration */
   345    429     int pageSize = 0;             /* Desired page size.  0 means default */
................................................................................
   346    430     void *pHeap = 0;              /* Allocated heap space */
   347    431     void *pLook = 0;              /* Allocated lookaside space */
   348    432     void *pPCache = 0;            /* Allocated storage for pcache */
   349    433     void *pScratch = 0;           /* Allocated storage for scratch */
   350    434     int doAutovac = 0;            /* True for --autovacuum */
   351    435     char *zSql;                   /* SQL to run */
   352    436     char *zToFree = 0;            /* Call sqlite3_free() on this afte running zSql */
   353         -  int iMode = FZMODE_Generic;   /* Operating mode */
   354         -  const char *zCkGlob = 0;      /* Inputs must match this glob */
          437  +  int verboseFlag = 0;          /* --verbose or -v flag */
          438  +  int quietFlag = 0;            /* --quiet or -q flag */
          439  +  int nTest = 0;                /* Number of test cases run */
          440  +  int multiTest = 0;            /* True if there will be multiple test cases */
          441  +  int lastPct = -1;             /* Previous percentage done output */
          442  +  sqlite3 *dataDb = 0;          /* Database holding compacted input data */
          443  +  sqlite3_stmt *pStmt = 0;      /* Statement to insert testcase into dataDb */
          444  +  const char *zDataOut = 0;     /* Write compacted data to this output file */
          445  +  int nHeader = 0;              /* Bytes of header comment text on input file */
          446  +  int oomFlag = 0;              /* --oom */
          447  +  int oomCnt = 0;               /* Counter for the OOM loop */
          448  +  char zErrBuf[200];            /* Space for the error message */
          449  +  const char *zFailCode;        /* Value of the TEST_FAILURE environment var */
          450  +  const char *zPrompt;          /* Initial prompt when large-file fuzzing */
          451  +  int nInFile = 0;              /* Number of input files to read */
          452  +  char **azInFile = 0;          /* Array of input file names */
          453  +  int jj;                       /* Loop counter for azInFile[] */
          454  +  sqlite3_int64 iBegin;         /* Start time for the whole program */
          455  +  sqlite3_int64 iStart, iEnd;   /* Start and end-times for a test case */
   355    456   
   356         -
          457  +  iBegin = timeOfDay();
          458  +  zFailCode = getenv("TEST_FAILURE");
   357    459     g.zArgv0 = argv[0];
          460  +  zPrompt = "<stdin>";
   358    461     for(i=1; i<argc; i++){
   359    462       const char *z = argv[i];
   360    463       if( z[0]=='-' ){
   361    464         z++;
   362    465         if( z[0]=='-' ) z++;
   363    466         if( strcmp(z,"autovacuum")==0 ){
   364    467           doAutovac = 1;
   365    468         }else
   366    469         if( strcmp(z, "f")==0 && i+1<argc ){
   367         -        if( in!=stdin ) abendError("only one -f allowed");
   368         -        in = fopen(argv[++i],"rb");
   369         -        if( in==0 )  abendError("cannot open input file \"%s\"", argv[i]);
          470  +        i++;
          471  +        goto addNewInFile;
   370    472         }else
   371    473         if( strcmp(z,"heap")==0 ){
   372    474           if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]);
   373    475           nHeap = integerValue(argv[i+1]);
   374    476           mnHeap = integerValue(argv[i+2]);
   375    477           i += 2;
   376    478         }else
   377    479         if( strcmp(z,"help")==0 ){
   378    480           showHelp();
   379    481           return 0;
   380    482         }else
   381         -      if( strcmp(z, "initdb")==0 && i+1<argc ){
   382         -        if( zInitDb!=0 ) abendError("only one --initdb allowed");
   383         -        zInitDb = argv[++i];
   384         -      }else
   385    483         if( strcmp(z,"lookaside")==0 ){
   386    484           if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
   387    485           nLook = integerValue(argv[i+1]);
   388    486           szLook = integerValue(argv[i+2]);
   389    487           i += 2;
   390    488         }else
   391         -      if( strcmp(z,"mode")==0 ){
   392         -        if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
   393         -        z = argv[++i];
   394         -        if( strcmp(z,"generic")==0 ){
   395         -          iMode = FZMODE_Printf;
   396         -          zCkGlob = 0;
   397         -        }else if( strcmp(z, "glob")==0 ){
   398         -          iMode = FZMODE_Glob;
   399         -          zCkGlob = "'*','*'";
   400         -        }else if( strcmp(z, "printf")==0 ){
   401         -          iMode = FZMODE_Printf;
   402         -          zCkGlob = "'*',*";
   403         -        }else if( strcmp(z, "strftime")==0 ){
   404         -          iMode = FZMODE_Strftime;
   405         -          zCkGlob = "'*',*";
   406         -        }else{
   407         -          abendError("unknown --mode: %s", z);
   408         -        }
          489  +      if( strcmp(z,"oom")==0 ){
          490  +        oomFlag = 1;
   409    491         }else
   410    492         if( strcmp(z,"pagesize")==0 ){
   411    493           if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
   412    494           pageSize = integerValue(argv[++i]);
   413    495         }else
   414    496         if( strcmp(z,"pcache")==0 ){
   415    497           if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
   416    498           nPCache = integerValue(argv[i+1]);
   417    499           szPCache = integerValue(argv[i+2]);
   418    500           i += 2;
          501  +      }else
          502  +      if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
          503  +        quietFlag = 1;
          504  +        verboseFlag = 0;
   419    505         }else
   420    506         if( strcmp(z,"scratch")==0 ){
   421    507           if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
   422    508           nScratch = integerValue(argv[i+1]);
   423    509           szScratch = integerValue(argv[i+2]);
   424    510           i += 2;
          511  +      }else
          512  +      if( strcmp(z, "unique-cases")==0 ){
          513  +        if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]);
          514  +        if( zDataOut ) abendError("only one --minimize allowed");
          515  +        zDataOut = argv[++i];
   425    516         }else
   426    517         if( strcmp(z,"utf16le")==0 ){
   427    518           zEncoding = "utf16le";
   428    519         }else
   429    520         if( strcmp(z,"utf16be")==0 ){
   430    521           zEncoding = "utf16be";
   431    522         }else
          523  +      if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
          524  +        quietFlag = 0;
          525  +        verboseFlag = 1;
          526  +      }else
   432    527         {
   433    528           abendError("unknown option: %s", argv[i]);
   434    529         }
   435    530       }else{
   436         -      abendError("unknown argument: %s", argv[i]);
          531  +      addNewInFile:
          532  +      nInFile++;
          533  +      azInFile = realloc(azInFile, sizeof(azInFile[0])*nInFile);
          534  +      if( azInFile==0 ) abendError("out of memory");
          535  +      azInFile[nInFile-1] = argv[i];
   437    536       }
   438    537     }
   439         -  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, 0);
          538  +
          539  +  /* Do global SQLite initialization */
          540  +  sqlite3_config(SQLITE_CONFIG_LOG, verboseFlag ? shellLog : shellLogNoop, 0);
   440    541     if( nHeap>0 ){
   441    542       pHeap = malloc( nHeap );
   442    543       if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap);
   443    544       rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
   444    545       if( rc ) abendError("heap configuration failed: %d\n", rc);
   445    546     }
          547  +  if( oomFlag ){
          548  +    sqlite3_config(SQLITE_CONFIG_GETMALLOC, &g.sOrigMem);
          549  +    g.sOomMem = g.sOrigMem;
          550  +    g.sOomMem.xMalloc = oomMalloc;
          551  +    g.sOomMem.xRealloc = oomRealloc;
          552  +    sqlite3_config(SQLITE_CONFIG_MALLOC, &g.sOomMem);
          553  +  }
   446    554     if( nLook>0 ){
   447    555       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
   448    556       if( szLook>0 ){
   449    557         pLook = malloc( nLook*szLook );
   450    558         if( pLook==0 ) fatalError("out of memory");
   451    559       }
   452    560     }
................................................................................
   460    568     if( nPCache>0 && szPCache>0 ){
   461    569       pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
   462    570       if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache",
   463    571                                    nPCache*(sqlite3_int64)szPCache);
   464    572       rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
   465    573       if( rc ) abendError("pcache configuration failed: %d", rc);
   466    574     }
   467         -  while( !feof(in) ){
   468         -    nAlloc += nAlloc+1000;
   469         -    zIn = realloc(zIn, nAlloc);
          575  +
          576  +  /* If the --unique-cases option was supplied, open the database that will
          577  +  ** be used to gather unique test cases.
          578  +  */
          579  +  if( zDataOut ){
          580  +    rc = sqlite3_open(":memory:", &dataDb);
          581  +    if( rc ) abendError("cannot open :memory: database");
          582  +    rc = sqlite3_exec(dataDb,
          583  +          "CREATE TABLE testcase(sql BLOB PRIMARY KEY, tm) WITHOUT ROWID;",0,0,0);
          584  +    if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
          585  +    rc = sqlite3_prepare_v2(dataDb,
          586  +          "INSERT OR IGNORE INTO testcase(sql,tm)VALUES(?1,?2)",
          587  +          -1, &pStmt, 0);
          588  +    if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
          589  +  }
          590  +
          591  +  /* Initialize the input buffer used to hold SQL text */
          592  +  if( nInFile==0 ) nInFile = 1;
          593  +  nAlloc = 1000;
          594  +  zIn = malloc(nAlloc);
   470    595       if( zIn==0 ) fatalError("out of memory");
          596  +
          597  +  /* Loop over all input files */
          598  +  for(jj=0; jj<nInFile; jj++){
          599  +
          600  +    /* Read the complete content of the next input file into zIn[] */
          601  +    FILE *in;
          602  +    if( azInFile ){
          603  +      int j, k;
          604  +      in = fopen(azInFile[jj],"rb");
          605  +      if( in==0 ){
          606  +        abendError("cannot open %s for reading", azInFile[jj]);
          607  +      }
          608  +      zPrompt = azInFile[jj];
          609  +      for(j=k=0; zPrompt[j]; j++) if( zPrompt[j]=='/' ) k = j+1;
          610  +      zPrompt += k;
          611  +    }else{
          612  +      in = stdin;
          613  +      zPrompt = "<stdin>";
          614  +    }
          615  +    while( !feof(in) ){
   471    616       got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); 
   472    617       nIn += (int)got;
   473    618       zIn[nIn] = 0;
   474    619       if( got==0 ) break;
   475         -  }
   476         -  if( zInitDb ){
   477         -    rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0);
   478         -    if( rc!=SQLITE_OK ){
   479         -      abendError("unable to open initialization database \"%s\"", zInitDb);
          620  +      if( nAlloc - nIn - 1 < 100 ){
          621  +        nAlloc += nAlloc+1000;
          622  +        zIn = realloc(zIn, nAlloc);
          623  +        if( zIn==0 ) fatalError("out of memory");
   480    624       }
   481    625     }
   482         -  for(i=0; i<nIn; i=iNext){
          626  +    if( in!=stdin ) fclose(in);
          627  +    lastPct = -1;
          628  +
          629  +    /* Skip initial lines of the input file that begin with "#" */
          630  +    for(i=0; i<nIn; i=iNext+1){
          631  +      if( zIn[i]!='#' ) break;
          632  +      for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){}
          633  +    }
          634  +    nHeader = i;
          635  +
          636  +    /* Process all test cases contained within the input file.
          637  +    */
          638  +    for(; i<nIn; i=iNext, nTest++, g.zTestName[0]=0){
   483    639       char cSaved;
   484    640       if( strncmp(&zIn[i], "/****<",6)==0 ){
   485    641         char *z = strstr(&zIn[i], ">****/");
   486    642         if( z ){
   487    643           z += 6;
          644  +          sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "%.*s", 
          645  +                           (int)(z-&zIn[i]) - 12, &zIn[i+6]);
          646  +          if( verboseFlag ){
   488    647           printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
          648  +            fflush(stdout);
          649  +          }
   489    650           i += (int)(z-&zIn[i]);
          651  +          multiTest = 1;
   490    652         }
   491    653       }
   492    654       for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
   493    655       cSaved = zIn[iNext];
   494    656       zIn[iNext] = 0;
   495         -    if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
   496         -      zIn[iNext] = cSaved;
   497         -      continue;
          657  +
          658  +
          659  +      /* Print out the SQL of the next test case is --verbose is enabled
          660  +      */
          661  +      zSql = &zIn[i];
          662  +      if( verboseFlag ){
          663  +        printf("INPUT (offset: %d, size: %d): [%s]\n",
          664  +                i, (int)strlen(&zIn[i]), &zIn[i]);
          665  +      }else if( multiTest && !quietFlag ){
          666  +        if( oomFlag ){
          667  +          printf("%s\n", g.zTestName);
          668  +        }else{
          669  +          int pct = (10*iNext)/nIn;
          670  +          if( pct!=lastPct ){
          671  +            if( lastPct<0 ) printf("%s:", zPrompt);
          672  +            printf(" %d%%", pct*10);
          673  +            lastPct = pct;
   498    674       }
          675  +        }
          676  +      }else if( nInFile>1 ){
          677  +        printf("%s\n", zPrompt);
          678  +      }
          679  +      fflush(stdout);
          680  +
          681  +      /* Run the next test case.  Run it multiple times in --oom mode
          682  +      */
          683  +      if( oomFlag ){
          684  +        oomCnt = g.iOomCntdown = 1;
          685  +        g.nOomFault = 0;
          686  +        g.bOomOnce = 1;
          687  +        if( verboseFlag ){
          688  +          printf("Once.%d\n", oomCnt);
          689  +          fflush(stdout);
          690  +        }
          691  +      }else{
          692  +        oomCnt = 0;
          693  +      }
          694  +      do{
   499    695       rc = sqlite3_open_v2(
   500    696         "main.db", &db,
   501    697         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
   502    698         0);
   503    699       if( rc!=SQLITE_OK ){
   504    700         abendError("Unable to open the in-memory database");
   505    701       }
   506    702       if( pLook ){
   507    703         rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
   508    704         if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
   509    705       }
   510         -    if( zInitDb ){
   511         -      sqlite3_backup *pBackup;
   512         -      pBackup = sqlite3_backup_init(db, "main", dbInit, "main");
   513         -      rc = sqlite3_backup_step(pBackup, -1);
   514         -      if( rc!=SQLITE_DONE ){
   515         -        abendError("attempt to initialize the in-memory database failed (rc=%d)",
   516         -                   rc);
   517         -      }
   518         -      sqlite3_backup_finish(pBackup);
   519         -    }
   520         -    sqlite3_trace(db, traceCallback, 0);
          706  +    #ifndef SQLITE_OMIT_TRACE
          707  +        sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0);
          708  +    #endif
   521    709       sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
   522    710       sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
   523    711       sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
   524    712       if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
   525    713       if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
   526    714       if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
   527         -    printf("INPUT (offset: %d, size: %d): [%s]\n",
   528         -            i, (int)strlen(&zIn[i]), &zIn[i]);
   529         -    zSql = &zIn[i];
   530         -    switch( iMode ){
   531         -      case FZMODE_Glob:
   532         -        zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql);
   533         -        break;
   534         -      case FZMODE_Printf:
   535         -        zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql);
   536         -        break;
   537         -      case FZMODE_Strftime:
   538         -        zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql);
   539         -        break;
   540         -    }
          715  +        iStart = timeOfDay();
          716  +        g.bOomEnable = 1;
          717  +        if( verboseFlag ){
          718  +          zErrMsg = 0;
   541    719       rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg);
          720  +          if( zErrMsg ){
          721  +            sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg);
          722  +            zErrMsg = 0;
          723  +          }
          724  +        }else {
          725  +          rc = sqlite3_exec(db, zSql, execNoop, 0, 0);
          726  +        }
          727  +        g.bOomEnable = 0;
          728  +        iEnd = timeOfDay();
          729  +        rc = sqlite3_close(db);
          730  +        if( rc ){
          731  +          abendError("sqlite3_close() failed with rc=%d", rc);
          732  +        }
          733  +        if( !zDataOut && sqlite3_memory_used()>0 ){
          734  +          abendError("memory in use after close: %lld bytes",sqlite3_memory_used());
          735  +        }
          736  +        if( oomFlag ){
          737  +          /* Limit the number of iterations of the OOM loop to OOM_MAX.  If the
          738  +          ** first pass (single failure) exceeds 2/3rds of OOM_MAX this skip the
          739  +          ** second pass (continuous failure after first) completely. */
          740  +          if( g.nOomFault==0 || oomCnt>OOM_MAX ){
          741  +            if( g.bOomOnce && oomCnt<=(OOM_MAX*2/3) ){
          742  +              oomCnt = g.iOomCntdown = 1;
          743  +              g.bOomOnce = 0;
          744  +            }else{
          745  +              oomCnt = 0;
          746  +            }
          747  +          }else{
          748  +            g.iOomCntdown = ++oomCnt;
          749  +            g.nOomFault = 0;
          750  +          }
          751  +          if( oomCnt ){
          752  +            if( verboseFlag ){
          753  +              printf("%s.%d\n", g.bOomOnce ? "Once" : "Multi", oomCnt);
          754  +              fflush(stdout);
          755  +            }
          756  +            nTest++;
          757  +          }
          758  +        }
          759  +      }while( oomCnt>0 );
          760  +
          761  +      /* Store unique test cases in the in the dataDb database if the
          762  +      ** --unique-cases flag is present
          763  +      */
          764  +      if( zDataOut ){
          765  +        sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC);
          766  +        sqlite3_bind_int64(pStmt, 2, iEnd - iStart);
          767  +        rc = sqlite3_step(pStmt);
          768  +        if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb));
          769  +        sqlite3_reset(pStmt);
          770  +      }
          771  +
          772  +      /* Free the SQL from the current test case
          773  +      */
   542    774       if( zToFree ){
   543    775         sqlite3_free(zToFree);
   544    776         zToFree = 0;
   545    777       }
   546    778       zIn[iNext] = cSaved;
   547    779   
          780  +      /* Show test-case results in --verbose mode
          781  +      */
          782  +      if( verboseFlag ){
   548    783       printf("RESULT-CODE: %d\n", rc);
   549    784       if( zErrMsg ){
   550         -      printf("ERROR-MSG: [%s]\n", zErrMsg);
   551         -      sqlite3_free(zErrMsg);
          785  +          printf("ERROR-MSG: [%s]\n", zErrBuf);
          786  +        }
          787  +        fflush(stdout);
          788  +      }
          789  +
          790  +      /* Simulate an error if the TEST_FAILURE environment variable is "5".
          791  +      ** This is used to verify that automated test script really do spot
          792  +      ** errors that occur in this test program.
          793  +      */
          794  +      if( zFailCode ){
          795  +        if( zFailCode[0]=='5' && zFailCode[1]==0 ){
          796  +          abendError("simulated failure");
          797  +        }else if( zFailCode[0]!=0 ){
          798  +          /* If TEST_FAILURE is something other than 5, just exit the test
          799  +          ** early */
          800  +          printf("\nExit early due to TEST_FAILURE being set");
          801  +          break;
          802  +        }
          803  +      }
          804  +    }
          805  +    if( !verboseFlag && multiTest && !quietFlag && !oomFlag ) printf("\n");
          806  +  }
          807  +
          808  +  /* Report total number of tests run
          809  +  */
          810  +  if( nTest>1 && !quietFlag ){
          811  +    sqlite3_int64 iElapse = timeOfDay() - iBegin;
          812  +    printf("%s: 0 errors out of %d tests in %d.%03d seconds\nSQLite %s %s\n",
          813  +           g.zArgv0, nTest, (int)(iElapse/1000), (int)(iElapse%1000),
          814  +           sqlite3_libversion(), sqlite3_sourceid());
          815  +  }
          816  +
          817  +  /* Write the unique test cases if the --unique-cases flag was used
          818  +  */
          819  +  if( zDataOut ){
          820  +    int n = 0;
          821  +    FILE *out = fopen(zDataOut, "wb");
          822  +    if( out==0 ) abendError("cannot open %s for writing", zDataOut);
          823  +    if( nHeader>0 ) fwrite(zIn, nHeader, 1, out);
          824  +    sqlite3_finalize(pStmt);
          825  +    rc = sqlite3_prepare_v2(dataDb, "SELECT sql, tm FROM testcase ORDER BY tm, sql",
          826  +                            -1, &pStmt, 0);
          827  +    if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
          828  +    while( sqlite3_step(pStmt)==SQLITE_ROW ){
          829  +      fprintf(out,"/****<%d:%dms>****/", ++n, sqlite3_column_int(pStmt,1));
          830  +      fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out);
   552    831       }
   553         -    rc = sqlite3_close(db);
   554         -    if( rc ){
   555         -      abendError("sqlite3_close() failed with rc=%d", rc);
          832  +    fclose(out);
          833  +    sqlite3_finalize(pStmt);
          834  +    sqlite3_close(dataDb);
   556    835       }
   557         -    if( sqlite3_memory_used()>0 ){
   558         -      abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
   559         -    }
   560         -  }
          836  +
          837  +  /* Clean up and exit.
          838  +  */
          839  +  free(azInFile);
   561    840     free(zIn);
   562    841     free(pHeap);
   563    842     free(pLook);
   564    843     free(pScratch);
   565    844     free(pPCache);
   566    845     return 0;
   567    846   }

Changes to tool/mksqlite3c.tcl.

    13     13   # For example, the "parse.c" and "parse.h" files to implement the
    14     14   # the parser are derived from "parse.y" using lemon.  And the 
    15     15   # "keywordhash.h" files is generated by a program named "mkkeywordhash".
    16     16   #
    17     17   # After the "tsrc" directory has been created and populated, run
    18     18   # this script:
    19     19   #
    20         -#      tclsh mksqlite3c.tcl
           20  +#      tclsh mksqlite3c.tcl --srcdir $SRC
    21     21   #
    22     22   # The amalgamated SQLite code will be written into sqlite3.c
    23     23   #
    24     24   
    25     25   # Begin by reading the "sqlite3.h" header file.  Extract the version number
    26     26   # from in this file.  The version number is needed to generate the header
    27     27   # comment of the amalgamation.
    28     28   #
    29         -if {[lsearch $argv --nostatic]>=0} {
    30         -  set addstatic 0
    31         -} else {
    32     29     set addstatic 1
    33         -}
    34         -if {[lsearch $argv --linemacros]>=0} {
           30  +set linemacros 0
           31  +for {set i 0} {$i<[llength $argv]} {incr i} {
           32  +  set x [lindex $argv $i]
           33  +  if {[regexp {^-+nostatic$} $x]} {
           34  +    set addstatic 0
           35  +  } elseif {[regexp {^-+linemacros} $x]} {
    35     36     set linemacros 1
    36     37   } else {
    37         -  set linemacros 0
           38  +    error "unknown command-line option: $x"
           39  +  }
    38     40   }
    39     41   set in [open tsrc/sqlite3.h]
    40     42   set cnt 0
    41     43   set VERSION ?????
    42     44   while {![eof $in]} {
    43     45     set line [gets $in]
    44     46     if {$line=="" && [eof $in]} break
................................................................................
   364    366      fts3_snippet.c
   365    367      fts3_unicode.c
   366    368      fts3_unicode2.c
   367    369   
   368    370      rtree.c
   369    371      icu.c
   370    372      fts3_icu.c
   371         -
          373  +   dbstat.c
   372    374      sqlite3session.c
   373    375   } {
   374    376     copy_file tsrc/$file
   375    377   }
   376    378   
   377    379   close $out

Changes to tool/sqldiff.c.

   493    493   */
   494    494   static void diff_one_table(const char *zTab, FILE *out){
   495    495     char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
   496    496     char **az = 0;            /* Columns in main */
   497    497     char **az2 = 0;           /* Columns in aux */
   498    498     int nPk;                  /* Primary key columns in main */
   499    499     int nPk2;                 /* Primary key columns in aux */
   500         -  int n;                    /* Number of columns in main */
          500  +  int n = 0;                /* Number of columns in main */
   501    501     int n2;                   /* Number of columns in aux */
   502    502     int nQ;                   /* Number of output columns in the diff query */
   503    503     int i;                    /* Loop counter */
   504    504     const char *zSep;         /* Separator string */
   505    505     Str sql;                  /* Comparison query */
   506    506     sqlite3_stmt *pStmt;      /* Query statement to do the diff */
   507    507   
................................................................................
   730    730   */
   731    731   static void summarize_one_table(const char *zTab, FILE *out){
   732    732     char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
   733    733     char **az = 0;            /* Columns in main */
   734    734     char **az2 = 0;           /* Columns in aux */
   735    735     int nPk;                  /* Primary key columns in main */
   736    736     int nPk2;                 /* Primary key columns in aux */
   737         -  int n;                    /* Number of columns in main */
          737  +  int n = 0;                /* Number of columns in main */
   738    738     int n2;                   /* Number of columns in aux */
   739    739     int i;                    /* Loop counter */
   740    740     const char *zSep;         /* Separator string */
   741    741     Str sql;                  /* Comparison query */
   742    742     sqlite3_stmt *pStmt;      /* Query statement to do the diff */
   743    743     sqlite3_int64 nUpdate;    /* Number of updated rows */
   744    744     sqlite3_int64 nUnchanged; /* Number of unmodified rows */
................................................................................
   893    893     switch( iDType ){
   894    894       case SQLITE_INTEGER:
   895    895         iX = sqlite3_value_int64(pVal);
   896    896         memcpy(&uX, &iX, 8);
   897    897         for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
   898    898         break;
   899    899       case SQLITE_FLOAT:
   900         -      rX = sqlite3_value_int64(pVal);
          900  +      rX = sqlite3_value_double(pVal);
   901    901         memcpy(&uX, &rX, 8);
   902    902         for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
   903    903         break;
   904    904       case SQLITE_TEXT:
   905    905         iX = sqlite3_value_bytes(pVal);
   906    906         putsVarint(out, (sqlite3_uint64)iX);
   907         -      fwrite(sqlite3_value_text(pVal),1,iX,out);
          907  +      fwrite(sqlite3_value_text(pVal),1,(size_t)iX,out);
   908    908         break;
   909    909       case SQLITE_BLOB:
   910    910         iX = sqlite3_value_bytes(pVal);
   911    911         putsVarint(out, (sqlite3_uint64)iX);
   912         -      fwrite(sqlite3_value_blob(pVal),1,iX,out);
          912  +      fwrite(sqlite3_value_blob(pVal),1,(size_t)iX,out);
   913    913         break;
   914    914       case SQLITE_NULL:
   915    915         break;
   916    916     }
   917    917   }
   918    918   
   919    919   /*
................................................................................
  1112   1112   */
  1113   1113   static void showHelp(void){
  1114   1114     printf("Usage: %s [options] DB1 DB2\n", g.zArgv0);
  1115   1115     printf(
  1116   1116   "Output SQL text that would transform DB1 into DB2.\n"
  1117   1117   "Options:\n"
  1118   1118   "  --changeset FILE      Write a CHANGESET into FILE\n"
         1119  +"  -L|--lib LIBRARY      Load an SQLite extension library\n"
  1119   1120   "  --primarykey          Use schema-defined PRIMARY KEYs\n"
  1120   1121   "  --schema              Show only differences in the schema\n"
  1121   1122   "  --summary             Show only a summary of the differences\n"
  1122   1123   "  --table TAB           Show only differences in table TAB\n"
  1123   1124     );
  1124   1125   }
  1125   1126   
................................................................................
  1130   1131     int rc;
  1131   1132     char *zErrMsg = 0;
  1132   1133     char *zSql;
  1133   1134     sqlite3_stmt *pStmt;
  1134   1135     char *zTab = 0;
  1135   1136     FILE *out = stdout;
  1136   1137     void (*xDiff)(const char*,FILE*) = diff_one_table;
         1138  +  int nExt = 0;
         1139  +  char **azExt = 0;
  1137   1140   
  1138   1141     g.zArgv0 = argv[0];
  1139   1142     for(i=1; i<argc; i++){
  1140   1143       const char *z = argv[i];
  1141   1144       if( z[0]=='-' ){
  1142   1145         z++;
  1143   1146         if( z[0]=='-' ) z++;
  1144   1147         if( strcmp(z,"changeset")==0 ){
         1148  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1145   1149           out = fopen(argv[++i], "wb");
  1146   1150           if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
  1147   1151           xDiff = changeset_one_table;
  1148   1152         }else
  1149   1153         if( strcmp(z,"debug")==0 ){
         1154  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1150   1155           g.fDebug = strtol(argv[++i], 0, 0);
  1151   1156         }else
  1152   1157         if( strcmp(z,"help")==0 ){
  1153   1158           showHelp();
  1154   1159           return 0;
         1160  +      }else
         1161  +      if( strcmp(z,"lib")==0 || strcmp(z,"L")==0 ){
         1162  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
         1163  +        azExt = realloc(azExt, sizeof(azExt[0])*(nExt+1));
         1164  +        if( azExt==0 ) cmdlineError("out of memory");
         1165  +        azExt[nExt++] = argv[++i];
  1155   1166         }else
  1156   1167         if( strcmp(z,"primarykey")==0 ){
  1157   1168           g.bSchemaPK = 1;
  1158   1169         }else
  1159   1170         if( strcmp(z,"schema")==0 ){
  1160   1171           g.bSchemaOnly = 1;
  1161   1172         }else
  1162   1173         if( strcmp(z,"summary")==0 ){
  1163   1174           xDiff = summarize_one_table;
  1164   1175         }else
  1165   1176         if( strcmp(z,"table")==0 ){
         1177  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1166   1178           zTab = argv[++i];
  1167   1179         }else
  1168   1180         {
  1169   1181           cmdlineError("unknown option: %s", argv[i]);
  1170   1182         }
  1171   1183       }else if( zDb1==0 ){
  1172   1184         zDb1 = argv[i];
................................................................................
  1183   1195     if( rc ){
  1184   1196       cmdlineError("cannot open database file \"%s\"", zDb1);
  1185   1197     }
  1186   1198     rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg);
  1187   1199     if( rc || zErrMsg ){
  1188   1200       cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
  1189   1201     }
         1202  +  sqlite3_enable_load_extension(g.db, 1);
         1203  +  for(i=0; i<nExt; i++){
         1204  +    rc = sqlite3_load_extension(g.db, azExt[i], 0, &zErrMsg);
         1205  +    if( rc || zErrMsg ){
         1206  +      cmdlineError("error loading %s: %s", azExt[i], zErrMsg);
         1207  +    }
         1208  +  }
         1209  +  free(azExt);
  1190   1210     zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
  1191   1211     rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
  1192   1212     if( rc || zErrMsg ){
  1193   1213       cmdlineError("cannot attach database \"%s\"", zDb2);
  1194   1214     }
  1195   1215     rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
  1196   1216     if( rc || zErrMsg ){