/ Check-in [299ed55c]
Login

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

Overview
Comment:Merge latest trunk changes with this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1:299ed55c909d327826ef47e204ef1e9afd54852b
User & Date: dan 2015-05-14 08:58:52
Context
2015-05-14
09:53
Merge changes from the index-is-operator branch into this one. Drop the partial support added for IS on this branch by [52e73eec]. check-in: 16ab9caf user: dan tags: ota-update
08:58
Merge latest trunk changes with this branch. check-in: 299ed55c user: dan tags: ota-update
2015-05-13
04:50
Enhancements to the MSVC makefile. check-in: 59e3e9e7 user: mistachkin tags: trunk
2015-04-23
19:38
Fix a problem in the "ota" utility program causing it to crash if SQLite failed to open the target database. check-in: a1a2b4a2 user: dan tags: ota-update
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   163    163   
   164    164   USE_AMALGAMATION = @USE_AMALGAMATION@
   165    165   
   166    166   # Object files for the SQLite library (non-amalgamation).
   167    167   #
   168    168   LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
   169    169            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
   170         -         callback.lo complete.lo ctime.lo date.lo delete.lo \
          170  +         callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \
   171    171            expr.lo fault.lo fkey.lo \
   172    172            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   173    173            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   174    174            fts3_tokenize_vtab.lo \
   175    175            fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   176    176            func.lo global.lo hash.lo \
   177    177            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
................................................................................
   209    209     $(TOP)/src/btree.h \
   210    210     $(TOP)/src/btreeInt.h \
   211    211     $(TOP)/src/build.c \
   212    212     $(TOP)/src/callback.c \
   213    213     $(TOP)/src/complete.c \
   214    214     $(TOP)/src/ctime.c \
   215    215     $(TOP)/src/date.c \
          216  +  $(TOP)/src/dbstat.c \
   216    217     $(TOP)/src/delete.c \
   217    218     $(TOP)/src/expr.c \
   218    219     $(TOP)/src/fault.c \
   219    220     $(TOP)/src/fkey.c \
   220    221     $(TOP)/src/func.c \
   221    222     $(TOP)/src/global.c \
   222    223     $(TOP)/src/hash.c \
................................................................................
   388    389     $(TOP)/src/test_pcache.c \
   389    390     $(TOP)/src/test_quota.c \
   390    391     $(TOP)/src/test_rtree.c \
   391    392     $(TOP)/src/test_schema.c \
   392    393     $(TOP)/src/test_server.c \
   393    394     $(TOP)/src/test_superlock.c \
   394    395     $(TOP)/src/test_syscall.c \
   395         -  $(TOP)/src/test_stat.c \
   396    396     $(TOP)/src/test_tclvar.c \
   397    397     $(TOP)/src/test_thread.c \
   398    398     $(TOP)/src/test_vfs.c \
   399    399     $(TOP)/src/test_wsd.c       \
   400    400     $(TOP)/ext/fts3/fts3_term.c \
   401    401     $(TOP)/ext/fts3/fts3_test.c \
   402    402     $(TOP)/ext/ota/test_ota.c 
................................................................................
   423    423     $(TOP)/src/attach.c \
   424    424     $(TOP)/src/backup.c \
   425    425     $(TOP)/src/bitvec.c \
   426    426     $(TOP)/src/btree.c \
   427    427     $(TOP)/src/build.c \
   428    428     $(TOP)/src/ctime.c \
   429    429     $(TOP)/src/date.c \
          430  +  $(TOP)/src/dbstat.c \
   430    431     $(TOP)/src/expr.c \
   431    432     $(TOP)/src/func.c \
   432    433     $(TOP)/src/insert.c \
   433    434     $(TOP)/src/wal.c \
   434    435     $(TOP)/src/main.c \
   435    436     $(TOP)/src/mem5.c \
   436    437     $(TOP)/src/os.c \
................................................................................
   508    509     $(TOP)/ext/fts3/fts3_tokenizer.h
   509    510   EXTHDR += \
   510    511     $(TOP)/ext/rtree/rtree.h
   511    512   EXTHDR += \
   512    513     $(TOP)/ext/icu/sqliteicu.h
   513    514   EXTHDR += \
   514    515     $(TOP)/ext/rtree/sqlite3rtree.h
          516  +
          517  +# executables needed for testing
          518  +#
          519  +TESTPROGS = \
          520  +  testfixture$(TEXE) \
          521  +  sqlite3$(TEXE) \
          522  +  sqlite3_analyzer$(TEXE) \
          523  +  sqldiff$(TEXE)
          524  +
   515    525   
   516    526   # This is the default Makefile target.  The objects listed here
   517    527   # are what get build when you type just "make" with no arguments.
   518    528   #
   519    529   all:	sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
   520    530   
   521    531   Makefile: $(TOP)/Makefile.in
................................................................................
   536    546   		-avoid-version
   537    547   
   538    548   sqlite3$(TEXE):	$(TOP)/src/shell.c libsqlite3.la sqlite3.h
   539    549   	$(LTLINK) $(READLINE_FLAGS) \
   540    550   		-o $@ $(TOP)/src/shell.c libsqlite3.la \
   541    551   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   542    552   
   543         -sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
          553  +sqldiff$(TEXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
   544    554   	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c	sqlite3.c $(TLIBS)
   545    555   
   546         -fuzzershell$(EXE):	$(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h
          556  +fuzzershell$(TEXE):	$(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h
   547    557   	$(LTLINK) -o $@ $(TOP)/tool/fuzzershell.c	sqlite3.c $(TLIBS)
   548    558   
   549         -mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
          559  +mptester$(TEXE):	sqlite3.c $(TOP)/mptest/mptest.c
   550    560   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   551    561   		$(TLIBS) -rpath "$(libdir)"
   552    562   
   553         -MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   554         -MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
   555         -mptest:	mptester$(EXE)
          563  +MPTEST1=./mptester$(TEXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
          564  +MPTEST2=./mptester$(TEXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
          565  +mptest:	mptester$(TEXE)
   556    566   	rm -f mptest.db
   557    567   	$(MPTEST1) --journalmode DELETE
   558    568   	$(MPTEST2) --journalmode WAL
   559    569   	$(MPTEST1) --journalmode WAL
   560    570   	$(MPTEST2) --journalmode PERSIST
   561    571   	$(MPTEST1) --journalmode PERSIST
   562    572   	$(MPTEST2) --journalmode TRUNCATE
................................................................................
   652    662   
   653    663   ctime.lo:	$(TOP)/src/ctime.c $(HDR)
   654    664   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/ctime.c
   655    665   
   656    666   date.lo:	$(TOP)/src/date.c $(HDR)
   657    667   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/date.c
   658    668   
          669  +dbstat.lo:	$(TOP)/src/dbstat.c $(HDR)
          670  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/dbstat.c
          671  +
   659    672   delete.lo:	$(TOP)/src/delete.c $(HDR)
   660    673   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/delete.c
   661    674   
   662    675   expr.lo:	$(TOP)/src/expr.c $(HDR)
   663    676   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/expr.c
   664    677   
   665    678   fault.lo:	$(TOP)/src/fault.c $(HDR)
................................................................................
   951    964   TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
   952    965   
   953    966   testfixture$(TEXE):	$(TESTFIXTURE_SRC)
   954    967   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \
   955    968   		-o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS)
   956    969   
   957    970   # A very detailed test running most or all test cases
   958         -fulltest:	testfixture$(TEXE) sqlite3$(TEXE)
          971  +fulltest:	$(TESTPROGS) fuzztest
   959    972   	./testfixture$(TEXE) $(TOP)/test/all.test
   960    973   
   961    974   # Really really long testing
   962         -soaktest:	testfixture$(TEXE) sqlite3$(TEXE)
          975  +soaktest:	$(TESTPROGS) fuzzoomtest
   963    976   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
   964    977   
   965         -# Do extra testing but not aeverything.
   966         -fulltestonly:	testfixture$(TEXE) sqlite3$(TEXE)
          978  +# Do extra testing but not everything.
          979  +fulltestonly:	$(TESTPROGS)
   967    980   	./testfixture$(TEXE) $(TOP)/test/full.test
   968    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  +
   969    989   # This is the common case.  Run many tests but not those that take
   970    990   # a really long time.
   971    991   #
   972         -test:	testfixture$(TEXE) sqlite3$(TEXE)
          992  +test:	$(TESTPROGS) fuzztest
   973    993   	./testfixture$(TEXE) $(TOP)/test/veryquick.test
   974    994   
   975    995   # Run a test using valgrind.  This can take a really long time
   976    996   # because valgrind is so much slower than a native machine.
   977    997   #
   978         -valgrindtest:	testfixture$(TEXE) sqlite3$(TEXE)
          998  +valgrindtest:	$(TESTPROGS) fuzzershell$(TEXE)
          999  +	valgrind -v ./fuzzershell$(TEXE) -f $(TOP)/test/fuzzdata1.txt
   979   1000   	OMIT_MISUSE=1 valgrind -v ./testfixture$(TEXE) $(TOP)/test/permutations.test valgrind
   980   1001   
   981   1002   # A very fast test that checks basic sanity.  The name comes from
   982   1003   # the 60s-era electronics testing:  "Turn it on and see if smoke
   983   1004   # comes out."
   984   1005   #
   985         -smoketest:	testfixture$(TEXE)
         1006  +smoketest:	$(TESTPROGS) fuzzershell$(TEXE)
   986   1007   	./testfixture$(TEXE) $(TOP)/test/main.test
   987   1008   
   988         -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
   989   1010   	echo "#define TCLSH 2" > $@
   990         -	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@
         1011  +	echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@
         1012  +	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
   991   1013   	echo "static const char *tclsh_main_loop(void){" >> $@
   992   1014   	echo "static const char *zMainloop = " >> $@
   993   1015   	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
   994   1016   	echo "; return zMainloop; }" >> $@
   995   1017   
   996   1018   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
   997   1019   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
................................................................................
  1093   1115   	rm -f sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
  1094   1116   	rm -f sqlite3.c
  1095   1117   	rm -f sqlite3rc.h
  1096   1118   	rm -f shell.c sqlite3ext.h
  1097   1119   	rm -f sqlite3_analyzer$(TEXE) sqlite3_analyzer.c
  1098   1120   	rm -f sqlite-*-output.vsix
  1099   1121   	rm -f mptester mptester.exe
         1122  +	rm -f fuzzershell fuzzershell.exe
         1123  +	rm -f sqldiff sqldiff.exe
  1100   1124   
  1101   1125   distclean:	clean
  1102   1126   	rm -f config.log config.status libtool Makefile sqlite3.pc
  1103   1127   
  1104   1128   #
  1105   1129   # Windows section
  1106   1130   #

Changes to Makefile.msc.

    31     31   
    32     32   # Set this non-0 to have the shell executable link against the core dynamic
    33     33   # link library.
    34     34   #
    35     35   !IFNDEF DYNAMIC_SHELL
    36     36   DYNAMIC_SHELL = 0
    37     37   !ENDIF
           38  +
           39  +# Set this non-0 to enable extra code that attempts to detect misuse of the
           40  +# SQLite API.
           41  +#
           42  +!IFNDEF API_ARMOR
           43  +API_ARMOR = 0
           44  +!ENDIF
    38     45   
    39     46   # If necessary, create a list of harmless compiler warnings to disable when
    40     47   # compiling the various tools.  For the SQLite source code itself, warnings,
    41     48   # if any, will be disabled from within it.
    42     49   #
    43     50   !IFNDEF NO_WARN
    44     51   !IF $(USE_FULLWARN)!=0
................................................................................
   143    150   !ENDIF
   144    151   
   145    152   # Set this to non-0 to use the Win32 native heap subsystem.
   146    153   #
   147    154   !IFNDEF WIN32HEAP
   148    155   WIN32HEAP = 0
   149    156   !ENDIF
          157  +
          158  +# Set this to non-0 to enable OSTRACE() macros, which can be useful when
          159  +# debugging.
          160  +#
          161  +!IFNDEF OSTRACE
          162  +OSTRACE = 0
          163  +!ENDIF
   150    164   
   151    165   # Set this to one of the following values to enable various debugging
   152    166   # features.  Each level includes the debugging options from the previous
   153    167   # levels.  Currently, the recognized values for DEBUG are:
   154    168   #
   155    169   # 0 == NDEBUG: Disables assert() and other runtime diagnostics.
   156    170   # 1 == SQLITE_ENABLE_API_ARMOR: extra attempts to detect misuse of the API.
................................................................................
   480    494   #
   481    495   !IF $(DEBUG)==0
   482    496   TCC = $(TCC) -DNDEBUG
   483    497   BCC = $(BCC) -DNDEBUG
   484    498   RCC = $(RCC) -DNDEBUG
   485    499   !ENDIF
   486    500   
   487         -!IF $(DEBUG)>0
   488         -TCC = $(TCC) -DSQLITE_ENABLE_API_ARMOR
   489         -RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR
          501  +!IF $(DEBUG)>0 || $(API_ARMOR)!=0
          502  +TCC = $(TCC) -DSQLITE_ENABLE_API_ARMOR=1
          503  +RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR=1
   490    504   !ENDIF
   491    505   
   492    506   !IF $(DEBUG)>2
   493         -TCC = $(TCC) -DSQLITE_DEBUG
   494         -RCC = $(RCC) -DSQLITE_DEBUG
          507  +TCC = $(TCC) -DSQLITE_DEBUG=1
          508  +RCC = $(RCC) -DSQLITE_DEBUG=1
   495    509   !ENDIF
   496    510   
   497         -!IF $(DEBUG)>4
   498         -TCC = $(TCC) -DSQLITE_DEBUG_OS_TRACE=1
   499         -RCC = $(RCC) -DSQLITE_DEBUG_OS_TRACE=1
          511  +!IF $(DEBUG)>4 || $(OSTRACE)!=0
          512  +TCC = $(TCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
          513  +RCC = $(RCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   500    514   !ENDIF
   501    515   
   502    516   !IF $(DEBUG)>5
   503         -TCC = $(TCC) -DSQLITE_ENABLE_IOTRACE
   504         -RCC = $(RCC) -DSQLITE_ENABLE_IOTRACE
          517  +TCC = $(TCC) -DSQLITE_ENABLE_IOTRACE=1
          518  +RCC = $(RCC) -DSQLITE_ENABLE_IOTRACE=1
   505    519   !ENDIF
   506    520   
   507    521   # Prevent warnings about "insecure" MSVC runtime library functions
   508    522   # being used.
   509    523   #
   510    524   TCC = $(TCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
   511    525   BCC = $(BCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
................................................................................
   804    818   # You should not have to change anything below this line
   805    819   ###############################################################################
   806    820   
   807    821   # Object files for the SQLite library (non-amalgamation).
   808    822   #
   809    823   LIBOBJS0 = vdbe.lo parse.lo alter.lo analyze.lo attach.lo auth.lo \
   810    824            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
   811         -         callback.lo complete.lo ctime.lo date.lo delete.lo \
          825  +         callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \
   812    826            expr.lo fault.lo fkey.lo \
   813    827            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   814    828            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   815    829            fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   816    830            func.lo global.lo hash.lo \
   817    831            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   818    832            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
................................................................................
   860    874     $(TOP)\src\btree.h \
   861    875     $(TOP)\src\btreeInt.h \
   862    876     $(TOP)\src\build.c \
   863    877     $(TOP)\src\callback.c \
   864    878     $(TOP)\src\complete.c \
   865    879     $(TOP)\src\ctime.c \
   866    880     $(TOP)\src\date.c \
          881  +  $(TOP)\src\dbstat.c \
   867    882     $(TOP)\src\delete.c \
   868    883     $(TOP)\src\expr.c \
   869    884     $(TOP)\src\fault.c \
   870    885     $(TOP)\src\fkey.c \
   871    886     $(TOP)\src\func.c \
   872    887     $(TOP)\src\global.c \
   873    888     $(TOP)\src\hash.c \
................................................................................
  1037   1052     $(TOP)\src\test_pcache.c \
  1038   1053     $(TOP)\src\test_quota.c \
  1039   1054     $(TOP)\src\test_rtree.c \
  1040   1055     $(TOP)\src\test_schema.c \
  1041   1056     $(TOP)\src\test_server.c \
  1042   1057     $(TOP)\src\test_superlock.c \
  1043   1058     $(TOP)\src\test_syscall.c \
  1044         -  $(TOP)\src\test_stat.c \
  1045   1059     $(TOP)\src\test_tclvar.c \
  1046   1060     $(TOP)\src\test_thread.c \
  1047   1061     $(TOP)\src\test_vfs.c \
  1048   1062     $(TOP)\src\test_wsd.c \
  1049   1063     $(TOP)\ext\fts3\fts3_term.c \
  1050   1064     $(TOP)\ext\fts3\fts3_test.c
  1051   1065   
................................................................................
  1072   1086     $(TOP)\src\attach.c \
  1073   1087     $(TOP)\src\backup.c \
  1074   1088     $(TOP)\src\bitvec.c \
  1075   1089     $(TOP)\src\btree.c \
  1076   1090     $(TOP)\src\build.c \
  1077   1091     $(TOP)\src\ctime.c \
  1078   1092     $(TOP)\src\date.c \
         1093  +  $(TOP)\src\dbstat.c \
  1079   1094     $(TOP)\src\expr.c \
  1080   1095     $(TOP)\src\func.c \
  1081   1096     $(TOP)\src\insert.c \
  1082   1097     $(TOP)\src\wal.c \
  1083   1098     $(TOP)\src\main.c \
  1084   1099     $(TOP)\src\mem5.c \
  1085   1100     $(TOP)\src\os.c \
................................................................................
  1159   1174     $(TOP)\ext\fts3\fts3_tokenizer.h
  1160   1175   EXTHDR = $(EXTHDR) \
  1161   1176     $(TOP)\ext\rtree\rtree.h
  1162   1177   EXTHDR = $(EXTHDR) \
  1163   1178     $(TOP)\ext\icu\sqliteicu.h
  1164   1179   EXTHDR = $(EXTHDR) \
  1165   1180     $(TOP)\ext\rtree\sqlite3rtree.h
         1181  +
         1182  +# executables needed for testing
         1183  +#
         1184  +TESTPROGS = \
         1185  +  testfixture.exe \
         1186  +  sqlite3.exe \
         1187  +  sqlite3_analyzer.exe \
         1188  +  sqldiff.exe
         1189  +
  1166   1190   
  1167   1191   # This is the default Makefile target.  The objects listed here
  1168   1192   # are what get build when you type just "make" with no arguments.
  1169   1193   #
  1170   1194   all:	dll libsqlite3.lib sqlite3.exe libtclsqlite3.lib
  1171   1195   
  1172   1196   libsqlite3.lib:	$(LIBOBJ)
................................................................................
  1315   1339   
  1316   1340   ctime.lo:	$(TOP)\src\ctime.c $(HDR)
  1317   1341   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\ctime.c
  1318   1342   
  1319   1343   date.lo:	$(TOP)\src\date.c $(HDR)
  1320   1344   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\date.c
  1321   1345   
         1346  +dbstat.lo:	$(TOP)\src\date.c $(HDR)
         1347  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\dbstat.c
         1348  +
  1322   1349   delete.lo:	$(TOP)\src\delete.c $(HDR)
  1323   1350   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\delete.c
  1324   1351   
  1325   1352   expr.lo:	$(TOP)\src\expr.c $(HDR)
  1326   1353   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\expr.c
  1327   1354   
  1328   1355   fault.lo:	$(TOP)\src\fault.c $(HDR)
................................................................................
  1620   1647   		-DBUILD_sqlite -I$(TCLINCDIR) \
  1621   1648   		$(TESTFIXTURE_SRC) \
  1622   1649   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1623   1650   
  1624   1651   extensiontest: testfixture.exe testloadext.dll
  1625   1652   	.\testfixture.exe $(TOP)\test\loadext.test
  1626   1653   
  1627         -fulltest:	testfixture.exe sqlite3.exe
         1654  +fulltest:	$(TESTPROGS) fuzztest
  1628   1655   	.\testfixture.exe $(TOP)\test\all.test
  1629   1656   
  1630         -soaktest:	testfixture.exe sqlite3.exe
         1657  +soaktest:	$(TESTPROGS) fuzzoomtest
  1631   1658   	.\testfixture.exe $(TOP)\test\all.test -soak=1
  1632   1659   
  1633         -fulltestonly:	testfixture.exe sqlite3.exe
         1660  +fulltestonly:	$(TESTPROGS) fuzztest
  1634   1661   	.\testfixture.exe $(TOP)\test\full.test
  1635   1662   
  1636   1663   queryplantest:	testfixture.exe sqlite3.exe
  1637   1664   	.\testfixture.exe $(TOP)\test\permutations.test queryplanner
  1638   1665   
  1639         -test:	testfixture.exe sqlite3.exe
         1666  +fuzztest:	fuzzershell.exe
         1667  +	.\fuzzershell.exe $(TOP)\test\fuzzdata1.txt $(TOP)\test\fuzzdata2.txt
         1668  +
         1669  +fuzzoomtest:	fuzzershell.exe
         1670  +	.\fuzzershell.exe -f $(TOP)\test\fuzzdata1.txt --oom
         1671  +
         1672  +test:	$(TESTPROGS) fuzztest
  1640   1673   	.\testfixture.exe $(TOP)\test\veryquick.test
  1641   1674   
  1642         -smoketest:	testfixture.exe
         1675  +smoketest:	$(TESTPROGS) fuzzershell.exe
  1643   1676   	.\testfixture.exe $(TOP)\test\main.test
  1644   1677   
  1645         -sqlite3_analyzer.c: $(SQLITE3C) $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
  1646         -	copy $(SQLITE3C) + $(TOP)\src\test_stat.c + $(TOP)\src\tclsqlite.c $@
         1678  +sqlite3_analyzer.c: $(SQLITE3C) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
         1679  +	echo #define TCLSH 2 > $@
         1680  +	echo #define SQLITE_ENABLE_DBSTAT_VTAB 1 >> $@
         1681  +	copy $@ + $(SQLITE3C) + $(TOP)\src\tclsqlite.c $@
  1647   1682   	echo static const char *tclsh_main_loop(void){ >> $@
  1648   1683   	echo static const char *zMainloop = >> $@
  1649   1684   	$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
  1650   1685   	echo ; return zMainloop; } >> $@
  1651   1686   
  1652   1687   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  1653         -	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
         1688  +	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_analyzer.c \
  1654   1689   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1655   1690   
  1656   1691   testloadext.lo:	$(TOP)\src\test_loadext.c
  1657   1692   	$(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c
  1658   1693   
  1659   1694   testloadext.dll: testloadext.lo
  1660   1695   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /OUT:$@ testloadext.lo
................................................................................
  1716   1751   	del /Q mptester.exe wordcount.exe 2>NUL
  1717   1752   	del /Q sqlite3.exe sqlite3.dll sqlite3.def 2>NUL
  1718   1753   	del /Q sqlite3.c sqlite3-*.c 2>NUL
  1719   1754   	del /Q sqlite3rc.h 2>NUL
  1720   1755   	del /Q shell.c sqlite3ext.h 2>NUL
  1721   1756   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  1722   1757   	del /Q sqlite-*-output.vsix 2>NUL
         1758  +	del /Q fuzzershell.exe sqldiff.exe 2>NUL
  1723   1759   
  1724   1760   # Dynamic link library section.
  1725   1761   #
  1726   1762   dll: sqlite3.dll
  1727   1763   
  1728   1764   sqlite3.def: libsqlite3.lib
  1729   1765   	echo EXPORTS > sqlite3.def
  1730   1766   	dumpbin /all libsqlite3.lib \
  1731   1767   		| $(NAWK) "/ 1 _?sqlite3_/ { sub(/^.* _?/,\"\");print }" \
  1732   1768   		| sort >> sqlite3.def
  1733   1769   
  1734   1770   sqlite3.dll: $(LIBOBJ) $(LIBRESOBJS) $(CORE_LINK_DEP)
  1735   1771   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL $(CORE_LINK_OPTS) /OUT:$@ $(LIBOBJ) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)

Changes to VERSION.

     1         -3.8.10
            1  +3.8.10.1

Changes to autoconf/Makefile.am.

     2      2   AM_CFLAGS = @THREADSAFE_FLAGS@ @DYNAMIC_EXTENSION_FLAGS@ -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE
     3      3   
     4      4   lib_LTLIBRARIES = libsqlite3.la
     5      5   libsqlite3_la_SOURCES = sqlite3.c
     6      6   libsqlite3_la_LDFLAGS = -no-undefined -version-info 8:6:8
     7      7   
     8      8   bin_PROGRAMS = sqlite3
     9         -sqlite3_SOURCES = shell.c sqlite3.h
    10         -sqlite3_LDADD = $(top_builddir)/libsqlite3.la @READLINE_LIBS@
    11         -sqlite3_DEPENDENCIES = $(top_builddir)/libsqlite3.la
            9  +sqlite3_SOURCES = shell.c sqlite3.c sqlite3.h
           10  +sqlite3_LDADD = @READLINE_LIBS@
           11  +sqlite3_CFLAGS = $(AM_CFLAGS)
    12     12   
    13     13   include_HEADERS = sqlite3.h sqlite3ext.h
    14     14   
    15     15   EXTRA_DIST = sqlite3.1 tea
    16     16   pkgconfigdir = ${libdir}/pkgconfig
    17     17   pkgconfig_DATA = sqlite3.pc
    18     18   
    19     19   man_MANS = sqlite3.1

Changes to configure.

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

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.
................................................................................
  1660   1671   static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
  1661   1672     Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  1662   1673     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  1663   1674     sqlite3_finalize(pCsr->pStmt);
  1664   1675     sqlite3Fts3ExprFree(pCsr->pExpr);
  1665   1676     sqlite3Fts3FreeDeferredTokens(pCsr);
  1666   1677     sqlite3_free(pCsr->aDoclist);
  1667         -  sqlite3_free(pCsr->aMatchinfo);
         1678  +  sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
  1668   1679     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  1669   1680     sqlite3_free(pCsr);
  1670   1681     return SQLITE_OK;
  1671   1682   }
  1672   1683   
  1673   1684   /*
  1674   1685   ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
................................................................................
  3161   3172     if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
  3162   3173     if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
  3163   3174     assert( iIdx==nVal );
  3164   3175   
  3165   3176     /* In case the cursor has been used before, clear it now. */
  3166   3177     sqlite3_finalize(pCsr->pStmt);
  3167   3178     sqlite3_free(pCsr->aDoclist);
  3168         -  sqlite3_free(pCsr->aMatchinfo);
         3179  +  sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
  3169   3180     sqlite3Fts3ExprFree(pCsr->pExpr);
  3170   3181     memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
  3171   3182   
  3172   3183     /* Set the lower and upper bounds on docids to return */
  3173   3184     pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
  3174   3185     pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
  3175   3186   

Changes to ext/fts3/fts3Int.h.

   193    193   
   194    194   typedef struct Fts3Doclist Fts3Doclist;
   195    195   typedef struct Fts3SegFilter Fts3SegFilter;
   196    196   typedef struct Fts3DeferredToken Fts3DeferredToken;
   197    197   typedef struct Fts3SegReader Fts3SegReader;
   198    198   typedef struct Fts3MultiSegReader Fts3MultiSegReader;
   199    199   
          200  +typedef struct MatchinfoBuffer MatchinfoBuffer;
          201  +
   200    202   /*
   201    203   ** A connection to a fulltext index is an instance of the following
   202    204   ** structure. The xCreate and xConnect methods create an instance
   203    205   ** of this structure and xDestroy and xDisconnect free that instance.
   204    206   ** All other methods receive a pointer to the structure as one of their
   205    207   ** arguments.
   206    208   */
................................................................................
   302    304     u8 bDesc;                       /* True to sort in descending order */
   303    305     int eEvalmode;                  /* An FTS3_EVAL_XX constant */
   304    306     int nRowAvg;                    /* Average size of database rows, in pages */
   305    307     sqlite3_int64 nDoc;             /* Documents in table */
   306    308     i64 iMinDocid;                  /* Minimum docid to return */
   307    309     i64 iMaxDocid;                  /* Maximum docid to return */
   308    310     int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
   309         -  u32 *aMatchinfo;                /* Information about most recent match */
   310         -  int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
   311         -  char *zMatchinfo;               /* Matchinfo specification */
          311  +  MatchinfoBuffer *pMIBuffer;     /* Buffer for matchinfo data */
   312    312   };
   313    313   
   314    314   #define FTS3_EVAL_FILTER    0
   315    315   #define FTS3_EVAL_NEXT      1
   316    316   #define FTS3_EVAL_MATCHINFO 2
   317    317   
   318    318   /*
................................................................................
   424    424   
   425    425     /* The following are used by the fts3_eval.c module. */
   426    426     sqlite3_int64 iDocid;      /* Current docid */
   427    427     u8 bEof;                   /* True this expression is at EOF already */
   428    428     u8 bStart;                 /* True if iDocid is valid */
   429    429     u8 bDeferred;              /* True if this expression is entirely deferred */
   430    430   
   431         -  u32 *aMI;
          431  +  /* The following are used by the fts3_snippet.c module. */
          432  +  int iPhrase;               /* Index of this phrase in matchinfo() results */
          433  +  u32 *aMI;                  /* See above */
   432    434   };
   433    435   
   434    436   /*
   435    437   ** Candidate values for Fts3Query.eType. Note that the order of the first
   436    438   ** four values is in order of precedence when parsing expressions. For 
   437    439   ** example, the following:
   438    440   **
................................................................................
   535    537   int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
   536    538   
   537    539   #define fts3GetVarint32(p, piVal) (                                           \
   538    540     (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
   539    541   )
   540    542   
   541    543   /* fts3.c */
          544  +void sqlite3Fts3ErrMsg(char**,const char*,...);
   542    545   int sqlite3Fts3PutVarint(char *, sqlite3_int64);
   543    546   int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
   544    547   int sqlite3Fts3GetVarint32(const char *, int *);
   545    548   int sqlite3Fts3VarintLen(sqlite3_uint64);
   546    549   void sqlite3Fts3Dequote(char *);
   547    550   void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
   548    551   int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
................................................................................
   559    562   
   560    563   /* fts3_snippet.c */
   561    564   void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
   562    565   void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
   563    566     const char *, const char *, int, int
   564    567   );
   565    568   void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
          569  +void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
   566    570   
   567    571   /* fts3_expr.c */
   568    572   int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
   569    573     char **, int, int, int, const char *, int, Fts3Expr **, char **
   570    574   );
   571    575   void sqlite3Fts3ExprFree(Fts3Expr *);
   572    576   #ifdef SQLITE_TEST

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 */
           31  +#define FTS3_MATCHINFO_LHITS_BM  'b'        /* nCol*nPhrase values */
    30     32   
    31     33   /*
    32     34   ** The default value for the second argument to matchinfo(). 
    33     35   */
    34     36   #define FTS3_MATCHINFO_DEFAULT   "pcx"
    35     37   
    36     38   
................................................................................
    84     86   */
    85     87   typedef struct MatchInfo MatchInfo;
    86     88   struct MatchInfo {
    87     89     Fts3Cursor *pCursor;            /* FTS3 Cursor */
    88     90     int nCol;                       /* Number of columns in table */
    89     91     int nPhrase;                    /* Number of matchable phrases in query */
    90     92     sqlite3_int64 nDoc;             /* Number of docs in database */
           93  +  char flag;
    91     94     u32 *aMatchinfo;                /* Pre-allocated buffer */
    92     95   };
    93     96   
           97  +/*
           98  +** An instance of this structure is used to manage a pair of buffers, each
           99  +** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
          100  +** for details.
          101  +*/
          102  +struct MatchinfoBuffer {
          103  +  u8 aRef[3];
          104  +  int nElem;
          105  +  int bGlobal;                    /* Set if global data is loaded */
          106  +  char *zMatchinfo;
          107  +  u32 aMatchinfo[0];
          108  +};
    94    109   
    95    110   
    96    111   /*
    97    112   ** The snippet() and offsets() functions both return text values. An instance
    98    113   ** of the following structure is used to accumulate those values while the
    99    114   ** functions are running. See fts3StringAppend() for details.
   100    115   */
................................................................................
   101    116   typedef struct StrBuffer StrBuffer;
   102    117   struct StrBuffer {
   103    118     char *z;                        /* Pointer to buffer containing string */
   104    119     int n;                          /* Length of z in bytes (excl. nul-term) */
   105    120     int nAlloc;                     /* Allocated size of buffer z in bytes */
   106    121   };
   107    122   
          123  +
          124  +/*************************************************************************
          125  +** Start of MatchinfoBuffer code.
          126  +*/
          127  +
          128  +/*
          129  +** Allocate a two-slot MatchinfoBuffer object.
          130  +*/
          131  +static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){
          132  +  MatchinfoBuffer *pRet;
          133  +  int nByte = sizeof(u32) * (2*nElem + 2) + sizeof(MatchinfoBuffer);
          134  +  int nStr = strlen(zMatchinfo);
          135  +
          136  +  pRet = sqlite3_malloc(nByte + nStr+1);
          137  +  if( pRet ){
          138  +    memset(pRet, 0, nByte);
          139  +    pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
          140  +    pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
          141  +    pRet->nElem = nElem;
          142  +    pRet->zMatchinfo = ((char*)pRet) + nByte;
          143  +    memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
          144  +    pRet->aRef[0] = 1;
          145  +  }
          146  +
          147  +  return pRet;
          148  +}
          149  +
          150  +static void fts3MIBufferFree(void *p){
          151  +  MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
          152  +
          153  +  assert( (u32*)p==&pBuf->aMatchinfo[1] 
          154  +       || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2] 
          155  +  );
          156  +  if( (u32*)p==&pBuf->aMatchinfo[1] ){
          157  +    pBuf->aRef[1] = 0;
          158  +  }else{
          159  +    pBuf->aRef[2] = 0;
          160  +  }
          161  +
          162  +  if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
          163  +    sqlite3_free(pBuf);
          164  +  }
          165  +}
          166  +
          167  +static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
          168  +  void (*xRet)(void*) = 0;
          169  +  u32 *aOut = 0;
          170  +
          171  +  if( p->aRef[1]==0 ){
          172  +    p->aRef[1] = 1;
          173  +    aOut = &p->aMatchinfo[1];
          174  +    xRet = fts3MIBufferFree;
          175  +  }
          176  +  else if( p->aRef[2]==0 ){
          177  +    p->aRef[2] = 1;
          178  +    aOut = &p->aMatchinfo[p->nElem+2];
          179  +    xRet = fts3MIBufferFree;
          180  +  }else{
          181  +    aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
          182  +    if( aOut ){
          183  +      xRet = sqlite3_free;
          184  +      if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
          185  +    }
          186  +  }
          187  +
          188  +  *paOut = aOut;
          189  +  return xRet;
          190  +}
          191  +
          192  +static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
          193  +  p->bGlobal = 1;
          194  +  memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
          195  +}
          196  +
          197  +/*
          198  +** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
          199  +*/
          200  +void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
          201  +  if( p ){
          202  +    assert( p->aRef[0]==1 );
          203  +    p->aRef[0] = 0;
          204  +    if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
          205  +      sqlite3_free(p);
          206  +    }
          207  +  }
          208  +}
          209  +
          210  +/* 
          211  +** End of MatchinfoBuffer code.
          212  +*************************************************************************/
          213  +
   108    214   
   109    215   /*
   110    216   ** This function is used to help iterate through a position-list. A position
   111    217   ** list is a list of unique integers, sorted from smallest to largest. Each
   112    218   ** element of the list is represented by an FTS3 varint that takes the value
   113    219   ** of the difference between the current element and the previous one plus
   114    220   ** two. For example, to store the position-list:
................................................................................
   138    244   static int fts3ExprIterate2(
   139    245     Fts3Expr *pExpr,                /* Expression to iterate phrases of */
   140    246     int *piPhrase,                  /* Pointer to phrase counter */
   141    247     int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
   142    248     void *pCtx                      /* Second argument to pass to callback */
   143    249   ){
   144    250     int rc;                         /* Return code */
   145         -  int eType = pExpr->eType;       /* Type of expression node pExpr */
          251  +  int eType = pExpr->eType;     /* Type of expression node pExpr */
   146    252   
   147    253     if( eType!=FTSQUERY_PHRASE ){
   148    254       assert( pExpr->pLeft && pExpr->pRight );
   149    255       rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
   150    256       if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
   151    257         rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
   152    258       }
................................................................................
   171    277     Fts3Expr *pExpr,                /* Expression to iterate phrases of */
   172    278     int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
   173    279     void *pCtx                      /* Second argument to pass to callback */
   174    280   ){
   175    281     int iPhrase = 0;                /* Variable used as the phrase counter */
   176    282     return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
   177    283   }
          284  +
   178    285   
   179    286   /*
   180    287   ** This is an fts3ExprIterate() callback used while loading the doclists
   181    288   ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
   182    289   ** fts3ExprLoadDoclists().
   183    290   */
   184    291   static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
................................................................................
   216    323     if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
   217    324     if( pnToken ) *pnToken = sCtx.nToken;
   218    325     return rc;
   219    326   }
   220    327   
   221    328   static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
   222    329     (*(int *)ctx)++;
   223         -  UNUSED_PARAMETER(pExpr);
   224         -  UNUSED_PARAMETER(iPhrase);
          330  +  pExpr->iPhrase = iPhrase;
   225    331     return SQLITE_OK;
   226    332   }
   227    333   static int fts3ExprPhraseCount(Fts3Expr *pExpr){
   228    334     int nPhrase = 0;
   229    335     (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
   230    336     return nPhrase;
   231    337   }
................................................................................
   438    544     ** the set of phrases in the expression to populate the aPhrase[] array.
   439    545     */
   440    546     sIter.pCsr = pCsr;
   441    547     sIter.iCol = iCol;
   442    548     sIter.nSnippet = nSnippet;
   443    549     sIter.nPhrase = nList;
   444    550     sIter.iCurrent = -1;
   445         -  rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
          551  +  rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
   446    552     if( rc==SQLITE_OK ){
   447    553   
   448    554       /* Set the *pmSeen output variable. */
   449    555       for(i=0; i<nList; i++){
   450    556         if( sIter.aPhrase[i].pHead ){
   451    557           *pmSeen |= (u64)1 << i;
   452    558         }
................................................................................
   738    844       c = *pEnd++ & 0x80;
   739    845       if( !c ) nEntry++;
   740    846     }
   741    847   
   742    848     *ppCollist = pEnd;
   743    849     return nEntry;
   744    850   }
          851  +
          852  +/*
          853  +** This function gathers 'y' or 'b' data for a single phrase.
          854  +*/
          855  +static void fts3ExprLHits(
          856  +  Fts3Expr *pExpr,                /* Phrase expression node */
          857  +  MatchInfo *p                    /* Matchinfo context */
          858  +){
          859  +  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
          860  +  int iStart;
          861  +  Fts3Phrase *pPhrase = pExpr->pPhrase;
          862  +  char *pIter = pPhrase->doclist.pList;
          863  +  int iCol = 0;
          864  +
          865  +  assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
          866  +  if( p->flag==FTS3_MATCHINFO_LHITS ){
          867  +    iStart = pExpr->iPhrase * p->nCol;
          868  +  }else{
          869  +    iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
          870  +  }
          871  +
          872  +  while( 1 ){
          873  +    int nHit = fts3ColumnlistCount(&pIter);
          874  +    if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
          875  +      if( p->flag==FTS3_MATCHINFO_LHITS ){
          876  +        p->aMatchinfo[iStart + iCol] = (u32)nHit;
          877  +      }else if( nHit ){
          878  +        p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
          879  +      }
          880  +    }
          881  +    assert( *pIter==0x00 || *pIter==0x01 );
          882  +    if( *pIter!=0x01 ) break;
          883  +    pIter++;
          884  +    pIter += fts3GetVarint32(pIter, &iCol);
          885  +  }
          886  +}
          887  +
          888  +/*
          889  +** Gather the results for matchinfo directives 'y' and 'b'.
          890  +*/
          891  +static void fts3ExprLHitGather(
          892  +  Fts3Expr *pExpr,
          893  +  MatchInfo *p
          894  +){
          895  +  assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
          896  +  if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
          897  +    if( pExpr->pLeft ){
          898  +      fts3ExprLHitGather(pExpr->pLeft, p);
          899  +      fts3ExprLHitGather(pExpr->pRight, p);
          900  +    }else{
          901  +      fts3ExprLHits(pExpr, p);
          902  +    }
          903  +  }
          904  +}
   745    905   
   746    906   /*
   747    907   ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
   748    908   ** for a single query. 
   749    909   **
   750    910   ** fts3ExprIterate() callback to load the 'global' elements of a
   751    911   ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements 
................................................................................
   817    977     if( (cArg==FTS3_MATCHINFO_NPHRASE)
   818    978      || (cArg==FTS3_MATCHINFO_NCOL)
   819    979      || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
   820    980      || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
   821    981      || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
   822    982      || (cArg==FTS3_MATCHINFO_LCS)
   823    983      || (cArg==FTS3_MATCHINFO_HITS)
          984  +   || (cArg==FTS3_MATCHINFO_LHITS)
          985  +   || (cArg==FTS3_MATCHINFO_LHITS_BM)
   824    986     ){
   825    987       return SQLITE_OK;
   826    988     }
   827         -  *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
          989  +  sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
   828    990     return SQLITE_ERROR;
   829    991   }
   830    992   
   831    993   static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
   832    994     int nVal;                       /* Number of integers output by cArg */
   833    995   
   834    996     switch( cArg ){
................................................................................
   839   1001         break;
   840   1002   
   841   1003       case FTS3_MATCHINFO_AVGLENGTH:
   842   1004       case FTS3_MATCHINFO_LENGTH:
   843   1005       case FTS3_MATCHINFO_LCS:
   844   1006         nVal = pInfo->nCol;
   845   1007         break;
         1008  +
         1009  +    case FTS3_MATCHINFO_LHITS:
         1010  +      nVal = pInfo->nCol * pInfo->nPhrase;
         1011  +      break;
         1012  +
         1013  +    case FTS3_MATCHINFO_LHITS_BM:
         1014  +      nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
         1015  +      break;
   846   1016   
   847   1017       default:
   848   1018         assert( cArg==FTS3_MATCHINFO_HITS );
   849   1019         nVal = pInfo->nCol * pInfo->nPhrase * 3;
   850   1020         break;
   851   1021     }
   852   1022   
................................................................................
  1034   1204   ){
  1035   1205     int rc = SQLITE_OK;
  1036   1206     int i;
  1037   1207     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  1038   1208     sqlite3_stmt *pSelect = 0;
  1039   1209   
  1040   1210     for(i=0; rc==SQLITE_OK && zArg[i]; i++){
  1041         -
         1211  +    pInfo->flag = zArg[i];
  1042   1212       switch( zArg[i] ){
  1043   1213         case FTS3_MATCHINFO_NPHRASE:
  1044   1214           if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
  1045   1215           break;
  1046   1216   
  1047   1217         case FTS3_MATCHINFO_NCOL:
  1048   1218           if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
................................................................................
  1093   1263   
  1094   1264         case FTS3_MATCHINFO_LCS:
  1095   1265           rc = fts3ExprLoadDoclists(pCsr, 0, 0);
  1096   1266           if( rc==SQLITE_OK ){
  1097   1267             rc = fts3MatchinfoLcs(pCsr, pInfo);
  1098   1268           }
  1099   1269           break;
         1270  +
         1271  +      case FTS3_MATCHINFO_LHITS_BM:
         1272  +      case FTS3_MATCHINFO_LHITS: {
         1273  +        int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
         1274  +        memset(pInfo->aMatchinfo, 0, nZero);
         1275  +        fts3ExprLHitGather(pCsr->pExpr, pInfo);
         1276  +        break;
         1277  +      }
  1100   1278   
  1101   1279         default: {
  1102   1280           Fts3Expr *pExpr;
  1103   1281           assert( zArg[i]==FTS3_MATCHINFO_HITS );
  1104   1282           pExpr = pCsr->pExpr;
  1105   1283           rc = fts3ExprLoadDoclists(pCsr, 0, 0);
  1106   1284           if( rc!=SQLITE_OK ) break;
................................................................................
  1125   1303   }
  1126   1304   
  1127   1305   
  1128   1306   /*
  1129   1307   ** Populate pCsr->aMatchinfo[] with data for the current row. The 
  1130   1308   ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
  1131   1309   */
  1132         -static int fts3GetMatchinfo(
         1310  +static void fts3GetMatchinfo(
         1311  +  sqlite3_context *pCtx,        /* Return results here */
  1133   1312     Fts3Cursor *pCsr,               /* FTS3 Cursor object */
  1134   1313     const char *zArg                /* Second argument to matchinfo() function */
  1135   1314   ){
  1136   1315     MatchInfo sInfo;
  1137   1316     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  1138   1317     int rc = SQLITE_OK;
  1139   1318     int bGlobal = 0;                /* Collect 'global' stats as well as local */
  1140   1319   
         1320  +  u32 *aOut = 0;
         1321  +  void (*xDestroyOut)(void*) = 0;
         1322  +
  1141   1323     memset(&sInfo, 0, sizeof(MatchInfo));
  1142   1324     sInfo.pCursor = pCsr;
  1143   1325     sInfo.nCol = pTab->nColumn;
  1144   1326   
  1145   1327     /* If there is cached matchinfo() data, but the format string for the 
  1146   1328     ** cache does not match the format string for this request, discard 
  1147   1329     ** the cached data. */
  1148         -  if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
  1149         -    assert( pCsr->aMatchinfo );
  1150         -    sqlite3_free(pCsr->aMatchinfo);
  1151         -    pCsr->zMatchinfo = 0;
  1152         -    pCsr->aMatchinfo = 0;
         1330  +  if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
         1331  +    sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
         1332  +    pCsr->pMIBuffer = 0;
  1153   1333     }
  1154   1334   
  1155         -  /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
         1335  +  /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
  1156   1336     ** matchinfo function has been called for this query. In this case 
  1157   1337     ** allocate the array used to accumulate the matchinfo data and
  1158   1338     ** initialize those elements that are constant for every row.
  1159   1339     */
  1160         -  if( pCsr->aMatchinfo==0 ){
         1340  +  if( pCsr->pMIBuffer==0 ){
  1161   1341       int nMatchinfo = 0;           /* Number of u32 elements in match-info */
  1162         -    int nArg;                     /* Bytes in zArg */
  1163   1342       int i;                        /* Used to iterate through zArg */
  1164   1343   
  1165   1344       /* Determine the number of phrases in the query */
  1166   1345       pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
  1167   1346       sInfo.nPhrase = pCsr->nPhrase;
  1168   1347   
  1169   1348       /* Determine the number of integers in the buffer returned by this call. */
  1170   1349       for(i=0; zArg[i]; i++){
         1350  +      char *zErr = 0;
         1351  +      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
         1352  +        sqlite3_result_error(pCtx, zErr, -1);
         1353  +        sqlite3_free(zErr);
         1354  +        return;
         1355  +      }
  1171   1356         nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
  1172   1357       }
  1173   1358   
  1174   1359       /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
  1175         -    nArg = (int)strlen(zArg);
  1176         -    pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
  1177         -    if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
         1360  +    pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
         1361  +    if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
  1178   1362   
  1179         -    pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
  1180         -    pCsr->nMatchinfo = nMatchinfo;
  1181         -    memcpy(pCsr->zMatchinfo, zArg, nArg+1);
  1182         -    memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
  1183   1363       pCsr->isMatchinfoNeeded = 1;
  1184   1364       bGlobal = 1;
  1185   1365     }
  1186   1366   
  1187         -  sInfo.aMatchinfo = pCsr->aMatchinfo;
  1188         -  sInfo.nPhrase = pCsr->nPhrase;
  1189         -  if( pCsr->isMatchinfoNeeded ){
         1367  +  if( rc==SQLITE_OK ){
         1368  +    xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
         1369  +    if( xDestroyOut==0 ){
         1370  +      rc = SQLITE_NOMEM;
         1371  +    }
         1372  +  }
         1373  +
         1374  +  if( rc==SQLITE_OK ){
         1375  +    sInfo.aMatchinfo = aOut;
         1376  +    sInfo.nPhrase = pCsr->nPhrase;
  1190   1377       rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
  1191         -    pCsr->isMatchinfoNeeded = 0;
         1378  +    if( bGlobal ){
         1379  +      fts3MIBufferSetGlobal(pCsr->pMIBuffer);
         1380  +    }
  1192   1381     }
  1193   1382   
  1194         -  return rc;
         1383  +  if( rc!=SQLITE_OK ){
         1384  +    sqlite3_result_error_code(pCtx, rc);
         1385  +    if( xDestroyOut ) xDestroyOut(aOut);
         1386  +  }else{
         1387  +    int n = pCsr->pMIBuffer->nElem * sizeof(u32);
         1388  +    sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
         1389  +  }
  1195   1390   }
  1196   1391   
  1197   1392   /*
  1198   1393   ** Implementation of snippet() function.
  1199   1394   */
  1200   1395   void sqlite3Fts3Snippet(
  1201   1396     sqlite3_context *pCtx,          /* SQLite function call context */
................................................................................
  1393   1588   
  1394   1589       /* Initialize the contents of sCtx.aTerm[] for column iCol. There is 
  1395   1590       ** no way that this operation can fail, so the return code from
  1396   1591       ** fts3ExprIterate() can be discarded.
  1397   1592       */
  1398   1593       sCtx.iCol = iCol;
  1399   1594       sCtx.iTerm = 0;
  1400         -    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
         1595  +    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
  1401   1596   
  1402   1597       /* Retreive the text stored in column iCol. If an SQL NULL is stored 
  1403   1598       ** in column iCol, jump immediately to the next iteration of the loop.
  1404   1599       ** If an OOM occurs while retrieving the data (this can happen if SQLite
  1405   1600       ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM 
  1406   1601       ** to the caller. 
  1407   1602       */
................................................................................
  1485   1680   */
  1486   1681   void sqlite3Fts3Matchinfo(
  1487   1682     sqlite3_context *pContext,      /* Function call context */
  1488   1683     Fts3Cursor *pCsr,               /* FTS3 table cursor */
  1489   1684     const char *zArg                /* Second arg to matchinfo() function */
  1490   1685   ){
  1491   1686     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  1492         -  int rc;
  1493         -  int i;
  1494   1687     const char *zFormat;
  1495   1688   
  1496   1689     if( zArg ){
  1497         -    for(i=0; zArg[i]; i++){
  1498         -      char *zErr = 0;
  1499         -      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
  1500         -        sqlite3_result_error(pContext, zErr, -1);
  1501         -        sqlite3_free(zErr);
  1502         -        return;
  1503         -      }
  1504         -    }
  1505   1690       zFormat = zArg;
  1506   1691     }else{
  1507   1692       zFormat = FTS3_MATCHINFO_DEFAULT;
  1508   1693     }
  1509   1694   
  1510   1695     if( !pCsr->pExpr ){
  1511   1696       sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
  1512   1697       return;
  1513         -  }
  1514         -
  1515         -  /* Retrieve matchinfo() data. */
  1516         -  rc = fts3GetMatchinfo(pCsr, zFormat);
  1517         -  sqlite3Fts3SegmentsClose(pTab);
  1518         -
  1519         -  if( rc!=SQLITE_OK ){
  1520         -    sqlite3_result_error_code(pContext, rc);
  1521   1698     }else{
  1522         -    int n = pCsr->nMatchinfo * sizeof(u32);
  1523         -    sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
         1699  +    /* Retrieve matchinfo() data. */
         1700  +    fts3GetMatchinfo(pContext, pCsr, zFormat);
         1701  +    sqlite3Fts3SegmentsClose(pTab);
  1524   1702     }
  1525   1703   }
  1526   1704   
  1527   1705   #endif

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 ext/rtree/rtree9.test.

    83     83   
    84     84   
    85     85   #-------------------------------------------------------------------------
    86     86   # Test the example 2d "circle" geometry callback.
    87     87   #
    88     88   register_circle_geom db
    89     89   
    90         -breakpoint
    91     90   do_execsql_test rtree9-5.1 {
    92     91     CREATE VIRTUAL TABLE rt2 USING rtree(id, xmin, xmax, ymin, ymax);
    93     92   
    94     93     INSERT INTO rt2 VALUES(1,    1,   2,  1,  2);
    95     94     INSERT INTO rt2 VALUES(2,    1,   2, -2, -1);
    96     95     INSERT INTO rt2 VALUES(3,    -2, -1, -2, -1);
    97     96     INSERT INTO rt2 VALUES(4,    -2, -1,  1,  2);

Changes to main.mk.

    49     49   TCCX += -I$(TOP)/ext/async -I$(TOP)/ext/userauth
    50     50   
    51     51   # Object files for the SQLite library.
    52     52   #
    53     53   LIBOBJ+= vdbe.o parse.o \
    54     54            alter.o analyze.o attach.o auth.o \
    55     55            backup.o bitvec.o btmutex.o btree.o build.o \
    56         -         callback.o complete.o ctime.o date.o delete.o expr.o fault.o fkey.o \
           56  +         callback.o complete.o ctime.o date.o dbstat.o delete.o expr.o fault.o fkey.o \
    57     57            fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    58     58            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
    59     59            fts3_tokenize_vtab.o \
    60     60   	 fts3_unicode.o fts3_unicode2.o \
    61     61            fts3_write.o func.o global.o hash.o \
    62     62            icu.o insert.o journal.o legacy.o loadext.o \
    63     63            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
................................................................................
    87     87     $(TOP)/src/btree.h \
    88     88     $(TOP)/src/btreeInt.h \
    89     89     $(TOP)/src/build.c \
    90     90     $(TOP)/src/callback.c \
    91     91     $(TOP)/src/complete.c \
    92     92     $(TOP)/src/ctime.c \
    93     93     $(TOP)/src/date.c \
           94  +  $(TOP)/src/dbstat.c \
    94     95     $(TOP)/src/delete.c \
    95     96     $(TOP)/src/expr.c \
    96     97     $(TOP)/src/fault.c \
    97     98     $(TOP)/src/fkey.c \
    98     99     $(TOP)/src/func.c \
    99    100     $(TOP)/src/global.c \
   100    101     $(TOP)/src/hash.c \
................................................................................
   270    271     $(TOP)/src/test_onefile.c \
   271    272     $(TOP)/src/test_osinst.c \
   272    273     $(TOP)/src/test_pcache.c \
   273    274     $(TOP)/src/test_quota.c \
   274    275     $(TOP)/src/test_rtree.c \
   275    276     $(TOP)/src/test_schema.c \
   276    277     $(TOP)/src/test_server.c \
   277         -  $(TOP)/src/test_stat.c \
   278    278     $(TOP)/src/test_sqllog.c \
   279    279     $(TOP)/src/test_superlock.c \
   280    280     $(TOP)/src/test_syscall.c \
   281    281     $(TOP)/src/test_tclvar.c \
   282    282     $(TOP)/src/test_thread.c \
   283    283     $(TOP)/src/test_vfs.c \
   284    284     $(TOP)/src/test_wsd.c
................................................................................
   306    306   
   307    307   TESTSRC2 = \
   308    308     $(TOP)/src/attach.c \
   309    309     $(TOP)/src/backup.c \
   310    310     $(TOP)/src/btree.c \
   311    311     $(TOP)/src/build.c \
   312    312     $(TOP)/src/date.c \
          313  +  $(TOP)/src/dbstat.c \
   313    314     $(TOP)/src/expr.c \
   314    315     $(TOP)/src/func.c \
   315    316     $(TOP)/src/insert.c \
   316    317     $(TOP)/src/wal.c \
   317    318     $(TOP)/src/main.c \
   318    319     $(TOP)/src/mem5.c \
   319    320     $(TOP)/src/os.c \
................................................................................
   389    390     $(TOP)/ext/fts3/fts3_tokenizer.h
   390    391   EXTHDR += \
   391    392     $(TOP)/ext/rtree/rtree.h
   392    393   EXTHDR += \
   393    394     $(TOP)/ext/icu/sqliteicu.h
   394    395   EXTHDR += \
   395    396     $(TOP)/ext/userauth/sqlite3userauth.h
          397  +
          398  +# executables needed for testing
          399  +#
          400  +TESTPROGS = \
          401  +  testfixture$(EXE) \
          402  +  sqlite3$(EXE) \
          403  +  sqlite3_analyzer$(EXE) \
          404  +  sqldiff$(EXE)
   396    405   
   397    406   # This is the default Makefile target.  The objects listed here
   398    407   # are what get build when you type just "make" with no arguments.
   399    408   #
   400    409   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
   401    410   
   402    411   libsqlite3.a:	$(LIBOBJ)
................................................................................
   604    613   
   605    614   # Rules for building test programs and for running tests
   606    615   #
   607    616   tclsqlite3:	$(TOP)/src/tclsqlite.c libsqlite3.a
   608    617   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite3 \
   609    618   		$(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB)
   610    619   
   611         -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
          620  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
   612    621   	echo "#define TCLSH 2" > $@
   613         -	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@
          622  +	echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@
          623  +	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
   614    624   	echo "static const char *tclsh_main_loop(void){" >> $@
   615    625   	echo "static const char *zMainloop = " >> $@
   616    626   	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
   617    627   	echo "; return zMainloop; }" >> $@
   618    628   
   619    629   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   620    630   	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
................................................................................
   636    646   
   637    647   fts3-testfixture$(EXE): sqlite3.c fts3amal.c $(TESTSRC) $(TOP)/src/tclsqlite.c
   638    648   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS)                  \
   639    649   	-DSQLITE_ENABLE_FTS3=1                                               \
   640    650   		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c fts3amal.c       \
   641    651   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   642    652   
   643         -fulltest:	testfixture$(EXE) sqlite3$(EXE)
          653  +fulltest:	$(TESTPROGS) fuzztest
   644    654   	./testfixture$(EXE) $(TOP)/test/all.test
   645    655   
   646         -soaktest:	testfixture$(EXE) sqlite3$(EXE)
          656  +soaktest:	$(TESTPROGS) fuzzoomtest
   647    657   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
   648    658   
   649         -fulltestonly:	testfixture$(EXE) sqlite3$(EXE)
          659  +fulltestonly:	$(TESTPROGS) fuzztest
   650    660   	./testfixture$(EXE) $(TOP)/test/full.test
   651    661   
   652    662   queryplantest:	testfixture$(EXE) sqlite3$(EXE)
   653    663   	./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner
   654    664   
   655         -test:	testfixture$(EXE) sqlite3$(EXE)
          665  +fuzztest:	fuzzershell$(EXE)
          666  +	./fuzzershell$(EXE) $(TOP)/test/fuzzdata1.txt $(TOP)/test/fuzzdata2.txt
          667  +
          668  +fuzzoomtest:	fuzzershell$(EXE)
          669  +	./fuzzershell$(EXE) -f $(TOP)/test/fuzzdata1.txt --oom
          670  +
          671  +test:	$(TESTPROGS) fuzztest
   656    672   	./testfixture$(EXE) $(TOP)/test/veryquick.test
          673  +
          674  +# Run a test using valgrind.  This can take a really long time
          675  +# because valgrind is so much slower than a native machine.
          676  +#
          677  +valgrindtest:	$(TESTPROGS) fuzzershell$(EXE)
          678  +	valgrind -v ./fuzzershell$(EXE) -f $(TOP)/test/fuzzdata1.txt
          679  +	OMIT_MISUSE=1 valgrind -v ./testfixture$(EXE) $(TOP)/test/permutations.test valgrind
          680  +
          681  +# A very fast test that checks basic sanity.  The name comes from
          682  +# the 60s-era electronics testing:  "Turn it on and see if smoke
          683  +# comes out."
          684  +#
          685  +smoketest:	$(TESTPROGS) fuzzershell$(EXE)
          686  +	./testfixture$(EXE) $(TOP)/test/main.test
   657    687   
   658    688   # The next two rules are used to support the "threadtest" target. Building
   659    689   # threadtest runs a few thread-safety tests that are implemented in C. This
   660    690   # target is invoked by the releasetest.tcl script.
   661    691   # 
   662    692   THREADTEST3_SRC = $(TOP)/test/threadtest3.c    \
   663    693                     $(TOP)/test/tt3_checkpoint.c \
................................................................................
   761    791   	rm -f wordcount wordcount.exe
   762    792   	rm -f sqlite3.c sqlite3-*.c fts?amal.c tclsqlite3.c
   763    793   	rm -f sqlite3rc.h
   764    794   	rm -f shell.c sqlite3ext.h
   765    795   	rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c
   766    796   	rm -f sqlite-*-output.vsix
   767    797   	rm -f mptester mptester.exe
          798  +	rm -f fuzzershell fuzzershell.exe
          799  +	rm -f sqldiff sqldiff.exe

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     }

Changes to src/ctime.c.

    70     70     "ENABLE_ATOMIC_WRITE",
    71     71   #endif
    72     72   #if SQLITE_ENABLE_CEROD
    73     73     "ENABLE_CEROD",
    74     74   #endif
    75     75   #if SQLITE_ENABLE_COLUMN_METADATA
    76     76     "ENABLE_COLUMN_METADATA",
           77  +#endif
           78  +#if SQLITE_ENABLE_DBSTAT_VTAB
           79  +  "ENABLE_DBSTAT_VTAB",
    77     80   #endif
    78     81   #if SQLITE_ENABLE_EXPENSIVE_ASSERT
    79     82     "ENABLE_EXPENSIVE_ASSERT",
    80     83   #endif
    81     84   #if SQLITE_ENABLE_FTS1
    82     85     "ENABLE_FTS1",
    83     86   #endif

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  +#include "sqliteInt.h"   /* Requires access to internal data structures */
           22  +#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
           23  +    && !defined(SQLITE_OMIT_VIRTUALTABLE)
    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 '/'.
................................................................................
   120    118     i64 iOffset;                    /* Value of 'pgOffset' column */
   121    119     int szPage;                     /* Value of 'pgSize' column */
   122    120   };
   123    121   
   124    122   struct StatTable {
   125    123     sqlite3_vtab base;
   126    124     sqlite3 *db;
          125  +  int iDb;                        /* Index of database to analyze */
   127    126   };
   128    127   
   129    128   #ifndef get2byte
   130    129   # define get2byte(x)   ((x)[0]<<8 | (x)[1])
   131    130   #endif
   132    131   
   133    132   /*
................................................................................
   136    135   static int statConnect(
   137    136     sqlite3 *db,
   138    137     void *pAux,
   139    138     int argc, const char *const*argv,
   140    139     sqlite3_vtab **ppVtab,
   141    140     char **pzErr
   142    141   ){
   143         -  StatTable *pTab;
          142  +  StatTable *pTab = 0;
          143  +  int rc = SQLITE_OK;
          144  +  int iDb;
   144    145   
   145         -  pTab = (StatTable *)sqlite3_malloc(sizeof(StatTable));
   146         -  memset(pTab, 0, sizeof(StatTable));
   147         -  pTab->db = db;
          146  +  if( argc>=4 ){
          147  +    iDb = sqlite3FindDbName(db, argv[3]);
          148  +    if( iDb<0 ){
          149  +      *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
          150  +      return SQLITE_ERROR;
          151  +    }
          152  +  }else{
          153  +    iDb = 0;
          154  +  }
          155  +  rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
          156  +  if( rc==SQLITE_OK ){
          157  +    pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
          158  +    if( pTab==0 ) rc = SQLITE_NOMEM;
          159  +  }
   148    160   
   149         -  sqlite3_declare_vtab(db, VTAB_SCHEMA);
   150         -  *ppVtab = &pTab->base;
   151         -  return SQLITE_OK;
          161  +  assert( rc==SQLITE_OK || pTab==0 );
          162  +  if( rc==SQLITE_OK ){
          163  +    memset(pTab, 0, sizeof(StatTable));
          164  +    pTab->db = db;
          165  +    pTab->iDb = iDb;
          166  +  }
          167  +
          168  +  *ppVtab = (sqlite3_vtab*)pTab;
          169  +  return rc;
   152    170   }
   153    171   
   154    172   /*
   155    173   ** Disconnect from or destroy a statvfs virtual table.
   156    174   */
   157    175   static int statDisconnect(sqlite3_vtab *pVtab){
   158    176     sqlite3_free(pVtab);
................................................................................
   191    209   ** Open a new statvfs cursor.
   192    210   */
   193    211   static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
   194    212     StatTable *pTab = (StatTable *)pVTab;
   195    213     StatCursor *pCsr;
   196    214     int rc;
   197    215   
   198         -  pCsr = (StatCursor *)sqlite3_malloc(sizeof(StatCursor));
   199         -  memset(pCsr, 0, sizeof(StatCursor));
   200         -  pCsr->base.pVtab = pVTab;
          216  +  pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
          217  +  if( pCsr==0 ){
          218  +    rc = SQLITE_NOMEM;
          219  +  }else{
          220  +    char *zSql;
          221  +    memset(pCsr, 0, sizeof(StatCursor));
          222  +    pCsr->base.pVtab = pVTab;
   201    223   
   202         -  rc = sqlite3_prepare_v2(pTab->db, 
   203         -      "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
   204         -      "  UNION ALL  "
   205         -      "SELECT name, rootpage, type FROM sqlite_master WHERE rootpage!=0"
   206         -      "  ORDER BY name", -1,
   207         -      &pCsr->pStmt, 0
   208         -  );
   209         -  if( rc!=SQLITE_OK ){
   210         -    sqlite3_free(pCsr);
   211         -    return rc;
          224  +    zSql = sqlite3_mprintf(
          225  +        "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
          226  +        "  UNION ALL  "
          227  +        "SELECT name, rootpage, type"
          228  +        "  FROM \"%w\".sqlite_master WHERE rootpage!=0"
          229  +        "  ORDER BY name", pTab->db->aDb[pTab->iDb].zName);
          230  +    if( zSql==0 ){
          231  +      rc = SQLITE_NOMEM;
          232  +    }else{
          233  +      rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
          234  +      sqlite3_free(zSql);
          235  +    }
          236  +    if( rc!=SQLITE_OK ){
          237  +      sqlite3_free(pCsr);
          238  +      pCsr = 0;
          239  +    }
   212    240     }
   213    241   
   214    242     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
   215         -  return SQLITE_OK;
          243  +  return rc;
   216    244   }
   217    245   
   218    246   static void statClearPage(StatPage *p){
   219    247     int i;
   220         -  for(i=0; i<p->nCell; i++){
   221         -    sqlite3_free(p->aCell[i].aOvfl);
          248  +  if( p->aCell ){
          249  +    for(i=0; i<p->nCell; i++){
          250  +      sqlite3_free(p->aCell[i].aOvfl);
          251  +    }
          252  +    sqlite3_free(p->aCell);
   222    253     }
   223    254     sqlite3PagerUnref(p->pPg);
   224         -  sqlite3_free(p->aCell);
   225    255     sqlite3_free(p->zPath);
   226    256     memset(p, 0, sizeof(StatPage));
   227    257   }
   228    258   
   229    259   static void statResetCsr(StatCursor *pCsr){
   230    260     int i;
   231    261     sqlite3_reset(pCsr->pStmt);
................................................................................
   302    332     if( p->nCell ){
   303    333       int i;                        /* Used to iterate through cells */
   304    334       int nUsable;                  /* Usable bytes per page */
   305    335   
   306    336       sqlite3BtreeEnter(pBt);
   307    337       nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
   308    338       sqlite3BtreeLeave(pBt);
   309         -    p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell));
          339  +    p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
          340  +    if( p->aCell==0 ) return SQLITE_NOMEM;
   310    341       memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
   311    342   
   312    343       for(i=0; i<p->nCell; i++){
   313    344         StatCell *pCell = &p->aCell[i];
   314    345   
   315    346         iOff = get2byte(&aData[nHdr+i*2]);
   316    347         if( !isLeaf ){
................................................................................
   334    365           assert( nPayload>=(u32)nLocal );
   335    366           assert( nLocal<=(nUsable-35) );
   336    367           if( nPayload>(u32)nLocal ){
   337    368             int j;
   338    369             int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
   339    370             pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
   340    371             pCell->nOvfl = nOvfl;
   341         -          pCell->aOvfl = sqlite3_malloc(sizeof(u32)*nOvfl);
          372  +          pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
          373  +          if( pCell->aOvfl==0 ) return SQLITE_NOMEM;
   342    374             pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
   343    375             for(j=1; j<nOvfl; j++){
   344    376               int rc;
   345    377               u32 iPrev = pCell->aOvfl[j-1];
   346    378               DbPage *pPg = 0;
   347    379               rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
   348    380               if( rc!=SQLITE_OK ){
................................................................................
   362    394   
   363    395   /*
   364    396   ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
   365    397   ** the current value of pCsr->iPageno.
   366    398   */
   367    399   static void statSizeAndOffset(StatCursor *pCsr){
   368    400     StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
   369         -  Btree *pBt = pTab->db->aDb[0].pBt;
          401  +  Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
   370    402     Pager *pPager = sqlite3BtreePager(pBt);
   371    403     sqlite3_file *fd;
   372    404     sqlite3_int64 x[2];
   373    405   
   374    406     /* The default page size and offset */
   375    407     pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
   376    408     pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
   377    409   
   378    410     /* If connected to a ZIPVFS backend, override the page size and
   379    411     ** offset with actual values obtained from ZIPVFS.
   380    412     */
   381    413     fd = sqlite3PagerFile(pPager);
   382    414     x[0] = pCsr->iPageno;
   383         -  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
          415  +  if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
   384    416       pCsr->iOffset = x[0];
   385    417       pCsr->szPage = (int)x[1];
   386    418     }
   387    419   }
   388    420   
   389    421   /*
   390    422   ** Move a statvfs cursor to the next entry in the file.
   391    423   */
   392    424   static int statNext(sqlite3_vtab_cursor *pCursor){
   393    425     int rc;
   394    426     int nPayload;
          427  +  char *z;
   395    428     StatCursor *pCsr = (StatCursor *)pCursor;
   396    429     StatTable *pTab = (StatTable *)pCursor->pVtab;
   397         -  Btree *pBt = pTab->db->aDb[0].pBt;
          430  +  Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
   398    431     Pager *pPager = sqlite3BtreePager(pBt);
   399    432   
   400    433     sqlite3_free(pCsr->zPath);
   401    434     pCsr->zPath = 0;
   402    435   
   403    436   statNextRestart:
   404    437     if( pCsr->aPage[0].pPg==0 ){
................................................................................
   410    443         if( nPage==0 ){
   411    444           pCsr->isEof = 1;
   412    445           return sqlite3_reset(pCsr->pStmt);
   413    446         }
   414    447         rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg);
   415    448         pCsr->aPage[0].iPgno = iRoot;
   416    449         pCsr->aPage[0].iCell = 0;
   417         -      pCsr->aPage[0].zPath = sqlite3_mprintf("/");
          450  +      pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
   418    451         pCsr->iPage = 0;
          452  +      if( z==0 ) rc = SQLITE_NOMEM;
   419    453       }else{
   420    454         pCsr->isEof = 1;
   421    455         return sqlite3_reset(pCsr->pStmt);
   422    456       }
   423    457     }else{
   424    458   
   425    459       /* Page p itself has already been visited. */
................................................................................
   434    468                           sqlite3BtreeGetReserveNoMutex(pBt);
   435    469           sqlite3BtreeLeave(pBt);
   436    470           pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
   437    471           pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
   438    472           pCsr->zPagetype = "overflow";
   439    473           pCsr->nCell = 0;
   440    474           pCsr->nMxPayload = 0;
   441         -        pCsr->zPath = sqlite3_mprintf(
          475  +        pCsr->zPath = z = sqlite3_mprintf(
   442    476               "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
   443    477           );
   444    478           if( pCell->iOvfl<pCell->nOvfl-1 ){
   445    479             pCsr->nUnused = 0;
   446    480             pCsr->nPayload = nUsable - 4;
   447    481           }else{
   448    482             pCsr->nPayload = pCell->nLastOvfl;
   449    483             pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
   450    484           }
   451    485           pCell->iOvfl++;
   452    486           statSizeAndOffset(pCsr);
   453         -        return SQLITE_OK;
          487  +        return z==0 ? SQLITE_NOMEM : SQLITE_OK;
   454    488         }
   455    489         if( p->iRightChildPg ) break;
   456    490         p->iCell++;
   457    491       }
   458    492   
   459    493       if( !p->iRightChildPg || p->iCell>p->nCell ){
   460    494         statClearPage(p);
................................................................................
   468    502       if( p->iCell==p->nCell ){
   469    503         p[1].iPgno = p->iRightChildPg;
   470    504       }else{
   471    505         p[1].iPgno = p->aCell[p->iCell].iChildPg;
   472    506       }
   473    507       rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg);
   474    508       p[1].iCell = 0;
   475         -    p[1].zPath = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
          509  +    p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
   476    510       p->iCell++;
          511  +    if( z==0 ) rc = SQLITE_NOMEM;
   477    512     }
   478    513   
   479    514   
   480    515     /* Populate the StatCursor fields with the values to be returned
   481    516     ** by the xColumn() and xRowid() methods.
   482    517     */
   483    518     if( rc==SQLITE_OK ){
   484    519       int i;
   485    520       StatPage *p = &pCsr->aPage[pCsr->iPage];
   486    521       pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
   487    522       pCsr->iPageno = p->iPgno;
   488    523   
   489         -    statDecodePage(pBt, p);
   490         -    statSizeAndOffset(pCsr);
          524  +    rc = statDecodePage(pBt, p);
          525  +    if( rc==SQLITE_OK ){
          526  +      statSizeAndOffset(pCsr);
   491    527   
   492         -    switch( p->flags ){
   493         -      case 0x05:             /* table internal */
   494         -      case 0x02:             /* index internal */
   495         -        pCsr->zPagetype = "internal";
   496         -        break;
   497         -      case 0x0D:             /* table leaf */
   498         -      case 0x0A:             /* index leaf */
   499         -        pCsr->zPagetype = "leaf";
   500         -        break;
   501         -      default:
   502         -        pCsr->zPagetype = "corrupted";
   503         -        break;
   504         -    }
   505         -    pCsr->nCell = p->nCell;
   506         -    pCsr->nUnused = p->nUnused;
   507         -    pCsr->nMxPayload = p->nMxPayload;
   508         -    pCsr->zPath = sqlite3_mprintf("%s", p->zPath);
   509         -    nPayload = 0;
   510         -    for(i=0; i<p->nCell; i++){
   511         -      nPayload += p->aCell[i].nLocal;
   512         -    }
   513         -    pCsr->nPayload = nPayload;
          528  +      switch( p->flags ){
          529  +        case 0x05:             /* table internal */
          530  +        case 0x02:             /* index internal */
          531  +          pCsr->zPagetype = "internal";
          532  +          break;
          533  +        case 0x0D:             /* table leaf */
          534  +        case 0x0A:             /* index leaf */
          535  +          pCsr->zPagetype = "leaf";
          536  +          break;
          537  +        default:
          538  +          pCsr->zPagetype = "corrupted";
          539  +          break;
          540  +      }
          541  +      pCsr->nCell = p->nCell;
          542  +      pCsr->nUnused = p->nUnused;
          543  +      pCsr->nMxPayload = p->nMxPayload;
          544  +      pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
          545  +      if( z==0 ) rc = SQLITE_NOMEM;
          546  +      nPayload = 0;
          547  +      for(i=0; i<p->nCell; i++){
          548  +        nPayload += p->aCell[i].nLocal;
          549  +      }
          550  +      pCsr->nPayload = nPayload;
          551  +    }
   514    552     }
   515    553   
   516    554     return rc;
   517    555   }
   518    556   
   519    557   static int statEof(sqlite3_vtab_cursor *pCursor){
   520    558     StatCursor *pCsr = (StatCursor *)pCursor;
................................................................................
   536    574     sqlite3_vtab_cursor *pCursor, 
   537    575     sqlite3_context *ctx, 
   538    576     int i
   539    577   ){
   540    578     StatCursor *pCsr = (StatCursor *)pCursor;
   541    579     switch( i ){
   542    580       case 0:            /* name */
   543         -      sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_STATIC);
          581  +      sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
   544    582         break;
   545    583       case 1:            /* path */
   546    584         sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
   547    585         break;
   548    586       case 2:            /* pageno */
   549    587         sqlite3_result_int64(ctx, pCsr->iPageno);
   550    588         break;
................................................................................
   562    600         break;
   563    601       case 7:            /* mx_payload */
   564    602         sqlite3_result_int(ctx, pCsr->nMxPayload);
   565    603         break;
   566    604       case 8:            /* pgoffset */
   567    605         sqlite3_result_int64(ctx, pCsr->iOffset);
   568    606         break;
   569         -    case 9:            /* pgsize */
          607  +    default:           /* pgsize */
          608  +      assert( i==9 );
   570    609         sqlite3_result_int(ctx, pCsr->szPage);
   571    610         break;
   572    611     }
   573    612     return SQLITE_OK;
   574    613   }
   575    614   
   576    615   static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
   577    616     StatCursor *pCsr = (StatCursor *)pCursor;
   578    617     *pRowid = pCsr->iPageno;
   579    618     return SQLITE_OK;
   580    619   }
   581    620   
   582         -int sqlite3_dbstat_register(sqlite3 *db){
          621  +/*
          622  +** Invoke this routine to register the "dbstat" virtual table module
          623  +*/
          624  +int sqlite3DbstatRegister(sqlite3 *db){
   583    625     static sqlite3_module dbstat_module = {
   584    626       0,                            /* iVersion */
   585    627       statConnect,                  /* xCreate */
   586    628       statConnect,                  /* xConnect */
   587    629       statBestIndex,                /* xBestIndex */
   588    630       statDisconnect,               /* xDisconnect */
   589    631       statDisconnect,               /* xDestroy */
................................................................................
   598    640       0,                            /* xBegin */
   599    641       0,                            /* xSync */
   600    642       0,                            /* xCommit */
   601    643       0,                            /* xRollback */
   602    644       0,                            /* xFindMethod */
   603    645       0,                            /* xRename */
   604    646     };
   605         -  sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
   606         -  return SQLITE_OK;
          647  +  return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
   607    648   }
   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 */
          649  +#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
          650  +int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
          651  +#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;
................................................................................
  2415   2427               || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
  2416   2428      && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
  2417   2429     ){
  2418   2430       char *zOpt;
  2419   2431       int eState;                   /* Parser state when parsing URI */
  2420   2432       int iIn;                      /* Input character index */
  2421   2433       int iOut = 0;                 /* Output character index */
  2422         -    int nByte = nUri+2;           /* Bytes of space to allocate */
         2434  +    u64 nByte = nUri+2;           /* Bytes of space to allocate */
  2423   2435   
  2424   2436       /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
  2425   2437       ** method that there may be extra parameters following the file-name.  */
  2426   2438       flags |= SQLITE_OPEN_URI;
  2427   2439   
  2428   2440       for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
  2429         -    zFile = sqlite3_malloc(nByte);
         2441  +    zFile = sqlite3_malloc64(nByte);
  2430   2442       if( !zFile ) return SQLITE_NOMEM;
  2431   2443   
  2432   2444       iIn = 5;
  2433   2445   #ifdef SQLITE_ALLOW_URI_AUTHORITY
  2434   2446       if( strncmp(zUri+5, "///", 3)==0 ){
  2435   2447         iIn = 7;
  2436   2448         /* The following condition causes URIs with five leading / characters
................................................................................
  2588   2600           }
  2589   2601         }
  2590   2602   
  2591   2603         zOpt = &zVal[nVal+1];
  2592   2604       }
  2593   2605   
  2594   2606     }else{
  2595         -    zFile = sqlite3_malloc(nUri+2);
         2607  +    zFile = sqlite3_malloc64(nUri+2);
  2596   2608       if( !zFile ) return SQLITE_NOMEM;
  2597   2609       memcpy(zFile, zUri, nUri);
  2598   2610       zFile[nUri] = '\0';
  2599   2611       zFile[nUri+1] = '\0';
  2600   2612       flags &= ~SQLITE_OPEN_URI;
  2601   2613     }
  2602   2614   
................................................................................
  2859   2871   #endif
  2860   2872   
  2861   2873   #ifdef SQLITE_ENABLE_RTREE
  2862   2874     if( !db->mallocFailed && rc==SQLITE_OK){
  2863   2875       rc = sqlite3RtreeInit(db);
  2864   2876     }
  2865   2877   #endif
         2878  +
         2879  +#ifdef SQLITE_ENABLE_DBSTAT_VTAB
         2880  +  if( !db->mallocFailed && rc==SQLITE_OK){
         2881  +    rc = sqlite3DbstatRegister(db);
         2882  +  }
         2883  +#endif
  2866   2884   
  2867   2885     /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  2868   2886     ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  2869   2887     ** mode.  Doing nothing at all also makes NORMAL the default.
  2870   2888     */
  2871   2889   #ifdef SQLITE_DEFAULT_LOCKING_MODE
  2872   2890     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;

Changes to src/malloc.c.

   222    222     memset(&mem0, 0, sizeof(mem0));
   223    223   }
   224    224   
   225    225   /*
   226    226   ** Return the amount of memory currently checked out.
   227    227   */
   228    228   sqlite3_int64 sqlite3_memory_used(void){
   229         -  int n, mx;
   230         -  sqlite3_int64 res;
   231         -  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
   232         -  res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
          229  +  sqlite3_int64 res, mx;
          230  +  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
   233    231     return res;
   234    232   }
   235    233   
   236    234   /*
   237    235   ** Return the maximum amount of memory that has ever been
   238    236   ** checked out since either the beginning of this process
   239    237   ** or since the most recent reset.
   240    238   */
   241    239   sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
   242         -  int n, mx;
   243         -  sqlite3_int64 res;
   244         -  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
   245         -  res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
   246         -  return res;
          240  +  sqlite3_int64 res, mx;
          241  +  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
          242  +  return mx;
   247    243   }
   248    244   
   249    245   /*
   250    246   ** Trigger the alarm 
   251    247   */
   252    248   static void sqlite3MallocAlarm(int nByte){
   253    249     void (*xCallback)(void*,sqlite3_int64,int);

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

Changes to src/resolve.c.

    95     95     Expr *pOrig;           /* The iCol-th column of the result set */
    96     96     Expr *pDup;            /* Copy of pOrig */
    97     97     sqlite3 *db;           /* The database connection */
    98     98   
    99     99     assert( iCol>=0 && iCol<pEList->nExpr );
   100    100     pOrig = pEList->a[iCol].pExpr;
   101    101     assert( pOrig!=0 );
   102         -  assert( (pOrig->flags & EP_Resolved)!=0 || zType[0]==0 );
   103    102     db = pParse->db;
   104    103     pDup = sqlite3ExprDup(db, pOrig, 0);
   105    104     if( pDup==0 ) return;
   106    105     if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
   107    106       incrAggFunctionDepth(pDup, nSubquery);
   108    107       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
   109    108       if( pDup==0 ) return;

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.

   986    986         }
   987    987         setTextMode(p->out);
   988    988         break;
   989    989       }
   990    990       case MODE_Insert: {
   991    991         p->cnt++;
   992    992         if( azArg==0 ) break;
   993         -      fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
          993  +      fprintf(p->out,"INSERT INTO %s",p->zDestTable);
          994  +      if( p->showHeader ){
          995  +        fprintf(p->out,"(");
          996  +        for(i=0; i<nArg; i++){
          997  +          char *zSep = i>0 ? ",": "";
          998  +          fprintf(p->out, "%s%s", zSep, azCol[i]);
          999  +        }
         1000  +        fprintf(p->out,")");
         1001  +      }
         1002  +      fprintf(p->out," VALUES(");
   994   1003         for(i=0; i<nArg; i++){
   995   1004           char *zSep = i>0 ? ",": "";
   996   1005           if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
   997   1006             fprintf(p->out,"%sNULL",zSep);
   998   1007           }else if( aiType && aiType[i]==SQLITE_TEXT ){
   999   1008             if( zSep[0] ) fprintf(p->out,"%s",zSep);
  1000   1009             output_quoted_string(p->out, azArg[i]);
................................................................................
  1187   1196   /*
  1188   1197   ** Allocate space and save off current error string.
  1189   1198   */
  1190   1199   static char *save_err_msg(
  1191   1200     sqlite3 *db            /* Database to query */
  1192   1201   ){
  1193   1202     int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
  1194         -  char *zErrMsg = sqlite3_malloc(nErrMsg);
         1203  +  char *zErrMsg = sqlite3_malloc64(nErrMsg);
  1195   1204     if( zErrMsg ){
  1196   1205       memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
  1197   1206     }
  1198   1207     return zErrMsg;
  1199   1208   }
  1200   1209   
  1201   1210   /*
................................................................................
  1424   1433       ** SQL trigger or foreign key.  */
  1425   1434       int p2 = sqlite3_column_int(pSql, 3);
  1426   1435       int p2op = (p2 + (iOp-iAddr));
  1427   1436   
  1428   1437       /* Grow the p->aiIndent array as required */
  1429   1438       if( iOp>=nAlloc ){
  1430   1439         nAlloc += 100;
  1431         -      p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
  1432         -      abYield = (int*)sqlite3_realloc(abYield, nAlloc*sizeof(int));
         1440  +      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
         1441  +      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
  1433   1442       }
  1434   1443       abYield[iOp] = str_in_array(zOp, azYield);
  1435   1444       p->aiIndent[iOp] = 0;
  1436   1445       p->nIndent = iOp+1;
  1437   1446   
  1438   1447       if( str_in_array(zOp, azNext) ){
  1439   1448         for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
................................................................................
  1542   1551         rc = sqlite3_step(pStmt);
  1543   1552         /* if we have a result set... */
  1544   1553         if( SQLITE_ROW == rc ){
  1545   1554           /* if we have a callback... */
  1546   1555           if( xCallback ){
  1547   1556             /* allocate space for col name ptr, value ptr, and type */
  1548   1557             int nCol = sqlite3_column_count(pStmt);
  1549         -          void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
         1558  +          void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
  1550   1559             if( !pData ){
  1551   1560               rc = SQLITE_NOMEM;
  1552   1561             }else{
  1553   1562               char **azCols = (char **)pData;      /* Names of result columns */
  1554   1563               char **azVals = &azCols[nCol];       /* Results */
  1555   1564               int *aiTypes = (int *)&azVals[nCol]; /* Result types */
  1556   1565               int i, x;
................................................................................
  1768   1777   
  1769   1778   /*
  1770   1779   ** Text of a help message
  1771   1780   */
  1772   1781   static char zHelp[] =
  1773   1782     ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
  1774   1783     ".bail on|off           Stop after hitting an error.  Default OFF\n"
         1784  +  ".binary on|off         Turn binary output on or off.  Default OFF\n"
  1775   1785     ".clone NEWDB           Clone data into NEWDB from the existing database\n"
  1776   1786     ".databases             List names and files of attached databases\n"
  1777   1787     ".dbinfo ?DB?           Show status information about the database\n"
  1778   1788     ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
  1779   1789     "                         If TABLE specified, only dump tables matching\n"
  1780   1790     "                         LIKE pattern TABLE.\n"
  1781   1791     ".echo on|off           Turn command echo on or off\n"
................................................................................
  1789   1799     ".import FILE TABLE     Import data from FILE into TABLE\n"
  1790   1800     ".indexes ?TABLE?       Show names of all indexes\n"
  1791   1801     "                         If TABLE specified, only show indexes for tables\n"
  1792   1802     "                         matching LIKE pattern TABLE.\n"
  1793   1803   #ifdef SQLITE_ENABLE_IOTRACE
  1794   1804     ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
  1795   1805   #endif
         1806  +  ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
  1796   1807   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1797   1808     ".load FILE ?ENTRY?     Load an extension library\n"
  1798   1809   #endif
  1799   1810     ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
  1800   1811     ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
  1801   1812     "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
  1802   1813     "                         csv      Comma-separated values\n"
................................................................................
  1858   1869     zName = (const char*)sqlite3_value_text(argv[0]);
  1859   1870     if( zName==0 ) return;
  1860   1871     in = fopen(zName, "rb");
  1861   1872     if( in==0 ) return;
  1862   1873     fseek(in, 0, SEEK_END);
  1863   1874     nIn = ftell(in);
  1864   1875     rewind(in);
  1865         -  pBuf = sqlite3_malloc( nIn );
         1876  +  pBuf = sqlite3_malloc64( nIn );
  1866   1877     if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
  1867   1878       sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
  1868   1879     }else{
  1869   1880       sqlite3_free(pBuf);
  1870   1881     }
  1871   1882     fclose(in);
  1872   1883   }
................................................................................
  1929   1940                               writefileFunc, 0, 0);
  1930   1941     }
  1931   1942   }
  1932   1943   
  1933   1944   /*
  1934   1945   ** Do C-language style dequoting.
  1935   1946   **
         1947  +**    \a    -> alarm
         1948  +**    \b    -> backspace
  1936   1949   **    \t    -> tab
  1937   1950   **    \n    -> newline
         1951  +**    \v    -> vertical tab
         1952  +**    \f    -> form feed
  1938   1953   **    \r    -> carriage return
         1954  +**    \s    -> space
  1939   1955   **    \"    -> "
  1940         -**    \NNN  -> ascii character NNN in octal
         1956  +**    \'    -> '
  1941   1957   **    \\    -> backslash
         1958  +**    \NNN  -> ascii character NNN in octal
  1942   1959   */
  1943   1960   static void resolve_backslashes(char *z){
  1944   1961     int i, j;
  1945   1962     char c;
  1946   1963     while( *z && *z!='\\' ) z++;
  1947   1964     for(i=j=0; (c = z[i])!=0; i++, j++){
  1948   1965       if( c=='\\' && z[i+1]!=0 ){
  1949   1966         c = z[++i];
  1950         -      if( c=='n' ){
  1951         -        c = '\n';
         1967  +      if( c=='a' ){
         1968  +        c = '\a';
         1969  +      }else if( c=='b' ){
         1970  +        c = '\b';
  1952   1971         }else if( c=='t' ){
  1953   1972           c = '\t';
         1973  +      }else if( c=='n' ){
         1974  +        c = '\n';
         1975  +      }else if( c=='v' ){
         1976  +        c = '\v';
         1977  +      }else if( c=='f' ){
         1978  +        c = '\f';
  1954   1979         }else if( c=='r' ){
  1955   1980           c = '\r';
         1981  +      }else if( c=='"' ){
         1982  +        c = '"';
         1983  +      }else if( c=='\'' ){
         1984  +        c = '\'';
  1956   1985         }else if( c=='\\' ){
  1957   1986           c = '\\';
  1958   1987         }else if( c>='0' && c<='7' ){
  1959   1988           c -= '0';
  1960   1989           if( z[i+1]>='0' && z[i+1]<='7' ){
  1961   1990             i++;
  1962   1991             c = (c<<3) + z[i] - '0';
................................................................................
  2118   2147     int cRowSep;        /* The row separator character.  (Usually "\n") */
  2119   2148   };
  2120   2149   
  2121   2150   /* Append a single byte to z[] */
  2122   2151   static void import_append_char(ImportCtx *p, int c){
  2123   2152     if( p->n+1>=p->nAlloc ){
  2124   2153       p->nAlloc += p->nAlloc + 100;
  2125         -    p->z = sqlite3_realloc(p->z, p->nAlloc);
         2154  +    p->z = sqlite3_realloc64(p->z, p->nAlloc);
  2126   2155       if( p->z==0 ){
  2127   2156         fprintf(stderr, "out of memory\n");
  2128   2157         exit(1);
  2129   2158       }
  2130   2159     }
  2131   2160     p->z[p->n++] = (char)c;
  2132   2161   }
  2133   2162   
  2134   2163   /* Read a single field of CSV text.  Compatible with rfc4180 and extended
  2135   2164   ** with the option of having a separator other than ",".
  2136   2165   **
  2137   2166   **   +  Input comes from p->in.
  2138   2167   **   +  Store results in p->z of length p->n.  Space to hold p->z comes
  2139         -**      from sqlite3_malloc().
         2168  +**      from sqlite3_malloc64().
  2140   2169   **   +  Use p->cSep as the column separator.  The default is ",".
  2141   2170   **   +  Use p->rSep as the row separator.  The default is "\n".
  2142   2171   **   +  Keep track of the line number in p->nLine.
  2143   2172   **   +  Store the character that terminates the field in p->cTerm.  Store
  2144   2173   **      EOF on end-of-file.
  2145   2174   **   +  Report syntax errors on stderr
  2146   2175   */
................................................................................
  2206   2235     return p->z;
  2207   2236   }
  2208   2237   
  2209   2238   /* Read a single field of ASCII delimited text.
  2210   2239   **
  2211   2240   **   +  Input comes from p->in.
  2212   2241   **   +  Store results in p->z of length p->n.  Space to hold p->z comes
  2213         -**      from sqlite3_malloc().
         2242  +**      from sqlite3_malloc64().
  2214   2243   **   +  Use p->cSep as the column separator.  The default is "\x1F".
  2215   2244   **   +  Use p->rSep as the row separator.  The default is "\x1E".
  2216   2245   **   +  Keep track of the row number in p->nLine.
  2217   2246   **   +  Store the character that terminates the field in p->cTerm.  Store
  2218   2247   **      EOF on end-of-file.
  2219   2248   **   +  Report syntax errors on stderr
  2220   2249   */
................................................................................
  2266   2295     if( rc ){
  2267   2296       fprintf(stderr, "Error %d: %s on [%s]\n",
  2268   2297               sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
  2269   2298               zQuery);
  2270   2299       goto end_data_xfer;
  2271   2300     }
  2272   2301     n = sqlite3_column_count(pQuery);
  2273         -  zInsert = sqlite3_malloc(200 + nTable + n*3);
         2302  +  zInsert = sqlite3_malloc64(200 + nTable + n*3);
  2274   2303     if( zInsert==0 ){
  2275   2304       fprintf(stderr, "out of memory\n");
  2276   2305       goto end_data_xfer;
  2277   2306     }
  2278   2307     sqlite3_snprintf(200+nTable,zInsert,
  2279   2308                      "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
  2280   2309     i = (int)strlen(zInsert);
................................................................................
  2681   2710       if( nArg==2 ){
  2682   2711         bail_on_error = booleanValue(azArg[1]);
  2683   2712       }else{
  2684   2713         fprintf(stderr, "Usage: .bail on|off\n");
  2685   2714         rc = 1;
  2686   2715       }
  2687   2716     }else
         2717  +
         2718  +  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
         2719  +    if( nArg==2 ){
         2720  +      if( booleanValue(azArg[1]) ){
         2721  +        setBinaryMode(p->out);
         2722  +      }else{
         2723  +        setTextMode(p->out);
         2724  +      }
         2725  +    }else{
         2726  +      fprintf(stderr, "Usage: .binary on|off\n");
         2727  +      rc = 1;
         2728  +    }
         2729  +  }else
  2688   2730   
  2689   2731     /* The undocumented ".breakpoint" command causes a call to the no-op
  2690   2732     ** routine named test_breakpoint().
  2691   2733     */
  2692   2734     if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
  2693   2735       test_breakpoint();
  2694   2736     }else
................................................................................
  3021   3063         xCloser(sCtx.in);
  3022   3064         return 1;
  3023   3065       }
  3024   3066       nCol = sqlite3_column_count(pStmt);
  3025   3067       sqlite3_finalize(pStmt);
  3026   3068       pStmt = 0;
  3027   3069       if( nCol==0 ) return 0; /* no columns, no error */
  3028         -    zSql = sqlite3_malloc( nByte*2 + 20 + nCol*2 );
         3070  +    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
  3029   3071       if( zSql==0 ){
  3030   3072         fprintf(stderr, "Error: out of memory\n");
  3031   3073         xCloser(sCtx.in);
  3032   3074         return 1;
  3033   3075       }
  3034   3076       sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
  3035   3077       j = strlen30(zSql);
................................................................................
  3161   3203           rc = 1;
  3162   3204         }else{
  3163   3205           sqlite3IoTrace = iotracePrintf;
  3164   3206         }
  3165   3207       }
  3166   3208     }else
  3167   3209   #endif
         3210  +  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
         3211  +    static const struct {
         3212  +       const char *zLimitName;   /* Name of a limit */
         3213  +       int limitCode;            /* Integer code for that limit */
         3214  +    } aLimit[] = {
         3215  +      { "length",                SQLITE_LIMIT_LENGTH                    },
         3216  +      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
         3217  +      { "column",                SQLITE_LIMIT_COLUMN                    },
         3218  +      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
         3219  +      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
         3220  +      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
         3221  +      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
         3222  +      { "attached",              SQLITE_LIMIT_ATTACHED                  },
         3223  +      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
         3224  +      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
         3225  +      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
         3226  +      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
         3227  +    };
         3228  +    int i, n2;
         3229  +    open_db(p, 0);
         3230  +    if( nArg==1 ){
         3231  +      for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
         3232  +        printf("%20s %d\n", aLimit[i].zLimitName, 
         3233  +               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
         3234  +      }
         3235  +    }else if( nArg>3 ){
         3236  +      fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
         3237  +      rc = 1;
         3238  +      goto meta_command_exit;
         3239  +    }else{
         3240  +      int iLimit = -1;
         3241  +      n2 = strlen30(azArg[1]);
         3242  +      for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
         3243  +        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
         3244  +          if( iLimit<0 ){
         3245  +            iLimit = i;
         3246  +          }else{
         3247  +            fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
         3248  +            rc = 1;
         3249  +            goto meta_command_exit;
         3250  +          }
         3251  +        }
         3252  +      }
         3253  +      if( iLimit<0 ){
         3254  +        fprintf(stderr, "unknown limit: \"%s\"\n"
         3255  +                        "enter \".limits\" with no arguments for a list.\n",
         3256  +                         azArg[1]);
         3257  +        rc = 1;
         3258  +        goto meta_command_exit;
         3259  +      }
         3260  +      if( nArg==3 ){
         3261  +        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
         3262  +                      (int)integerValue(azArg[2]));
         3263  +      }
         3264  +      printf("%20s %d\n", aLimit[iLimit].zLimitName,
         3265  +             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
         3266  +    }
         3267  +  }else
  3168   3268   
  3169   3269   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3170   3270     if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
  3171   3271       const char *zFile, *zProc;
  3172   3272       char *zErrMsg = 0;
  3173   3273       if( nArg<2 ){
  3174   3274         fprintf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
................................................................................
  3646   3746       }else{
  3647   3747         sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
  3648   3748       }
  3649   3749       while( sqlite3_step(pStmt)==SQLITE_ROW ){
  3650   3750         if( nRow>=nAlloc ){
  3651   3751           char **azNew;
  3652   3752           int n2 = nAlloc*2 + 10;
  3653         -        azNew = sqlite3_realloc(azResult, sizeof(azResult[0])*n2);
         3753  +        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
  3654   3754           if( azNew==0 ){
  3655   3755             fprintf(stderr, "Error: out of memory\n");
  3656   3756             break;
  3657   3757           }
  3658   3758           nAlloc = n2;
  3659   3759           azResult = azNew;
  3660   3760         }

Changes to src/sqlite.h.in.

  3894   3894   #define SQLITE3_TEXT     3
  3895   3895   
  3896   3896   /*
  3897   3897   ** CAPI3REF: Result Values From A Query
  3898   3898   ** KEYWORDS: {column access functions}
  3899   3899   ** METHOD: sqlite3_stmt
  3900   3900   **
  3901         -** These routines form the "result set" interface.
  3902         -**
  3903   3901   ** ^These routines return information about a single column of the current
  3904   3902   ** result row of a query.  ^In every case the first argument is a pointer
  3905   3903   ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  3906   3904   ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
  3907   3905   ** and the second argument is the index of the column for which information
  3908   3906   ** should be returned. ^The leftmost column of the result set has the index 0.
  3909   3907   ** ^The number of columns in the result can be determined using
................................................................................
  3955   3953   ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
  3956   3954   ** bytes in the string, not the number of characters.
  3957   3955   **
  3958   3956   ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
  3959   3957   ** even empty strings, are always zero-terminated.  ^The return
  3960   3958   ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
  3961   3959   **
  3962         -** ^The object returned by [sqlite3_column_value()] is an
  3963         -** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
  3964         -** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
         3960  +** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
         3961  +** [unprotected sqlite3_value] object.  In a multithreaded environment,
         3962  +** an unprotected sqlite3_value object may only be used safely with
         3963  +** [sqlite3_bind_value()] and [sqlite3_result_value()].
  3965   3964   ** If the [unprotected sqlite3_value] object returned by
  3966   3965   ** [sqlite3_column_value()] is used in any other way, including calls
  3967   3966   ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
  3968         -** or [sqlite3_value_bytes()], then the behavior is undefined.
         3967  +** or [sqlite3_value_bytes()], the behavior is not threadsafe.
  3969   3968   **
  3970   3969   ** These routines attempt to convert the value where appropriate.  ^For
  3971   3970   ** example, if the internal representation is FLOAT and a text result
  3972   3971   ** is requested, [sqlite3_snprintf()] is used internally to perform the
  3973   3972   ** conversion automatically.  ^(The following table details the conversions
  3974   3973   ** that are applied:
  3975   3974   **
................................................................................
  3992   3991   ** <tr><td>  TEXT    <td>   BLOB    <td> No change
  3993   3992   ** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
  3994   3993   ** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
  3995   3994   ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
  3996   3995   ** </table>
  3997   3996   ** </blockquote>)^
  3998   3997   **
  3999         -** The table above makes reference to standard C library functions atoi()
  4000         -** and atof().  SQLite does not really use these functions.  It has its
  4001         -** own equivalent internal routines.  The atoi() and atof() names are
  4002         -** used in the table for brevity and because they are familiar to most
  4003         -** C programmers.
  4004         -**
  4005   3998   ** Note that when type conversions occur, pointers returned by prior
  4006   3999   ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
  4007   4000   ** sqlite3_column_text16() may be invalidated.
  4008   4001   ** Type conversions and pointer invalidations might occur
  4009   4002   ** in the following cases:
  4010   4003   **
  4011   4004   ** <ul>
................................................................................
  4022   4015   **
  4023   4016   ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
  4024   4017   ** not invalidate a prior pointer, though of course the content of the buffer
  4025   4018   ** that the prior pointer references will have been modified.  Other kinds
  4026   4019   ** of conversion are done in place when it is possible, but sometimes they
  4027   4020   ** are not possible and in those cases prior pointers are invalidated.
  4028   4021   **
  4029         -** The safest and easiest to remember policy is to invoke these routines
         4022  +** The safest policy is to invoke these routines
  4030   4023   ** in one of the following ways:
  4031   4024   **
  4032   4025   ** <ul>
  4033   4026   **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
  4034   4027   **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
  4035   4028   **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
  4036   4029   ** </ul>
................................................................................
  4042   4035   ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
  4043   4036   ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  4044   4037   ** with calls to sqlite3_column_bytes().
  4045   4038   **
  4046   4039   ** ^The pointers returned are valid until a type conversion occurs as
  4047   4040   ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  4048   4041   ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
  4049         -** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
         4042  +** and BLOBs is freed automatically.  Do <em>not</em> pass the pointers returned
  4050   4043   ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  4051   4044   ** [sqlite3_free()].
  4052   4045   **
  4053   4046   ** ^(If a memory allocation error occurs during the evaluation of any
  4054   4047   ** of these routines, a default value is returned.  The default value
  4055   4048   ** is either the integer 0, the floating point number 0.0, or a NULL
  4056   4049   ** pointer.  Subsequent calls to [sqlite3_errcode()] will return

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   
................................................................................
  1553   1579     int nRef;                 /* Number of pointers to this structure */
  1554   1580     u8 bConstraint;           /* True if constraints are supported */
  1555   1581     int iSavepoint;           /* Depth of the SAVEPOINT stack */
  1556   1582     VTable *pNext;            /* Next in linked list (see above) */
  1557   1583   };
  1558   1584   
  1559   1585   /*
  1560         -** Each SQL table is represented in memory by an instance of the
  1561         -** following structure.
  1562         -**
  1563         -** Table.zName is the name of the table.  The case of the original
  1564         -** CREATE TABLE statement is stored, but case is not significant for
  1565         -** comparisons.
  1566         -**
  1567         -** Table.nCol is the number of columns in this table.  Table.aCol is a
  1568         -** pointer to an array of Column structures, one for each column.
  1569         -**
  1570         -** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
  1571         -** the column that is that key.   Otherwise Table.iPKey is negative.  Note
  1572         -** that the datatype of the PRIMARY KEY must be INTEGER for this field to
  1573         -** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
  1574         -** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
  1575         -** is generated for each row of the table.  TF_HasPrimaryKey is set if
  1576         -** the table has any PRIMARY KEY, INTEGER or otherwise.
  1577         -**
  1578         -** Table.tnum is the page number for the root BTree page of the table in the
  1579         -** database file.  If Table.iDb is the index of the database table backend
  1580         -** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
  1581         -** holds temporary tables and indices.  If TF_Ephemeral is set
  1582         -** then the table is stored in a file that is automatically deleted
  1583         -** when the VDBE cursor to the table is closed.  In this case Table.tnum 
  1584         -** refers VDBE cursor number that holds the table open, not to the root
  1585         -** page number.  Transient tables are used to hold the results of a
  1586         -** sub-query that appears instead of a real table name in the FROM clause 
  1587         -** of a SELECT statement.
         1586  +** The schema for each SQL table and view is represented in memory
         1587  +** by an instance of the following structure.
  1588   1588   */
  1589   1589   struct Table {
  1590   1590     char *zName;         /* Name of the table or view */
  1591   1591     Column *aCol;        /* Information about each column */
  1592   1592     Index *pIndex;       /* List of SQL indexes on this table. */
  1593   1593     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  1594   1594     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  1595   1595     char *zColAff;       /* String defining the affinity of each column */
  1596   1596   #ifndef SQLITE_OMIT_CHECK
  1597   1597     ExprList *pCheck;    /* All CHECK constraints */
  1598   1598   #endif
  1599         -  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  1600         -  int tnum;            /* Root BTree node for this table (see note above) */
  1601         -  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
         1599  +  int tnum;            /* Root BTree page for this table */
         1600  +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
  1602   1601     i16 nCol;            /* Number of columns in this table */
  1603   1602     u16 nRef;            /* Number of pointers to this Table */
         1603  +  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  1604   1604     LogEst szTabRow;     /* Estimated size of each table row in bytes */
  1605   1605   #ifdef SQLITE_ENABLE_COSTMULT
  1606   1606     LogEst costMult;     /* Cost multiplier for using this table */
  1607   1607   #endif
  1608   1608     u8 tabFlags;         /* Mask of TF_* values */
  1609   1609     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1610   1610   #ifndef SQLITE_OMIT_ALTERTABLE
................................................................................
  2831   2831   */
  2832   2832   struct StrAccum {
  2833   2833     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
  2834   2834     char *zBase;         /* A base allocation.  Not from malloc. */
  2835   2835     char *zText;         /* The string collected so far */
  2836   2836     int  nChar;          /* Length of the string so far */
  2837   2837     int  nAlloc;         /* Amount of space allocated in zText */
  2838         -  int  mxAlloc;        /* Maximum allowed string length */
  2839         -  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
         2838  +  int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
  2840   2839     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
  2841   2840   };
  2842   2841   #define STRACCUM_NOMEM   1
  2843   2842   #define STRACCUM_TOOBIG  2
  2844   2843   
  2845   2844   /*
  2846   2845   ** A pointer to this structure is used to communicate information
................................................................................
  3149   3148   #define SQLITE_PRINTF_INTERNAL 0x01
  3150   3149   #define SQLITE_PRINTF_SQLFUNC  0x02
  3151   3150   void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
  3152   3151   void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
  3153   3152   char *sqlite3MPrintf(sqlite3*,const char*, ...);
  3154   3153   char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  3155   3154   char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
  3156         -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
         3155  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  3157   3156     void sqlite3DebugPrintf(const char*, ...);
  3158   3157   #endif
  3159   3158   #if defined(SQLITE_TEST)
  3160   3159     void *sqlite3TestTextToPtr(const char*);
  3161   3160   #endif
  3162   3161   
  3163   3162   #if defined(SQLITE_DEBUG)
................................................................................
  3496   3495   int sqlite3DecOrHexToI64(const char*, i64*);
  3497   3496   void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
  3498   3497   void sqlite3Error(sqlite3*,int);
  3499   3498   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3500   3499   u8 sqlite3HexToInt(int h);
  3501   3500   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3502   3501   
  3503         -#if defined(SQLITE_TEST) 
         3502  +#if defined(SQLITE_NEED_ERR_NAME)
  3504   3503   const char *sqlite3ErrName(int);
  3505   3504   #endif
  3506   3505   
  3507   3506   const char *sqlite3ErrStr(int);
  3508   3507   int sqlite3ReadSchema(Parse *pParse);
  3509   3508   CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  3510   3509   CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
................................................................................
  3590   3589     void (*)(sqlite3_context*,int,sqlite3_value **),
  3591   3590     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  3592   3591     FuncDestructor *pDestructor
  3593   3592   );
  3594   3593   int sqlite3ApiExit(sqlite3 *db, int);
  3595   3594   int sqlite3OpenTempDatabase(Parse *);
  3596   3595   
  3597         -void sqlite3StrAccumInit(StrAccum*, char*, int, int);
         3596  +void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
  3598   3597   void sqlite3StrAccumAppend(StrAccum*,const char*,int);
  3599   3598   void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  3600   3599   void sqlite3AppendChar(StrAccum*,int,char);
  3601   3600   char *sqlite3StrAccumFinish(StrAccum*);
  3602   3601   void sqlite3StrAccumReset(StrAccum*);
  3603   3602   void sqlite3SelectDestInit(SelectDest*,int,int);
  3604   3603   Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
................................................................................
  3863   3862   /*
  3864   3863   ** Threading interface
  3865   3864   */
  3866   3865   #if SQLITE_MAX_WORKER_THREADS>0
  3867   3866   int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
  3868   3867   int sqlite3ThreadJoin(SQLiteThread*, void**);
  3869   3868   #endif
         3869  +
         3870  +#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
         3871  +int sqlite3DbstatRegister(sqlite3*);
         3872  +#endif
  3870   3873   
  3871   3874   #endif /* _SQLITEINT_H_ */

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.

  3700   3700     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){
  3701   3701       return TCL_ERROR;
  3702   3702     }
  3703   3703     Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
  3704   3704   
  3705   3705     return TCL_OK;
  3706   3706   }
  3707         -#endif
         3707  +#endif /* SQLITE_TEST */
  3708   3708   
  3709   3709   /*
  3710   3710   ** Configure the interpreter passed as the first argument to have access
  3711   3711   ** to the commands and linked variables that make up:
  3712   3712   **
  3713   3713   **   * the [sqlite3] extension itself, 
  3714   3714   **
................................................................................
  3718   3718   **     test suite.
  3719   3719   */
  3720   3720   static void init_all(Tcl_Interp *interp){
  3721   3721     Sqlite3_Init(interp);
  3722   3722   
  3723   3723   #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5)
  3724   3724     Md5_Init(interp);
  3725         -#endif
  3726         -
  3727         -  /* Install the [register_dbstat_vtab] command to access the implementation
  3728         -  ** of virtual table dbstat (source file test_stat.c). This command is
  3729         -  ** required for testfixture and sqlite3_analyzer, but not by the production
  3730         -  ** Tcl extension.  */
  3731         -#if defined(SQLITE_TEST) || TCLSH==2
  3732         -  {
  3733         -    extern int SqlitetestStat_Init(Tcl_Interp*);
  3734         -    SqlitetestStat_Init(interp);
  3735         -  }
  3736   3725   #endif
  3737   3726   
  3738   3727   #ifdef SQLITE_TEST
  3739   3728     {
  3740   3729       extern int Sqliteconfig_Init(Tcl_Interp*);
  3741   3730       extern int Sqlitetest1_Init(Tcl_Interp*);
  3742   3731       extern int Sqlitetest2_Init(Tcl_Interp*);

Changes to src/test1.c.

  6676   6676       case 4: {
  6677   6677         Tcl_Panic("Deliberate panic");
  6678   6678         break;
  6679   6679       }
  6680   6680     }
  6681   6681     return TCL_OK;
  6682   6682   }  
  6683         -  
         6683  +
         6684  +/*
         6685  +** tclcmd:   register_dbstat_vtab DB
         6686  +**
         6687  +** Cause the dbstat virtual table to be available on the connection DB
         6688  +*/
         6689  +static int test_register_dbstat_vtab(
         6690  +  void *clientData,
         6691  +  Tcl_Interp *interp,
         6692  +  int objc,
         6693  +  Tcl_Obj *CONST objv[]
         6694  +){
         6695  +#ifdef SQLITE_OMIT_VIRTUALTABLE
         6696  +  Tcl_AppendResult(interp, "dbstat not available because of "
         6697  +                           "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
         6698  +  return TCL_ERROR;
         6699  +#else
         6700  +  struct SqliteDb { sqlite3 *db; };
         6701  +  char *zDb;
         6702  +  Tcl_CmdInfo cmdInfo;
         6703  +
         6704  +  if( objc!=2 ){
         6705  +    Tcl_WrongNumArgs(interp, 1, objv, "DB");
         6706  +    return TCL_ERROR;
         6707  +  }
         6708  +
         6709  +  zDb = Tcl_GetString(objv[1]);
         6710  +  if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
         6711  +    sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
         6712  +    sqlite3DbstatRegister(db);
         6713  +  }
         6714  +  return TCL_OK;
         6715  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
         6716  +}
  6684   6717   
  6685   6718   /*
  6686   6719   ** Register commands with the TCL interpreter.
  6687   6720   */
  6688   6721   int Sqlitetest1_Init(Tcl_Interp *interp){
  6689   6722     extern int sqlite3_search_count;
  6690   6723     extern int sqlite3_found_count;
................................................................................
  6748   6781     };
  6749   6782     static struct {
  6750   6783        char *zName;
  6751   6784        Tcl_ObjCmdProc *xProc;
  6752   6785        void *clientData;
  6753   6786     } aObjCmd[] = {
  6754   6787        { "bad_behavior",                  test_bad_behavior,  (void*)&iZero },
         6788  +     { "register_dbstat_vtab",          test_register_dbstat_vtab  },
  6755   6789        { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },
  6756   6790        { "sqlite3_bind_int",              test_bind_int,      0 },
  6757   6791        { "sqlite3_bind_zeroblob",         test_bind_zeroblob, 0 },
  6758   6792        { "sqlite3_bind_int64",            test_bind_int64,    0 },
  6759   6793        { "sqlite3_bind_double",           test_bind_double,   0 },
  6760   6794        { "sqlite3_bind_null",             test_bind_null     ,0 },
  6761   6795        { "sqlite3_bind_text",             test_bind_text     ,0 },

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         -    sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
          458  +    if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
          459  +      sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
          460  +    }
   459    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());

Changes to src/vdbe.c.

   981    981     if( rc==SQLITE_BUSY ){
   982    982       p->rc = rc = SQLITE_BUSY;
   983    983     }else{
   984    984       assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
   985    985       assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
   986    986       rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
   987    987     }
   988         -  pOp = &aOp[pcx];
   989    988     goto vdbe_return;
   990    989   }
   991    990   
   992    991   /* Opcode: Integer P1 P2 * * *
   993    992   ** Synopsis: r[P2]=P1
   994    993   **
   995    994   ** The 32-bit integer value P1 is written into register P2.
................................................................................
  1201   1200     do{
  1202   1201       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  1203   1202       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  1204   1203       assert( memIsValid(pIn1) );
  1205   1204       memAboutToChange(p, pOut);
  1206   1205       sqlite3VdbeMemMove(pOut, pIn1);
  1207   1206   #ifdef SQLITE_DEBUG
  1208         -    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  1209         -      pOut->pScopyFrom += p1 - pOp->p2;
         1207  +    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
         1208  +      pOut->pScopyFrom += pOp->p2 - p1;
  1210   1209       }
  1211   1210   #endif
         1211  +    Deephemeralize(pOut);
  1212   1212       REGISTER_TRACE(p2++, pOut);
  1213   1213       pIn1++;
  1214   1214       pOut++;
  1215   1215     }while( --n );
  1216   1216     break;
  1217   1217   }
  1218   1218   
................................................................................
  2473   2473          || (offset > pC->payloadSize)
  2474   2474         ){
  2475   2475           rc = SQLITE_CORRUPT_BKPT;
  2476   2476           goto op_column_error;
  2477   2477         }
  2478   2478       }
  2479   2479   
  2480         -    /* If after trying to extra new entries from the header, nHdrParsed is
         2480  +    /* If after trying to extract new entries from the header, nHdrParsed is
  2481   2481       ** still not up to p2, that means that the record has fewer than p2
  2482   2482       ** columns.  So the result will be either the default value or a NULL.
  2483   2483       */
  2484   2484       if( pC->nHdrParsed<=p2 ){
  2485   2485         if( pOp->p4type==P4_MEM ){
  2486   2486           sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
  2487   2487         }else{
................................................................................
  5908   5908   #endif /* ifndef SQLITE_OMIT_WAL */
  5909   5909   
  5910   5910     if( rc ){
  5911   5911       eNew = eOld;
  5912   5912     }
  5913   5913     eNew = sqlite3PagerSetJournalMode(pPager, eNew);
  5914   5914   
  5915         -  pOut = &aMem[pOp->p2];
  5916   5915     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  5917   5916     pOut->z = (char *)sqlite3JournalModename(eNew);
  5918   5917     pOut->n = sqlite3Strlen30(pOut->z);
  5919   5918     pOut->enc = SQLITE_UTF8;
  5920   5919     sqlite3VdbeChangeEncoding(pOut, encoding);
  5921   5920     break;
  5922   5921   };
................................................................................
  6103   6102   
  6104   6103       /* Initialize vdbe cursor object */
  6105   6104       pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
  6106   6105       if( pCur ){
  6107   6106         pCur->pVtabCursor = pVtabCursor;
  6108   6107         pVtab->nRef++;
  6109   6108       }else{
  6110         -      db->mallocFailed = 1;
         6109  +      assert( db->mallocFailed );
  6111   6110         pModule->xClose(pVtabCursor);
         6111  +      goto no_mem;
  6112   6112       }
  6113   6113     }
  6114   6114     break;
  6115   6115   }
  6116   6116   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6117   6117   
  6118   6118   #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         -    pMem->pScopyFrom = 0;
          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;
         2171  +  rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
  2173   2172   
  2174         -    rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
  2175         -
  2176         -    /* Set up the required files for pIncr. A multi-theaded IncrMerge object
  2177         -    ** requires two temp files to itself, whereas a single-threaded object
  2178         -    ** only requires a region of pTask->file2. */
  2179         -    if( rc==SQLITE_OK ){
  2180         -      int mxSz = pIncr->mxSz;
         2173  +  /* Set up the required files for pIncr. A multi-theaded IncrMerge object
         2174  +  ** requires two temp files to itself, whereas a single-threaded object
         2175  +  ** only requires a region of pTask->file2. */
         2176  +  if( rc==SQLITE_OK ){
         2177  +    int mxSz = pIncr->mxSz;
  2181   2178   #if SQLITE_MAX_WORKER_THREADS>0
  2182         -      if( pIncr->bUseThread ){
  2183         -        rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
  2184         -        if( rc==SQLITE_OK ){
  2185         -          rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
  2186         -        }
  2187         -      }else
         2179  +    if( pIncr->bUseThread ){
         2180  +      rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
         2181  +      if( rc==SQLITE_OK ){
         2182  +        rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
         2183  +      }
         2184  +    }else
  2188   2185   #endif
  2189         -      /*if( !pIncr->bUseThread )*/{
  2190         -        if( pTask->file2.pFd==0 ){
  2191         -          assert( pTask->file2.iEof>0 );
  2192         -          rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
  2193         -          pTask->file2.iEof = 0;
  2194         -        }
  2195         -        if( rc==SQLITE_OK ){
  2196         -          pIncr->aFile[1].pFd = pTask->file2.pFd;
  2197         -          pIncr->iStartOff = pTask->file2.iEof;
  2198         -          pTask->file2.iEof += mxSz;
  2199         -        }
         2186  +    /*if( !pIncr->bUseThread )*/{
         2187  +      if( pTask->file2.pFd==0 ){
         2188  +        assert( pTask->file2.iEof>0 );
         2189  +        rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
         2190  +        pTask->file2.iEof = 0;
         2191  +      }
         2192  +      if( rc==SQLITE_OK ){
         2193  +        pIncr->aFile[1].pFd = pTask->file2.pFd;
         2194  +        pIncr->iStartOff = pTask->file2.iEof;
         2195  +        pTask->file2.iEof += mxSz;
  2200   2196         }
  2201   2197       }
         2198  +  }
  2202   2199   
  2203   2200   #if SQLITE_MAX_WORKER_THREADS>0
  2204         -    if( rc==SQLITE_OK && pIncr->bUseThread ){
  2205         -      /* 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. */
  2208         -      assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
  2209         -      rc = vdbeIncrPopulate(pIncr);
  2210         -    }
         2201  +  if( rc==SQLITE_OK && pIncr->bUseThread ){
         2202  +    /* Use the current thread to populate aFile[1], even though this
         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  +    */
         2212  +    assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
         2213  +    rc = vdbeIncrPopulate(pIncr);
         2214  +  }
  2211   2215   #endif
  2212   2216   
  2213         -    if( rc==SQLITE_OK
  2214         -     && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK)
  2215         -    ){
  2216         -      rc = vdbePmaReaderNext(pReadr);
  2217         -    }
         2217  +  if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
         2218  +    rc = vdbePmaReaderNext(pReadr);
  2218   2219     }
         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.
  2239         -**
  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].
         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.
         2244  +** 
         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){
  2245         -  void *pCtx = (void*)pReadr;
  2246         -  return vdbeSorterCreateThread(pReadr->pIncr->pTask, vdbePmaReaderBgInit, pCtx);
  2247         -}
         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 ){
         2257  +      void *pCtx = (void*)pReadr;
         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.

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

Added test/analyzer1.test.

            1  +# 2015-05-11
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Quick tests for the sqlite3_analyzer tool
           13  +#
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +
           17  +ifcapable !vtab {
           18  +  finish_test
           19  +  return
           20  +}
           21  +
           22  +if {$tcl_platform(platform)=="windows"} {
           23  +  set PROG "sqlite3_analyzer.exe"
           24  +} else {
           25  +  set PROG "./sqlite3_analyzer"
           26  +}
           27  +db close
           28  +forcedelete test.db test.db-journal test.db-wal
           29  +sqlite3 db test.db
           30  +
           31  +do_test analyzer1-1.0 {
           32  +  db eval {
           33  +    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
           34  +    CREATE TABLE t2(a INT PRIMARY KEY, b) WITHOUT ROWID;
           35  +    WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<250)
           36  +    INSERT INTO t1(a,b) SELECT x, randomblob(200) FROM c;
           37  +    INSERT INTO t2(a,b) SELECT a, b FROM t1;
           38  +  }
           39  +  set line "exec $PROG test.db"
           40  +  unset -nocomplain ::MSG
           41  +  catch {eval $line} ::MSG
           42  +} {0}
           43  +do_test analyzer1-1.1 {
           44  +  regexp {^/\*\* Disk-Space Utilization.*COMMIT;\W*$} $::MSG
           45  +} {1}
           46  +
           47  +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.1  {
          511  +    SELECT rowid, mit(matchinfo(tt, 'y')) FROM tt WHERE tt MATCH $expr
          512  +  } $res
          513  +
          514  +  set r2 [list]
          515  +  foreach {rowid L} $res {
          516  +    lappend r2 $rowid
          517  +    set M [list]
          518  +    foreach {a b} $L {
          519  +      lappend M [expr ($a ? 1 : 0) + ($b ? 2 : 0)]
          520  +    }
          521  +    lappend r2 $M
          522  +  }
          523  +
          524  +  do_execsql_test 11.1.$tn.2  {
          525  +    SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr
          526  +  } $r2
          527  +  breakpoint
          528  +
          529  +  do_execsql_test 11.1.$tn.2  {
          530  +    SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr
          531  +  } $r2
          532  +}
          533  +set sqlite_fts3_enable_parentheses 0
          534  +
          535  +#---------------------------------------------------------------------------
          536  +# Test the 'b' matchinfo flag
          537  +#
          538  +set sqlite_fts3_enable_parentheses 1
          539  +reset_db
          540  +db func mit mit
          541  +
          542  +do_test 12.0 {
          543  +  set cols [list]
          544  +  for {set i 0} {$i < 50} {incr i} { lappend cols "c$i" }
          545  +  execsql "CREATE VIRTUAL TABLE tt USING fts3([join $cols ,])"
          546  +} {}
          547  +
          548  +do_execsql_test 12.1 {
          549  +  INSERT INTO tt (rowid, c4, c45) VALUES(1, 'abc', 'abc');
          550  +  SELECT mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH 'abc';
          551  +} [list [list [expr 1<<4] [expr 1<<(45-32)]]]
   452    552   
          553  +set sqlite_fts3_enable_parentheses 0
   453    554   finish_test
          555  +

Changes to test/fts3query.test.

   169    169   } {
   170    170     1 "SELECT matchinfo(content) FROM t2 WHERE t2 MATCH 'history'" matchinfo
   171    171     2 "SELECT offsets(content) FROM t2 WHERE t2 MATCH 'history'"   offsets
   172    172     3 "SELECT snippet(content) FROM t2 WHERE t2 MATCH 'history'"   snippet
   173    173     4 "SELECT optimize(content) FROM t2 WHERE t2 MATCH 'history'"  optimize
   174    174   }
   175    175   do_catchsql_test 5.5.1 {
   176         -  SELECT matchinfo(t2, 'abc') FROM t2 WHERE t2 MATCH 'history'
   177         -} {1 {unrecognized matchinfo request: b}}
          176  +  SELECT matchinfo(t2, 'abcd') FROM t2 WHERE t2 MATCH 'history'
          177  +} {1 {unrecognized matchinfo request: d}}
   178    178   
   179    179   do_execsql_test 5.5 { DROP TABLE t2 }
   180    180   
   181    181   
   182    182   # Test the snippet() function with 1 to 6 arguments.
   183    183   # 
   184    184   do_execsql_test 6.1 {

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  +  db2 eval {CREATE TEMP TABLE t(l); PRAGMA journal_mode=off;}
          563  +  db2 close
          564  +} {}
          565  +do_execsql_test jrnlmode-9.2 {
          566  +  PRAGMA locking_mode = exclusive;
          567  +  CREATE TABLE tx(a);
          568  +  PRAGMA journal_mode = off;
          569  +} {exclusive off}
          570  +
   557    571   
   558    572   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_ENABLE_OTA
   112    120       -DSQLITE_MAX_ATTACHED=125
   113    121     }
   114    122     "Device-One" {
   115    123       -O2
   116    124       -DSQLITE_DEBUG=1
................................................................................
   196    204     # different names for them all so that they results appear in separate
   197    205     # subdirectories.
   198    206     #
   199    207     Fail0 {-O0}
   200    208     Fail2 {-O0}
   201    209     Fail3 {-O0}
   202    210     Fail4 {-O0}
          211  +  FuzzFail1 {-O0}
          212  +  FuzzFail2 {-O0}
   203    213   }]
   204    214   
   205    215   array set ::Platforms [strip_comments {
   206    216     Linux-x86_64 {
   207    217       "Check-Symbols"           checksymbols
   208    218       "Debug-One"               "mptest test"
   209    219       "Have-Not"                test
................................................................................
   211    221       "Unlock-Notify"           "QUICKTEST_INCLUDE=notify2.test test"
   212    222       "Update-Delete-Limit"     test
   213    223       "Extra-Robustness"        test
   214    224       "Device-Two"              test
   215    225       "No-lookaside"            test
   216    226       "Devkit"                  test
   217    227       "Sanitize"                {QUICKTEST_OMIT=func4.test,nan.test test}
          228  +    "Fast-One"                fuzzoomtest
   218    229       "Valgrind"                valgrindtest
   219    230       "Default"                 "threadtest fulltest"
   220    231       "Device-One"              fulltest
   221    232     }
   222    233     Linux-i686 {
   223    234       "Devkit"                  test
   224    235       "Have-Not"                test
................................................................................
   252    263     #
   253    264     Failure-Detection {
   254    265       Fail0     "TEST_FAILURE=0 test"
   255    266       Sanitize  "TEST_FAILURE=1 test"
   256    267       Fail2     "TEST_FAILURE=2 valgrindtest"
   257    268       Fail3     "TEST_FAILURE=3 valgrindtest"
   258    269       Fail4     "TEST_FAILURE=4 test"
          270  +    FuzzFail1 "TEST_FAILURE=5 test"
          271  +    FuzzFail2 "TEST_FAILURE=5 valgrindtest"
   259    272     }
   260    273   }]
   261    274   
   262    275   
   263    276   # End of configuration section.
   264    277   #########################################################################
   265    278   #########################################################################
................................................................................
   348    361     # OPTS Makefile variable. Variable $cflags holds the value for
   349    362     # CFLAGS. The makefile will pass OPTS to both gcc and lemon, but
   350    363     # CFLAGS is only passed to gcc.
   351    364     #
   352    365     set cflags [expr {$::MSVC ? "-Zi" : "-g"}]
   353    366     set opts ""
   354    367     set title ${name}($testtarget)
   355         -  set configOpts ""
          368  +  set configOpts $::WITHTCL
   356    369   
   357    370     regsub -all {#[^\n]*\n} $config \n config
   358    371     foreach arg $config {
   359    372       if {[regexp {^-[UD]} $arg]} {
   360    373         lappend opts $arg
   361    374       } elseif {[regexp {^[A-Z]+=} $arg]} {
   362    375         lappend testtarget $arg
................................................................................
   479    492     set ::SRCDIR    [file normalize [file dirname [file dirname $::argv0]]]
   480    493     set ::QUICK     0
   481    494     set ::MSVC      0
   482    495     set ::BUILDONLY 0
   483    496     set ::DRYRUN    0
   484    497     set ::EXEC      exec
   485    498     set ::TRACE     0
          499  +  set ::WITHTCL   {}
   486    500     set config {}
   487    501     set platform $::tcl_platform(os)-$::tcl_platform(machine)
   488    502   
   489    503     for {set i 0} {$i < [llength $argv]} {incr i} {
   490    504       set x [lindex $argv $i]
   491    505       if {[regexp {^--[a-z]} $x]} {set x [string range $x 1 end]}
   492    506       switch -glob -- $x {
................................................................................
   552    566         -g {
   553    567           if {$::MSVC} {
   554    568             lappend ::EXTRACONFIG -Zi
   555    569           } else {
   556    570             lappend ::EXTRACONFIG [lindex $argv $i]
   557    571           }
   558    572         }
          573  +
          574  +      -with-tcl=* {
          575  +        set ::WITHTCL -$x
          576  +      }
   559    577   
   560    578         -D* -
   561    579         -O* -
   562    580         -enable-* -
   563    581         -disable-* -
   564    582         *=* {
   565    583           lappend ::EXTRACONFIG [lindex $argv $i]
................................................................................
   640    658       incr NTEST
   641    659       run_test_suite $zConfig $target $config_options
   642    660   
   643    661       # If the configuration included the SQLITE_DEBUG option, then remove
   644    662       # it and run veryquick.test. If it did not include the SQLITE_DEBUG option
   645    663       # add it and run veryquick.test.
   646    664       if {$target!="checksymbols" && $target!="valgrindtest"
   647         -           && !$::BUILDONLY && $::QUICK<2} {
          665  +           && $target!="fuzzoomtest" && !$::BUILDONLY && $::QUICK<2} {
   648    666         set debug_idx [lsearch -glob $config_options -DSQLITE_DEBUG*]
   649    667         set xtarget $target
   650    668         regsub -all {fulltest[a-z]*} $xtarget test xtarget
          669  +      regsub -all {fuzzoomtest} $xtarget fuzztest xtarget
   651    670         if {$debug_idx < 0} {
   652    671           incr NTEST
   653    672           append config_options " -DSQLITE_DEBUG=1"
   654    673           run_test_suite "${zConfig}_debug" $xtarget $config_options
   655    674         } else {
   656    675           incr NTEST
   657    676           regsub { *-DSQLITE_MEMDEBUG[^ ]* *} $config_options { } config_options

Changes to test/select4.test.

   154    154   } {1 {ORDER BY clause should come after UNION not before}}
   155    155   do_test select4-2.4 {
   156    156     set v [catch {execsql {
   157    157       SELECT 0 ORDER BY (SELECT 0) UNION SELECT 0;
   158    158     }} msg]
   159    159     lappend v $msg
   160    160   } {1 {ORDER BY clause should come after UNION not before}}
          161  +do_execsql_test select4-2.5 {
          162  +  SELECT 123 AS x ORDER BY (SELECT x ORDER BY 1);
          163  +} {123}
   161    164   
   162    165   # Except operator
   163    166   #
   164    167   do_test select4-3.1.1 {
   165    168     execsql {
   166    169       SELECT DISTINCT log FROM t1
   167    170       EXCEPT
................................................................................
   269    272     set v [catch {execsql {
   270    273       SELECT DISTINCT log FROM t1 ORDER BY log
   271    274       INTERSECT
   272    275       SELECT n FROM t1 WHERE log=3
   273    276       ORDER BY log;
   274    277     }} msg]
   275    278     lappend v $msg
          279  +} {1 {ORDER BY clause should come after INTERSECT not before}}
          280  +do_catchsql_test select4-4.4 {
          281  +  SELECT 3 IN (
          282  +    SELECT 0 ORDER BY 1
          283  +    INTERSECT
          284  +    SELECT 1
          285  +    INTERSECT 
          286  +    SELECT 2
          287  +    ORDER BY 1
          288  +  );
   276    289   } {1 {ORDER BY clause should come after INTERSECT not before}}
   277    290   
   278    291   # Various error messages while processing UNION or INTERSECT
   279    292   #
   280    293   do_test select4-5.1 {
   281    294     set v [catch {execsql {
   282    295       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/sqldiff1.test.

            1  +# 2015-05-11
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Quick tests for the sqldiff tool
           13  +#
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +
           17  +if {$tcl_platform(platform)=="windows"} {
           18  +  set PROG "sqldiff.exe"
           19  +} else {
           20  +  set PROG "./sqldiff"
           21  +}
           22  +db close
           23  +forcedelete test.db test2.db
           24  +sqlite3 db test.db
           25  +
           26  +do_test sqldiff-1.0 {
           27  +  db eval {
           28  +    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
           29  +    CREATE TABLE t2(a INT PRIMARY KEY, b) WITHOUT ROWID;
           30  +    WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
           31  +    INSERT INTO t1(a,b) SELECT x, printf('abc-%d-xyz',x) FROM c;
           32  +    INSERT INTO t2(a,b) SELECT a, b FROM t1;
           33  +  }
           34  +  db backup test2.db
           35  +  db eval {
           36  +    ATTACH 'test2.db' AS x2;
           37  +    DELETE FROM x2.t1 WHERE a=49;
           38  +    DELETE FROM x2.t2 WHERE a=48;
           39  +    INSERT INTO x2.t1(a,b) VALUES(1234,'hello');
           40  +    INSERT INTO x2.t2(a,b) VALUES(50.5,'xyzzy');
           41  +    CREATE TABLE x2.t3(a,b,c);
           42  +    INSERT INTO x2.t3 VALUES(111,222,333);
           43  +    CREATE TABLE main.t4(x,y,z);
           44  +    INSERT INTO t4 SELECT * FROM t3;
           45  +  }
           46  +  set line "exec $PROG test.db test2.db"
           47  +  unset -nocomplain ::MSG
           48  +  catch {eval $line} ::MSG
           49  +} {0}
           50  +do_test sqldiff-1.1 {
           51  +  set ::MSG
           52  +} {DELETE FROM t1 WHERE a=49;
           53  +INSERT INTO t1(a,b) VALUES(1234,'hello');
           54  +DELETE FROM t2 WHERE a=48;
           55  +INSERT INTO t2(a,b) VALUES(50.5,'xyzzy');
           56  +CREATE TABLE t3(a,b,c);
           57  +INSERT INTO t3(rowid,a,b,c) VALUES(1,111,222,333);
           58  +DROP TABLE t4;}
           59  +
           60  +finish_test

Changes to test/stat.test.

   162    162   
   163    163   db close
   164    164   forcedelete test.db
   165    165   sqlite3 db test.db
   166    166   register_dbstat_vtab db
   167    167   do_execsql_test stat-5.1 {
   168    168     PRAGMA auto_vacuum = OFF;
   169         -  CREATE VIRTUAL TABLE temp.stat USING dbstat;
   170         -  CREATE TABLE t1(x);
          169  +  CREATE TABLE tx(y);
          170  +  ATTACH ':memory:' AS aux1;
          171  +  CREATE VIRTUAL TABLE temp.stat USING dbstat(aux1);
          172  +  CREATE TABLE aux1.t1(x);
   171    173     INSERT INTO t1 VALUES(zeroblob(1513));
   172    174     INSERT INTO t1 VALUES(zeroblob(1514));
   173    175     SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
   174    176       FROM stat WHERE name = 't1';
   175    177   } [list \
   176    178     t1 / 2 leaf 2 993 5 1517                \
   177    179     t1 /000+000000 3 overflow 0 1020 0 0    \
   178    180     t1 /001+000000 4 overflow 0 1020 0 0    \
   179    181   ]
   180    182   
          183  +do_catchsql_test stat-6.1 {
          184  +  CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx);
          185  +} {1 {no such database: mainx}}
          186  +
   181    187   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         -  const char *zArgv0;         /* Name of program */
           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;
    80         -  fprintf(stderr, "%s: ", g.zArgv0);
          134  +  if( g.zTestName[0] ){
          135  +    fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
          136  +  }else{
          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;
    93         -  fprintf(stderr, "%s: ", g.zArgv0);
          151  +  if( g.zTestName[0] ){
          152  +    fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
          153  +  }else{
          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);
          220  +}
          221  +static void traceNoop(void *NotUsed, const char *zMsg){
          222  +  return;
   148    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         -"  --autovacuum        Enable AUTOVACUUM mode\n"
   247         -"  -f FILE             Read SQL text from FILE instead of standard input\n"
   248         -"  --heap SZ MIN       Memory allocator uses SZ bytes & min allocation MIN\n"
   249         -"  --help              Show this help text\n"    
   250         -"  --initdb DBFILE     Initialize the in-memory database using template DBFILE\n"
   251         -"  --lookaside N SZ    Configure lookaside for N slots of SZ bytes each\n"
   252         -"  --pagesize N        Set the page size to N\n"
   253         -"  --pcache N SZ       Configure N pages of pagecache each of size SZ bytes\n"
   254         -"  --scratch N SZ      Configure scratch memory for N slots of SZ bytes each\n"
   255         -"  --utf16be           Set text encoding to UTF-16BE\n"
   256         -"  --utf16le           Set text encoding to UTF-16LE\n"
          323  +"  --autovacuum          Enable AUTOVACUUM mode\n"
          324  +"  --heap SZ MIN         Memory allocator uses SZ bytes & min allocation MIN\n"
          325  +"  --help                Show this help text\n"    
          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"
          328  +"  --pagesize N          Set the page size to N\n"
          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"
          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"
          334  +"  --utf16be             Set text encoding to UTF-16BE\n"
          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         -  char *zIn = 0;          /* Input text */
   329         -  int nAlloc = 0;         /* Number of bytes allocated for zIn[] */
   330         -  int nIn = 0;            /* Number of bytes of zIn[] used */
   331         -  size_t got;             /* Bytes read from input */
   332         -  FILE *in = stdin;       /* Where to read SQL text from */
   333         -  int rc = SQLITE_OK;     /* Result codes from API functions */
   334         -  int i;                  /* Loop counter */
   335         -  int iNext;              /* Next block of SQL */
   336         -  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         -  char *zErrMsg = 0;      /* Error message returned from sqlite3_exec() */
          415  +  char *zIn = 0;                /* Input text */
          416  +  int nAlloc = 0;               /* Number of bytes allocated for zIn[] */
          417  +  int nIn = 0;                  /* Number of bytes of zIn[] used */
          418  +  size_t got;                   /* Bytes read from input */
          419  +  int rc = SQLITE_OK;           /* Result codes from API functions */
          420  +  int i;                        /* Loop counter */
          421  +  int iNext;                    /* Next block of SQL */
          422  +  sqlite3 *db;                  /* Open database */
          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);
   470         -    if( zIn==0 ) fatalError("out of memory");
   471         -    got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); 
   472         -    nIn += (int)got;
   473         -    zIn[nIn] = 0;
   474         -    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);
   480         -    }
          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));
   481    589     }
   482         -  for(i=0; i<nIn; i=iNext){
   483         -    char cSaved;
   484         -    if( strncmp(&zIn[i], "/****<",6)==0 ){
   485         -      char *z = strstr(&zIn[i], ">****/");
   486         -      if( z ){
   487         -        z += 6;
   488         -        printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
   489         -        i += (int)(z-&zIn[i]);
          590  +
          591  +  /* Initialize the input buffer used to hold SQL text */
          592  +  if( nInFile==0 ) nInFile = 1;
          593  +  nAlloc = 1000;
          594  +  zIn = malloc(nAlloc);
          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) ){
          616  +      got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); 
          617  +      nIn += (int)got;
          618  +      zIn[nIn] = 0;
          619  +      if( got==0 ) break;
          620  +      if( nAlloc - nIn - 1 < 100 ){
          621  +        nAlloc += nAlloc+1000;
          622  +        zIn = realloc(zIn, nAlloc);
          623  +        if( zIn==0 ) fatalError("out of memory");
          624  +      }
          625  +    }
          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){
          639  +      char cSaved;
          640  +      if( strncmp(&zIn[i], "/****<",6)==0 ){
          641  +        char *z = strstr(&zIn[i], ">****/");
          642  +        if( z ){
          643  +          z += 6;
          644  +          sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "%.*s", 
          645  +                           (int)(z-&zIn[i]) - 12, &zIn[i+6]);
          646  +          if( verboseFlag ){
          647  +            printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
          648  +            fflush(stdout);
          649  +          }
          650  +          i += (int)(z-&zIn[i]);
          651  +          multiTest = 1;
          652  +        }
          653  +      }
          654  +      for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
          655  +      cSaved = zIn[iNext];
          656  +      zIn[iNext] = 0;
          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;
          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{
          695  +        rc = sqlite3_open_v2(
          696  +          "main.db", &db,
          697  +          SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
          698  +          0);
          699  +        if( rc!=SQLITE_OK ){
          700  +          abendError("Unable to open the in-memory database");
          701  +        }
          702  +        if( pLook ){
          703  +          rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLook);
          704  +          if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
          705  +        }
          706  +    #ifndef SQLITE_OMIT_TRACE
          707  +        sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0);
          708  +    #endif
          709  +        sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
          710  +        sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
          711  +        sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
          712  +        if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
          713  +        if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
          714  +        if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
          715  +        iStart = timeOfDay();
          716  +        g.bOomEnable = 1;
          717  +        if( verboseFlag ){
          718  +          zErrMsg = 0;
          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  +      */
          774  +      if( zToFree ){
          775  +        sqlite3_free(zToFree);
          776  +        zToFree = 0;
          777  +      }
          778  +      zIn[iNext] = cSaved;
          779  +
          780  +      /* Show test-case results in --verbose mode
          781  +      */
          782  +      if( verboseFlag ){
          783  +        printf("RESULT-CODE: %d\n", rc);
          784  +        if( 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  +        }
   490    803         }
   491    804       }
   492         -    for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
   493         -    cSaved = zIn[iNext];
   494         -    zIn[iNext] = 0;
   495         -    if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
   496         -      zIn[iNext] = cSaved;
   497         -      continue;
   498         -    }
   499         -    rc = sqlite3_open_v2(
   500         -      "main.db", &db,
   501         -      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
   502         -      0);
   503         -    if( rc!=SQLITE_OK ){
   504         -      abendError("Unable to open the in-memory database");
   505         -    }
   506         -    if( pLook ){
   507         -      rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
   508         -      if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
   509         -    }
   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);
          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);
   519    831       }
   520         -    sqlite3_trace(db, traceCallback, 0);
   521         -    sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
   522         -    sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
   523         -    sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
   524         -    if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
   525         -    if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
   526         -    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         -    }
   541         -    rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg);
   542         -    if( zToFree ){
   543         -      sqlite3_free(zToFree);
   544         -      zToFree = 0;
   545         -    }
   546         -    zIn[iNext] = cSaved;
          832  +    fclose(out);
          833  +    sqlite3_finalize(pStmt);
          834  +    sqlite3_close(dataDb);
          835  +  }
   547    836   
   548         -    printf("RESULT-CODE: %d\n", rc);
   549         -    if( zErrMsg ){
   550         -      printf("ERROR-MSG: [%s]\n", zErrMsg);
   551         -      sqlite3_free(zErrMsg);
   552         -    }
   553         -    rc = sqlite3_close(db);
   554         -    if( rc ){
   555         -      abendError("sqlite3_close() failed with rc=%d", rc);
   556         -    }
   557         -    if( sqlite3_memory_used()>0 ){
   558         -      abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
   559         -    }
   560         -  }
          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         -  set addstatic 1
    33         -}
    34         -if {[lsearch $argv --linemacros]>=0} {
    35         -  set linemacros 1
    36         -} else {
    37         -  set linemacros 0
           29  +set addstatic 1
           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]} {
           36  +    set linemacros 1
           37  +  } else {
           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_unicode.c
   365    367      fts3_unicode2.c
   366    368   
   367    369      rtree.c
   368    370      icu.c
   369    371      fts3_icu.c
   370    372      sqlite3ota.c
          373  +   dbstat.c
   371    374   } {
   372    375     copy_file tsrc/$file
   373    376   }
   374    377   
   375    378   close $out

Changes to tool/spaceanal.tcl.

    84     84   
    85     85   # Open the database
    86     86   #
    87     87   if {[catch {sqlite3 db $file_to_analyze -uri 1} msg]} {
    88     88     puts stderr "error trying to open $file_to_analyze: $msg"
    89     89     exit 1
    90     90   }
    91         -register_dbstat_vtab db
    92     91   
    93     92   db eval {SELECT count(*) FROM sqlite_master}
    94     93   set pageSize [expr {wide([db one {PRAGMA page_size}])}]
    95     94   
    96     95   if {$flags(-pageinfo)} {
    97     96     db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat}
    98     97     db eval {SELECT name, path, pageno FROM temp.stat ORDER BY pageno} {

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];
         1142  +  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  1139   1143     for(i=1; i<argc; i++){
  1140   1144       const char *z = argv[i];
  1141   1145       if( z[0]=='-' ){
  1142   1146         z++;
  1143   1147         if( z[0]=='-' ) z++;
  1144   1148         if( strcmp(z,"changeset")==0 ){
         1149  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1145   1150           out = fopen(argv[++i], "wb");
  1146   1151           if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
  1147   1152           xDiff = changeset_one_table;
  1148   1153         }else
  1149   1154         if( strcmp(z,"debug")==0 ){
         1155  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1150   1156           g.fDebug = strtol(argv[++i], 0, 0);
  1151   1157         }else
  1152   1158         if( strcmp(z,"help")==0 ){
  1153   1159           showHelp();
  1154   1160           return 0;
  1155   1161         }else
         1162  +#ifndef SQLITE_OMIT_LOAD_EXTENSION
         1163  +      if( strcmp(z,"lib")==0 || strcmp(z,"L")==0 ){
         1164  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
         1165  +        azExt = realloc(azExt, sizeof(azExt[0])*(nExt+1));
         1166  +        if( azExt==0 ) cmdlineError("out of memory");
         1167  +        azExt[nExt++] = argv[++i];
         1168  +      }else
         1169  +#endif
  1156   1170         if( strcmp(z,"primarykey")==0 ){
  1157   1171           g.bSchemaPK = 1;
  1158   1172         }else
  1159   1173         if( strcmp(z,"schema")==0 ){
  1160   1174           g.bSchemaOnly = 1;
  1161   1175         }else
  1162   1176         if( strcmp(z,"summary")==0 ){
  1163   1177           xDiff = summarize_one_table;
  1164   1178         }else
  1165   1179         if( strcmp(z,"table")==0 ){
         1180  +        if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1166   1181           zTab = argv[++i];
  1167   1182         }else
  1168   1183         {
  1169   1184           cmdlineError("unknown option: %s", argv[i]);
  1170   1185         }
  1171   1186       }else if( zDb1==0 ){
  1172   1187         zDb1 = argv[i];
................................................................................
  1183   1198     if( rc ){
  1184   1199       cmdlineError("cannot open database file \"%s\"", zDb1);
  1185   1200     }
  1186   1201     rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg);
  1187   1202     if( rc || zErrMsg ){
  1188   1203       cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
  1189   1204     }
         1205  +#ifndef SQLITE_OMIT_LOAD_EXTENSION
         1206  +  sqlite3_enable_load_extension(g.db, 1);
         1207  +  for(i=0; i<nExt; i++){
         1208  +    rc = sqlite3_load_extension(g.db, azExt[i], 0, &zErrMsg);
         1209  +    if( rc || zErrMsg ){
         1210  +      cmdlineError("error loading %s: %s", azExt[i], zErrMsg);
         1211  +    }
         1212  +  }
         1213  +#endif
         1214  +  free(azExt);
  1190   1215     zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
  1191   1216     rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
  1192   1217     if( rc || zErrMsg ){
  1193   1218       cmdlineError("cannot attach database \"%s\"", zDb2);
  1194   1219     }
  1195   1220     rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
  1196   1221     if( rc || zErrMsg ){