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

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

Overview
Comment:Merge all changes for version 3.8.9 into the ota-update branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1: ec9d907a57fcea72c8a64e521e8a1b5777d67568
User & Date: drh 2015-04-08 14:01:07
Context
2015-04-15
14:26
Merge all recent trunk enhancements and fixes into the ota-update branch. check-in: 9bd3e445 user: drh tags: ota-update
2015-04-08
14:01
Merge all changes for version 3.8.9 into the ota-update branch. check-in: ec9d907a user: drh tags: ota-update
12:16
Version 3.8.9 check-in: 8a8ffc86 user: drh tags: trunk, release, version-3.8.9
2015-03-25
15:23
Extend [52e73eec] so that the IS optimization may be used on primary keys with more than 3 columns. check-in: 4e8796af user: dan tags: ota-update
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

  1055   1055   	$(INSTALL) -d $(DESTDIR)$(includedir)
  1056   1056   	$(INSTALL) -m 0644 sqlite3.h $(DESTDIR)$(includedir)
  1057   1057   	$(INSTALL) -m 0644 $(TOP)/src/sqlite3ext.h $(DESTDIR)$(includedir)
  1058   1058   	$(INSTALL) -d $(DESTDIR)$(pkgconfigdir)
  1059   1059   	$(INSTALL) -m 0644 sqlite3.pc $(DESTDIR)$(pkgconfigdir)
  1060   1060   
  1061   1061   pkgIndex.tcl:
  1062         -	echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3.so sqlite3]' > $@
         1062  +	echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3$(SHLIB_SUFFIX) sqlite3]' > $@
  1063   1063   tcl_install:	lib_install libtclsqlite3.la pkgIndex.tcl
  1064   1064   	$(INSTALL) -d $(DESTDIR)$(TCLLIBDIR)
  1065   1065   	$(LTINSTALL) libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR)
  1066   1066   	rm -f $(DESTDIR)$(TCLLIBDIR)/libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR)/libtclsqlite3.a
  1067   1067   	$(INSTALL) -m 0644 pkgIndex.tcl $(DESTDIR)$(TCLLIBDIR)
  1068   1068   
  1069   1069   clean:	

Changes to Makefile.msc.

   308    308   # also be noted here that building any target with these "stdcall" options
   309    309   # will most likely fail if the Tcl library is also required.  This is due
   310    310   # to how the Tcl library functions are declared and exported (i.e. without
   311    311   # an explicit calling convention, which results in "cdecl").
   312    312   #
   313    313   !IF $(USE_STDCALL)!=0
   314    314   !IF "$(PLATFORM)"=="x86"
   315         -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl
   316         -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl
          315  +CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
          316  +SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
   317    317   !ELSE
   318    318   !IFNDEF PLATFORM
   319         -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl
   320         -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl
          319  +CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
          320  +SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
   321    321   !ELSE
   322    322   CORE_CCONV_OPTS =
   323    323   SHELL_CCONV_OPTS =
   324    324   !ENDIF
   325    325   !ENDIF
   326    326   !ELSE
   327    327   CORE_CCONV_OPTS =
   328    328   SHELL_CCONV_OPTS =
   329    329   !ENDIF
   330    330   
   331    331   # These are additional compiler options used for the core library.
   332    332   #
   333    333   !IFNDEF CORE_COMPILE_OPTS
   334         -!IF $(USE_STDCALL)!=0
          334  +!IF $(DYNAMIC_SHELL)!=0
   335    335   CORE_COMPILE_OPTS = $(CORE_CCONV_OPTS) -DSQLITE_API=__declspec(dllexport)
   336    336   !ELSE
   337    337   CORE_COMPILE_OPTS = $(CORE_CCONV_OPTS)
   338    338   !ENDIF
   339    339   !ENDIF
   340    340   
   341    341   # These are the additional targets that the core library should depend on
   342    342   # when linking.
   343    343   #
   344    344   !IFNDEF CORE_LINK_DEP
   345         -!IF $(USE_STDCALL)!=0
          345  +!IF $(DYNAMIC_SHELL)!=0
   346    346   CORE_LINK_DEP =
   347    347   !ELSE
   348    348   CORE_LINK_DEP = sqlite3.def
   349    349   !ENDIF
   350    350   !ENDIF
   351    351   
   352    352   # These are additional linker options used for the core library.
   353    353   #
   354    354   !IFNDEF CORE_LINK_OPTS
   355         -!IF $(USE_STDCALL)!=0
          355  +!IF $(DYNAMIC_SHELL)!=0
   356    356   CORE_LINK_OPTS =
   357    357   !ELSE
   358    358   CORE_LINK_OPTS = /DEF:sqlite3.def
   359    359   !ENDIF
   360    360   !ENDIF
   361    361   
   362    362   # These are additional compiler options used for the shell executable.
................................................................................
  1153   1153   sqlite3.exe:	$(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
  1154   1154   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c \
  1155   1155   		/link /pdb:sqlite3sh.pdb $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
  1156   1156   
  1157   1157   mptester.exe:	$(TOP)\mptest\mptest.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
  1158   1158   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(TOP)\mptest\mptest.c \
  1159   1159   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(SHELL_LINK_OPTS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
         1160  +
         1161  +MPTEST1 = mptester mptest.db $(TOP)/mptest/crash01.test --repeat 20
         1162  +MPTEST2 = mptester mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
         1163  +
         1164  +mptest:	mptester.exe
         1165  +	del /Q mptest.db 2>NUL
         1166  +	$(MPTEST1) --journalmode DELETE
         1167  +	$(MPTEST2) --journalmode WAL
         1168  +	$(MPTEST1) --journalmode WAL
         1169  +	$(MPTEST2) --journalmode PERSIST
         1170  +	$(MPTEST1) --journalmode PERSIST
         1171  +	$(MPTEST2) --journalmode TRUNCATE
         1172  +	$(MPTEST1) --journalmode TRUNCATE
         1173  +	$(MPTEST2) --journalmode DELETE
  1160   1174   
  1161   1175   # This target creates a directory named "tsrc" and fills it with
  1162   1176   # copies of all of the C source code and header files needed to
  1163   1177   # build on the target system.  Some of the C source code and header
  1164   1178   # files are automatically generated.  This target takes care of
  1165   1179   # all that automatic generation.
  1166   1180   #

Changes to configure.

   880    880   TCL_INCLUDE_SPEC
   881    881   TCL_LIB_FILE
   882    882   TCL_LIB_FLAG
   883    883   TCL_LIB_SPEC
   884    884   TCL_STUB_LIB_FILE
   885    885   TCL_STUB_LIB_FLAG
   886    886   TCL_STUB_LIB_SPEC
          887  +TCL_SHLIB_SUFFIX
   887    888   HAVE_TCL
   888    889   TARGET_READLINE_LIBS
   889    890   TARGET_READLINE_INC
   890    891   TARGET_HAVE_READLINE
   891    892   TARGET_DEBUG
   892    893   USE_AMALGAMATION
   893    894   OPT_FEATURE_FLAGS
................................................................................
  3720   3721   { $as_echo "$as_me:$LINENO: checking the name lister ($NM) interface" >&5
  3721   3722   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3722   3723   if test "${lt_cv_nm_interface+set}" = set; then
  3723   3724     $as_echo_n "(cached) " >&6
  3724   3725   else
  3725   3726     lt_cv_nm_interface="BSD nm"
  3726   3727     echo "int some_variable = 0;" > conftest.$ac_ext
  3727         -  (eval echo "\"\$as_me:3727: $ac_compile\"" >&5)
         3728  +  (eval echo "\"\$as_me:3728: $ac_compile\"" >&5)
  3728   3729     (eval "$ac_compile" 2>conftest.err)
  3729   3730     cat conftest.err >&5
  3730         -  (eval echo "\"\$as_me:3730: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3731  +  (eval echo "\"\$as_me:3731: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3731   3732     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3732   3733     cat conftest.err >&5
  3733         -  (eval echo "\"\$as_me:3733: output\"" >&5)
         3734  +  (eval echo "\"\$as_me:3734: output\"" >&5)
  3734   3735     cat conftest.out >&5
  3735   3736     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3736   3737       lt_cv_nm_interface="MS dumpbin"
  3737   3738     fi
  3738   3739     rm -f conftest*
  3739   3740   fi
  3740   3741   { $as_echo "$as_me:$LINENO: result: $lt_cv_nm_interface" >&5
................................................................................
  4948   4949   	;;
  4949   4950       esac
  4950   4951     fi
  4951   4952     rm -rf conftest*
  4952   4953     ;;
  4953   4954   *-*-irix6*)
  4954   4955     # Find out which ABI we are using.
  4955         -  echo '#line 4955 "configure"' > conftest.$ac_ext
         4956  +  echo '#line 4956 "configure"' > conftest.$ac_ext
  4956   4957     if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
  4957   4958     (eval $ac_compile) 2>&5
  4958   4959     ac_status=$?
  4959   4960     $as_echo "$as_me:$LINENO: \$? = $ac_status" >&5
  4960   4961     (exit $ac_status); }; then
  4961   4962       if test "$lt_cv_prog_gnu_ld" = yes; then
  4962   4963         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  6817   6818      # Note that $ac_compile itself does not contain backslashes and begins
  6818   6819      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6819   6820      # The option is referenced via a variable to avoid confusing sed.
  6820   6821      lt_compile=`echo "$ac_compile" | $SED \
  6821   6822      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6822   6823      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6823   6824      -e 's:$: $lt_compiler_flag:'`
  6824         -   (eval echo "\"\$as_me:6824: $lt_compile\"" >&5)
         6825  +   (eval echo "\"\$as_me:6825: $lt_compile\"" >&5)
  6825   6826      (eval "$lt_compile" 2>conftest.err)
  6826   6827      ac_status=$?
  6827   6828      cat conftest.err >&5
  6828         -   echo "$as_me:6828: \$? = $ac_status" >&5
         6829  +   echo "$as_me:6829: \$? = $ac_status" >&5
  6829   6830      if (exit $ac_status) && test -s "$ac_outfile"; then
  6830   6831        # The compiler can only warn and ignore the option if not recognized
  6831   6832        # So say no if there are warnings other than the usual output.
  6832   6833        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6833   6834        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6834   6835        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6835   6836          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  7156   7157      # Note that $ac_compile itself does not contain backslashes and begins
  7157   7158      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7158   7159      # The option is referenced via a variable to avoid confusing sed.
  7159   7160      lt_compile=`echo "$ac_compile" | $SED \
  7160   7161      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7161   7162      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7162   7163      -e 's:$: $lt_compiler_flag:'`
  7163         -   (eval echo "\"\$as_me:7163: $lt_compile\"" >&5)
         7164  +   (eval echo "\"\$as_me:7164: $lt_compile\"" >&5)
  7164   7165      (eval "$lt_compile" 2>conftest.err)
  7165   7166      ac_status=$?
  7166   7167      cat conftest.err >&5
  7167         -   echo "$as_me:7167: \$? = $ac_status" >&5
         7168  +   echo "$as_me:7168: \$? = $ac_status" >&5
  7168   7169      if (exit $ac_status) && test -s "$ac_outfile"; then
  7169   7170        # The compiler can only warn and ignore the option if not recognized
  7170   7171        # So say no if there are warnings other than the usual output.
  7171   7172        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  7172   7173        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  7173   7174        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  7174   7175          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7261   7262      # (2) before a word containing "conftest.", or (3) at the end.
  7262   7263      # Note that $ac_compile itself does not contain backslashes and begins
  7263   7264      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7264   7265      lt_compile=`echo "$ac_compile" | $SED \
  7265   7266      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7266   7267      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7267   7268      -e 's:$: $lt_compiler_flag:'`
  7268         -   (eval echo "\"\$as_me:7268: $lt_compile\"" >&5)
         7269  +   (eval echo "\"\$as_me:7269: $lt_compile\"" >&5)
  7269   7270      (eval "$lt_compile" 2>out/conftest.err)
  7270   7271      ac_status=$?
  7271   7272      cat out/conftest.err >&5
  7272         -   echo "$as_me:7272: \$? = $ac_status" >&5
         7273  +   echo "$as_me:7273: \$? = $ac_status" >&5
  7273   7274      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7274   7275      then
  7275   7276        # The compiler can only warn and ignore the option if not recognized
  7276   7277        # So say no if there are warnings
  7277   7278        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7278   7279        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7279   7280        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7316   7317      # (2) before a word containing "conftest.", or (3) at the end.
  7317   7318      # Note that $ac_compile itself does not contain backslashes and begins
  7318   7319      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7319   7320      lt_compile=`echo "$ac_compile" | $SED \
  7320   7321      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7321   7322      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7322   7323      -e 's:$: $lt_compiler_flag:'`
  7323         -   (eval echo "\"\$as_me:7323: $lt_compile\"" >&5)
         7324  +   (eval echo "\"\$as_me:7324: $lt_compile\"" >&5)
  7324   7325      (eval "$lt_compile" 2>out/conftest.err)
  7325   7326      ac_status=$?
  7326   7327      cat out/conftest.err >&5
  7327         -   echo "$as_me:7327: \$? = $ac_status" >&5
         7328  +   echo "$as_me:7328: \$? = $ac_status" >&5
  7328   7329      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7329   7330      then
  7330   7331        # The compiler can only warn and ignore the option if not recognized
  7331   7332        # So say no if there are warnings
  7332   7333        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7333   7334        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7334   7335        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
 10129  10130   else
 10130  10131     	  if test "$cross_compiling" = yes; then :
 10131  10132     lt_cv_dlopen_self=cross
 10132  10133   else
 10133  10134     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
 10134  10135     lt_status=$lt_dlunknown
 10135  10136     cat > conftest.$ac_ext <<_LT_EOF
 10136         -#line 10136 "configure"
        10137  +#line 10137 "configure"
 10137  10138   #include "confdefs.h"
 10138  10139   
 10139  10140   #if HAVE_DLFCN_H
 10140  10141   #include <dlfcn.h>
 10141  10142   #endif
 10142  10143   
 10143  10144   #include <stdio.h>
................................................................................
 10225  10226   else
 10226  10227     	  if test "$cross_compiling" = yes; then :
 10227  10228     lt_cv_dlopen_self_static=cross
 10228  10229   else
 10229  10230     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
 10230  10231     lt_status=$lt_dlunknown
 10231  10232     cat > conftest.$ac_ext <<_LT_EOF
 10232         -#line 10232 "configure"
        10233  +#line 10233 "configure"
 10233  10234   #include "confdefs.h"
 10234  10235   
 10235  10236   #if HAVE_DLFCN_H
 10236  10237   #include <dlfcn.h>
 10237  10238   #endif
 10238  10239   
 10239  10240   #include <stdio.h>
................................................................................
 12810  12811       eval "TCL_LIB_FILE=\"${TCL_LIB_FILE}\""
 12811  12812       eval "TCL_LIB_FLAG=\"${TCL_LIB_FLAG}\""
 12812  12813       eval "TCL_LIB_SPEC=\"${TCL_LIB_SPEC}\""
 12813  12814   
 12814  12815       eval "TCL_STUB_LIB_FILE=\"${TCL_STUB_LIB_FILE}\""
 12815  12816       eval "TCL_STUB_LIB_FLAG=\"${TCL_STUB_LIB_FLAG}\""
 12816  12817       eval "TCL_STUB_LIB_SPEC=\"${TCL_STUB_LIB_SPEC}\""
        12818  +
 12817  12819   
 12818  12820   
 12819  12821   
 12820  12822   
 12821  12823   
 12822  12824   
 12823  12825   

Changes to configure.ac.

   426    426       AC_SUBST(TCL_LIB_FILE)
   427    427       AC_SUBST(TCL_LIB_FLAG)
   428    428       AC_SUBST(TCL_LIB_SPEC)
   429    429       
   430    430       AC_SUBST(TCL_STUB_LIB_FILE)
   431    431       AC_SUBST(TCL_STUB_LIB_FLAG)
   432    432       AC_SUBST(TCL_STUB_LIB_SPEC)
          433  +    AC_SUBST(TCL_SHLIB_SUFFIX)
   433    434     fi
   434    435   fi
   435    436   if test "${use_tcl}" = "no" ; then
   436    437     HAVE_TCL=""
   437    438   else
   438    439     HAVE_TCL=1
   439    440   fi

Changes to ext/fts3/fts3.c.

  1104   1104     char *zCsr;                     /* Space for holding column names */
  1105   1105     int nDb;                        /* Bytes required to hold database name */
  1106   1106     int nName;                      /* Bytes required to hold table name */
  1107   1107     int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
  1108   1108     const char **aCol;              /* Array of column names */
  1109   1109     sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
  1110   1110   
  1111         -  int nIndex;                     /* Size of aIndex[] array */
         1111  +  int nIndex = 0;                 /* Size of aIndex[] array */
  1112   1112     struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
  1113   1113   
  1114   1114     /* The results of parsing supported FTS4 key=value options: */
  1115   1115     int bNoDocsize = 0;             /* True to omit %_docsize table */
  1116   1116     int bDescIdx = 0;               /* True to store descending indexes */
  1117   1117     char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
  1118   1118     char *zCompress = 0;            /* compress=? parameter (or NULL) */
................................................................................
  2498   2498   **
  2499   2499   ** If the docids in the input doclists are sorted in ascending order,
  2500   2500   ** parameter bDescDoclist should be false. If they are sorted in ascending 
  2501   2501   ** order, it should be passed a non-zero value.
  2502   2502   **
  2503   2503   ** The right-hand input doclist is overwritten by this function.
  2504   2504   */
  2505         -static void fts3DoclistPhraseMerge(
         2505  +static int fts3DoclistPhraseMerge(
  2506   2506     int bDescDoclist,               /* True if arguments are desc */
  2507   2507     int nDist,                      /* Distance from left to right (1=adjacent) */
  2508   2508     char *aLeft, int nLeft,         /* Left doclist */
  2509         -  char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
         2509  +  char **paRight, int *pnRight    /* IN/OUT: Right/output doclist */
  2510   2510   ){
  2511   2511     sqlite3_int64 i1 = 0;
  2512   2512     sqlite3_int64 i2 = 0;
  2513   2513     sqlite3_int64 iPrev = 0;
         2514  +  char *aRight = *paRight;
  2514   2515     char *pEnd1 = &aLeft[nLeft];
  2515   2516     char *pEnd2 = &aRight[*pnRight];
  2516   2517     char *p1 = aLeft;
  2517   2518     char *p2 = aRight;
  2518   2519     char *p;
  2519   2520     int bFirstOut = 0;
  2520         -  char *aOut = aRight;
         2521  +  char *aOut;
  2521   2522   
  2522   2523     assert( nDist>0 );
  2523         -
         2524  +  if( bDescDoclist ){
         2525  +    aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
         2526  +    if( aOut==0 ) return SQLITE_NOMEM;
         2527  +  }else{
         2528  +    aOut = aRight;
         2529  +  }
  2524   2530     p = aOut;
         2531  +
  2525   2532     fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  2526   2533     fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  2527   2534   
  2528   2535     while( p1 && p2 ){
  2529   2536       sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  2530   2537       if( iDiff==0 ){
  2531   2538         char *pSave = p;
................................................................................
  2546   2553       }else{
  2547   2554         fts3PoslistCopy(0, &p2);
  2548   2555         fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2549   2556       }
  2550   2557     }
  2551   2558   
  2552   2559     *pnRight = (int)(p - aOut);
         2560  +  if( bDescDoclist ){
         2561  +    sqlite3_free(aRight);
         2562  +    *paRight = aOut;
         2563  +  }
         2564  +
         2565  +  return SQLITE_OK;
  2553   2566   }
  2554   2567   
  2555   2568   /*
  2556   2569   ** Argument pList points to a position list nList bytes in size. This
  2557   2570   ** function checks to see if the position list contains any entries for
  2558   2571   ** a token in position 0 (of any column). If so, it writes argument iDelta
  2559   2572   ** to the output buffer pOut, followed by a position list consisting only
................................................................................
  2670   2683     Fts3Table *p,                   /* FTS table handle */
  2671   2684     TermSelect *pTS,                /* TermSelect object to merge into */
  2672   2685     char *aDoclist,                 /* Pointer to doclist */
  2673   2686     int nDoclist                    /* Size of aDoclist in bytes */
  2674   2687   ){
  2675   2688     if( pTS->aaOutput[0]==0 ){
  2676   2689       /* If this is the first term selected, copy the doclist to the output
  2677         -    ** buffer using memcpy(). */
  2678         -    pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
         2690  +    ** buffer using memcpy(). 
         2691  +    **
         2692  +    ** Add FTS3_VARINT_MAX bytes of unused space to the end of the 
         2693  +    ** allocation. This is so as to ensure that the buffer is big enough
         2694  +    ** to hold the current doclist AND'd with any other doclist. If the
         2695  +    ** doclists are stored in order=ASC order, this padding would not be
         2696  +    ** required (since the size of [doclistA AND doclistB] is always less
         2697  +    ** than or equal to the size of [doclistA] in that case). But this is
         2698  +    ** not true for order=DESC. For example, a doclist containing (1, -1) 
         2699  +    ** may be smaller than (-1), as in the first example the -1 may be stored
         2700  +    ** as a single-byte delta, whereas in the second it must be stored as a
         2701  +    ** FTS3_VARINT_MAX byte varint.
         2702  +    **
         2703  +    ** Similar padding is added in the fts3DoclistOrMerge() function.
         2704  +    */
         2705  +    pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
  2679   2706       pTS->anOutput[0] = nDoclist;
  2680   2707       if( pTS->aaOutput[0] ){
  2681   2708         memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
  2682   2709       }else{
  2683   2710         return SQLITE_NOMEM;
  2684   2711       }
  2685   2712     }else{
................................................................................
  3927   3954   /*
  3928   3955   ** Arguments pList/nList contain the doclist for token iToken of phrase p.
  3929   3956   ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
  3930   3957   **
  3931   3958   ** This function assumes that pList points to a buffer allocated using
  3932   3959   ** sqlite3_malloc(). This function takes responsibility for eventually
  3933   3960   ** freeing the buffer.
         3961  +**
         3962  +** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
  3934   3963   */
  3935         -static void fts3EvalPhraseMergeToken(
         3964  +static int fts3EvalPhraseMergeToken(
  3936   3965     Fts3Table *pTab,                /* FTS Table pointer */
  3937   3966     Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
  3938   3967     int iToken,                     /* Token pList/nList corresponds to */
  3939   3968     char *pList,                    /* Pointer to doclist */
  3940   3969     int nList                       /* Number of bytes in pList */
  3941   3970   ){
         3971  +  int rc = SQLITE_OK;
  3942   3972     assert( iToken!=p->iDoclistToken );
  3943   3973   
  3944   3974     if( pList==0 ){
  3945   3975       sqlite3_free(p->doclist.aAll);
  3946   3976       p->doclist.aAll = 0;
  3947   3977       p->doclist.nAll = 0;
  3948   3978     }
................................................................................
  3973   4003         pRight = p->doclist.aAll;
  3974   4004         nRight = p->doclist.nAll;
  3975   4005         pLeft = pList;
  3976   4006         nLeft = nList;
  3977   4007         nDiff = p->iDoclistToken - iToken;
  3978   4008       }
  3979   4009   
  3980         -    fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
         4010  +    rc = fts3DoclistPhraseMerge(
         4011  +        pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
         4012  +    );
  3981   4013       sqlite3_free(pLeft);
  3982   4014       p->doclist.aAll = pRight;
  3983   4015       p->doclist.nAll = nRight;
  3984   4016     }
  3985   4017   
  3986   4018     if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
         4019  +  return rc;
  3987   4020   }
  3988   4021   
  3989   4022   /*
  3990   4023   ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
  3991   4024   ** does not take deferred tokens into account.
  3992   4025   **
  3993   4026   ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
................................................................................
  4005   4038       assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
  4006   4039   
  4007   4040       if( pToken->pSegcsr ){
  4008   4041         int nThis = 0;
  4009   4042         char *pThis = 0;
  4010   4043         rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
  4011   4044         if( rc==SQLITE_OK ){
  4012         -        fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
         4045  +        rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
  4013   4046         }
  4014   4047       }
  4015   4048       assert( pToken->pSegcsr==0 );
  4016   4049     }
  4017   4050   
  4018   4051     return rc;
  4019   4052   }
................................................................................
  4807   4840           ** part of a multi-token phrase. Either way, the entire doclist will
  4808   4841           ** (eventually) be loaded into memory. It may as well be now. */
  4809   4842           Fts3PhraseToken *pToken = pTC->pToken;
  4810   4843           int nList = 0;
  4811   4844           char *pList = 0;
  4812   4845           rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
  4813   4846           assert( rc==SQLITE_OK || pList==0 );
         4847  +        if( rc==SQLITE_OK ){
         4848  +          rc = fts3EvalPhraseMergeToken(
         4849  +              pTab, pTC->pPhrase, pTC->iToken,pList,nList
         4850  +          );
         4851  +        }
  4814   4852           if( rc==SQLITE_OK ){
  4815   4853             int nCount;
  4816         -          fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
  4817   4854             nCount = fts3DoclistCountDocids(
  4818   4855                 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
  4819   4856             );
  4820   4857             if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
  4821   4858           }
  4822   4859         }
  4823   4860       }

Changes to main.mk.

   407    407   	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE)                  \
   408    408   		$(TOP)/src/shell.c                                  \
   409    409   		libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
   410    410   
   411    411   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   412    412   	$(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   413    413   		$(TLIBS) $(THREADLIB)
          414  +
          415  +MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
          416  +MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
          417  +mptest:	mptester$(EXE)
          418  +	rm -f mptest.db
          419  +	$(MPTEST1) --journalmode DELETE
          420  +	$(MPTEST2) --journalmode WAL
          421  +	$(MPTEST1) --journalmode WAL
          422  +	$(MPTEST2) --journalmode PERSIST
          423  +	$(MPTEST1) --journalmode PERSIST
          424  +	$(MPTEST2) --journalmode TRUNCATE
          425  +	$(MPTEST1) --journalmode TRUNCATE
          426  +	$(MPTEST2) --journalmode DELETE
   414    427   
   415    428   sqlite3.o:	sqlite3.c
   416    429   	$(TCCX) -I. -c sqlite3.c
   417    430   
   418    431   # This target creates a directory named "tsrc" and fills it with
   419    432   # copies of all of the C source code and header files needed to
   420    433   # build on the target system.  Some of the C source code and header

Changes to mptest/mptest.c.

  1308   1308     if( zClient ){
  1309   1309       iClient = atoi(zClient);
  1310   1310       if( iClient<1 ) fatalError("illegal client number: %d\n", iClient);
  1311   1311       sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d",
  1312   1312                        GETPID(), iClient);
  1313   1313     }else{
  1314   1314       if( g.iTrace>0 ){
         1315  +      printf("BEGIN: %s", argv[0]);
         1316  +      for(i=1; i<argc; i++) printf(" %s", argv[i]);
         1317  +      printf("\n");
  1315   1318         printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
  1316   1319         for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
  1317   1320           printf("-DSQLITE_%s\n", zCOption);
  1318   1321         }
  1319   1322         fflush(stdout);
  1320   1323       }
  1321   1324       iClient =  0;
  1322   1325       unlink(g.zDbFile);
  1323   1326       openFlags |= SQLITE_OPEN_CREATE;
  1324   1327     }
  1325   1328     rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
  1326   1329     if( rc ) fatalError("cannot open [%s]", g.zDbFile);
         1330  +  if( zJMode ){
         1331  +#if defined(_WIN32)
         1332  +    if( sqlite3_stricmp(zJMode,"persist")==0
         1333  +     || sqlite3_stricmp(zJMode,"truncate")==0
         1334  +    ){
         1335  +      printf("Changing journal mode to DELETE from %s", zJMode);
         1336  +      zJMode = "DELETE";
         1337  +    }
         1338  +#endif
         1339  +    runSql("PRAGMA journal_mode=%Q;", zJMode);
         1340  +  }
  1327   1341     sqlite3_enable_load_extension(g.db, 1);
  1328   1342     sqlite3_busy_handler(g.db, busyHandler, 0);
  1329   1343     sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
  1330   1344                             vfsNameFunc, 0, 0);
  1331   1345     sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0,
  1332   1346                             evalFunc, 0, 0);
  1333   1347     g.iTimeout = DEFAULT_TIMEOUT;
................................................................................
  1351   1365     }else{
  1352   1366       sqlite3_stmt *pStmt;
  1353   1367       int iTimeout;
  1354   1368       if( n==0 ){
  1355   1369         fatalError("missing script filename");
  1356   1370       }
  1357   1371       if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
  1358         -    if( zJMode ) runSql("PRAGMA journal_mode=%Q;", zJMode);
  1359   1372       runSql(
  1360   1373         "DROP TABLE IF EXISTS task;\n"
  1361   1374         "DROP TABLE IF EXISTS counters;\n"
  1362   1375         "DROP TABLE IF EXISTS client;\n"
  1363   1376         "CREATE TABLE task(\n"
  1364   1377         "  id INTEGER PRIMARY KEY,\n"
  1365   1378         "  name TEXT,\n"
................................................................................
  1405   1418       sqlite3_finalize(pStmt);
  1406   1419     }
  1407   1420     sqlite3_close(g.db);
  1408   1421     maybeClose(g.pLog);
  1409   1422     maybeClose(g.pErrLog);
  1410   1423     if( iClient==0 ){
  1411   1424       printf("Summary: %d errors out of %d tests\n", g.nError, g.nTest);
         1425  +    printf("END: %s", argv[0]);
         1426  +    for(i=1; i<argc; i++) printf(" %s", argv[i]);
         1427  +    printf("\n");
  1412   1428     }
  1413   1429     return g.nError>0;
  1414   1430   }

Changes to src/btree.c.

   596    596   **
   597    597   ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
   598    598   ** prior to calling this routine.  
   599    599   */
   600    600   static int saveCursorPosition(BtCursor *pCur){
   601    601     int rc;
   602    602   
   603         -  assert( CURSOR_VALID==pCur->eState );
          603  +  assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
   604    604     assert( 0==pCur->pKey );
   605    605     assert( cursorHoldsMutex(pCur) );
   606    606   
          607  +  if( pCur->eState==CURSOR_SKIPNEXT ){
          608  +    pCur->eState = CURSOR_VALID;
          609  +  }else{
          610  +    pCur->skipNext = 0;
          611  +  }
   607    612     rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
   608    613     assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
   609    614   
   610    615     /* If this is an intKey table, then the above call to BtreeKeySize()
   611    616     ** stores the integer key in pCur->nKey. In this case this value is
   612    617     ** all that is required. Otherwise, if pCur is not open on an intKey
   613    618     ** table, then malloc space for and store the pCur->nKey bytes of key 
................................................................................
   670    675   static int SQLITE_NOINLINE saveCursorsOnList(
   671    676     BtCursor *p,         /* The first cursor that needs saving */
   672    677     Pgno iRoot,          /* Only save cursor with this iRoot. Save all if zero */
   673    678     BtCursor *pExcept    /* Do not save this cursor */
   674    679   ){
   675    680     do{
   676    681       if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
   677         -      if( p->eState==CURSOR_VALID ){
          682  +      if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
   678    683           int rc = saveCursorPosition(p);
   679    684           if( SQLITE_OK!=rc ){
   680    685             return rc;
   681    686           }
   682    687         }else{
   683    688           testcase( p->iPage>0 );
   684    689           btreeReleaseAllCursorPages(p);
................................................................................
   742    747   ** when saveCursorPosition() was called. Note that this call deletes the 
   743    748   ** saved position info stored by saveCursorPosition(), so there can be
   744    749   ** at most one effective restoreCursorPosition() call after each 
   745    750   ** saveCursorPosition().
   746    751   */
   747    752   static int btreeRestoreCursorPosition(BtCursor *pCur){
   748    753     int rc;
          754  +  int skipNext;
   749    755     assert( cursorHoldsMutex(pCur) );
   750    756     assert( pCur->eState>=CURSOR_REQUIRESEEK );
   751    757     if( pCur->eState==CURSOR_FAULT ){
   752    758       return pCur->skipNext;
   753    759     }
   754    760     pCur->eState = CURSOR_INVALID;
   755         -  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
          761  +  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
   756    762     if( rc==SQLITE_OK ){
   757    763       sqlite3_free(pCur->pKey);
   758    764       pCur->pKey = 0;
   759    765       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
          766  +    pCur->skipNext |= skipNext;
   760    767       if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
   761    768         pCur->eState = CURSOR_SKIPNEXT;
   762    769       }
   763    770     }
   764    771     return rc;
   765    772   }
   766    773   
................................................................................
   804    811     assert( pCur!=0 );
   805    812     assert( pCur->eState!=CURSOR_VALID );
   806    813     rc = restoreCursorPosition(pCur);
   807    814     if( rc ){
   808    815       *pDifferentRow = 1;
   809    816       return rc;
   810    817     }
   811         -  if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
          818  +  if( pCur->eState!=CURSOR_VALID ){
   812    819       *pDifferentRow = 1;
   813    820     }else{
          821  +    assert( pCur->skipNext==0 );
   814    822       *pDifferentRow = 0;
   815    823     }
   816    824     return SQLITE_OK;
   817    825   }
   818    826   
   819    827   #ifndef SQLITE_OMIT_AUTOVACUUM
   820    828   /*
................................................................................
  1947   1955   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1948   1956     /*
  1949   1957     ** If this Btree is a candidate for shared cache, try to find an
  1950   1958     ** existing BtShared object that we can share with
  1951   1959     */
  1952   1960     if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
  1953   1961       if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
         1962  +      int nFilename = sqlite3Strlen30(zFilename)+1;
  1954   1963         int nFullPathname = pVfs->mxPathname+1;
  1955         -      char *zFullPathname = sqlite3Malloc(nFullPathname);
         1964  +      char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
  1956   1965         MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
         1966  +
  1957   1967         p->sharable = 1;
  1958   1968         if( !zFullPathname ){
  1959   1969           sqlite3_free(p);
  1960   1970           return SQLITE_NOMEM;
  1961   1971         }
  1962   1972         if( isMemdb ){
  1963         -        memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
         1973  +        memcpy(zFullPathname, zFilename, nFilename);
  1964   1974         }else{
  1965   1975           rc = sqlite3OsFullPathname(pVfs, zFilename,
  1966   1976                                      nFullPathname, zFullPathname);
  1967   1977           if( rc ){
  1968   1978             sqlite3_free(zFullPathname);
  1969   1979             sqlite3_free(p);
  1970   1980             return rc;
................................................................................
  3621   3631   
  3622   3632     assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
  3623   3633     if( pBtree ){
  3624   3634       sqlite3BtreeEnter(pBtree);
  3625   3635       for(p=pBtree->pBt->pCursor; p; p=p->pNext){
  3626   3636         int i;
  3627   3637         if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
  3628         -        if( p->eState==CURSOR_VALID ){
         3638  +        if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
  3629   3639             rc = saveCursorPosition(p);
  3630   3640             if( rc!=SQLITE_OK ){
  3631   3641               (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
  3632   3642               break;
  3633   3643             }
  3634   3644           }
  3635   3645         }else{
................................................................................
  4027   4037   ** Failure is not possible.  This function always returns SQLITE_OK.
  4028   4038   ** It might just as well be a procedure (returning void) but we continue
  4029   4039   ** to return an integer result code for historical reasons.
  4030   4040   */
  4031   4041   int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
  4032   4042     assert( cursorHoldsMutex(pCur) );
  4033   4043     assert( pCur->eState==CURSOR_VALID );
         4044  +  assert( pCur->iPage>=0 );
         4045  +  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
  4034   4046     assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
  4035   4047     getCellInfo(pCur);
  4036   4048     *pSize = pCur->info.nPayload;
  4037   4049     return SQLITE_OK;
  4038   4050   }
  4039   4051   
  4040   4052   /*
................................................................................
  4505   4517     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  4506   4518     if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
  4507   4519       return SQLITE_CORRUPT_BKPT;
  4508   4520     }
  4509   4521     return SQLITE_OK;
  4510   4522   }
  4511   4523   
  4512         -#if 0
         4524  +#if SQLITE_DEBUG
  4513   4525   /*
  4514   4526   ** Page pParent is an internal (non-leaf) tree page. This function 
  4515   4527   ** asserts that page number iChild is the left-child if the iIdx'th
  4516   4528   ** cell in page pParent. Or, if iIdx is equal to the total number of
  4517   4529   ** cells in pParent, that page number iChild is the right-child of
  4518   4530   ** the page.
  4519   4531   */
  4520   4532   static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
         4533  +  if( CORRUPT_DB ) return;  /* The conditions tested below might not be true
         4534  +                            ** in a corrupt database */
  4521   4535     assert( iIdx<=pParent->nCell );
  4522   4536     if( iIdx==pParent->nCell ){
  4523   4537       assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
  4524   4538     }else{
  4525   4539       assert( get4byte(findCell(pParent, iIdx))==iChild );
  4526   4540     }
  4527   4541   }
................................................................................
  4538   4552   ** the largest cell index.
  4539   4553   */
  4540   4554   static void moveToParent(BtCursor *pCur){
  4541   4555     assert( cursorHoldsMutex(pCur) );
  4542   4556     assert( pCur->eState==CURSOR_VALID );
  4543   4557     assert( pCur->iPage>0 );
  4544   4558     assert( pCur->apPage[pCur->iPage] );
  4545         -
  4546         -  /* UPDATE: It is actually possible for the condition tested by the assert
  4547         -  ** below to be untrue if the database file is corrupt. This can occur if
  4548         -  ** one cursor has modified page pParent while a reference to it is held 
  4549         -  ** by a second cursor. Which can only happen if a single page is linked
  4550         -  ** into more than one b-tree structure in a corrupt database.  */
  4551         -#if 0
  4552   4559     assertParentIndex(
  4553   4560       pCur->apPage[pCur->iPage-1], 
  4554   4561       pCur->aiIdx[pCur->iPage-1], 
  4555   4562       pCur->apPage[pCur->iPage]->pgno
  4556   4563     );
  4557         -#endif
  4558   4564     testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
  4559   4565   
  4560   4566     releasePage(pCur->apPage[pCur->iPage]);
  4561   4567     pCur->iPage--;
  4562   4568     pCur->info.nSize = 0;
  4563   4569     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  4564   4570   }
................................................................................
  7498   7504         }
  7499   7505   
  7500   7506         pPage->nOverflow = 0;
  7501   7507   
  7502   7508         /* The next iteration of the do-loop balances the parent page. */
  7503   7509         releasePage(pPage);
  7504   7510         pCur->iPage--;
         7511  +      assert( pCur->iPage>=0 );
  7505   7512       }
  7506   7513     }while( rc==SQLITE_OK );
  7507   7514   
  7508   7515     if( pFree ){
  7509   7516       sqlite3PageFree(pFree);
  7510   7517     }
  7511   7518     return rc;
................................................................................
  7974   7981     int hdr;
  7975   7982     u16 szCell;
  7976   7983   
  7977   7984     assert( sqlite3_mutex_held(pBt->mutex) );
  7978   7985     if( pgno>btreePagecount(pBt) ){
  7979   7986       return SQLITE_CORRUPT_BKPT;
  7980   7987     }
  7981         -
  7982   7988     rc = getAndInitPage(pBt, pgno, &pPage, 0);
  7983   7989     if( rc ) return rc;
         7990  +  if( pPage->bBusy ){
         7991  +    rc = SQLITE_CORRUPT_BKPT;
         7992  +    goto cleardatabasepage_out;
         7993  +  }
         7994  +  pPage->bBusy = 1;
  7984   7995     hdr = pPage->hdrOffset;
  7985   7996     for(i=0; i<pPage->nCell; i++){
  7986   7997       pCell = findCell(pPage, i);
  7987   7998       if( !pPage->leaf ){
  7988   7999         rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
  7989   8000         if( rc ) goto cleardatabasepage_out;
  7990   8001       }
................................................................................
  8001   8012     if( freePageFlag ){
  8002   8013       freePage(pPage, &rc);
  8003   8014     }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
  8004   8015       zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
  8005   8016     }
  8006   8017   
  8007   8018   cleardatabasepage_out:
         8019  +  pPage->bBusy = 0;
  8008   8020     releasePage(pPage);
  8009   8021     return rc;
  8010   8022   }
  8011   8023   
  8012   8024   /*
  8013   8025   ** Delete all information from a single table in the database.  iTable is
  8014   8026   ** the page number of the root of the table.  After this routine returns,

Changes to src/btreeInt.h.

   276    276     u8 intKey;           /* True if table b-trees.  False for index b-trees */
   277    277     u8 intKeyLeaf;       /* True if the leaf of an intKey table */
   278    278     u8 noPayload;        /* True if internal intKey page (thus w/o data) */
   279    279     u8 leaf;             /* True if a leaf page */
   280    280     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   281    281     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
   282    282     u8 max1bytePayload;  /* min(maxLocal,127) */
          283  +  u8 bBusy;            /* Prevent endless loops on corrupt database files */
   283    284     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
   284    285     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   285    286     u16 cellOffset;      /* Index in aData of first cell pointer */
   286    287     u16 nFree;           /* Number of free bytes on the page */
   287    288     u16 nCell;           /* Number of cells on this page, local and ovfl */
   288    289     u16 maskPage;        /* Mask for page offset */
   289    290     u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th

Changes to src/main.c.

    58     58   #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
    59     59   /*
    60     60   ** If the following function pointer is not NULL and if
    61     61   ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
    62     62   ** I/O active are written using this function.  These messages
    63     63   ** are intended for debugging activity only.
    64     64   */
    65         -/* not-private */ void (*sqlite3IoTrace)(const char*, ...) = 0;
           65  +SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
    66     66   #endif
    67     67   
    68     68   /*
    69     69   ** If the following global variable points to a string which is the
    70     70   ** name of a directory, then that directory will be used to store
    71     71   ** temporary files.
    72     72   **

Changes to src/malloc.c.

   158    158     sqlite3_soft_heap_limit64(n);
   159    159   }
   160    160   
   161    161   /*
   162    162   ** Initialize the memory allocation subsystem.
   163    163   */
   164    164   int sqlite3MallocInit(void){
          165  +  int rc;
   165    166     if( sqlite3GlobalConfig.m.xMalloc==0 ){
   166    167       sqlite3MemSetDefault();
   167    168     }
   168    169     memset(&mem0, 0, sizeof(mem0));
   169    170     if( sqlite3GlobalConfig.bCoreMutex ){
   170    171       mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   171    172     }
................................................................................
   193    194     }
   194    195     if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
   195    196         || sqlite3GlobalConfig.nPage<1 ){
   196    197       sqlite3GlobalConfig.pPage = 0;
   197    198       sqlite3GlobalConfig.szPage = 0;
   198    199       sqlite3GlobalConfig.nPage = 0;
   199    200     }
   200         -  return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
          201  +  rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
          202  +  if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
          203  +  return rc;
   201    204   }
   202    205   
   203    206   /*
   204    207   ** Return true if the heap is currently under memory pressure - in other
   205    208   ** words if the amount of heap used is close to the limit set by
   206    209   ** sqlite3_soft_heap_limit().
   207    210   */

Changes to src/os_unix.c.

  3779   3779   /*
  3780   3780   ** Information and control of an open file handle.
  3781   3781   */
  3782   3782   static int unixFileControl(sqlite3_file *id, int op, void *pArg){
  3783   3783     unixFile *pFile = (unixFile*)id;
  3784   3784     switch( op ){
  3785   3785       case SQLITE_FCNTL_WAL_BLOCK: {
  3786         -      pFile->ctrlFlags |= UNIXFILE_BLOCK;
         3786  +      /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */
  3787   3787         return SQLITE_OK;
  3788   3788       }
  3789   3789       case SQLITE_FCNTL_LOCKSTATE: {
  3790   3790         *(int*)pArg = pFile->eFileLock;
  3791   3791         return SQLITE_OK;
  3792   3792       }
  3793   3793       case SQLITE_FCNTL_LAST_ERRNO: {

Changes to src/os_win.c.

   193    193           DWORD, DWORD, DWORD, LPCWSTR);
   194    194   #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
   195    195   
   196    196   WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
   197    197   #endif /* SQLITE_OS_WINRT */
   198    198   
   199    199   /*
   200         -** This file mapping API is common to both Win32 and WinRT.
          200  +** These file mapping APIs are common to both Win32 and WinRT.
   201    201   */
          202  +
          203  +WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
   202    204   WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
   203    205   #endif /* SQLITE_WIN32_FILEMAPPING_API */
   204    206   
   205    207   /*
   206    208   ** Some Microsoft compilers lack this definition.
   207    209   */
   208    210   #ifndef INVALID_FILE_ATTRIBUTES
................................................................................
  1079   1081   #else
  1080   1082     { "UuidCreateSequential",     (SYSCALL)0,                      0 },
  1081   1083   #endif
  1082   1084   
  1083   1085   #define osUuidCreateSequential \
  1084   1086           ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
  1085   1087   
         1088  +#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
         1089  +  { "FlushViewOfFile",          (SYSCALL)FlushViewOfFile,        0 },
         1090  +#else
         1091  +  { "FlushViewOfFile",          (SYSCALL)0,                      0 },
         1092  +#endif
         1093  +
         1094  +#define osFlushViewOfFile \
         1095  +        ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
         1096  +
  1086   1097   }; /* End of the overrideable system calls */
  1087   1098   
  1088   1099   /*
  1089   1100   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
  1090   1101   ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
  1091   1102   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
  1092   1103   ** system call named zName.
................................................................................
  1962   1973     }
  1963   1974     return 0;
  1964   1975   }
  1965   1976   
  1966   1977   /*
  1967   1978   ** Log a I/O error retry episode.
  1968   1979   */
  1969         -static void winLogIoerr(int nRetry){
         1980  +static void winLogIoerr(int nRetry, int lineno){
  1970   1981     if( nRetry ){
  1971         -    sqlite3_log(SQLITE_IOERR,
  1972         -      "delayed %dms for lock/sharing conflict",
  1973         -      winIoerrRetryDelay*nRetry*(nRetry+1)/2
         1982  +    sqlite3_log(SQLITE_NOTICE,
         1983  +      "delayed %dms for lock/sharing conflict at line %d",
         1984  +      winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
  1974   1985       );
  1975   1986     }
  1976   1987   }
  1977   1988   
  1978   1989   #if SQLITE_OS_WINCE
  1979   1990   /*************************************************************************
  1980   1991   ** This section contains code for WinCE only.
................................................................................
  2446   2457     winFile *pFile = (winFile*)id;
  2447   2458   
  2448   2459     assert( id!=0 );
  2449   2460   #ifndef SQLITE_OMIT_WAL
  2450   2461     assert( pFile->pShm==0 );
  2451   2462   #endif
  2452   2463     assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
  2453         -  OSTRACE(("CLOSE file=%p\n", pFile->h));
         2464  +  OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
         2465  +           osGetCurrentProcessId(), pFile, pFile->h));
  2454   2466   
  2455   2467   #if SQLITE_MAX_MMAP_SIZE>0
  2456   2468     winUnmapfile(pFile);
  2457   2469   #endif
  2458   2470   
  2459   2471     do{
  2460   2472       rc = osCloseHandle(pFile->h);
................................................................................
  2475   2487       sqlite3_free(pFile->zDeleteOnClose);
  2476   2488     }
  2477   2489   #endif
  2478   2490     if( rc ){
  2479   2491       pFile->h = NULL;
  2480   2492     }
  2481   2493     OpenCounter(-1);
  2482         -  OSTRACE(("CLOSE file=%p, rc=%s\n", pFile->h, rc ? "ok" : "failed"));
         2494  +  OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
         2495  +           osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
  2483   2496     return rc ? SQLITE_OK
  2484   2497               : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
  2485   2498                             "winClose", pFile->zPath);
  2486   2499   }
  2487   2500   
  2488   2501   /*
  2489   2502   ** Read data from a file into a buffer.  Return SQLITE_OK if all
................................................................................
  2503   2516     DWORD nRead;                    /* Number of bytes actually read from file */
  2504   2517     int nRetry = 0;                 /* Number of retrys */
  2505   2518   
  2506   2519     assert( id!=0 );
  2507   2520     assert( amt>0 );
  2508   2521     assert( offset>=0 );
  2509   2522     SimulateIOError(return SQLITE_IOERR_READ);
  2510         -  OSTRACE(("READ file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
         2523  +  OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
         2524  +           "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
  2511   2525              pFile->h, pBuf, amt, offset, pFile->locktype));
  2512   2526   
  2513   2527   #if SQLITE_MAX_MMAP_SIZE>0
  2514   2528     /* Deal with as much of this read request as possible by transfering
  2515   2529     ** data from the memory mapping using memcpy().  */
  2516   2530     if( offset<pFile->mmapSize ){
  2517   2531       if( offset+amt <= pFile->mmapSize ){
  2518   2532         memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
  2519         -      OSTRACE(("READ-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
         2533  +      OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
         2534  +               osGetCurrentProcessId(), pFile, pFile->h));
  2520   2535         return SQLITE_OK;
  2521   2536       }else{
  2522   2537         int nCopy = (int)(pFile->mmapSize - offset);
  2523   2538         memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
  2524   2539         pBuf = &((u8 *)pBuf)[nCopy];
  2525   2540         amt -= nCopy;
  2526   2541         offset += nCopy;
  2527   2542       }
  2528   2543     }
  2529   2544   #endif
  2530   2545   
  2531   2546   #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
  2532   2547     if( winSeekFile(pFile, offset) ){
  2533         -    OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
         2548  +    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
         2549  +             osGetCurrentProcessId(), pFile, pFile->h));
  2534   2550       return SQLITE_FULL;
  2535   2551     }
  2536   2552     while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
  2537   2553   #else
  2538   2554     memset(&overlapped, 0, sizeof(OVERLAPPED));
  2539   2555     overlapped.Offset = (LONG)(offset & 0xffffffff);
  2540   2556     overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
  2541   2557     while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
  2542   2558            osGetLastError()!=ERROR_HANDLE_EOF ){
  2543   2559   #endif
  2544   2560       DWORD lastErrno;
  2545   2561       if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
  2546   2562       pFile->lastErrno = lastErrno;
  2547         -    OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
         2563  +    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
         2564  +             osGetCurrentProcessId(), pFile, pFile->h));
  2548   2565       return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
  2549   2566                          "winRead", pFile->zPath);
  2550   2567     }
  2551         -  winLogIoerr(nRetry);
         2568  +  winLogIoerr(nRetry, __LINE__);
  2552   2569     if( nRead<(DWORD)amt ){
  2553   2570       /* Unread parts of the buffer must be zero-filled */
  2554   2571       memset(&((char*)pBuf)[nRead], 0, amt-nRead);
  2555         -    OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
         2572  +    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
         2573  +             osGetCurrentProcessId(), pFile, pFile->h));
  2556   2574       return SQLITE_IOERR_SHORT_READ;
  2557   2575     }
  2558   2576   
  2559         -  OSTRACE(("READ file=%p, rc=SQLITE_OK\n", pFile->h));
         2577  +  OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
         2578  +           osGetCurrentProcessId(), pFile, pFile->h));
  2560   2579     return SQLITE_OK;
  2561   2580   }
  2562   2581   
  2563   2582   /*
  2564   2583   ** Write data from a buffer into a file.  Return SQLITE_OK on success
  2565   2584   ** or some other error code on failure.
  2566   2585   */
................................................................................
  2575   2594     int nRetry = 0;                 /* Number of retries */
  2576   2595   
  2577   2596     assert( amt>0 );
  2578   2597     assert( pFile );
  2579   2598     SimulateIOError(return SQLITE_IOERR_WRITE);
  2580   2599     SimulateDiskfullError(return SQLITE_FULL);
  2581   2600   
  2582         -  OSTRACE(("WRITE file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
         2601  +  OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
         2602  +           "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
  2583   2603              pFile->h, pBuf, amt, offset, pFile->locktype));
  2584   2604   
  2585   2605   #if SQLITE_MAX_MMAP_SIZE>0
  2586   2606     /* Deal with as much of this write request as possible by transfering
  2587   2607     ** data from the memory mapping using memcpy().  */
  2588   2608     if( offset<pFile->mmapSize ){
  2589   2609       if( offset+amt <= pFile->mmapSize ){
  2590   2610         memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
  2591         -      OSTRACE(("WRITE-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
         2611  +      OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
         2612  +               osGetCurrentProcessId(), pFile, pFile->h));
  2592   2613         return SQLITE_OK;
  2593   2614       }else{
  2594   2615         int nCopy = (int)(pFile->mmapSize - offset);
  2595   2616         memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
  2596   2617         pBuf = &((u8 *)pBuf)[nCopy];
  2597   2618         amt -= nCopy;
  2598   2619         offset += nCopy;
................................................................................
  2647   2668         rc = 1;
  2648   2669       }
  2649   2670     }
  2650   2671   
  2651   2672     if( rc ){
  2652   2673       if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
  2653   2674          || ( pFile->lastErrno==ERROR_DISK_FULL )){
  2654         -      OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
         2675  +      OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
         2676  +               osGetCurrentProcessId(), pFile, pFile->h));
  2655   2677         return winLogError(SQLITE_FULL, pFile->lastErrno,
  2656   2678                            "winWrite1", pFile->zPath);
  2657   2679       }
  2658         -    OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
         2680  +    OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
         2681  +             osGetCurrentProcessId(), pFile, pFile->h));
  2659   2682       return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
  2660   2683                          "winWrite2", pFile->zPath);
  2661   2684     }else{
  2662         -    winLogIoerr(nRetry);
         2685  +    winLogIoerr(nRetry, __LINE__);
  2663   2686     }
  2664         -  OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
         2687  +  OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
         2688  +           osGetCurrentProcessId(), pFile, pFile->h));
  2665   2689     return SQLITE_OK;
  2666   2690   }
  2667   2691   
  2668   2692   /*
  2669   2693   ** Truncate an open file to a specified size
  2670   2694   */
  2671   2695   static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
  2672   2696     winFile *pFile = (winFile*)id;  /* File handle object */
  2673   2697     int rc = SQLITE_OK;             /* Return code for this function */
  2674   2698     DWORD lastErrno;
  2675   2699   
  2676   2700     assert( pFile );
  2677   2701     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
  2678         -  OSTRACE(("TRUNCATE file=%p, size=%lld, lock=%d\n",
  2679         -           pFile->h, nByte, pFile->locktype));
         2702  +  OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
         2703  +           osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
  2680   2704   
  2681   2705     /* If the user has configured a chunk-size for this file, truncate the
  2682   2706     ** file so that it consists of an integer number of chunks (i.e. the
  2683   2707     ** actual file size after the operation may be larger than the requested
  2684   2708     ** size).
  2685   2709     */
  2686   2710     if( pFile->szChunk>0 ){
................................................................................
  2704   2728     ** use read() and write() to access data beyond this point from now on.
  2705   2729     */
  2706   2730     if( pFile->pMapRegion && nByte<pFile->mmapSize ){
  2707   2731       pFile->mmapSize = nByte;
  2708   2732     }
  2709   2733   #endif
  2710   2734   
  2711         -  OSTRACE(("TRUNCATE file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
         2735  +  OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
         2736  +           osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
  2712   2737     return rc;
  2713   2738   }
  2714   2739   
  2715   2740   #ifdef SQLITE_TEST
  2716   2741   /*
  2717   2742   ** Count the number of fullsyncs and normal syncs.  This is used to test
  2718   2743   ** that syncs and fullsyncs are occuring at the right times.
................................................................................
  2749   2774     );
  2750   2775   
  2751   2776     /* Unix cannot, but some systems may return SQLITE_FULL from here. This
  2752   2777     ** line is to test that doing so does not cause any problems.
  2753   2778     */
  2754   2779     SimulateDiskfullError( return SQLITE_FULL );
  2755   2780   
  2756         -  OSTRACE(("SYNC file=%p, flags=%x, lock=%d\n",
  2757         -           pFile->h, flags, pFile->locktype));
         2781  +  OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
         2782  +           osGetCurrentProcessId(), pFile, pFile->h, flags,
         2783  +           pFile->locktype));
  2758   2784   
  2759   2785   #ifndef SQLITE_TEST
  2760   2786     UNUSED_PARAMETER(flags);
  2761   2787   #else
  2762   2788     if( (flags&0x0F)==SQLITE_SYNC_FULL ){
  2763   2789       sqlite3_fullsync_count++;
  2764   2790     }
................................................................................
  2765   2791     sqlite3_sync_count++;
  2766   2792   #endif
  2767   2793   
  2768   2794     /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
  2769   2795     ** no-op
  2770   2796     */
  2771   2797   #ifdef SQLITE_NO_SYNC
  2772         -  OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
         2798  +  OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
         2799  +           osGetCurrentProcessId(), pFile, pFile->h));
  2773   2800     return SQLITE_OK;
  2774   2801   #else
         2802  +#if SQLITE_MAX_MMAP_SIZE>0
         2803  +  if( pFile->pMapRegion ){
         2804  +    if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
         2805  +      OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
         2806  +               "rc=SQLITE_OK\n", osGetCurrentProcessId(),
         2807  +               pFile, pFile->pMapRegion));
         2808  +    }else{
         2809  +      pFile->lastErrno = osGetLastError();
         2810  +      OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
         2811  +               "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
         2812  +               pFile, pFile->pMapRegion));
         2813  +      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
         2814  +                         "winSync1", pFile->zPath);
         2815  +    }
         2816  +  }
         2817  +#endif
  2775   2818     rc = osFlushFileBuffers(pFile->h);
  2776   2819     SimulateIOError( rc=FALSE );
  2777   2820     if( rc ){
  2778         -    OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
         2821  +    OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
         2822  +             osGetCurrentProcessId(), pFile, pFile->h));
  2779   2823       return SQLITE_OK;
  2780   2824     }else{
  2781   2825       pFile->lastErrno = osGetLastError();
  2782         -    OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
         2826  +    OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
         2827  +             osGetCurrentProcessId(), pFile, pFile->h));
  2783   2828       return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
  2784         -                       "winSync", pFile->zPath);
         2829  +                       "winSync2", pFile->zPath);
  2785   2830     }
  2786   2831   #endif
  2787   2832   }
  2788   2833   
  2789   2834   /*
  2790   2835   ** Determine the current size of a file in bytes
  2791   2836   */
................................................................................
  4748   4793                                 dwFlagsAndAttributes,
  4749   4794                                 NULL))==INVALID_HANDLE_VALUE &&
  4750   4795                                 winRetryIoerr(&cnt, &lastErrno) ){
  4751   4796                  /* Noop */
  4752   4797       }
  4753   4798     }
  4754   4799   #endif
  4755         -  winLogIoerr(cnt);
         4800  +  winLogIoerr(cnt, __LINE__);
  4756   4801   
  4757   4802     OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
  4758   4803              dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
  4759   4804   
  4760   4805     if( h==INVALID_HANDLE_VALUE ){
  4761   4806       pFile->lastErrno = lastErrno;
  4762   4807       winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
................................................................................
  4932   4977         }
  4933   4978       } while(1);
  4934   4979     }
  4935   4980   #endif
  4936   4981     if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
  4937   4982       rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
  4938   4983     }else{
  4939         -    winLogIoerr(cnt);
         4984  +    winLogIoerr(cnt, __LINE__);
  4940   4985     }
  4941   4986     sqlite3_free(zConverted);
  4942   4987     OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
  4943   4988     return rc;
  4944   4989   }
  4945   4990   
  4946   4991   /*
................................................................................
  4982   5027             && sAttrData.nFileSizeHigh==0
  4983   5028             && sAttrData.nFileSizeLow==0 ){
  4984   5029           attr = INVALID_FILE_ATTRIBUTES;
  4985   5030         }else{
  4986   5031           attr = sAttrData.dwFileAttributes;
  4987   5032         }
  4988   5033       }else{
  4989         -      winLogIoerr(cnt);
         5034  +      winLogIoerr(cnt, __LINE__);
  4990   5035         if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
  4991   5036           sqlite3_free(zConverted);
  4992   5037           return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
  4993   5038                              zFilename);
  4994   5039         }else{
  4995   5040           attr = INVALID_FILE_ATTRIBUTES;
  4996   5041         }
................................................................................
  5551   5596       winGetSystemCall,    /* xGetSystemCall */
  5552   5597       winNextSystemCall,   /* xNextSystemCall */
  5553   5598     };
  5554   5599   #endif
  5555   5600   
  5556   5601     /* Double-check that the aSyscall[] array has been constructed
  5557   5602     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  5558         -  assert( ArraySize(aSyscall)==79 );
         5603  +  assert( ArraySize(aSyscall)==80 );
  5559   5604   
  5560   5605     /* get memory map allocation granularity */
  5561   5606     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  5562   5607   #if SQLITE_OS_WINRT
  5563   5608     osGetNativeSystemInfo(&winSysInfo);
  5564   5609   #else
  5565   5610     osGetSystemInfo(&winSysInfo);

Changes to src/printf.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 ){
   263    263           flag_leftjustify = 1;
   264         -        width = -width;
          264  +        width = width >= -2147483647 ? -width : 0;
   265    265         }
   266    266         c = *++fmt;
   267    267       }else{
          268  +      unsigned wx = 0;
   268    269         while( c>='0' && c<='9' ){
   269         -        width = width*10 + c - '0';
          270  +        wx = wx*10 + c - '0';
   270    271           c = *++fmt;
   271    272         }
          273  +      testcase( wx>0x7fffffff );
          274  +      width = wx & 0x7fffffff;
   272    275       }
          276  +
   273    277       /* Get the precision */
   274    278       if( c=='.' ){
   275    279         precision = 0;
   276    280         c = *++fmt;
   277    281         if( c=='*' ){
   278    282           if( bArgList ){
   279    283             precision = (int)getIntArg(pArgList);
   280    284           }else{
   281    285             precision = va_arg(ap,int);
   282    286           }
   283         -        if( precision<0 ) precision = -precision;
   284    287           c = *++fmt;
          288  +        if( precision<0 ){
          289  +          precision = precision >= -2147483647 ? -precision : -1;
          290  +        }
   285    291         }else{
          292  +        unsigned px = 0;
   286    293           while( c>='0' && c<='9' ){
   287         -          precision = precision*10 + c - '0';
          294  +          px = px*10 + c - '0';
   288    295             c = *++fmt;
   289    296           }
          297  +        testcase( px>0x7fffffff );
          298  +        precision = px & 0x7fffffff;
   290    299         }
   291    300       }else{
   292    301         precision = -1;
   293    302       }
   294    303       /* Get the conversion type modifier */
   295    304       if( c=='l' ){
   296    305         flag_long = 1;
................................................................................
   446    455             prefix = '-';
   447    456           }else{
   448    457             if( flag_plussign )          prefix = '+';
   449    458             else if( flag_blanksign )    prefix = ' ';
   450    459             else                         prefix = 0;
   451    460           }
   452    461           if( xtype==etGENERIC && precision>0 ) precision--;
   453         -        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
          462  +        testcase( precision>0xfff );
          463  +        for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
   454    464           if( xtype==etFLOAT ) realvalue += rounder;
   455    465           /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
   456    466           exp = 0;
   457    467           if( sqlite3IsNaN((double)realvalue) ){
   458    468             bufpt = "NaN";
   459    469             length = 3;
   460    470             break;
................................................................................
   501    511             flag_rtz = flag_altform2;
   502    512           }
   503    513           if( xtype==etEXP ){
   504    514             e2 = 0;
   505    515           }else{
   506    516             e2 = exp;
   507    517           }
   508         -        if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
   509         -          bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
          518  +        if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
          519  +          bufpt = zExtra 
          520  +              = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
   510    521             if( bufpt==0 ){
   511    522               setStrAccumError(pAccum, STRACCUM_NOMEM);
   512    523               return;
   513    524             }
   514    525           }
   515    526           zOut = bufpt;
   516    527           nsd = 16 + flag_altform2*10;
................................................................................
   734    745   ** able to accept at least N more bytes of text.
   735    746   **
   736    747   ** Return the number of bytes of text that StrAccum is able to accept
   737    748   ** after the attempted enlargement.  The value returned might be zero.
   738    749   */
   739    750   static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
   740    751     char *zNew;
   741         -  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
          752  +  assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
   742    753     if( p->accError ){
   743    754       testcase(p->accError==STRACCUM_TOOBIG);
   744    755       testcase(p->accError==STRACCUM_NOMEM);
   745    756       return 0;
   746    757     }
   747    758     if( !p->useMalloc ){
   748    759       N = p->nAlloc - p->nChar - 1;
................................................................................
   783    794     return N;
   784    795   }
   785    796   
   786    797   /*
   787    798   ** Append N copies of character c to the given string buffer.
   788    799   */
   789    800   void sqlite3AppendChar(StrAccum *p, int N, char c){
   790         -  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
          801  +  testcase( p->nChar + (i64)N > 0x7fffffff );
          802  +  if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
          803  +    return;
          804  +  }
   791    805     while( (N--)>0 ) p->zText[p->nChar++] = c;
   792    806   }
   793    807   
   794    808   /*
   795    809   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
   796    810   ** So enlarge if first, then do the append.
   797    811   **

Changes to src/resolve.c.

  1182   1182       */
  1183   1183       memset(&sNC, 0, sizeof(sNC));
  1184   1184       sNC.pParse = pParse;
  1185   1185       if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
  1186   1186           sqlite3ResolveExprNames(&sNC, p->pOffset) ){
  1187   1187         return WRC_Abort;
  1188   1188       }
         1189  +
         1190  +    /* If the SF_Converted flags is set, then this Select object was
         1191  +    ** was created by the convertCompoundSelectToSubquery() function.
         1192  +    ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
         1193  +    ** as if it were part of the sub-query, not the parent. This block
         1194  +    ** moves the pOrderBy down to the sub-query. It will be moved back
         1195  +    ** after the names have been resolved.  */
         1196  +    if( p->selFlags & SF_Converted ){
         1197  +      Select *pSub = p->pSrc->a[0].pSelect;
         1198  +      assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy );
         1199  +      assert( pSub->pPrior && pSub->pOrderBy==0 );
         1200  +      pSub->pOrderBy = p->pOrderBy;
         1201  +      p->pOrderBy = 0;
         1202  +    }
  1189   1203     
  1190   1204       /* Recursively resolve names in all subqueries
  1191   1205       */
  1192   1206       for(i=0; i<p->pSrc->nSrc; i++){
  1193   1207         struct SrcList_item *pItem = &p->pSrc->a[i];
  1194   1208         if( pItem->pSelect ){
  1195   1209           NameContext *pNC;         /* Used to iterate name contexts */
................................................................................
  1263   1277       if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
  1264   1278   
  1265   1279       /* The ORDER BY and GROUP BY clauses may not refer to terms in
  1266   1280       ** outer queries 
  1267   1281       */
  1268   1282       sNC.pNext = 0;
  1269   1283       sNC.ncFlags |= NC_AllowAgg;
         1284  +
         1285  +    /* If this is a converted compound query, move the ORDER BY clause from 
         1286  +    ** the sub-query back to the parent query. At this point each term
         1287  +    ** within the ORDER BY clause has been transformed to an integer value.
         1288  +    ** These integers will be replaced by copies of the corresponding result
         1289  +    ** set expressions by the call to resolveOrderGroupBy() below.  */
         1290  +    if( p->selFlags & SF_Converted ){
         1291  +      Select *pSub = p->pSrc->a[0].pSelect;
         1292  +      p->pOrderBy = pSub->pOrderBy;
         1293  +      pSub->pOrderBy = 0;
         1294  +    }
  1270   1295   
  1271   1296       /* Process the ORDER BY clause for singleton SELECT statements.
  1272   1297       ** The ORDER BY clause for compounds SELECT statements is handled
  1273   1298       ** below, after all of the result-sets for all of the elements of
  1274   1299       ** the compound have been resolved.
  1275   1300       */
  1276   1301       if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){

Changes to src/select.c.

  3880   3880     p->pWhere = 0;
  3881   3881     pNew->pGroupBy = 0;
  3882   3882     pNew->pHaving = 0;
  3883   3883     pNew->pOrderBy = 0;
  3884   3884     p->pPrior = 0;
  3885   3885     p->pNext = 0;
  3886   3886     p->selFlags &= ~SF_Compound;
         3887  +  assert( (p->selFlags & SF_Converted)==0 );
         3888  +  p->selFlags |= SF_Converted;
  3887   3889     assert( pNew->pPrior!=0 );
  3888   3890     pNew->pPrior->pNext = pNew;
  3889   3891     pNew->pLimit = 0;
  3890   3892     pNew->pOffset = 0;
  3891   3893     return WRC_Continue;
  3892   3894   }
  3893   3895   

Changes to src/shell.c.

   366    366   /*
   367    367   ** This routine works like printf in that its first argument is a
   368    368   ** format string and subsequent arguments are values to be substituted
   369    369   ** in place of % fields.  The result of formatting this string
   370    370   ** is written to iotrace.
   371    371   */
   372    372   #ifdef SQLITE_ENABLE_IOTRACE
   373         -static void iotracePrintf(const char *zFormat, ...){
          373  +static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
   374    374     va_list ap;
   375    375     char *z;
   376    376     if( iotrace==0 ) return;
   377    377     va_start(ap, zFormat);
   378    378     z = sqlite3_vmprintf(zFormat, ap);
   379    379     va_end(ap);
   380    380     fprintf(iotrace, "%s", z);
................................................................................
  3140   3140         fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
  3141   3141         rc = 1;
  3142   3142       }
  3143   3143     }else
  3144   3144   
  3145   3145   #ifdef SQLITE_ENABLE_IOTRACE
  3146   3146     if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
  3147         -    extern void (*sqlite3IoTrace)(const char*, ...);
         3147  +    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
  3148   3148       if( iotrace && iotrace!=stdout ) fclose(iotrace);
  3149   3149       iotrace = 0;
  3150   3150       if( nArg<2 ){
  3151   3151         sqlite3IoTrace = 0;
  3152   3152       }else if( strcmp(azArg[1], "-")==0 ){
  3153   3153         sqlite3IoTrace = iotracePrintf;
  3154   3154         iotrace = stdout;
................................................................................
  3799   3799   
  3800   3800           case SQLITE_TESTCTRL_IMPOSTER:
  3801   3801             if( nArg==5 ){
  3802   3802               rc = sqlite3_test_control(testctrl, p->db, 
  3803   3803                             azArg[2],
  3804   3804                             integerValue(azArg[3]),
  3805   3805                             integerValue(azArg[4]));
         3806  +            fprintf(p->out, "%d (0x%08x)\n", rc, rc);
  3806   3807             }else{
  3807         -            fprintf(stderr,"Usage: .testctrl initmode dbName onoff tnum\n");
  3808         -            rc = 1;
         3808  +            fprintf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
  3809   3809             }
  3810   3810             break;
  3811   3811   
  3812   3812           case SQLITE_TESTCTRL_BITVEC_TEST:         
  3813   3813           case SQLITE_TESTCTRL_FAULT_INSTALL:       
  3814   3814           case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: 
  3815   3815           case SQLITE_TESTCTRL_SCRATCHMALLOC:       

Changes to src/sqlite.h.in.

    39     39   */
    40     40   #ifdef __cplusplus
    41     41   extern "C" {
    42     42   #endif
    43     43   
    44     44   
    45     45   /*
    46         -** Add the ability to override 'extern'
           46  +** Provide the ability to override linkage features of the interface.
    47     47   */
    48     48   #ifndef SQLITE_EXTERN
    49     49   # define SQLITE_EXTERN extern
    50     50   #endif
    51         -
    52         -/*
    53         -** Add the ability to override 'cdecl'
    54         -*/
           51  +#ifndef SQLITE_API
           52  +# define SQLITE_API
           53  +#endif
    55     54   #ifndef SQLITE_CDECL
    56     55   # define SQLITE_CDECL
    57     56   #endif
           57  +#ifndef SQLITE_STDCALL
           58  +# define SQLITE_STDCALL
           59  +#endif
    58     60   
    59     61   /*
    60     62   ** These no-op macros are used in front of interfaces to mark those
    61     63   ** interfaces as either deprecated or experimental.  New applications
    62     64   ** should not use deprecated interfaces - they are supported for backwards
    63     65   ** compatibility only.  Application writers should be aware that
    64     66   ** experimental interfaces are subject to change in point releases.

Changes to src/sqliteInt.h.

  2385   2385   #define SF_Compound        0x0040  /* Part of a compound query */
  2386   2386   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2387   2387   #define SF_AllValues       0x0100  /* All terms of compound are VALUES */
  2388   2388   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2389   2389   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  2390   2390   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  2391   2391   #define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
         2392  +#define SF_Converted       0x2000  /* By convertCompoundSelectToSubquery() */
  2392   2393   
  2393   2394   
  2394   2395   /*
  2395   2396   ** The results of a SELECT can be distributed in several ways, as defined
  2396   2397   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2397   2398   ** Type".
  2398   2399   **
................................................................................
  3799   3800   ** If the SQLITE_ENABLE IOTRACE exists then the global variable
  3800   3801   ** sqlite3IoTrace is a pointer to a printf-like routine used to
  3801   3802   ** print I/O tracing messages. 
  3802   3803   */
  3803   3804   #ifdef SQLITE_ENABLE_IOTRACE
  3804   3805   # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
  3805   3806     void sqlite3VdbeIOTraceSql(Vdbe*);
  3806         -SQLITE_EXTERN void (*sqlite3IoTrace)(const char*,...);
         3807  +SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
  3807   3808   #else
  3808   3809   # define IOTRACE(A)
  3809   3810   # define sqlite3VdbeIOTraceSql(X)
  3810   3811   #endif
  3811   3812   
  3812   3813   /*
  3813   3814   ** These routines are available for the mem2.c debugging memory allocator

Changes to src/status.c.

   144    144     sqlite3_mutex_enter(pMutex);
   145    145     *pCurrent = wsdStat.nowValue[op];
   146    146     *pHighwater = wsdStat.mxValue[op];
   147    147     if( resetFlag ){
   148    148       wsdStat.mxValue[op] = wsdStat.nowValue[op];
   149    149     }
   150    150     sqlite3_mutex_leave(pMutex);
          151  +  (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
   151    152     return SQLITE_OK;
   152    153   }
   153    154   int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
   154    155     sqlite3_int64 iCur, iHwtr;
   155    156     int rc;
   156    157   #ifdef SQLITE_ENABLE_API_ARMOR
   157    158     if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;

Changes to src/vdbe.c.

  1916   1916           applyNumericAffinity(pIn3,0);
  1917   1917         }
  1918   1918       }else if( affinity==SQLITE_AFF_TEXT ){
  1919   1919         if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
  1920   1920           testcase( pIn1->flags & MEM_Int );
  1921   1921           testcase( pIn1->flags & MEM_Real );
  1922   1922           sqlite3VdbeMemStringify(pIn1, encoding, 1);
         1923  +        testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
         1924  +        flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
  1923   1925         }
  1924   1926         if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
  1925   1927           testcase( pIn3->flags & MEM_Int );
  1926   1928           testcase( pIn3->flags & MEM_Real );
  1927   1929           sqlite3VdbeMemStringify(pIn3, encoding, 1);
         1930  +        testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
         1931  +        flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
  1928   1932         }
  1929   1933       }
  1930   1934       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
  1931   1935       if( pIn1->flags & MEM_Zero ){
  1932   1936         sqlite3VdbeMemExpandBlob(pIn1);
  1933   1937         flags1 &= ~MEM_Zero;
  1934   1938       }
................................................................................
  1957   1961     }else{
  1958   1962       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
  1959   1963       if( res ){
  1960   1964         pc = pOp->p2-1;
  1961   1965       }
  1962   1966     }
  1963   1967     /* Undo any changes made by applyAffinity() to the input registers. */
         1968  +  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
  1964   1969     pIn1->flags = flags1;
         1970  +  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
  1965   1971     pIn3->flags = flags3;
  1966   1972     break;
  1967   1973   }
  1968   1974   
  1969   1975   /* Opcode: Permutation * * * P4 *
  1970   1976   **
  1971   1977   ** Set the permutation used by the OP_Compare operator to be the array

Changes to test/attach.test.

   854    854     }
   855    855   } {noname inmem}
   856    856   do_test attach-10.2 {
   857    857     lrange [execsql {
   858    858       PRAGMA database_list;
   859    859     }] 9 end
   860    860   } {4 noname {} 5 inmem {}}
          861  +
          862  +# Attach with a very long URI filename.
          863  +#
          864  +db close
          865  +sqlite3 db test.db -uri 1
          866  +do_execsql_test attach-11.1 {
          867  +  ATTACH printf('file:%09000x/x.db?mode=memory&cache=shared',1) AS aux1;
          868  +  CREATE TABLE aux1.t1(x,y);
          869  +  INSERT INTO aux1.t1(x,y) VALUES(1,2),(3,4);
          870  +  SELECT * FROM aux1.t1;
          871  +} {1 2 3 4}
          872  +
   861    873   
   862    874   finish_test

Added test/btree02.test.

            1  +# 2015-03-25
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# The focus of this script is making multiple calls to saveCursorPosition()
           14  +# and restoreCursorPosition() when cursors have eState==CURSOR_SKIPNEXT
           15  +# 
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +
           20  +load_static_extension db eval
           21  +do_execsql_test btree02-100 {
           22  +  CREATE TABLE t1(a TEXT, ax INTEGER, b INT, PRIMARY KEY(a,ax)) WITHOUT ROWID;
           23  +  WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<10)
           24  +    INSERT INTO t1(a,ax,b) SELECT printf('%02x',i), random(), i FROM c;
           25  +  CREATE INDEX t1a ON t1(a);
           26  +  CREATE TABLE t2(x,y);
           27  +  CREATE TABLE t3(cnt);
           28  +  WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<4)
           29  +    INSERT INTO t3(cnt) SELECT i FROM c;
           30  +  SELECT count(*) FROM t1;
           31  +} {10}
           32  +do_test btree02-110 {
           33  +  db eval BEGIN
           34  +  set i 0
           35  +  db eval {SELECT a, ax, b, cnt FROM t1 CROSS JOIN t3 WHERE b IS NOT NULL} {
           36  +    db eval {INSERT INTO t2(x,y) VALUES($b,$cnt)}
           37  +    # puts "a,b,cnt = ($a,$b,$cnt)"
           38  +    incr i
           39  +    if {$i%2==1} {
           40  +      set bx [expr {$b+1000}]
           41  +      # puts "INSERT ($a),$bx"
           42  +      db eval {INSERT INTO t1(a,ax,b) VALUES(printf('(%s)',$a),random(),$bx)}
           43  +    } else {
           44  +      # puts "DELETE a=$a"
           45  +      db eval {DELETE FROM t1 WHERE a=$a}
           46  +    }
           47  +    db eval {COMMIT; BEGIN}
           48  +  }  
           49  +  db one {COMMIT; SELECT count(*) FROM t1;}
           50  +} {20}
           51  +
           52  +finish_test

Added test/corruptJ.test.

            1  +# 2015-03-30
            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  +# Corruption consisting of a database page that thinks it is a child
           13  +# of itself.
           14  +#
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +set testprefix corruptJ
           19  +
           20  +if {[permutation]=="mmap"} {
           21  +  finish_test
           22  +  return
           23  +}
           24  +
           25  +# Do not use a codec for tests in this file, as the database file is
           26  +# manipulated directly using tcl scripts (using the [hexio_write] command).
           27  +#
           28  +do_not_use_codec
           29  +database_may_be_corrupt
           30  +
           31  +# Initialize the database.
           32  +#
           33  +do_execsql_test 1.1 {
           34  +  PRAGMA page_size=1024;
           35  +  PRAGMA auto_vacuum=0;
           36  +  CREATE TABLE t1(a,b);
           37  +  WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<10)
           38  +    INSERT INTO t1(a,b) SELECT i, zeroblob(700) FROM c;
           39  +} {}
           40  +db close
           41  +
           42  +# Corrupt the root page of the t1 table such that the left-child pointer
           43  +# for the very first cell points back to the root.  Then try to DROP the
           44  +# table.  The clearDatabasePage() routine should not loop.
           45  +#
           46  +do_test 1.2 {
           47  +  hexio_write test.db [expr {2*1024-2}] 02
           48  +  sqlite3 db test.db
           49  +  catchsql { DROP TABLE t1 }
           50  +} {1 {database disk image is malformed}}
           51  +
           52  +# Similar test using a WITHOUT ROWID table
           53  +#
           54  +do_test 2.1 {
           55  +  db close
           56  +  forcedelete test.db
           57  +  sqlite3 db test.db
           58  +  db eval {
           59  +    PRAGMA page_size=1024;
           60  +    PRAGMA auto_vacuum=0;
           61  +    CREATE TABLE t1(a,b,PRIMARY KEY(a,b)) WITHOUT ROWID;
           62  +    WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
           63  +      INSERT INTO t1(a,b) SELECT i, zeroblob(200) FROM c;
           64  +  }
           65  +} {}
           66  +
           67  +# The table is three levels deep.  Corrupt the left child of an intermediate
           68  +# page so that it points back to the root page.
           69  +#
           70  +do_test 2.2 {
           71  +  db close
           72  +  hexio_read test.db [expr {9*1024+391}] 8
           73  +} {00000008814D0401}
           74  +do_test 2.2b {
           75  +  hexio_write test.db [expr {9*1024+391}] 00000002
           76  +  sqlite3 db test.db
           77  +  catchsql { PRAGMA secure_delete=ON; DROP TABLE t1; }
           78  +} {1 {database disk image is malformed}}
           79  +
           80  +finish_test

Changes to test/e_walauto.test.

    11     11   #
    12     12   
    13     13   set testdir [file dirname $argv0]
    14     14   source $testdir/tester.tcl
    15     15   source $testdir/wal_common.tcl
    16     16   set testprefix e_walauto
    17     17   
           18  +# Do not run this test on OpenBSD, as it depends on read() and mmap both
           19  +# accessing the same coherent view of the "test.db-shm" file. This doesn't
           20  +# work on OpenBSD.
           21  +#
           22  +if {$tcl_platform(os) == "OpenBSD"} {
           23  +  finish_test
           24  +  return
           25  +}
    18     26   
    19     27   proc read_nbackfill {} {
    20     28     seek $::shmfd 96
    21     29     binary scan [read $::shmfd 4] n nBackfill
    22     30     set nBackfill
    23     31   }
    24     32   proc read_mxframe {} {

Changes to test/fts3fault2.test.

   150    150         );
   151    151       }
   152    152       execsql { SELECT docid FROM ft WHERE ft MATCH 'th*' }
   153    153     } -test {
   154    154       faultsim_test_result {0 {1 2}}
   155    155     }
   156    156   }
          157  +
          158  +reset_db
          159  +do_test 6.0 {
          160  +  execsql {
          161  +    CREATE VIRTUAL TABLE t6 USING fts4(x,order=DESC);
          162  +    INSERT INTO t6(docid, x) VALUES(-1,'a b');
          163  +    INSERT INTO t6(docid, x) VALUES(1, 'b');
          164  +  }
          165  +  faultsim_save_and_close
          166  +} {}
          167  +
          168  +do_faultsim_test 6.1 -faults oom* -prep {
          169  +  faultsim_restore_and_reopen
          170  +  db eval {SELECT * FROM sqlite_master}
          171  +} -body {
          172  +  execsql { SELECT docid FROM t6 WHERE t6 MATCH '"a* b"' }
          173  +} -test {
          174  +  faultsim_test_result {0 -1}
          175  +}
   157    176   
   158    177   finish_test

Changes to test/fts3prefix.test.

   269    269     CREATE VIRTUAL TABLE t2 USING fts4(prefix=);
   270    270     INSERT INTO t1 VALUES('He dressed himself in cycling clothes');
   271    271     INSERT INTO t2 VALUES('He dressed himself in cycling clothes');
   272    272   } {}
   273    273   do_execsql_test 6.5.2 {
   274    274     SELECT md5sum(quote(root)) FROM t1_segdir;
   275    275   } [db eval {SELECT md5sum(quote(root)) FROM t2_segdir}]
          276  +
          277  +
          278  +do_execsql_test 7.0 {
          279  +  CREATE VIRTUAL TABLE t6 USING fts4(x,order=DESC);
          280  +  INSERT INTO t6(docid, x) VALUES(-1,'a b');
          281  +  INSERT INTO t6(docid, x) VALUES(1, 'b');
          282  +}
          283  +do_execsql_test 7.1 {
          284  +  SELECT docid FROM t6 WHERE t6 MATCH '"a* b"';
          285  +} {-1}
          286  +do_execsql_test 7.2 {
          287  +  SELECT docid FROM t6 WHERE t6 MATCH 'a*';
          288  +} {-1}
          289  +do_execsql_test 7.3 {
          290  +  SELECT docid FROM t6 WHERE t6 MATCH 'a* b';
          291  +} {-1}
          292  +
          293  +
   276    294   
   277    295   finish_test

Changes to test/printf.test.

   468    468     sqlite3_mprintf_int {abc: (% 6d) (% 6x) (% 6o) :xyz}\
   469    469          0xff676981 0xff676981 0xff676981
   470    470   } {abc: (-9999999) (ff676981) (37731664601) :xyz}
   471    471   do_test printf-1.16.7 {
   472    472     sqlite3_mprintf_int {abc: (%#6d) (%#6x) (%#6o) :xyz}\
   473    473          0xff676981 0xff676981 0xff676981
   474    474   } {abc: (-9999999) (0xff676981) (037731664601) :xyz}
          475  +do_test printf-1.17.1 {
          476  +  sqlite3_mprintf_int {abd: %2147483647d %2147483647x %2147483647o} 1 1 1
          477  +} {}
          478  +do_test printf-1.17.2 {
          479  +  sqlite3_mprintf_int {abd: %*d %x} 2147483647 1 1
          480  +} {}
          481  +do_test printf-1.17.3 {
          482  +  sqlite3_mprintf_int {abd: %*d %x} -2147483648 1 1
          483  +} {abd: 1 1}
          484  +do_test printf-1.17.4 {
          485  +  sqlite3_mprintf_int {abd: %.2147483648d %x %x} 1 1 1
          486  +} {/.*/}
   475    487   do_test printf-2.1.1.1 {
   476    488     sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 0.001
   477    489   } {abc: (0.0) :xyz}
   478    490   do_test printf-2.1.1.2 {
   479    491     sqlite3_mprintf_double {abc: (%*.*e) :xyz} 1 1 0.001
   480    492   } {abc: (1.0e-03) :xyz}
   481    493   do_test printf-2.1.1.3 {
................................................................................
   522    534   } {abc: 1 1 (0.0) :xyz}
   523    535   do_test printf-2.1.2.8 {
   524    536     sqlite3_mprintf_double {abc: %d %d (%1.1e) :xyz} 1 1 1.0e-20
   525    537   } {abc: 1 1 (1.0e-20) :xyz}
   526    538   do_test printf-2.1.2.9 {
   527    539     sqlite3_mprintf_double {abc: %d %d (%1.1g) :xyz} 1 1 1.0e-20
   528    540   } {abc: 1 1 (1e-20) :xyz}
          541  +do_test printf-2.1.2.10 {
          542  +  sqlite3_mprintf_double {abc: %*.*f}  2000000000 1000000000 1.0e-20
          543  +} {abc: }
   529    544   do_test printf-2.1.3.1 {
   530    545     sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 1.0
   531    546   } {abc: (1.0) :xyz}
   532    547   do_test printf-2.1.3.2 {
   533    548     sqlite3_mprintf_double {abc: (%*.*e) :xyz} 1 1 1.0
   534    549   } {abc: (1.0e+00) :xyz}
   535    550   do_test printf-2.1.3.3 {
................................................................................
  3462   3477   } [format {%d %d A String: (%s)} 1 2 {This is the string}]
  3463   3478   do_test printf-3.5 {
  3464   3479     sqlite3_mprintf_str {%d %d A String: (%30s)} 1 2 {This is the string}
  3465   3480   } [format {%d %d A String: (%30s)} 1 2 {This is the string}]
  3466   3481   do_test printf-3.6 {
  3467   3482     sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string}
  3468   3483   } [format {%d %d A String: (%-30s)} 1 2 {This is the string}]
         3484  +do_test printf-3.7 {
         3485  +  sqlite3_mprintf_str {%d A String: (%*s)} 1 2147483647 {This is the string}
         3486  +} []
         3487  +do_test printf-3.8 {
         3488  +  sqlite3_mprintf_str {%d A String: (%*s)} 1 -2147483648 {This is the string}
         3489  +} {1 A String: (This is the string)}
         3490  +do_test printf-3.9 {
         3491  +  sqlite3_mprintf_str {%d A String: (%.*s)} 1 -2147483648 {This is the string}
         3492  +} {1 A String: (This is the string)}
  3469   3493   do_test snprintf-3.11 {
  3470   3494     sqlite3_snprintf_str 2 {x%d %d %s} 10 10 {This is the string}
  3471   3495   } {x}
  3472   3496   do_test snprintf-3.12 {
  3473   3497     sqlite3_snprintf_str 3 {x%d %d %s} 10 10 {This is the string}
  3474   3498   } {x1}
  3475   3499   do_test snprintf-3.13 {
................................................................................
  3681   3705   } {Inf}
  3682   3706   do_test printf-13.5 {
  3683   3707     sqlite3_mprintf_hexdouble %.20f fff0000000000000
  3684   3708   } {-Inf}
  3685   3709   do_test printf-13.6 {
  3686   3710     sqlite3_mprintf_hexdouble %.20f fff8000000000000
  3687   3711   } {NaN}
         3712  +do_test printf-13.7 {
         3713  +  sqlite3_mprintf_hexdouble %2147483648.10000f 4693b8b5b5056e17
         3714  +} {/100000000000000000000000000000000.00/}
  3688   3715   
  3689   3716   do_test printf-14.1 {
  3690   3717     sqlite3_mprintf_str {abc-%y-123} 0 0 {not used}
  3691   3718   } {abc-}
  3692   3719   do_test printf-14.2 {
  3693   3720     sqlite3_mprintf_n_test {xyzzy}
  3694   3721   } 5

Changes to test/selectA.test.

  1370   1370   do_execsql_test 4.2.2 {
  1371   1371     SELECT c, f(d,c,d,c,d) FROM t7
  1372   1372     UNION ALL
  1373   1373     SELECT a, b FROM t6 
  1374   1374     ORDER BY 1,2
  1375   1375   } {/2 . 3 . 4 . 5 . 6 . 7 ./}
  1376   1376   
         1377  +
         1378  +proc strip_rnd {explain} {
         1379  +  regexp -all {sqlite_sq_[0123456789ABCDEF]*} $explain sqlite_sq
         1380  +}
         1381  +
         1382  +proc do_same_test {tn q1 args} {
         1383  +  set r2 [strip_rnd [db eval "EXPLAIN $q1"]]
         1384  +  set i 1
         1385  +  foreach q $args {
         1386  +    set tst [subst -nocommands {strip_rnd [db eval "EXPLAIN $q"]}]
         1387  +    uplevel do_test $tn.$i [list $tst] [list $r2]
         1388  +    incr i
         1389  +  }
         1390  +}
         1391  +
         1392  +do_execsql_test 5.0 {
         1393  +  CREATE TABLE t8(a, b);
         1394  +  CREATE TABLE t9(c, d);
         1395  +} {}
         1396  +
         1397  +do_same_test 5.1 {
         1398  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY a;
         1399  +} {
         1400  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY t8.a;
         1401  +} {
         1402  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY 1;
         1403  +} {
         1404  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY c;
         1405  +} {
         1406  +  SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY t9.c;
         1407  +}
         1408  +
         1409  +do_same_test 5.2 {
         1410  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY a COLLATE NOCASE
         1411  +} {
         1412  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY t8.a COLLATE NOCASE
         1413  +} {
         1414  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY 1 COLLATE NOCASE
         1415  +} {
         1416  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY c COLLATE NOCASE
         1417  +} {
         1418  +  SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY t9.c COLLATE NOCASE
         1419  +}
         1420  +
         1421  +do_same_test 5.3 {
         1422  +  SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY b, c COLLATE NOCASE
         1423  +} {
         1424  +  SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY 2, 1 COLLATE NOCASE
         1425  +} {
         1426  +  SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY d, a COLLATE NOCASE
         1427  +} {
         1428  +  SELECT a, b FROM t8 EXCEPT SELECT * FROM t9 ORDER BY t9.d, c COLLATE NOCASE
         1429  +} {
         1430  +  SELECT * FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY d, t8.a COLLATE NOCASE
         1431  +}
         1432  +
         1433  +do_catchsql_test 5.4 {
         1434  +  SELECT * FROM t8 UNION SELECT * FROM t9 ORDER BY a+b COLLATE NOCASE
         1435  +} {1 {1st ORDER BY term does not match any column in the result set}}
         1436  +
  1377   1437   
  1378   1438   finish_test

Changes to test/walblock.test.

    11     11   #
    12     12   
    13     13   set testdir [file dirname $argv0]
    14     14   source $testdir/tester.tcl
    15     15   source $testdir/lock_common.tcl
    16     16   source $testdir/wal_common.tcl
    17     17   
           18  +finish_test; return;    #  Feature currently not implemented.
    18     19   ifcapable !wal {finish_test ; return }
    19     20   if {$::tcl_platform(platform)!="unix"} { finish_test ; return }
    20     21   set testprefix walblock
    21     22   
    22     23   catch { db close }
    23     24   testvfs tvfs -fullshm 1
    24     25   foreach f [glob test.db*] { forcedelete $f }
................................................................................
   107    108     after 500 {set ::continue 1}
   108    109     vwait ::continue
   109    110     set ::out 
   110    111   } {1 2 3 4 5 6 7 8 9 10}
   111    112   
   112    113   
   113    114   finish_test
   114         -
   115         -
   116         -
   117         -

Changes to test/win32lock.test.

    60     60       lappend win32_lock_ok $::delay1
    61     61       do_test win32lock-1.2-$delay1-ok {
    62     62          set ::msg
    63     63       } {1 100000 2 50000 3 25000 4 12500}
    64     64       if {[info exists ::log] && $::log!=""} {
    65     65         do_test win32lock-1.2-$delay1-log1 {
    66     66           regsub {\d+} $::log # x
           67  +        regsub { at line \d+} $x "" x
    67     68           set x
    68     69         } {{delayed #ms for lock/sharing conflict}}
    69     70       }
    70     71     }
    71     72     if {[llength $win32_lock_ok] && [llength $win32_lock_error]} break
    72     73     incr delay1 25
    73     74     if {$delay1 > 12500} {
................................................................................
   108    109       lappend win32_lock_ok $::delay1
   109    110       do_test win32lock-2.2-$delay1-ok {
   110    111          set ::msg
   111    112       } {1 100000 2 50000 3 25000 4 12500}
   112    113       if {[info exists ::log] && $::log!=""} {
   113    114         do_test win32lock-2.2-$delay1-log1 {
   114    115           regsub {\d+} $::log # x
          116  +        regsub { at line \d+} $x "" x
   115    117           set x
   116    118         } {{delayed #ms for lock/sharing conflict}}
   117    119       }
   118    120     }
   119    121     if {[llength $win32_lock_ok] && [llength $win32_lock_error]} break
   120    122     incr delay1 1
   121    123     if {$delay1 > 500} {

Changes to tool/lemon.c.

  1727   1727     char **next,
  1728   1728     int (*cmp)(const char*,const char*)
  1729   1729   ){
  1730   1730     unsigned long offset;
  1731   1731     char *ep;
  1732   1732     char *set[LISTSIZE];
  1733   1733     int i;
  1734         -  offset = (unsigned long)next - (unsigned long)list;
         1734  +  offset = (unsigned long)((char*)next - (char*)list);
  1735   1735     for(i=0; i<LISTSIZE; i++) set[i] = 0;
  1736   1736     while( list ){
  1737   1737       ep = list;
  1738   1738       list = NEXT(list);
  1739   1739       NEXT(ep) = 0;
  1740   1740       for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
  1741   1741         ep = merge(ep,set[i],cmp,offset);
................................................................................
  1871   1871           break;
  1872   1872         case OPT_DBL:
  1873   1873         case OPT_FDBL:
  1874   1874           dv = strtod(cp,&end);
  1875   1875           if( *end ){
  1876   1876             if( err ){
  1877   1877               fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
  1878         -            errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
         1878  +            errline(i,(int)((char*)end-(char*)argv[i]),err);
  1879   1879             }
  1880   1880             errcnt++;
  1881   1881           }
  1882   1882           break;
  1883   1883         case OPT_INT:
  1884   1884         case OPT_FINT:
  1885   1885           lv = strtol(cp,&end,0);
  1886   1886           if( *end ){
  1887   1887             if( err ){
  1888   1888               fprintf(err,"%sillegal character in integer argument.\n",emsg);
  1889         -            errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
         1889  +            errline(i,(int)((char*)end-(char*)argv[i]),err);
  1890   1890             }
  1891   1891             errcnt++;
  1892   1892           }
  1893   1893           break;
  1894   1894         case OPT_STR:
  1895   1895         case OPT_FSTR:
  1896   1896           sv = cp;

Changes to tool/mksqlite3c-noext.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-noext.tcl
    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         -# from in this file.  The versioon number is needed to generate the header
           26  +# from in this file.  The version number is needed to generate the header
    27     27   # comment of the amalgamation.
    28     28   #
    29     29   if {[lsearch $argv --nostatic]>=0} {
    30     30     set addstatic 0
    31     31   } else {
    32     32     set addstatic 1
    33     33   }
................................................................................
    76     76   */
    77     77   #define SQLITE_CORE 1
    78     78   #define SQLITE_AMALGAMATION 1}]
    79     79   if {$addstatic} {
    80     80     puts $out \
    81     81   {#ifndef SQLITE_PRIVATE
    82     82   # define SQLITE_PRIVATE static
    83         -#endif
    84         -#ifndef SQLITE_API
    85         -# define SQLITE_API
    86     83   #endif}
    87     84   }
    88     85   
    89     86   # These are the header files used by SQLite.  The first time any of these 
    90     87   # files are seen in a #include statement in the C code, include the complete
    91     88   # text of the file in-line.  The file only needs to be included once.
    92     89   #
................................................................................
   102     99      os_common.h
   103    100      os_setup.h
   104    101      os_win.h
   105    102      os.h
   106    103      pager.h
   107    104      parse.h
   108    105      pcache.h
          106  +   pragma.h
   109    107      sqlite3ext.h
   110    108      sqlite3.h
   111    109      sqliteicu.h
   112    110      sqliteInt.h
   113    111      sqliteLimit.h
   114    112      vdbe.h
   115    113      vdbeInt.h
          114  +   vxworks.h
   116    115      wal.h
          116  +   whereInt.h
   117    117   } {
   118    118     set available_hdr($hdr) 1
   119    119   }
   120    120   set available_hdr(sqliteInt.h) 0
          121  +
          122  +# These headers should be copied into the amalgamation without modifying any
          123  +# of their function declarations or definitions.
          124  +set varonly_hdr(sqlite3.h) 1
          125  +
          126  +# These are the functions that accept a variable number of arguments.  They
          127  +# always need to use the "cdecl" calling convention even when another calling
          128  +# convention (e.g. "stcall") is being used for the rest of the library.
          129  +set cdecllist {
          130  +  sqlite3_config
          131  +  sqlite3_db_config
          132  +  sqlite3_log
          133  +  sqlite3_mprintf
          134  +  sqlite3_snprintf
          135  +  sqlite3_test_control
          136  +  sqlite3_vtab_config
          137  +}
   121    138   
   122    139   # 78 stars used for comment formatting.
   123    140   set s78 \
   124    141   {*****************************************************************************}
   125    142   
   126    143   # Insert a comment into the code
   127    144   #
................................................................................
   131    148     set nstar [expr {60 - $n}]
   132    149     set stars [string range $s78 0 $nstar]
   133    150     puts $out "/************** $text $stars/"
   134    151   }
   135    152   
   136    153   # Read the source file named $filename and write it into the
   137    154   # sqlite3.c output file.  If any #include statements are seen,
   138         -# process them approprately.
          155  +# process them appropriately.
   139    156   #
   140    157   proc copy_file {filename} {
   141         -  global seen_hdr available_hdr out addstatic linemacros
          158  +  global seen_hdr available_hdr varonly_hdr cdecllist out addstatic linemacros
   142    159     set ln 0
   143    160     set tail [file tail $filename]
   144    161     section_comment "Begin file $tail"
   145    162     if {$linemacros} {puts $out "#line 1 \"$filename\""}
   146    163     set in [open $filename r]
   147    164     set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+(sqlite3[_a-zA-Z0-9]+)(\[|;| =)}
   148         -  set declpattern {[a-zA-Z][a-zA-Z_0-9 ]+ \**(sqlite3[_a-zA-Z0-9]+)\(}
          165  +  set declpattern {([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3[_a-zA-Z0-9]+)(\(.*)}
   149    166     if {[file extension $filename]==".h"} {
   150    167       set declpattern " *$declpattern"
   151    168     }
   152         -  set declpattern ^$declpattern
          169  +  set declpattern ^$declpattern\$
   153    170     while {![eof $in]} {
   154    171       set line [gets $in]
   155    172       incr ln
   156    173       if {[regexp {^\s*#\s*include\s+["<]([^">]+)[">]} $line all hdr]} {
   157    174         if {[info exists available_hdr($hdr)]} {
   158    175           if {$available_hdr($hdr)} {
   159    176             if {$hdr!="os_common.h" && $hdr!="hwtime.h"} {
................................................................................
   161    178             }
   162    179             section_comment "Include $hdr in the middle of $tail"
   163    180             copy_file tsrc/$hdr
   164    181             section_comment "Continuing where we left off in $tail"
   165    182             if {$linemacros} {puts $out "#line [expr {$ln+1}] \"$filename\""}
   166    183           }
   167    184         } elseif {![info exists seen_hdr($hdr)]} {
   168         -        set seen_hdr($hdr) 1
          185  +        if {![regexp {/\*\s+amalgamator:\s+dontcache\s+\*/} $line]} {
          186  +          set seen_hdr($hdr) 1
          187  +        }
          188  +        puts $out $line
          189  +      } elseif {[regexp {/\*\s+amalgamator:\s+keep\s+\*/} $line]} {
          190  +        # This include file must be kept because there was a "keep"
          191  +        # directive inside of a line comment.
   169    192           puts $out $line
   170    193         } else {
   171         -        puts $out "/* $line */"
          194  +        # Comment out the entire line, replacing any nested comment
          195  +        # begin/end markers with the harmless substring "**".
          196  +        puts $out "/* [string map [list /* ** */ **] $line] */"
   172    197         }
   173    198       } elseif {[regexp {^#ifdef __cplusplus} $line]} {
   174    199         puts $out "#if 0"
   175    200       } elseif {!$linemacros && [regexp {^#line} $line]} {
   176    201         # Skip #line directives.
   177    202       } elseif {$addstatic && ![regexp {^(static|typedef)} $line]} {
   178         -      regsub {^SQLITE_API } $line {} line
   179         -      if {[regexp $declpattern $line all funcname]} {
          203  +      # Skip adding the SQLITE_PRIVATE or SQLITE_API keyword before
          204  +      # functions if this header file does not need it.
          205  +      if {![info exists varonly_hdr($tail)]
          206  +       && [regexp $declpattern $line all rettype funcname rest]} {
          207  +        regsub {^SQLITE_API } $line {} line
   180    208           # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions.
   181    209           # so that linkage can be modified at compile-time.
   182    210           if {[regexp {^sqlite3_} $funcname]} {
   183         -          puts $out "SQLITE_API $line"
          211  +          set line SQLITE_API
          212  +          append line " " [string trim $rettype]
          213  +          if {[string index $rettype end] ne "*"} {
          214  +            append line " "
          215  +          }
          216  +          if {[lsearch -exact $cdecllist $funcname] >= 0} {
          217  +            append line SQLITE_CDECL
          218  +          } else {
          219  +            append line SQLITE_STDCALL
          220  +          }
          221  +          append line " " $funcname $rest
          222  +          puts $out $line
   184    223           } else {
   185    224             puts $out "SQLITE_PRIVATE $line"
   186    225           }
   187    226         } elseif {[regexp $varpattern $line all varname]} {
   188         -        # Add the SQLITE_PRIVATE before variable declarations or
   189         -        # definitions for internal use
   190         -        if {![regexp {^sqlite3_} $varname]} {
   191         -          regsub {^extern } $line {} line
   192         -          puts $out "SQLITE_PRIVATE $line"
   193         -        } else {
   194         -          if {[regexp {const char sqlite3_version\[\];} $line]} {
   195         -            set line {const char sqlite3_version[] = SQLITE_VERSION;}
          227  +          # Add the SQLITE_PRIVATE before variable declarations or
          228  +          # definitions for internal use
          229  +          regsub {^SQLITE_API } $line {} line
          230  +          if {![regexp {^sqlite3_} $varname]} {
          231  +            regsub {^extern } $line {} line
          232  +            puts $out "SQLITE_PRIVATE $line"
          233  +          } else {
          234  +            if {[regexp {const char sqlite3_version\[\];} $line]} {
          235  +              set line {const char sqlite3_version[] = SQLITE_VERSION;}
          236  +            }
          237  +            regsub {^SQLITE_EXTERN } $line {} line
          238  +            puts $out "SQLITE_API $line"
   196    239             }
   197         -          regsub {^SQLITE_EXTERN } $line {} line
   198         -          puts $out "SQLITE_API $line"
   199         -        }
   200    240         } elseif {[regexp {^(SQLITE_EXTERN )?void \(\*sqlite3IoTrace\)} $line]} {
          241  +        regsub {^SQLITE_API } $line {} line
   201    242           regsub {^SQLITE_EXTERN } $line {} line
   202         -        puts $out "SQLITE_PRIVATE $line"
          243  +        puts $out $line
   203    244         } elseif {[regexp {^void \(\*sqlite3Os} $line]} {
          245  +        regsub {^SQLITE_API } $line {} line
   204    246           puts $out "SQLITE_PRIVATE $line"
   205    247         } else {
   206    248           puts $out $line
   207    249         }
   208    250       } else {
   209    251         puts $out $line
   210    252       }

Changes to tool/mksqlite3c.tcl.

    76     76   */
    77     77   #define SQLITE_CORE 1
    78     78   #define SQLITE_AMALGAMATION 1}]
    79     79   if {$addstatic} {
    80     80     puts $out \
    81     81   {#ifndef SQLITE_PRIVATE
    82     82   # define SQLITE_PRIVATE static
    83         -#endif
    84         -#ifndef SQLITE_API
    85         -# define SQLITE_API
    86     83   #endif}
    87     84   }
    88     85   
    89     86   # These are the header files used by SQLite.  The first time any of these 
    90     87   # files are seen in a #include statement in the C code, include the complete
    91     88   # text of the file in-line.  The file only needs to be included once.
    92     89   #
................................................................................
   123    120      vxworks.h
   124    121      wal.h
   125    122      whereInt.h
   126    123   } {
   127    124     set available_hdr($hdr) 1
   128    125   }
   129    126   set available_hdr(sqliteInt.h) 0
          127  +
          128  +# These headers should be copied into the amalgamation without modifying any
          129  +# of their function declarations or definitions.
          130  +set varonly_hdr(sqlite3.h) 1
          131  +
          132  +# These are the functions that accept a variable number of arguments.  They
          133  +# always need to use the "cdecl" calling convention even when another calling
          134  +# convention (e.g. "stcall") is being used for the rest of the library.
          135  +set cdecllist {
          136  +  sqlite3_config
          137  +  sqlite3_db_config
          138  +  sqlite3_log
          139  +  sqlite3_mprintf
          140  +  sqlite3_snprintf
          141  +  sqlite3_test_control
          142  +  sqlite3_vtab_config
          143  +}
   130    144   
   131    145   # 78 stars used for comment formatting.
   132    146   set s78 \
   133    147   {*****************************************************************************}
   134    148   
   135    149   # Insert a comment into the code
   136    150   #
................................................................................
   143    157   }
   144    158   
   145    159   # Read the source file named $filename and write it into the
   146    160   # sqlite3.c output file.  If any #include statements are seen,
   147    161   # process them appropriately.
   148    162   #
   149    163   proc copy_file {filename} {
   150         -  global seen_hdr available_hdr out addstatic linemacros
          164  +  global seen_hdr available_hdr varonly_hdr cdecllist out addstatic linemacros
   151    165     set ln 0
   152    166     set tail [file tail $filename]
   153    167     section_comment "Begin file $tail"
   154    168     if {$linemacros} {puts $out "#line 1 \"$filename\""}
   155    169     set in [open $filename r]
   156    170     set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+(sqlite3[_a-zA-Z0-9]+)(\[|;| =)}
   157         -  set declpattern {[a-zA-Z][a-zA-Z_0-9 ]+ \**(sqlite3[_a-zA-Z0-9]+)\(}
          171  +  set declpattern {([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3[_a-zA-Z0-9]+)(\(.*)}
   158    172     if {[file extension $filename]==".h"} {
   159    173       set declpattern " *$declpattern"
   160    174     }
   161         -  set declpattern ^$declpattern
          175  +  set declpattern ^$declpattern\$
   162    176     while {![eof $in]} {
   163    177       set line [gets $in]
   164    178       incr ln
   165    179       if {[regexp {^\s*#\s*include\s+["<]([^">]+)[">]} $line all hdr]} {
   166    180         if {[info exists available_hdr($hdr)]} {
   167    181           if {$available_hdr($hdr)} {
   168    182             if {$hdr!="os_common.h" && $hdr!="hwtime.h"} {
................................................................................
   188    202           puts $out "/* [string map [list /* ** */ **] $line] */"
   189    203         }
   190    204       } elseif {[regexp {^#ifdef __cplusplus} $line]} {
   191    205         puts $out "#if 0"
   192    206       } elseif {!$linemacros && [regexp {^#line} $line]} {
   193    207         # Skip #line directives.
   194    208       } elseif {$addstatic && ![regexp {^(static|typedef)} $line]} {
   195         -      regsub {^SQLITE_API } $line {} line
   196         -      if {[regexp $declpattern $line all funcname]} {
          209  +      # Skip adding the SQLITE_PRIVATE or SQLITE_API keyword before
          210  +      # functions if this header file does not need it.
          211  +      if {![info exists varonly_hdr($tail)]
          212  +       && [regexp $declpattern $line all rettype funcname rest]} {
          213  +        regsub {^SQLITE_API } $line {} line
   197    214           # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions.
   198    215           # so that linkage can be modified at compile-time.
   199    216           if {[regexp {^sqlite3(_|ota_)} $funcname]} {
   200         -          puts $out "SQLITE_API $line"
          217  +          set line SQLITE_API
          218  +          append line " " [string trim $rettype]
          219  +          if {[string index $rettype end] ne "*"} {
          220  +            append line " "
          221  +          }
          222  +          if {[lsearch -exact $cdecllist $funcname] >= 0} {
          223  +            append line SQLITE_CDECL
          224  +          } else {
          225  +            append line SQLITE_STDCALL
          226  +          }
          227  +          append line " " $funcname $rest
          228  +          puts $out $line
   201    229           } else {
   202    230             puts $out "SQLITE_PRIVATE $line"
   203    231           }
   204    232         } elseif {[regexp $varpattern $line all varname]} {
   205         -        # Add the SQLITE_PRIVATE before variable declarations or
   206         -        # definitions for internal use
   207         -        if {![regexp {^sqlite3_} $varname]} {
   208         -          regsub {^extern } $line {} line
   209         -          puts $out "SQLITE_PRIVATE $line"
   210         -        } else {
   211         -          if {[regexp {const char sqlite3_version\[\];} $line]} {
   212         -            set line {const char sqlite3_version[] = SQLITE_VERSION;}
          233  +          # Add the SQLITE_PRIVATE before variable declarations or
          234  +          # definitions for internal use
          235  +          regsub {^SQLITE_API } $line {} line
          236  +          if {![regexp {^sqlite3_} $varname]} {
          237  +            regsub {^extern } $line {} line
          238  +            puts $out "SQLITE_PRIVATE $line"
          239  +          } else {
          240  +            if {[regexp {const char sqlite3_version\[\];} $line]} {
          241  +              set line {const char sqlite3_version[] = SQLITE_VERSION;}
          242  +            }
          243  +            regsub {^SQLITE_EXTERN } $line {} line
          244  +            puts $out "SQLITE_API $line"
   213    245             }
   214         -          regsub {^SQLITE_EXTERN } $line {} line
   215         -          puts $out "SQLITE_API $line"
   216         -        }
   217    246         } elseif {[regexp {^(SQLITE_EXTERN )?void \(\*sqlite3IoTrace\)} $line]} {
          247  +        regsub {^SQLITE_API } $line {} line
   218    248           regsub {^SQLITE_EXTERN } $line {} line
   219    249           puts $out $line
   220    250         } elseif {[regexp {^void \(\*sqlite3Os} $line]} {
          251  +        regsub {^SQLITE_API } $line {} line
   221    252           puts $out "SQLITE_PRIVATE $line"
   222    253         } else {
   223    254           puts $out $line
   224    255         }
   225    256       } else {
   226    257         puts $out $line
   227    258       }

Changes to tool/mksqlite3h.tcl.

    59     59     }
    60     60   }
    61     61   close $in
    62     62   
    63     63   # Set up patterns for recognizing API declarations.
    64     64   #
    65     65   set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)}
    66         -set declpattern {^ *[a-zA-Z][a-zA-Z_0-9 ]+ \**sqlite3_[_a-zA-Z0-9]+\(}
           66  +set declpattern {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$}
    67     67   
    68     68   # Force the output to use unix line endings, even on Windows.
    69     69   fconfigure stdout -translation lf
    70     70   
    71     71   set filelist [subst {
    72     72     $TOP/src/sqlite.h.in
    73     73     $TOP/ext/rtree/sqlite3rtree.h
    74     74   }]
           75  +
           76  +# These are the functions that accept a variable number of arguments.  They
           77  +# always need to use the "cdecl" calling convention even when another calling
           78  +# convention (e.g. "stcall") is being used for the rest of the library.
           79  +set cdecllist {
           80  +  sqlite3_config
           81  +  sqlite3_db_config
           82  +  sqlite3_log
           83  +  sqlite3_mprintf
           84  +  sqlite3_snprintf
           85  +  sqlite3_test_control
           86  +  sqlite3_vtab_config
           87  +}
    75     88   
    76     89   # Process the source files.
    77     90   #
    78     91   foreach file $filelist {
    79     92     set in [open $file]
    80     93     while {![eof $in]} {
    81     94     
................................................................................
    85     98       # line when copying sqlite3rtree.h into sqlite3.h.
    86     99       #
    87    100       if {[string match {*#include*<sqlite3.h>*} $line]} continue
    88    101     
    89    102       regsub -- --VERS--           $line $zVersion line
    90    103       regsub -- --VERSION-NUMBER-- $line $nVersion line
    91    104       regsub -- --SOURCE-ID--      $line "$zDate $zUuid" line
    92         -  
    93         -    if {[regexp {define SQLITE_EXTERN extern} $line]} {
    94         -      puts $line
    95         -      puts [gets $in]
    96         -      puts ""
    97         -      puts "#ifndef SQLITE_API"
    98         -      puts "# define SQLITE_API"
    99         -      puts "#endif"
   100         -      set line ""
   101         -    }
   102         -  
   103         -    if {([regexp $varpattern $line] && ![regexp {^ *typedef} $line])
   104         -     || ([regexp $declpattern $line])
   105         -    } {
          105  +
          106  +    if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} {
   106    107         set line "SQLITE_API $line"
          108  +    } else {
          109  +      if {[regexp $declpattern $line all rettype funcname rest]} {
          110  +        set line SQLITE_API
          111  +        append line " " [string trim $rettype]
          112  +        if {[string index $rettype end] ne "*"} {
          113  +          append line " "
          114  +        }
          115  +        if {[lsearch -exact $cdecllist $funcname] >= 0} {
          116  +          append line SQLITE_CDECL
          117  +        } else {
          118  +          append line SQLITE_STDCALL
          119  +        }
          120  +        append line " " $funcname $rest
          121  +      }
   107    122       }
   108    123       puts $line
   109    124     }
   110    125     close $in
   111    126   }

Added tool/showlocks.c.

            1  +/*
            2  +** This file implements a simple command-line utility that shows all of the
            3  +** Posix Advisory Locks on a file.
            4  +**
            5  +** Usage:
            6  +**
            7  +**     showlocks FILENAME
            8  +**
            9  +** To compile:  gcc -o showlocks showlocks.c
           10  +*/
           11  +#include <stdio.h>
           12  +#include <unistd.h>
           13  +#include <fcntl.h>
           14  +#include <stdlib.h>
           15  +#include <string.h>
           16  +
           17  +/* This utility only looks for locks in the first 2 billion bytes */
           18  +#define MX_LCK 2147483647
           19  +
           20  +/*
           21  +** Print all locks on the inode of "fd" that occur in between
           22  +** lwr and upr, inclusive.
           23  +*/
           24  +static int showLocksInRange(int fd, off_t lwr, off_t upr){
           25  +  int cnt = 0;
           26  +  struct flock x;
           27  +
           28  +  x.l_type = F_WRLCK;
           29  +  x.l_whence = SEEK_SET;
           30  +  x.l_start = lwr;
           31  +  x.l_len = upr-lwr;
           32  +  fcntl(fd, F_GETLK, &x);
           33  +  if( x.l_type==F_UNLCK ) return 0;
           34  +  printf("start: %-12d len: %-5d pid: %-5d type: %s\n",
           35  +       (int)x.l_start, (int)x.l_len,
           36  +       x.l_pid, x.l_type==F_WRLCK ? "WRLCK" : "RDLCK");
           37  +  cnt++;
           38  +  if( x.l_start>lwr ){
           39  +    cnt += showLocksInRange(fd, lwr, x.l_start-1);
           40  +  }
           41  +  if( x.l_start+x.l_len<upr ){
           42  +    cnt += showLocksInRange(fd, x.l_start+x.l_len+1, upr);
           43  +  }
           44  +  return cnt;
           45  +}
           46  +
           47  +int main(int argc, char **argv){
           48  +  int fd;
           49  +  int cnt;
           50  +
           51  +  if( argc!=2 ){
           52  +    fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
           53  +    return 1;
           54  +  }
           55  +  fd = open(argv[1], O_RDWR, 0);
           56  +  if( fd<0 ){
           57  +    fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
           58  +    return 1;
           59  +  }
           60  +  cnt = showLocksInRange(fd, 0, MX_LCK);
           61  +  if( cnt==0 ) printf("no locks\n");  
           62  +  close(fd);
           63  +  return 0;
           64  +}