/ Check-in [c458db41]
Login

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

Overview
Comment:Merge updates from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | expShell
Files: files | file ages | folders
SHA1: c458db41bbb6bd13d47531685e25a09f512c7f5f
User & Date: mistachkin 2015-04-07 21:18:18
Context
2015-04-16
04:20
Merge updates from trunk. Closed-Leaf check-in: 22827542 user: mistachkin tags: expShell
2015-04-07
21:18
Merge updates from trunk. check-in: c458db41 user: mistachkin tags: expShell
21:17
Correct typo in new test numbering. check-in: d176c9fa user: mistachkin tags: expShell
15:39
Avoid signed integer overflow when converting oversized in-line integer widths and precisions in printf(). check-in: 8e4ac2ce user: drh tags: trunk
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.

   906    906   **
   907    907   ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
   908    908   ** the output value undefined. Otherwise SQLITE_OK is returned.
   909    909   **
   910    910   ** This function is used when parsing the "prefix=" FTS4 parameter.
   911    911   */
   912    912   static int fts3GobbleInt(const char **pp, int *pnOut){
          913  +  const int MAX_NPREFIX = 10000000;
   913    914     const char *p;                  /* Iterator pointer */
   914    915     int nInt = 0;                   /* Output value */
   915    916   
   916    917     for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
   917    918       nInt = nInt * 10 + (p[0] - '0');
          919  +    if( nInt>MAX_NPREFIX ){
          920  +      nInt = 0;
          921  +      break;
          922  +    }
   918    923     }
   919    924     if( p==*pp ) return SQLITE_ERROR;
   920    925     *pnOut = nInt;
   921    926     *pp = p;
   922    927     return SQLITE_OK;
   923    928   }
   924    929   
................................................................................
   962    967     }
   963    968   
   964    969     memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
   965    970     if( zParam ){
   966    971       const char *p = zParam;
   967    972       int i;
   968    973       for(i=1; i<nIndex; i++){
   969         -      int nPrefix;
          974  +      int nPrefix = 0;
   970    975         if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
   971         -      if( nPrefix<=0 ){
          976  +      assert( nPrefix>=0 );
          977  +      if( nPrefix==0 ){
   972    978           nIndex--;
   973    979           i--;
   974    980         }else{
   975    981           aIndex[i].nPrefix = nPrefix;
   976    982         }
   977    983         p++;
   978    984       }
................................................................................
  1098   1104     char *zCsr;                     /* Space for holding column names */
  1099   1105     int nDb;                        /* Bytes required to hold database name */
  1100   1106     int nName;                      /* Bytes required to hold table name */
  1101   1107     int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
  1102   1108     const char **aCol;              /* Array of column names */
  1103   1109     sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
  1104   1110   
  1105         -  int nIndex;                     /* Size of aIndex[] array */
         1111  +  int nIndex = 0;                 /* Size of aIndex[] array */
  1106   1112     struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
  1107   1113   
  1108   1114     /* The results of parsing supported FTS4 key=value options: */
  1109   1115     int bNoDocsize = 0;             /* True to omit %_docsize table */
  1110   1116     int bDescIdx = 0;               /* True to store descending indexes */
  1111   1117     char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
  1112   1118     char *zCompress = 0;            /* compress=? parameter (or NULL) */
................................................................................
  2492   2498   **
  2493   2499   ** If the docids in the input doclists are sorted in ascending order,
  2494   2500   ** parameter bDescDoclist should be false. If they are sorted in ascending 
  2495   2501   ** order, it should be passed a non-zero value.
  2496   2502   **
  2497   2503   ** The right-hand input doclist is overwritten by this function.
  2498   2504   */
  2499         -static void fts3DoclistPhraseMerge(
         2505  +static int fts3DoclistPhraseMerge(
  2500   2506     int bDescDoclist,               /* True if arguments are desc */
  2501   2507     int nDist,                      /* Distance from left to right (1=adjacent) */
  2502   2508     char *aLeft, int nLeft,         /* Left doclist */
  2503         -  char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
         2509  +  char **paRight, int *pnRight    /* IN/OUT: Right/output doclist */
  2504   2510   ){
  2505   2511     sqlite3_int64 i1 = 0;
  2506   2512     sqlite3_int64 i2 = 0;
  2507   2513     sqlite3_int64 iPrev = 0;
         2514  +  char *aRight = *paRight;
  2508   2515     char *pEnd1 = &aLeft[nLeft];
  2509   2516     char *pEnd2 = &aRight[*pnRight];
  2510   2517     char *p1 = aLeft;
  2511   2518     char *p2 = aRight;
  2512   2519     char *p;
  2513   2520     int bFirstOut = 0;
  2514         -  char *aOut = aRight;
         2521  +  char *aOut;
  2515   2522   
  2516   2523     assert( nDist>0 );
  2517         -
         2524  +  if( bDescDoclist ){
         2525  +    aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
         2526  +    if( aOut==0 ) return SQLITE_NOMEM;
         2527  +  }else{
         2528  +    aOut = aRight;
         2529  +  }
  2518   2530     p = aOut;
         2531  +
  2519   2532     fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  2520   2533     fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  2521   2534   
  2522   2535     while( p1 && p2 ){
  2523   2536       sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  2524   2537       if( iDiff==0 ){
  2525   2538         char *pSave = p;
................................................................................
  2540   2553       }else{
  2541   2554         fts3PoslistCopy(0, &p2);
  2542   2555         fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2543   2556       }
  2544   2557     }
  2545   2558   
  2546   2559     *pnRight = (int)(p - aOut);
         2560  +  if( bDescDoclist ){
         2561  +    sqlite3_free(aRight);
         2562  +    *paRight = aOut;
         2563  +  }
         2564  +
         2565  +  return SQLITE_OK;
  2547   2566   }
  2548   2567   
  2549   2568   /*
  2550   2569   ** Argument pList points to a position list nList bytes in size. This
  2551   2570   ** function checks to see if the position list contains any entries for
  2552   2571   ** a token in position 0 (of any column). If so, it writes argument iDelta
  2553   2572   ** to the output buffer pOut, followed by a position list consisting only
................................................................................
  2664   2683     Fts3Table *p,                   /* FTS table handle */
  2665   2684     TermSelect *pTS,                /* TermSelect object to merge into */
  2666   2685     char *aDoclist,                 /* Pointer to doclist */
  2667   2686     int nDoclist                    /* Size of aDoclist in bytes */
  2668   2687   ){
  2669   2688     if( pTS->aaOutput[0]==0 ){
  2670   2689       /* If this is the first term selected, copy the doclist to the output
  2671         -    ** buffer using memcpy(). */
  2672         -    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);
  2673   2706       pTS->anOutput[0] = nDoclist;
  2674   2707       if( pTS->aaOutput[0] ){
  2675   2708         memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
  2676   2709       }else{
  2677   2710         return SQLITE_NOMEM;
  2678   2711       }
  2679   2712     }else{
................................................................................
  3921   3954   /*
  3922   3955   ** Arguments pList/nList contain the doclist for token iToken of phrase p.
  3923   3956   ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
  3924   3957   **
  3925   3958   ** This function assumes that pList points to a buffer allocated using
  3926   3959   ** sqlite3_malloc(). This function takes responsibility for eventually
  3927   3960   ** freeing the buffer.
         3961  +**
         3962  +** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
  3928   3963   */
  3929         -static void fts3EvalPhraseMergeToken(
         3964  +static int fts3EvalPhraseMergeToken(
  3930   3965     Fts3Table *pTab,                /* FTS Table pointer */
  3931   3966     Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
  3932   3967     int iToken,                     /* Token pList/nList corresponds to */
  3933   3968     char *pList,                    /* Pointer to doclist */
  3934   3969     int nList                       /* Number of bytes in pList */
  3935   3970   ){
         3971  +  int rc = SQLITE_OK;
  3936   3972     assert( iToken!=p->iDoclistToken );
  3937   3973   
  3938   3974     if( pList==0 ){
  3939   3975       sqlite3_free(p->doclist.aAll);
  3940   3976       p->doclist.aAll = 0;
  3941   3977       p->doclist.nAll = 0;
  3942   3978     }
................................................................................
  3967   4003         pRight = p->doclist.aAll;
  3968   4004         nRight = p->doclist.nAll;
  3969   4005         pLeft = pList;
  3970   4006         nLeft = nList;
  3971   4007         nDiff = p->iDoclistToken - iToken;
  3972   4008       }
  3973   4009   
  3974         -    fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
         4010  +    rc = fts3DoclistPhraseMerge(
         4011  +        pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
         4012  +    );
  3975   4013       sqlite3_free(pLeft);
  3976   4014       p->doclist.aAll = pRight;
  3977   4015       p->doclist.nAll = nRight;
  3978   4016     }
  3979   4017   
  3980   4018     if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
         4019  +  return rc;
  3981   4020   }
  3982   4021   
  3983   4022   /*
  3984   4023   ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
  3985   4024   ** does not take deferred tokens into account.
  3986   4025   **
  3987   4026   ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
................................................................................
  3999   4038       assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
  4000   4039   
  4001   4040       if( pToken->pSegcsr ){
  4002   4041         int nThis = 0;
  4003   4042         char *pThis = 0;
  4004   4043         rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
  4005   4044         if( rc==SQLITE_OK ){
  4006         -        fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
         4045  +        rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
  4007   4046         }
  4008   4047       }
  4009   4048       assert( pToken->pSegcsr==0 );
  4010   4049     }
  4011   4050   
  4012   4051     return rc;
  4013   4052   }
................................................................................
  4801   4840           ** part of a multi-token phrase. Either way, the entire doclist will
  4802   4841           ** (eventually) be loaded into memory. It may as well be now. */
  4803   4842           Fts3PhraseToken *pToken = pTC->pToken;
  4804   4843           int nList = 0;
  4805   4844           char *pList = 0;
  4806   4845           rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
  4807   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  +        }
  4808   4852           if( rc==SQLITE_OK ){
  4809   4853             int nCount;
  4810         -          fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
  4811   4854             nCount = fts3DoclistCountDocids(
  4812   4855                 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
  4813   4856             );
  4814   4857             if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
  4815   4858           }
  4816   4859         }
  4817   4860       }

Changes to ext/fts3/fts3_tokenizer.c.

    65     65   
    66     66     zName = sqlite3_value_text(argv[0]);
    67     67     nName = sqlite3_value_bytes(argv[0])+1;
    68     68   
    69     69     if( argc==2 ){
    70     70       void *pOld;
    71     71       int n = sqlite3_value_bytes(argv[1]);
    72         -    if( n!=sizeof(pPtr) ){
           72  +    if( zName==0 || n!=sizeof(pPtr) ){
    73     73         sqlite3_result_error(context, "argument type mismatch", -1);
    74     74         return;
    75     75       }
    76     76       pPtr = *(void **)sqlite3_value_blob(argv[1]);
    77     77       pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
    78     78       if( pOld==pPtr ){
    79     79         sqlite3_result_error(context, "out of memory", -1);
    80     80         return;
    81     81       }
    82     82     }else{
    83         -    pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
           83  +    if( zName ){
           84  +      pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
           85  +    }
    84     86       if( !pPtr ){
    85     87         char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
    86     88         sqlite3_result_error(context, zErr, -1);
    87     89         sqlite3_free(zErr);
    88     90         return;
    89     91       }
    90     92     }

Changes to main.mk.

   403    403   	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE)                  \
   404    404   		$(TOP)/src/shell.c                                  \
   405    405   		libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
   406    406   
   407    407   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   408    408   	$(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   409    409   		$(TLIBS) $(THREADLIB)
          410  +
          411  +MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
          412  +MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
          413  +mptest:	mptester$(EXE)
          414  +	rm -f mptest.db
          415  +	$(MPTEST1) --journalmode DELETE
          416  +	$(MPTEST2) --journalmode WAL
          417  +	$(MPTEST1) --journalmode WAL
          418  +	$(MPTEST2) --journalmode PERSIST
          419  +	$(MPTEST1) --journalmode PERSIST
          420  +	$(MPTEST2) --journalmode TRUNCATE
          421  +	$(MPTEST1) --journalmode TRUNCATE
          422  +	$(MPTEST2) --journalmode DELETE
   410    423   
   411    424   sqlite3.o:	sqlite3.c
   412    425   	$(TCCX) -I. -c sqlite3.c
   413    426   
   414    427   # This target creates a directory named "tsrc" and fills it with
   415    428   # copies of all of the C source code and header files needed to
   416    429   # 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;
................................................................................
  2013   2023   #endif
  2014   2024     if( pBt==0 ){
  2015   2025       /*
  2016   2026       ** The following asserts make sure that structures used by the btree are
  2017   2027       ** the right size.  This is to guard against size changes that result
  2018   2028       ** when compiling on a different architecture.
  2019   2029       */
  2020         -    assert( sizeof(i64)==8 || sizeof(i64)==4 );
  2021         -    assert( sizeof(u64)==8 || sizeof(u64)==4 );
         2030  +    assert( sizeof(i64)==8 );
         2031  +    assert( sizeof(u64)==8 );
  2022   2032       assert( sizeof(u32)==4 );
  2023   2033       assert( sizeof(u16)==2 );
  2024   2034       assert( sizeof(Pgno)==4 );
  2025   2035     
  2026   2036       pBt = sqlite3MallocZero( sizeof(*pBt) );
  2027   2037       if( pBt==0 ){
  2028   2038         rc = SQLITE_NOMEM;
................................................................................
  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/build.c.

  3179   3179               sqlite3ErrorMsg(pParse, 
  3180   3180                   "conflicting ON CONFLICT clauses specified", 0);
  3181   3181             }
  3182   3182             if( pIdx->onError==OE_Default ){
  3183   3183               pIdx->onError = pIndex->onError;
  3184   3184             }
  3185   3185           }
         3186  +        pRet = pIdx;
  3186   3187           goto exit_create_index;
  3187   3188         }
  3188   3189       }
  3189   3190     }
  3190   3191   
  3191   3192     /* Link the new Index structure to its table and to the other
  3192   3193     ** in-memory database structures. 

Changes to src/expr.c.

   393    393   ** the height is greater than the maximum allowed expression depth,
   394    394   ** leave an error in pParse.
   395    395   **
   396    396   ** Also propagate all EP_Propagate flags from the Expr.x.pList into
   397    397   ** Expr.flags. 
   398    398   */
   399    399   void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
          400  +  if( pParse->nErr ) return;
   400    401     exprSetHeight(p);
   401    402     sqlite3ExprCheckHeight(pParse, p->nHeight);
   402    403   }
   403    404   
   404    405   /*
   405    406   ** Return the maximum height of any expression tree referenced
   406    407   ** by the select statement passed as an argument.

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   **
................................................................................
   123    123   
   124    124   #ifdef SQLITE_OMIT_WSD
   125    125     rc = sqlite3_wsd_init(4096, 24);
   126    126     if( rc!=SQLITE_OK ){
   127    127       return rc;
   128    128     }
   129    129   #endif
          130  +
          131  +  /* If the following assert() fails on some obscure processor/compiler
          132  +  ** combination, the work-around is to set the correct pointer
          133  +  ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
          134  +  assert( SQLITE_PTRSIZE==sizeof(char*) );
   130    135   
   131    136     /* If SQLite is already completely initialized, then this call
   132    137     ** to sqlite3_initialize() should be a no-op.  But the initialization
   133    138     ** must be complete.  So isInit must not be set until the very end
   134    139     ** of this routine.
   135    140     */
   136    141     if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;

Changes to src/malloc.c.

    70     70     ** True if heap is nearly "full" where "full" is defined by the
    71     71     ** sqlite3_soft_heap_limit() setting.
    72     72     */
    73     73     int nearlyFull;
    74     74   } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
    75     75   
    76     76   #define mem0 GLOBAL(struct Mem0Global, mem0)
           77  +
           78  +/*
           79  +** Return the memory allocator mutex. sqlite3_status() needs it.
           80  +*/
           81  +sqlite3_mutex *sqlite3MallocMutex(void){
           82  +  return mem0.mutex;
           83  +}
    77     84   
    78     85   /*
    79     86   ** This routine runs when the memory allocator sees that the
    80     87   ** total memory allocation is about to exceed the soft heap
    81     88   ** limit.
    82     89   */
    83     90   static void softHeapLimitEnforcer(
................................................................................
    93    100   ** Change the alarm callback
    94    101   */
    95    102   static int sqlite3MemoryAlarm(
    96    103     void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
    97    104     void *pArg,
    98    105     sqlite3_int64 iThreshold
    99    106   ){
   100         -  int nUsed;
          107  +  sqlite3_int64 nUsed;
   101    108     sqlite3_mutex_enter(mem0.mutex);
   102    109     mem0.alarmCallback = xCallback;
   103    110     mem0.alarmArg = pArg;
   104    111     mem0.alarmThreshold = iThreshold;
   105    112     nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   106    113     mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
   107    114     sqlite3_mutex_leave(mem0.mutex);
................................................................................
   151    158     sqlite3_soft_heap_limit64(n);
   152    159   }
   153    160   
   154    161   /*
   155    162   ** Initialize the memory allocation subsystem.
   156    163   */
   157    164   int sqlite3MallocInit(void){
          165  +  int rc;
   158    166     if( sqlite3GlobalConfig.m.xMalloc==0 ){
   159    167       sqlite3MemSetDefault();
   160    168     }
   161    169     memset(&mem0, 0, sizeof(mem0));
   162    170     if( sqlite3GlobalConfig.bCoreMutex ){
   163    171       mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   164    172     }
................................................................................
   186    194     }
   187    195     if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
   188    196         || sqlite3GlobalConfig.nPage<1 ){
   189    197       sqlite3GlobalConfig.pPage = 0;
   190    198       sqlite3GlobalConfig.szPage = 0;
   191    199       sqlite3GlobalConfig.nPage = 0;
   192    200     }
   193         -  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;
   194    204   }
   195    205   
   196    206   /*
   197    207   ** Return true if the heap is currently under memory pressure - in other
   198    208   ** words if the amount of heap used is close to the limit set by
   199    209   ** sqlite3_soft_heap_limit().
   200    210   */
................................................................................
   262    272   static int mallocWithAlarm(int n, void **pp){
   263    273     int nFull;
   264    274     void *p;
   265    275     assert( sqlite3_mutex_held(mem0.mutex) );
   266    276     nFull = sqlite3GlobalConfig.m.xRoundup(n);
   267    277     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
   268    278     if( mem0.alarmCallback!=0 ){
   269         -    int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
          279  +    sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   270    280       if( nUsed >= mem0.alarmThreshold - nFull ){
   271    281         mem0.nearlyFull = 1;
   272    282         sqlite3MallocAlarm(nFull);
   273    283       }else{
   274    284         mem0.nearlyFull = 0;
   275    285       }
   276    286     }
................................................................................
   279    289     if( p==0 && mem0.alarmCallback ){
   280    290       sqlite3MallocAlarm(nFull);
   281    291       p = sqlite3GlobalConfig.m.xMalloc(nFull);
   282    292     }
   283    293   #endif
   284    294     if( p ){
   285    295       nFull = sqlite3MallocSize(p);
   286         -    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
   287         -    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
          296  +    sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
          297  +    sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
   288    298     }
   289    299     *pp = p;
   290    300     return nFull;
   291    301   }
   292    302   
   293    303   /*
   294    304   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
................................................................................
   357    367   
   358    368     sqlite3_mutex_enter(mem0.mutex);
   359    369     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   360    370     if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
   361    371       p = mem0.pScratchFree;
   362    372       mem0.pScratchFree = mem0.pScratchFree->pNext;
   363    373       mem0.nScratchFree--;
   364         -    sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
          374  +    sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
   365    375       sqlite3_mutex_leave(mem0.mutex);
   366    376     }else{
   367    377       sqlite3_mutex_leave(mem0.mutex);
   368    378       p = sqlite3Malloc(n);
   369    379       if( sqlite3GlobalConfig.bMemstat && p ){
   370    380         sqlite3_mutex_enter(mem0.mutex);
   371         -      sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
          381  +      sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
   372    382         sqlite3_mutex_leave(mem0.mutex);
   373    383       }
   374    384       sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
   375    385     }
   376    386     assert( sqlite3_mutex_notheld(mem0.mutex) );
   377    387   
   378    388   
................................................................................
   405    415         ScratchFreeslot *pSlot;
   406    416         pSlot = (ScratchFreeslot*)p;
   407    417         sqlite3_mutex_enter(mem0.mutex);
   408    418         pSlot->pNext = mem0.pScratchFree;
   409    419         mem0.pScratchFree = pSlot;
   410    420         mem0.nScratchFree++;
   411    421         assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
   412         -      sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
          422  +      sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
   413    423         sqlite3_mutex_leave(mem0.mutex);
   414    424       }else{
   415    425         /* Release memory back to the heap */
   416    426         assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
   417         -      assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
          427  +      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
   418    428         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   419    429         if( sqlite3GlobalConfig.bMemstat ){
   420    430           int iSize = sqlite3MallocSize(p);
   421    431           sqlite3_mutex_enter(mem0.mutex);
   422         -        sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
   423         -        sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
   424         -        sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
          432  +        sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
          433  +        sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
          434  +        sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
   425    435           sqlite3GlobalConfig.m.xFree(p);
   426    436           sqlite3_mutex_leave(mem0.mutex);
   427    437         }else{
   428    438           sqlite3GlobalConfig.m.xFree(p);
   429    439         }
   430    440       }
   431    441     }
................................................................................
   448    458   */
   449    459   int sqlite3MallocSize(void *p){
   450    460     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   451    461     return sqlite3GlobalConfig.m.xSize(p);
   452    462   }
   453    463   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   454    464     if( db==0 ){
   455         -    assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          465  +    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
   456    466       assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   457    467       return sqlite3MallocSize(p);
   458    468     }else{
   459    469       assert( sqlite3_mutex_held(db->mutex) );
   460    470       if( isLookaside(db, p) ){
   461    471         return db->lookaside.sz;
   462    472       }else{
   463    473         assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   464         -      assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          474  +      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   465    475         return sqlite3GlobalConfig.m.xSize(p);
   466    476       }
   467    477     }
   468    478   }
   469    479   sqlite3_uint64 sqlite3_msize(void *p){
   470         -  assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          480  +  assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
   471    481     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   472    482     return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
   473    483   }
   474    484   
   475    485   /*
   476    486   ** Free memory previously obtained from sqlite3Malloc().
   477    487   */
   478    488   void sqlite3_free(void *p){
   479    489     if( p==0 ) return;  /* IMP: R-49053-54554 */
   480    490     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   481         -  assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          491  +  assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
   482    492     if( sqlite3GlobalConfig.bMemstat ){
   483    493       sqlite3_mutex_enter(mem0.mutex);
   484         -    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   485         -    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
          494  +    sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
          495  +    sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
   486    496       sqlite3GlobalConfig.m.xFree(p);
   487    497       sqlite3_mutex_leave(mem0.mutex);
   488    498     }else{
   489    499       sqlite3GlobalConfig.m.xFree(p);
   490    500     }
   491    501   }
   492    502   
................................................................................
   519    529         pBuf->pNext = db->lookaside.pFree;
   520    530         db->lookaside.pFree = pBuf;
   521    531         db->lookaside.nOut--;
   522    532         return;
   523    533       }
   524    534     }
   525    535     assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   526         -  assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          536  +  assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   527    537     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   528    538     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   529    539     sqlite3_free(p);
   530    540   }
   531    541   
   532    542   /*
   533    543   ** Change the size of an existing memory allocation
   534    544   */
   535    545   void *sqlite3Realloc(void *pOld, u64 nBytes){
   536    546     int nOld, nNew, nDiff;
   537    547     void *pNew;
   538    548     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   539         -  assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
          549  +  assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
   540    550     if( pOld==0 ){
   541    551       return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
   542    552     }
   543    553     if( nBytes==0 ){
   544    554       sqlite3_free(pOld); /* IMP: R-26507-47431 */
   545    555       return 0;
   546    556     }
................................................................................
   566    576       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   567    577       if( pNew==0 && mem0.alarmCallback ){
   568    578         sqlite3MallocAlarm((int)nBytes);
   569    579         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   570    580       }
   571    581       if( pNew ){
   572    582         nNew = sqlite3MallocSize(pNew);
   573         -      sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
          583  +      sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   574    584       }
   575    585       sqlite3_mutex_leave(mem0.mutex);
   576    586     }else{
   577    587       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   578    588     }
   579    589     assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
   580    590     return pNew;
................................................................................
   699    709         pNew = sqlite3DbMallocRaw(db, n);
   700    710         if( pNew ){
   701    711           memcpy(pNew, p, db->lookaside.sz);
   702    712           sqlite3DbFree(db, p);
   703    713         }
   704    714       }else{
   705    715         assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   706         -      assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          716  +      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   707    717         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   708    718         pNew = sqlite3_realloc64(p, n);
   709    719         if( !pNew ){
   710    720           db->mallocFailed = 1;
   711    721         }
   712    722         sqlite3MemdebugSetType(pNew,
   713    723               (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));

Changes to src/os_unix.c.

  1532   1532     struct flock lock;
  1533   1533     int tErrno = 0;
  1534   1534   
  1535   1535     assert( pFile );
  1536   1536     OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
  1537   1537         azFileLock(eFileLock), azFileLock(pFile->eFileLock),
  1538   1538         azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
  1539         -      osGetpid()));
         1539  +      osGetpid(0)));
  1540   1540   
  1541   1541     /* If there is already a lock of this type or more restrictive on the
  1542   1542     ** unixFile, do nothing. Don't use the end_lock: exit path, as
  1543   1543     ** unixEnterMutex() hasn't been called yet.
  1544   1544     */
  1545   1545     if( pFile->eFileLock>=eFileLock ){
  1546   1546       OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
................................................................................
  1740   1740     unixInodeInfo *pInode;
  1741   1741     struct flock lock;
  1742   1742     int rc = SQLITE_OK;
  1743   1743   
  1744   1744     assert( pFile );
  1745   1745     OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
  1746   1746         pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
  1747         -      osGetpid()));
         1747  +      osGetpid(0)));
  1748   1748   
  1749   1749     assert( eFileLock<=SHARED_LOCK );
  1750   1750     if( pFile->eFileLock<=eFileLock ){
  1751   1751       return SQLITE_OK;
  1752   1752     }
  1753   1753     unixEnterMutex();
  1754   1754     pInode = pFile->pInode;
................................................................................
  2167   2167   static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
  2168   2168     unixFile *pFile = (unixFile*)id;
  2169   2169     char *zLockFile = (char *)pFile->lockingContext;
  2170   2170     int rc;
  2171   2171   
  2172   2172     assert( pFile );
  2173   2173     OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
  2174         -           pFile->eFileLock, osGetpid()));
         2174  +           pFile->eFileLock, osGetpid(0)));
  2175   2175     assert( eFileLock<=SHARED_LOCK );
  2176   2176     
  2177   2177     /* no-op if possible */
  2178   2178     if( pFile->eFileLock==eFileLock ){
  2179   2179       return SQLITE_OK;
  2180   2180     }
  2181   2181   
................................................................................
  2385   2385   ** the requested locking level, this routine is a no-op.
  2386   2386   */
  2387   2387   static int flockUnlock(sqlite3_file *id, int eFileLock) {
  2388   2388     unixFile *pFile = (unixFile*)id;
  2389   2389     
  2390   2390     assert( pFile );
  2391   2391     OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
  2392         -           pFile->eFileLock, osGetpid()));
         2392  +           pFile->eFileLock, osGetpid(0)));
  2393   2393     assert( eFileLock<=SHARED_LOCK );
  2394   2394     
  2395   2395     /* no-op if possible */
  2396   2396     if( pFile->eFileLock==eFileLock ){
  2397   2397       return SQLITE_OK;
  2398   2398     }
  2399   2399     
................................................................................
  2553   2553   static int semXUnlock(sqlite3_file *id, int eFileLock) {
  2554   2554     unixFile *pFile = (unixFile*)id;
  2555   2555     sem_t *pSem = pFile->pInode->pSem;
  2556   2556   
  2557   2557     assert( pFile );
  2558   2558     assert( pSem );
  2559   2559     OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
  2560         -           pFile->eFileLock, osGetpid()));
         2560  +           pFile->eFileLock, osGetpid(0)));
  2561   2561     assert( eFileLock<=SHARED_LOCK );
  2562   2562     
  2563   2563     /* no-op if possible */
  2564   2564     if( pFile->eFileLock==eFileLock ){
  2565   2565       return SQLITE_OK;
  2566   2566     }
  2567   2567     
................................................................................
  2767   2767     unixFile *pFile = (unixFile*)id;
  2768   2768     unixInodeInfo *pInode = pFile->pInode;
  2769   2769     afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  2770   2770     
  2771   2771     assert( pFile );
  2772   2772     OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
  2773   2773              azFileLock(eFileLock), azFileLock(pFile->eFileLock),
  2774         -           azFileLock(pInode->eFileLock), pInode->nShared , osGetpid()));
         2774  +           azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
  2775   2775   
  2776   2776     /* If there is already a lock of this type or more restrictive on the
  2777   2777     ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
  2778   2778     ** unixEnterMutex() hasn't been called yet.
  2779   2779     */
  2780   2780     if( pFile->eFileLock>=eFileLock ){
  2781   2781       OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
................................................................................
  2953   2953   #ifdef SQLITE_TEST
  2954   2954     int h = pFile->h;
  2955   2955   #endif
  2956   2956   
  2957   2957     assert( pFile );
  2958   2958     OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
  2959   2959              pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
  2960         -           osGetpid()));
         2960  +           osGetpid(0)));
  2961   2961   
  2962   2962     assert( eFileLock<=SHARED_LOCK );
  2963   2963     if( pFile->eFileLock<=eFileLock ){
  2964   2964       return SQLITE_OK;
  2965   2965     }
  2966   2966     unixEnterMutex();
  2967   2967     pInode = pFile->pInode;
................................................................................
  3778   3778   
  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  +    case SQLITE_FCNTL_WAL_BLOCK: {
         3786  +      /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */
         3787  +      return SQLITE_OK;
         3788  +    }
  3785   3789       case SQLITE_FCNTL_LOCKSTATE: {
  3786   3790         *(int*)pArg = pFile->eFileLock;
  3787   3791         return SQLITE_OK;
  3788   3792       }
  3789   3793       case SQLITE_FCNTL_LAST_ERRNO: {
  3790   3794         *(int*)pArg = pFile->lastErrno;
  3791   3795         return SQLITE_OK;
................................................................................
  4628   4632           assert( (p->sharedMask & mask)==0 );
  4629   4633           p->exclMask |= mask;
  4630   4634         }
  4631   4635       }
  4632   4636     }
  4633   4637     sqlite3_mutex_leave(pShmNode->mutex);
  4634   4638     OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
  4635         -           p->id, osGetpid(), p->sharedMask, p->exclMask));
         4639  +           p->id, osGetpid(0), p->sharedMask, p->exclMask));
  4636   4640     return rc;
  4637   4641   }
  4638   4642   
  4639   4643   /*
  4640   4644   ** Implement a memory barrier or memory fence on shared memory.  
  4641   4645   **
  4642   4646   ** All loads and stores begun before the barrier must complete before
................................................................................
  5723   5727     );
  5724   5728   
  5725   5729     /* Detect a pid change and reset the PRNG.  There is a race condition
  5726   5730     ** here such that two or more threads all trying to open databases at
  5727   5731     ** the same instant might all reset the PRNG.  But multiple resets
  5728   5732     ** are harmless.
  5729   5733     */
  5730         -  if( randomnessPid!=osGetpid() ){
  5731         -    randomnessPid = osGetpid();
         5734  +  if( randomnessPid!=osGetpid(0) ){
         5735  +    randomnessPid = osGetpid(0);
  5732   5736       sqlite3_randomness(0,0);
  5733   5737     }
  5734   5738   
  5735   5739     memset(p, 0, sizeof(unixFile));
  5736   5740   
  5737   5741     if( eType==SQLITE_OPEN_MAIN_DB ){
  5738   5742       UnixUnusedFd *pUnused;
................................................................................
  6115   6119     ** in the random seed.
  6116   6120     **
  6117   6121     ** When testing, initializing zBuf[] to zero is all we do.  That means
  6118   6122     ** that we always use the same random number sequence.  This makes the
  6119   6123     ** tests repeatable.
  6120   6124     */
  6121   6125     memset(zBuf, 0, nBuf);
  6122         -  randomnessPid = osGetpid();  
         6126  +  randomnessPid = osGetpid(0);  
  6123   6127   #if !defined(SQLITE_TEST)
  6124   6128     {
  6125   6129       int fd, got;
  6126   6130       fd = robust_open("/dev/urandom", O_RDONLY, 0);
  6127   6131       if( fd<0 ){
  6128   6132         time_t t;
  6129   6133         time(&t);
................................................................................
  6436   6440   #ifdef LOCKPROXYDIR
  6437   6441     len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
  6438   6442   #else
  6439   6443   # ifdef _CS_DARWIN_USER_TEMP_DIR
  6440   6444     {
  6441   6445       if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
  6442   6446         OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
  6443         -               lPath, errno, osGetpid()));
         6447  +               lPath, errno, osGetpid(0)));
  6444   6448         return SQLITE_IOERR_LOCK;
  6445   6449       }
  6446   6450       len = strlcat(lPath, "sqliteplocks", maxLen);    
  6447   6451     }
  6448   6452   # else
  6449   6453     len = strlcpy(lPath, "/tmp/", maxLen);
  6450   6454   # endif
................................................................................
  6458   6462     dbLen = (int)strlen(dbPath);
  6459   6463     for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
  6460   6464       char c = dbPath[i];
  6461   6465       lPath[i+len] = (c=='/')?'_':c;
  6462   6466     }
  6463   6467     lPath[i+len]='\0';
  6464   6468     strlcat(lPath, ":auto:", maxLen);
  6465         -  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, osGetpid()));
         6469  +  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
  6466   6470     return SQLITE_OK;
  6467   6471   }
  6468   6472   
  6469   6473   /* 
  6470   6474    ** Creates the lock file and any missing directories in lockPath
  6471   6475    */
  6472   6476   static int proxyCreateLockPath(const char *lockPath){
................................................................................
  6485   6489            || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
  6486   6490           buf[i]='\0';
  6487   6491           if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
  6488   6492             int err=errno;
  6489   6493             if( err!=EEXIST ) {
  6490   6494               OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
  6491   6495                        "'%s' proxy lock path=%s pid=%d\n",
  6492         -                     buf, strerror(err), lockPath, osGetpid()));
         6496  +                     buf, strerror(err), lockPath, osGetpid(0)));
  6493   6497               return err;
  6494   6498             }
  6495   6499           }
  6496   6500         }
  6497   6501         start=i+1;
  6498   6502       }
  6499   6503       buf[i] = lockPath[i];
  6500   6504     }
  6501         -  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, osGetpid()));
         6505  +  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, osGetpid(0)));
  6502   6506     return 0;
  6503   6507   }
  6504   6508   
  6505   6509   /*
  6506   6510   ** Create a new VFS file descriptor (stored in memory obtained from
  6507   6511   ** sqlite3_malloc) and open the file named "path" in the file descriptor.
  6508   6512   **
................................................................................
  6800   6804       int hostIdMatch = 0;
  6801   6805       int readLen = 0;
  6802   6806       int tryOldLockPath = 0;
  6803   6807       int forceNewLockPath = 0;
  6804   6808       
  6805   6809       OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
  6806   6810                (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
  6807         -             osGetpid()));
         6811  +             osGetpid(0)));
  6808   6812   
  6809   6813       rc = proxyGetHostID(myHostID, &pError);
  6810   6814       if( (rc&0xff)==SQLITE_IOERR ){
  6811   6815         storeLastErrno(pFile, pError);
  6812   6816         goto end_takeconch;
  6813   6817       }
  6814   6818       rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
................................................................................
  7010   7014     proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
  7011   7015     unixFile *conchFile;        /* Name of the conch file */
  7012   7016   
  7013   7017     pCtx = (proxyLockingContext *)pFile->lockingContext;
  7014   7018     conchFile = pCtx->conchFile;
  7015   7019     OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
  7016   7020              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
  7017         -           osGetpid()));
         7021  +           osGetpid(0)));
  7018   7022     if( pCtx->conchHeld>0 ){
  7019   7023       rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
  7020   7024     }
  7021   7025     pCtx->conchHeld = 0;
  7022   7026     OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
  7023   7027              (rc==SQLITE_OK ? "ok" : "failed")));
  7024   7028     return rc;
................................................................................
  7152   7156     if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
  7153   7157       lockPath=NULL;
  7154   7158     }else{
  7155   7159       lockPath=(char *)path;
  7156   7160     }
  7157   7161     
  7158   7162     OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
  7159         -           (lockPath ? lockPath : ":auto:"), osGetpid()));
         7163  +           (lockPath ? lockPath : ":auto:"), osGetpid(0)));
  7160   7164   
  7161   7165     pCtx = sqlite3_malloc( sizeof(*pCtx) );
  7162   7166     if( pCtx==0 ){
  7163   7167       return SQLITE_NOMEM;
  7164   7168     }
  7165   7169     memset(pCtx, 0, sizeof(*pCtx));
  7166   7170   
................................................................................
  7224   7228   
  7225   7229   /*
  7226   7230   ** This routine handles sqlite3_file_control() calls that are specific
  7227   7231   ** to proxy locking.
  7228   7232   */
  7229   7233   static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
  7230   7234     switch( op ){
  7231         -    case SQLITE_FCNTL_WAL_BLOCK: {
  7232         -      id->ctrlFlags |= UNIXFILE_BLOCK;
  7233         -      return SQLITE_OK;
  7234         -    }
  7235   7235       case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
  7236   7236         unixFile *pFile = (unixFile*)id;
  7237   7237         if( pFile->pMethod == &proxyIoMethods ){
  7238   7238           proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
  7239   7239           proxyTakeConch(pFile);
  7240   7240           if( pCtx->lockProxyPath ){
  7241   7241             *(const char **)pArg = pCtx->lockProxyPath;

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/pcache1.c.

   191    191   **
   192    192   ** Multiple threads can run this routine at the same time.  Global variables
   193    193   ** in pcache1 need to be protected via mutex.
   194    194   */
   195    195   static void *pcache1Alloc(int nByte){
   196    196     void *p = 0;
   197    197     assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
   198         -  sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
   199    198     if( nByte<=pcache1.szSlot ){
   200    199       sqlite3_mutex_enter(pcache1.mutex);
   201    200       p = (PgHdr1 *)pcache1.pFree;
   202    201       if( p ){
   203    202         pcache1.pFree = pcache1.pFree->pNext;
   204    203         pcache1.nFreeSlot--;
   205    204         pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
   206    205         assert( pcache1.nFreeSlot>=0 );
   207         -      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
          206  +      sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
          207  +      sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
   208    208       }
   209    209       sqlite3_mutex_leave(pcache1.mutex);
   210    210     }
   211    211     if( p==0 ){
   212    212       /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
   213    213       ** it from sqlite3Malloc instead.
   214    214       */
   215    215       p = sqlite3Malloc(nByte);
   216    216   #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
   217    217       if( p ){
   218    218         int sz = sqlite3MallocSize(p);
   219    219         sqlite3_mutex_enter(pcache1.mutex);
   220         -      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
          220  +      sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
          221  +      sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
   221    222         sqlite3_mutex_leave(pcache1.mutex);
   222    223       }
   223    224   #endif
   224    225       sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
   225    226     }
   226    227     return p;
   227    228   }
................................................................................
   231    232   */
   232    233   static int pcache1Free(void *p){
   233    234     int nFreed = 0;
   234    235     if( p==0 ) return 0;
   235    236     if( p>=pcache1.pStart && p<pcache1.pEnd ){
   236    237       PgFreeslot *pSlot;
   237    238       sqlite3_mutex_enter(pcache1.mutex);
   238         -    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
          239  +    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
   239    240       pSlot = (PgFreeslot*)p;
   240    241       pSlot->pNext = pcache1.pFree;
   241    242       pcache1.pFree = pSlot;
   242    243       pcache1.nFreeSlot++;
   243    244       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
   244    245       assert( pcache1.nFreeSlot<=pcache1.nSlot );
   245    246       sqlite3_mutex_leave(pcache1.mutex);
   246    247     }else{
   247    248       assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
   248    249       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   249    250       nFreed = sqlite3MallocSize(p);
   250    251   #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
   251    252       sqlite3_mutex_enter(pcache1.mutex);
   252         -    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
          253  +    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
   253    254       sqlite3_mutex_leave(pcache1.mutex);
   254    255   #endif
   255    256       sqlite3_free(p);
   256    257     }
   257    258     return nFreed;
   258    259   }
   259    260   
................................................................................
   981    982     sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
   982    983   }
   983    984   
   984    985   /*
   985    986   ** Return the size of the header on each page of this PCACHE implementation.
   986    987   */
   987    988   int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
          989  +
          990  +/*
          991  +** Return the global mutex used by this PCACHE implementation.  The
          992  +** sqlite3_status() routine needs access to this mutex.
          993  +*/
          994  +sqlite3_mutex *sqlite3Pcache1Mutex(void){
          995  +  return pcache1.mutex;
          996  +}
   988    997   
   989    998   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   990    999   /*
   991   1000   ** This function is called to free superfluous dynamically allocated memory
   992   1001   ** held by the pager system. Memory in use by any SQLite pager allocated
   993   1002   ** by the current thread may be sqlite3_free()ed.
   994   1003   **

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   
................................................................................
  4031   4033       pSavedWith = pParse->pWith;
  4032   4034       pParse->pWith = pWith;
  4033   4035       sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
  4034   4036   
  4035   4037       for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
  4036   4038       pEList = pLeft->pEList;
  4037   4039       if( pCte->pCols ){
  4038         -      if( pEList->nExpr!=pCte->pCols->nExpr ){
         4040  +      if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
  4039   4041           sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
  4040   4042               pCte->zName, pEList->nExpr, pCte->pCols->nExpr
  4041   4043           );
  4042   4044           pParse->pWith = pSavedWith;
  4043   4045           return SQLITE_ERROR;
  4044   4046         }
  4045   4047         pEList = pCte->pCols;

Changes to src/shell.c.

    23     23   #if defined(INCLUDE_MSVC_H)
    24     24   #include "msvc.h"
    25     25   #endif
    26     26   
    27     27   /*
    28     28   ** No support for loadable extensions in VxWorks.
    29     29   */
    30         -#if defined(_WRS_KERNEL) && !SQLITE_OMIT_LOAD_EXTENSION
           30  +#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
    31     31   # define SQLITE_OMIT_LOAD_EXTENSION 1
    32     32   #endif
    33     33   
    34     34   /*
    35     35   ** Enable large-file support for fopen() and friends on unix.
    36     36   */
    37     37   #ifndef SQLITE_DISABLE_LFS
................................................................................
   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);
................................................................................
  3181   3181         fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
  3182   3182         rc = 1;
  3183   3183       }
  3184   3184     }else
  3185   3185   
  3186   3186   #ifdef SQLITE_ENABLE_IOTRACE
  3187   3187     if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
  3188         -    extern void (*sqlite3IoTrace)(const char*, ...);
         3188  +    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
  3189   3189       if( iotrace && iotrace!=stdout ) fclose(iotrace);
  3190   3190       iotrace = 0;
  3191   3191       if( nArg<2 ){
  3192   3192         sqlite3IoTrace = 0;
  3193   3193       }else if( strcmp(azArg[1], "-")==0 ){
  3194   3194         sqlite3IoTrace = iotracePrintf;
  3195   3195         iotrace = stdout;
................................................................................
  3840   3840   
  3841   3841           case SQLITE_TESTCTRL_IMPOSTER:
  3842   3842             if( nArg==5 ){
  3843   3843               rc = sqlite3_test_control(testctrl, p->db, 
  3844   3844                             azArg[2],
  3845   3845                             integerValue(azArg[3]),
  3846   3846                             integerValue(azArg[4]));
         3847  +            fprintf(p->out, "%d (0x%08x)\n", rc, rc);
  3847   3848             }else{
  3848         -            fprintf(stderr,"Usage: .testctrl initmode dbName onoff tnum\n");
  3849         -            rc = 1;
         3849  +            fprintf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
  3850   3850             }
  3851   3851             break;
  3852   3852   
  3853   3853           case SQLITE_TESTCTRL_BITVEC_TEST:         
  3854   3854           case SQLITE_TESTCTRL_FAULT_INSTALL:       
  3855   3855           case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: 
  3856   3856           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.
................................................................................
  1546   1548   ** interpreted as a boolean, which enables or disables the collection of
  1547   1549   ** memory allocation statistics. ^(When memory allocation statistics are
  1548   1550   ** disabled, the following SQLite interfaces become non-operational:
  1549   1551   **   <ul>
  1550   1552   **   <li> [sqlite3_memory_used()]
  1551   1553   **   <li> [sqlite3_memory_highwater()]
  1552   1554   **   <li> [sqlite3_soft_heap_limit64()]
  1553         -**   <li> [sqlite3_status()]
         1555  +**   <li> [sqlite3_status64()]
  1554   1556   **   </ul>)^
  1555   1557   ** ^Memory allocation statistics are enabled by default unless SQLite is
  1556   1558   ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  1557   1559   ** allocation statistics are disabled by default.
  1558   1560   ** </dd>
  1559   1561   **
  1560   1562   ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
................................................................................
  5623   5625   ** take care that any prior string is freed by a call to [sqlite3_free()]
  5624   5626   ** prior to assigning a new string to zErrMsg.  ^After the error message
  5625   5627   ** is delivered up to the client application, the string will be automatically
  5626   5628   ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
  5627   5629   */
  5628   5630   struct sqlite3_vtab {
  5629   5631     const sqlite3_module *pModule;  /* The module for this virtual table */
  5630         -  int nRef;                       /* NO LONGER USED */
         5632  +  int nRef;                       /* Number of open cursors */
  5631   5633     char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
  5632   5634     /* Virtual table implementations will typically add additional fields */
  5633   5635   };
  5634   5636   
  5635   5637   /*
  5636   5638   ** CAPI3REF: Virtual Table Cursor Object
  5637   5639   ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
................................................................................
  6301   6303   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  6302   6304   #define SQLITE_TESTCTRL_IMPOSTER                25
  6303   6305   #define SQLITE_TESTCTRL_LAST                    25
  6304   6306   
  6305   6307   /*
  6306   6308   ** CAPI3REF: SQLite Runtime Status
  6307   6309   **
  6308         -** ^This interface is used to retrieve runtime status information
         6310  +** ^These interfaces are used to retrieve runtime status information
  6309   6311   ** about the performance of SQLite, and optionally to reset various
  6310   6312   ** highwater marks.  ^The first argument is an integer code for
  6311   6313   ** the specific parameter to measure.  ^(Recognized integer codes
  6312   6314   ** are of the form [status parameters | SQLITE_STATUS_...].)^
  6313   6315   ** ^The current value of the parameter is returned into *pCurrent.
  6314   6316   ** ^The highest recorded value is returned in *pHighwater.  ^If the
  6315   6317   ** resetFlag is true, then the highest record value is reset after
  6316   6318   ** *pHighwater is written.  ^(Some parameters do not record the highest
  6317   6319   ** value.  For those parameters
  6318   6320   ** nothing is written into *pHighwater and the resetFlag is ignored.)^
  6319   6321   ** ^(Other parameters record only the highwater mark and not the current
  6320   6322   ** value.  For these latter parameters nothing is written into *pCurrent.)^
  6321   6323   **
  6322         -** ^The sqlite3_status() routine returns SQLITE_OK on success and a
  6323         -** non-zero [error code] on failure.
         6324  +** ^The sqlite3_status() and sqlite3_status64() routines return
         6325  +** SQLITE_OK on success and a non-zero [error code] on failure.
  6324   6326   **
  6325         -** This routine is threadsafe but is not atomic.  This routine can be
  6326         -** called while other threads are running the same or different SQLite
  6327         -** interfaces.  However the values returned in *pCurrent and
  6328         -** *pHighwater reflect the status of SQLite at different points in time
  6329         -** and it is possible that another thread might change the parameter
  6330         -** in between the times when *pCurrent and *pHighwater are written.
         6327  +** If either the current value or the highwater mark is too large to
         6328  +** be represented by a 32-bit integer, then the values returned by
         6329  +** sqlite3_status() are undefined.
  6331   6330   **
  6332   6331   ** See also: [sqlite3_db_status()]
  6333   6332   */
  6334   6333   int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
         6334  +int sqlite3_status64(
         6335  +  int op,
         6336  +  sqlite3_int64 *pCurrent,
         6337  +  sqlite3_int64 *pHighwater,
         6338  +  int resetFlag
         6339  +);
  6335   6340   
  6336   6341   
  6337   6342   /*
  6338   6343   ** CAPI3REF: Status Parameters
  6339   6344   ** KEYWORDS: {status parameters}
  6340   6345   **
  6341   6346   ** These integer constants designate various run-time status parameters

Changes to src/sqliteInt.h.

   590    590   ** The LogEst can be negative to indicate fractional values. 
   591    591   ** Examples:
   592    592   **
   593    593   **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
   594    594   */
   595    595   typedef INT16_TYPE LogEst;
   596    596   
          597  +/*
          598  +** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
          599  +*/
          600  +#ifndef SQLITE_PTRSIZE
          601  +# if defined(__SIZEOF_POINTER__)
          602  +#   define SQLITE_PTRSIZE __SIZEOF_POINTER__
          603  +# elif defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
          604  +       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)
          605  +#   define SQLITE_PTRSIZE 4
          606  +# else
          607  +#   define SQLITE_PTRSIZE 8
          608  +# endif
          609  +#endif
          610  +
   597    611   /*
   598    612   ** Macros to determine whether the machine is big or little endian,
   599    613   ** and whether or not that determination is run-time or compile-time.
   600    614   **
   601    615   ** For best performance, an attempt is made to guess at the byte-order
   602    616   ** using C-preprocessor macros.  If that is unsuccessful, or if
   603    617   ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
................................................................................
  1094   1108       u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
  1095   1109       u8 imposterTable;           /* Building an imposter table */
  1096   1110     } init;
  1097   1111     int nVdbeActive;              /* Number of VDBEs currently running */
  1098   1112     int nVdbeRead;                /* Number of active VDBEs that read or write */
  1099   1113     int nVdbeWrite;               /* Number of active VDBEs that read and write */
  1100   1114     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
         1115  +  int nVDestroy;                /* Number of active OP_VDestroy operations */
  1101   1116     int nExtension;               /* Number of loaded extensions */
  1102   1117     void **aExtension;            /* Array of shared library handles */
  1103   1118     void (*xTrace)(void*,const char*);        /* Trace function */
  1104   1119     void *pTraceArg;                          /* Argument to the trace function */
  1105   1120     void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  1106   1121     void *pProfileArg;                        /* Argument to profile function */
  1107   1122     void *pCommitArg;                 /* Argument to xCommitCallback() */   
................................................................................
  2370   2385   #define SF_Compound        0x0040  /* Part of a compound query */
  2371   2386   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2372   2387   #define SF_AllValues       0x0100  /* All terms of compound are VALUES */
  2373   2388   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2374   2389   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  2375   2390   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  2376   2391   #define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
         2392  +#define SF_Converted       0x2000  /* By convertCompoundSelectToSubquery() */
  2377   2393   
  2378   2394   
  2379   2395   /*
  2380   2396   ** The results of a SELECT can be distributed in several ways, as defined
  2381   2397   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2382   2398   ** Type".
  2383   2399   **
................................................................................
  3093   3109     sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
  3094   3110     sqlite3_mutex_methods const *sqlite3NoopMutex(void);
  3095   3111     sqlite3_mutex *sqlite3MutexAlloc(int);
  3096   3112     int sqlite3MutexInit(void);
  3097   3113     int sqlite3MutexEnd(void);
  3098   3114   #endif
  3099   3115   
  3100         -int sqlite3StatusValue(int);
  3101         -void sqlite3StatusAdd(int, int);
         3116  +sqlite3_int64 sqlite3StatusValue(int);
         3117  +void sqlite3StatusUp(int, int);
         3118  +void sqlite3StatusDown(int, int);
  3102   3119   void sqlite3StatusSet(int, int);
         3120  +
         3121  +/* Access to mutexes used by sqlite3_status() */
         3122  +sqlite3_mutex *sqlite3Pcache1Mutex(void);
         3123  +sqlite3_mutex *sqlite3MallocMutex(void);
  3103   3124   
  3104   3125   #ifndef SQLITE_OMIT_FLOATING_POINT
  3105   3126     int sqlite3IsNaN(double);
  3106   3127   #else
  3107   3128   # define sqlite3IsNaN(X)  0
  3108   3129   #endif
  3109   3130   
................................................................................
  3779   3800   ** If the SQLITE_ENABLE IOTRACE exists then the global variable
  3780   3801   ** sqlite3IoTrace is a pointer to a printf-like routine used to
  3781   3802   ** print I/O tracing messages. 
  3782   3803   */
  3783   3804   #ifdef SQLITE_ENABLE_IOTRACE
  3784   3805   # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
  3785   3806     void sqlite3VdbeIOTraceSql(Vdbe*);
  3786         -SQLITE_EXTERN void (*sqlite3IoTrace)(const char*,...);
         3807  +SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
  3787   3808   #else
  3788   3809   # define IOTRACE(A)
  3789   3810   # define sqlite3VdbeIOTraceSql(X)
  3790   3811   #endif
  3791   3812   
  3792   3813   /*
  3793   3814   ** These routines are available for the mem2.c debugging memory allocator

Changes to src/status.c.

    17     17   #include "vdbeInt.h"
    18     18   
    19     19   /*
    20     20   ** Variables in which to record status information.
    21     21   */
    22     22   typedef struct sqlite3StatType sqlite3StatType;
    23     23   static SQLITE_WSD struct sqlite3StatType {
    24         -  int nowValue[10];         /* Current value */
    25         -  int mxValue[10];          /* Maximum value */
           24  +#if SQLITE_PTRSIZE>4
           25  +  sqlite3_int64 nowValue[10];         /* Current value */
           26  +  sqlite3_int64 mxValue[10];          /* Maximum value */
           27  +#else
           28  +  u32 nowValue[10];                   /* Current value */
           29  +  u32 mxValue[10];                    /* Maximum value */
           30  +#endif
    26     31   } sqlite3Stat = { {0,}, {0,} };
           32  +
           33  +/*
           34  +** Elements of sqlite3Stat[] are protected by either the memory allocator
           35  +** mutex, or by the pcache1 mutex.  The following array determines which.
           36  +*/
           37  +static const char statMutex[] = {
           38  +  0,  /* SQLITE_STATUS_MEMORY_USED */
           39  +  1,  /* SQLITE_STATUS_PAGECACHE_USED */
           40  +  1,  /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
           41  +  0,  /* SQLITE_STATUS_SCRATCH_USED */
           42  +  0,  /* SQLITE_STATUS_SCRATCH_OVERFLOW */
           43  +  0,  /* SQLITE_STATUS_MALLOC_SIZE */
           44  +  0,  /* SQLITE_STATUS_PARSER_STACK */
           45  +  1,  /* SQLITE_STATUS_PAGECACHE_SIZE */
           46  +  0,  /* SQLITE_STATUS_SCRATCH_SIZE */
           47  +  0,  /* SQLITE_STATUS_MALLOC_COUNT */
           48  +};
    27     49   
    28     50   
    29     51   /* The "wsdStat" macro will resolve to the status information
    30     52   ** state vector.  If writable static data is unsupported on the target,
    31     53   ** we have to locate the state vector at run-time.  In the more common
    32     54   ** case where writable static data is supported, wsdStat can refer directly
    33     55   ** to the "sqlite3Stat" state vector declared above.
................................................................................
    37     59   # define wsdStat x[0]
    38     60   #else
    39     61   # define wsdStatInit
    40     62   # define wsdStat sqlite3Stat
    41     63   #endif
    42     64   
    43     65   /*
    44         -** Return the current value of a status parameter.
           66  +** Return the current value of a status parameter.  The caller must
           67  +** be holding the appropriate mutex.
    45     68   */
    46         -int sqlite3StatusValue(int op){
           69  +sqlite3_int64 sqlite3StatusValue(int op){
    47     70     wsdStatInit;
    48     71     assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
           72  +  assert( op>=0 && op<ArraySize(statMutex) );
           73  +  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
           74  +                                           : sqlite3MallocMutex()) );
    49     75     return wsdStat.nowValue[op];
    50     76   }
    51     77   
    52     78   /*
    53         -** Add N to the value of a status record.  It is assumed that the
    54         -** caller holds appropriate locks.
           79  +** Add N to the value of a status record.  The caller must hold the
           80  +** appropriate mutex.  (Locking is checked by assert()).
           81  +**
           82  +** The StatusUp() routine can accept positive or negative values for N.
           83  +** The value of N is added to the current status value and the high-water
           84  +** mark is adjusted if necessary.
           85  +**
           86  +** The StatusDown() routine lowers the current value by N.  The highwater
           87  +** mark is unchanged.  N must be non-negative for StatusDown().
    55     88   */
    56         -void sqlite3StatusAdd(int op, int N){
           89  +void sqlite3StatusUp(int op, int N){
    57     90     wsdStatInit;
    58     91     assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
           92  +  assert( op>=0 && op<ArraySize(statMutex) );
           93  +  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
           94  +                                           : sqlite3MallocMutex()) );
    59     95     wsdStat.nowValue[op] += N;
    60     96     if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
    61     97       wsdStat.mxValue[op] = wsdStat.nowValue[op];
    62     98     }
    63     99   }
          100  +void sqlite3StatusDown(int op, int N){
          101  +  wsdStatInit;
          102  +  assert( N>=0 );
          103  +  assert( op>=0 && op<ArraySize(statMutex) );
          104  +  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
          105  +                                           : sqlite3MallocMutex()) );
          106  +  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
          107  +  wsdStat.nowValue[op] -= N;
          108  +}
    64    109   
    65    110   /*
    66         -** Set the value of a status to X.
          111  +** Set the value of a status to X.  The highwater mark is adjusted if
          112  +** necessary.  The caller must hold the appropriate mutex.
    67    113   */
    68    114   void sqlite3StatusSet(int op, int X){
    69    115     wsdStatInit;
    70    116     assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
          117  +  assert( op>=0 && op<ArraySize(statMutex) );
          118  +  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
          119  +                                           : sqlite3MallocMutex()) );
    71    120     wsdStat.nowValue[op] = X;
    72    121     if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
    73    122       wsdStat.mxValue[op] = wsdStat.nowValue[op];
    74    123     }
    75    124   }
    76    125   
    77    126   /*
    78    127   ** Query status information.
    79         -**
    80         -** This implementation assumes that reading or writing an aligned
    81         -** 32-bit integer is an atomic operation.  If that assumption is not true,
    82         -** then this routine is not threadsafe.
    83    128   */
    84         -int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
          129  +int sqlite3_status64(
          130  +  int op,
          131  +  sqlite3_int64 *pCurrent,
          132  +  sqlite3_int64 *pHighwater,
          133  +  int resetFlag
          134  +){
          135  +  sqlite3_mutex *pMutex;
    85    136     wsdStatInit;
    86    137     if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
    87    138       return SQLITE_MISUSE_BKPT;
    88    139     }
    89    140   #ifdef SQLITE_ENABLE_API_ARMOR
    90    141     if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
    91    142   #endif
          143  +  pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
          144  +  sqlite3_mutex_enter(pMutex);
    92    145     *pCurrent = wsdStat.nowValue[op];
    93    146     *pHighwater = wsdStat.mxValue[op];
    94    147     if( resetFlag ){
    95    148       wsdStat.mxValue[op] = wsdStat.nowValue[op];
    96    149     }
          150  +  sqlite3_mutex_leave(pMutex);
          151  +  (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
    97    152     return SQLITE_OK;
          153  +}
          154  +int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
          155  +  sqlite3_int64 iCur, iHwtr;
          156  +  int rc;
          157  +#ifdef SQLITE_ENABLE_API_ARMOR
          158  +  if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
          159  +#endif
          160  +  rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
          161  +  if( rc==0 ){
          162  +    *pCurrent = (int)iCur;
          163  +    *pHighwater = (int)iHwtr;
          164  +  }
          165  +  return rc;
    98    166   }
    99    167   
   100    168   /*
   101    169   ** Query status information for a single database connection
   102    170   */
   103    171   int sqlite3_db_status(
   104    172     sqlite3 *db,          /* The database connection whose status is desired */

Changes to src/tokenize.c.

   455    455       if( lastTokenParsed!=TK_SEMI ){
   456    456         sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
   457    457         pParse->zTail = &zSql[i];
   458    458       }
   459    459       sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
   460    460     }
   461    461   #ifdef YYTRACKMAXSTACKDEPTH
          462  +  sqlite3_mutex_enter(sqlite3MallocMutex());
   462    463     sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
   463    464         sqlite3ParserStackPeak(pEngine)
   464    465     );
          466  +  sqlite3_mutex_leave(sqlite3MallocMutex());
   465    467   #endif /* YYDEBUG */
   466    468     sqlite3ParserFree(pEngine, sqlite3_free);
   467    469     db->lookaside.bEnabled = enableLookaside;
   468    470     if( db->mallocFailed ){
   469    471       pParse->rc = SQLITE_NOMEM;
   470    472     }
   471    473     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){

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
................................................................................
  4942   4948   ** the last one in the database) then a zero is stored in register P2.
  4943   4949   ** If AUTOVACUUM is disabled then a zero is stored in register P2.
  4944   4950   **
  4945   4951   ** See also: Clear
  4946   4952   */
  4947   4953   case OP_Destroy: {     /* out2-prerelease */
  4948   4954     int iMoved;
  4949         -  int iCnt;
  4950         -  Vdbe *pVdbe;
  4951   4955     int iDb;
  4952   4956   
  4953   4957     assert( p->readOnly==0 );
  4954         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  4955         -  iCnt = 0;
  4956         -  for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
  4957         -    if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader 
  4958         -     && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 
  4959         -    ){
  4960         -      iCnt++;
  4961         -    }
  4962         -  }
  4963         -#else
  4964         -  iCnt = db->nVdbeRead;
  4965         -#endif
  4966   4958     pOut->flags = MEM_Null;
  4967         -  if( iCnt>1 ){
         4959  +  if( db->nVdbeRead > db->nVDestroy+1 ){
  4968   4960       rc = SQLITE_LOCKED;
  4969   4961       p->errorAction = OE_Abort;
  4970   4962     }else{
  4971   4963       iDb = pOp->p3;
  4972         -    assert( iCnt==1 );
  4973   4964       assert( DbMaskTest(p->btreeMask, iDb) );
  4974   4965       iMoved = 0;  /* Not needed.  Only to silence a warning. */
  4975   4966       rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
  4976   4967       pOut->flags = MEM_Int;
  4977   4968       pOut->u.i = iMoved;
  4978   4969   #ifndef SQLITE_OMIT_AUTOVACUUM
  4979   4970       if( rc==SQLITE_OK && iMoved!=0 ){
................................................................................
  6029   6020   /* Opcode: VCreate P1 P2 * * *
  6030   6021   **
  6031   6022   ** P2 is a register that holds the name of a virtual table in database 
  6032   6023   ** P1. Call the xCreate method for that table.
  6033   6024   */
  6034   6025   case OP_VCreate: {
  6035   6026     Mem sMem;          /* For storing the record being decoded */
         6027  +  const char *zTab;  /* Name of the virtual table */
         6028  +
  6036   6029     memset(&sMem, 0, sizeof(sMem));
  6037   6030     sMem.db = db;
         6031  +  /* Because P2 is always a static string, it is impossible for the
         6032  +  ** sqlite3VdbeMemCopy() to fail */
         6033  +  assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
         6034  +  assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
  6038   6035     rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
  6039         -  if( rc==SQLITE_OK ){
  6040         -    const char *zTab = (const char*)sqlite3_value_text(&sMem);
  6041         -    assert( zTab || db->mallocFailed );
  6042         -    if( zTab ){
  6043         -      rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
  6044         -    }
         6036  +  assert( rc==SQLITE_OK );
         6037  +  zTab = (const char*)sqlite3_value_text(&sMem);
         6038  +  assert( zTab || db->mallocFailed );
         6039  +  if( zTab ){
         6040  +    rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
  6045   6041     }
  6046   6042     sqlite3VdbeMemRelease(&sMem);
  6047   6043     break;
  6048   6044   }
  6049   6045   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6050   6046   
  6051   6047   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6052   6048   /* Opcode: VDestroy P1 * * P4 *
  6053   6049   **
  6054   6050   ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
  6055   6051   ** of that table.
  6056   6052   */
  6057   6053   case OP_VDestroy: {
  6058         -  p->inVtabMethod = 2;
         6054  +  db->nVDestroy++;
  6059   6055     rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
  6060         -  p->inVtabMethod = 0;
         6056  +  db->nVDestroy--;
  6061   6057     break;
  6062   6058   }
  6063   6059   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6064   6060   
  6065   6061   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6066   6062   /* Opcode: VOpen P1 * * P4 *
  6067   6063   **
................................................................................
  6069   6065   ** P1 is a cursor number.  This opcode opens a cursor to the virtual
  6070   6066   ** table and stores that cursor in P1.
  6071   6067   */
  6072   6068   case OP_VOpen: {
  6073   6069     VdbeCursor *pCur;
  6074   6070     sqlite3_vtab_cursor *pVtabCursor;
  6075   6071     sqlite3_vtab *pVtab;
  6076         -  sqlite3_module *pModule;
         6072  +  const sqlite3_module *pModule;
  6077   6073   
  6078   6074     assert( p->bIsReader );
  6079   6075     pCur = 0;
  6080   6076     pVtabCursor = 0;
  6081   6077     pVtab = pOp->p4.pVtab->pVtab;
  6082         -  pModule = (sqlite3_module *)pVtab->pModule;
  6083         -  assert(pVtab && pModule);
         6078  +  if( pVtab==0 || NEVER(pVtab->pModule==0) ){
         6079  +    rc = SQLITE_LOCKED;
         6080  +    break;
         6081  +  }
         6082  +  pModule = pVtab->pModule;
  6084   6083     rc = pModule->xOpen(pVtab, &pVtabCursor);
  6085   6084     sqlite3VtabImportErrmsg(p, pVtab);
  6086   6085     if( SQLITE_OK==rc ){
  6087   6086       /* Initialize sqlite3_vtab_cursor base class */
  6088   6087       pVtabCursor->pVtab = pVtab;
  6089   6088   
  6090   6089       /* Initialize vdbe cursor object */
  6091   6090       pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
  6092   6091       if( pCur ){
  6093   6092         pCur->pVtabCursor = pVtabCursor;
         6093  +      pVtab->nRef++;
  6094   6094       }else{
  6095   6095         db->mallocFailed = 1;
  6096   6096         pModule->xClose(pVtabCursor);
  6097   6097       }
  6098   6098     }
  6099   6099     break;
  6100   6100   }
................................................................................
  6152   6152     {
  6153   6153       res = 0;
  6154   6154       apArg = p->apArg;
  6155   6155       for(i = 0; i<nArg; i++){
  6156   6156         apArg[i] = &pArgc[i+1];
  6157   6157       }
  6158   6158   
  6159         -    p->inVtabMethod = 1;
  6160   6159       rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  6161         -    p->inVtabMethod = 0;
  6162   6160       sqlite3VtabImportErrmsg(p, pVtab);
  6163   6161       if( rc==SQLITE_OK ){
  6164   6162         res = pModule->xEof(pVtabCursor);
  6165   6163       }
  6166   6164       VdbeBranchTaken(res!=0,2);
  6167   6165       if( res ){
  6168   6166         pc = pOp->p2 - 1;
................................................................................
  6244   6242   
  6245   6243     /* Invoke the xNext() method of the module. There is no way for the
  6246   6244     ** underlying implementation to return an error if one occurs during
  6247   6245     ** xNext(). Instead, if an error occurs, true is returned (indicating that 
  6248   6246     ** data is available) and the error code returned when xColumn or
  6249   6247     ** some other method is next invoked on the save virtual table cursor.
  6250   6248     */
  6251         -  p->inVtabMethod = 1;
  6252   6249     rc = pModule->xNext(pCur->pVtabCursor);
  6253         -  p->inVtabMethod = 0;
  6254   6250     sqlite3VtabImportErrmsg(p, pVtab);
  6255   6251     if( rc==SQLITE_OK ){
  6256   6252       res = pModule->xEof(pCur->pVtabCursor);
  6257   6253     }
  6258   6254     VdbeBranchTaken(!res,2);
  6259   6255     if( !res ){
  6260   6256       /* If there is data, jump to P2 */
................................................................................
  6321   6317   ** is set to the value of the rowid for the row just inserted.
  6322   6318   **
  6323   6319   ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
  6324   6320   ** apply in the case of a constraint failure on an insert or update.
  6325   6321   */
  6326   6322   case OP_VUpdate: {
  6327   6323     sqlite3_vtab *pVtab;
  6328         -  sqlite3_module *pModule;
         6324  +  const sqlite3_module *pModule;
  6329   6325     int nArg;
  6330   6326     int i;
  6331   6327     sqlite_int64 rowid;
  6332   6328     Mem **apArg;
  6333   6329     Mem *pX;
  6334   6330   
  6335   6331     assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback 
  6336   6332          || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
  6337   6333     );
  6338   6334     assert( p->readOnly==0 );
  6339   6335     pVtab = pOp->p4.pVtab->pVtab;
  6340         -  pModule = (sqlite3_module *)pVtab->pModule;
         6336  +  if( pVtab==0 || NEVER(pVtab->pModule==0) ){
         6337  +    rc = SQLITE_LOCKED;
         6338  +    break;
         6339  +  }
         6340  +  pModule = pVtab->pModule;
  6341   6341     nArg = pOp->p2;
  6342   6342     assert( pOp->p4type==P4_VTAB );
  6343   6343     if( ALWAYS(pModule->xUpdate) ){
  6344   6344       u8 vtabOnConflict = db->vtabOnConflict;
  6345   6345       apArg = p->apArg;
  6346   6346       pX = &aMem[pOp->p3];
  6347   6347       for(i=0; i<nArg; i++){

Changes to src/vdbeInt.h.

   309    309   
   310    310   /*
   311    311   ** An instance of the virtual machine.  This structure contains the complete
   312    312   ** state of the virtual machine.
   313    313   **
   314    314   ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
   315    315   ** is really a pointer to an instance of this structure.
   316         -**
   317         -** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
   318         -** any virtual table method invocations made by the vdbe program. It is
   319         -** set to 2 for xDestroy method calls and 1 for all other methods. This
   320         -** variable is used for two purposes: to allow xDestroy methods to execute
   321         -** "DROP TABLE" statements and to prevent some nasty side effects of
   322         -** malloc failure when SQLite is invoked recursively by a virtual table 
   323         -** method function.
   324    316   */
   325    317   struct Vdbe {
   326    318     sqlite3 *db;            /* The database connection that owns this statement */
   327    319     Op *aOp;                /* Space to hold the virtual machine's program */
   328    320     Mem *aMem;              /* The memory locations */
   329    321     Mem **apArg;            /* Arguments to currently executing user function */
   330    322     Mem *aColName;          /* Column names to return */
................................................................................
   347    339   #ifdef SQLITE_DEBUG
   348    340     int rcApp;              /* errcode set by sqlite3_result_error_code() */
   349    341   #endif
   350    342     u16 nResColumn;         /* Number of columns in one row of the result set */
   351    343     u8 errorAction;         /* Recovery action to do in case of an error */
   352    344     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   353    345     bft explain:2;          /* True if EXPLAIN present on SQL command */
   354         -  bft inVtabMethod:2;     /* See comments above */
   355    346     bft changeCntOn:1;      /* True to update the change-counter */
   356    347     bft expired:1;          /* True if the VM needs to be recompiled */
   357    348     bft runOnlyOnce:1;      /* Automatically expire on reset */
   358    349     bft usesStmtJournal:1;  /* True if uses a statement journal */
   359    350     bft readOnly:1;         /* True for statements that do not write */
   360    351     bft bIsReader:1;        /* True for statements that read */
   361    352     bft isPrepareV2:1;      /* True if prepared with prepare_v2() */

Changes to src/vdbeaux.c.

  1114   1114           zP4 = "(blob)";
  1115   1115         }
  1116   1116         break;
  1117   1117       }
  1118   1118   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1119   1119       case P4_VTAB: {
  1120   1120         sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
  1121         -      sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
         1121  +      sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
  1122   1122         break;
  1123   1123       }
  1124   1124   #endif
  1125   1125       case P4_INTARRAY: {
  1126   1126         sqlite3_snprintf(nTemp, zTemp, "intarray");
  1127   1127         break;
  1128   1128       }
................................................................................
  1778   1778     }else if( pCx->pCursor ){
  1779   1779       sqlite3BtreeCloseCursor(pCx->pCursor);
  1780   1780     }
  1781   1781   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1782   1782     else if( pCx->pVtabCursor ){
  1783   1783       sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
  1784   1784       const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
  1785         -    p->inVtabMethod = 1;
         1785  +    assert( pVtabCursor->pVtab->nRef>0 );
         1786  +    pVtabCursor->pVtab->nRef--;
  1786   1787       pModule->xClose(pVtabCursor);
  1787         -    p->inVtabMethod = 0;
  1788   1788     }
  1789   1789   #endif
  1790   1790   }
  1791   1791   
  1792   1792   /*
  1793   1793   ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
  1794   1794   ** is used, for example, when a trigger sub-program is halted to restore

Changes to src/vtab.c.

   776    776   */
   777    777   int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
   778    778     int rc = SQLITE_OK;
   779    779     Table *pTab;
   780    780   
   781    781     pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
   782    782     if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
   783         -    VTable *p = vtabDisconnectAll(db, pTab);
   784         -
   785         -    assert( rc==SQLITE_OK );
          783  +    VTable *p;
          784  +    for(p=pTab->pVTable; p; p=p->pNext){
          785  +      assert( p->pVtab );
          786  +      if( p->pVtab->nRef>0 ){
          787  +        return SQLITE_LOCKED;
          788  +      }
          789  +    }
          790  +    p = vtabDisconnectAll(db, pTab);
   786    791       rc = p->pMod->pModule->xDestroy(p->pVtab);
   787         -
   788    792       /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
   789    793       if( rc==SQLITE_OK ){
   790    794         assert( pTab->pVTable==p && p->pNext==0 );
   791    795         p->pVtab = 0;
   792    796         pTab->pVTable = 0;
   793    797         sqlite3VtabUnlock(p);
   794    798       }

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/fts3atoken.test.

   199    199   do_catchsql_test 6.1.2 {
   200    200     CREATE VIRTUAL TABLE t3 USING fts4(tokenize=);
   201    201   } {1 {unknown tokenizer: }}
   202    202   do_catchsql_test 6.1.3 {
   203    203     CREATE VIRTUAL TABLE t3 USING fts4(tokenize="   ");
   204    204   } {1 {unknown tokenizer:    }}
   205    205   
          206  +do_catchsql_test 6.2.1 {
          207  +  SELECT fts3_tokenizer(NULL);
          208  +} {1 {unknown tokenizer: }}
          209  +do_catchsql_test 6.2.2 {
          210  +  SELECT fts3_tokenizer(NULL, X'1234567812345678');
          211  +} {1 {argument type mismatch}}
          212  +do_catchsql_test 6.2.3 {
          213  +  SELECT fts3_tokenizer(NULL, X'12345678');
          214  +} {1 {argument type mismatch}}
          215  +
   206    216   
   207    217   finish_test

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/misc1.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # This file implements tests for miscellanous features that were
    14     14   # left out of other test files.
    15     15   #
    16         -# $Id: misc1.test,v 1.42 2007/11/05 14:58:23 drh Exp $
    17     16   
    18     17   set testdir [file dirname $argv0]
    19     18   source $testdir/tester.tcl
    20     19   
    21     20   # Mimic the SQLite 2 collation type NUMERIC.
    22     21   db collate numeric numeric_collate
    23     22   proc numeric_collate {lhs rhs} {
................................................................................
   626    625   # presumably.
   627    626   #
   628    627   do_execsql_test misc1-20.1 {
   629    628     CREATE TABLE t0(x INTEGER DEFAULT(0==0) NOT NULL);
   630    629     REPLACE INTO t0(x) VALUES('');
   631    630     SELECT rowid, quote(x) FROM t0;
   632    631   } {1 ''}
          632  +
          633  +# 2015-03-22: NULL pointer dereference after a syntax error
          634  +#
          635  +do_catchsql_test misc1-21.1 {
          636  +  select''like''like''like#0;
          637  +} {1 {near "#0": syntax error}}
          638  +do_catchsql_test misc1-21.2 {
          639  +  VALUES(0,0x0MATCH#0;
          640  +} {1 {near ";": syntax error}}
   633    641   
   634    642   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/shell4.test.

   118    118   } {1 {Usage: .trace FILE|off}}
   119    119   do_test shell4-2.2 {
   120    120     catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace off\n.trace off\n"
   121    121   } {0 {}}
   122    122   do_test shell4-2.3 {
   123    123     catchcmd ":memory:" ".trace stdout\n.trace\n.trace off\n.dump\n"
   124    124   } {/^1 {PRAGMA.*Usage:.*}$/}
          125  +ifcapable trace {
   125    126   do_test shell4-2.4 {
   126    127     catchcmd ":memory:" ".trace stdout\nCREATE TABLE t1(x);SELECT * FROM t1;"
   127    128   } {0 {CREATE TABLE t1(x);
   128    129   SELECT * FROM t1;}}
   129    130   do_test shell4-2.5 {
   130    131     catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace stdout\nSELECT * FROM t1;"
   131    132   } {0 {SELECT * FROM t1;}}
          133  +}
   132    134   
   133    135   
   134    136   finish_test

Changes to test/vtab1.test.

  1433   1433     } {SQLITE_DONE}
  1434   1434     
  1435   1435     do_test 22.4.2 {
  1436   1436       sqlite3_finalize $stmt
  1437   1437     } {SQLITE_OK}
  1438   1438   }
  1439   1439   
         1440  +
         1441  +#-------------------------------------------------------------------------
         1442  +# The following tests verify that a DROP TABLE command on a virtual
         1443  +# table does not cause other operations to crash.
         1444  +#
         1445  +#   23.1: Dropping a vtab while a SELECT is running on it.
         1446  +#
         1447  +#   23.2: Dropping a vtab while a SELECT that will, but has not yet,
         1448  +#         open a cursor on the vtab, is running. In this case the
         1449  +#         DROP TABLE succeeds and the SELECT hits an error.
         1450  +#   
         1451  +#   23.3: Dropping a vtab from within a user-defined-function callback
         1452  +#         in the middle of an "INSERT INTO vtab SELECT ..." statement.
         1453  +#
         1454  +reset_db
         1455  +load_static_extension db wholenumber
         1456  +load_static_extension db eval
         1457  +register_echo_module db
         1458  +
         1459  +do_test 23.1 {
         1460  +  execsql { CREATE VIRTUAL TABLE t1 USING wholenumber }
         1461  +  set res ""
         1462  +  db eval { SELECT value FROM t1 WHERE value<10 } {
         1463  +    if {$value == 5} {
         1464  +      set res [catchsql { DROP TABLE t1 }]
         1465  +    }
         1466  +  }
         1467  +  set res
         1468  +} {1 {database table is locked}}
         1469  +
         1470  +do_test 23.2 {
         1471  +  execsql { 
         1472  +    CREATE TABLE t2(value);
         1473  +    INSERT INTO t2 VALUES(1), (2), (3);
         1474  +  }
         1475  +
         1476  +  set res2 [list [catch {
         1477  +    db eval {
         1478  +      SELECT value FROM t2 UNION ALL 
         1479  +      SELECT value FROM t1 WHERE value<10
         1480  +    } {
         1481  +      if {$value == 2} { set res1 [catchsql { DROP TABLE t1 }] }
         1482  +    }
         1483  +  } msg] $msg]
         1484  +  list $res1 $res2
         1485  +} {{0 {}} {1 {database table is locked}}}
         1486  +
         1487  +do_test 23.3.1 {
         1488  +  execsql { CREATE VIRTUAL TABLE t1e USING echo(t2) }
         1489  +  execsql { INSERT INTO t1e SELECT 4 }
         1490  +  catchsql { INSERT INTO t1e SELECT eval('DROP TABLE t1e') }
         1491  +} {1 {database table is locked}}
         1492  +do_execsql_test 23.3.2 { SELECT * FROM t1e } {1 2 3 4}
         1493  +
  1440   1494   finish_test

Changes to test/vtab2.test.

   131    131   } {}
   132    132   do_test vtab2-4.5 {
   133    133     execsql { SELECT * FROM fkey }
   134    134   } {t1 a}
   135    135   
   136    136   #-------------------------------------------------------------------------
   137    137   #
   138         -reset_db
   139         -do_execsql_test 5.1 {
   140         -  PRAGMA encoding='UTF16';
   141         -}
          138  +ifcapable fts3 {
          139  +  reset_db
          140  +  do_execsql_test 5.1 {
          141  +    PRAGMA encoding='UTF16';
          142  +  }
   142    143   
   143         -do_test 5.2 {
   144         -  sqlite3_exec_hex db { CREATE VIRTUAL TABLE %C8 USING fts3 }
   145         -} {0 {}}
          144  +  do_test 5.2 {
          145  +    sqlite3_exec_hex db { CREATE VIRTUAL TABLE %C8 USING fts3 }
          146  +  } {0 {}}
   146    147   
   147         -do_test 5.3 {
   148         -  sqlite3_exec_hex db { CREATE VIRTUAL TABLE %C9 USING s }
   149         -} {/1 {malformed database schema.* already exists}/}
          148  +  do_test 5.3 {
          149  +    sqlite3_exec_hex db { CREATE VIRTUAL TABLE %C9 USING s }
          150  +  } {/1 {malformed database schema.* already exists}/}
          151  +}
   150    152   
   151    153   
   152    154   
   153    155   finish_test
   154    156   

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 }
................................................................................
    88     89   
    89     90   tvfs script barrier_callback
    90     91   tvfs filter xShmBarrier
    91     92   proc barrier_callback {method args} {
    92     93     set ::out ""
    93     94     testfixture $::C { db eval { SELECT * FROM t1 } } {set ::out}
    94     95   
    95         -  do_test "1.2.2.(blocking 5 seconds)" { 
           96  +  do_test "1.2.2.(blocking 10 seconds)" { 
    96     97       set ::continue 0
    97         -    after 5000 {set ::continue 1}
           98  +    after 10000 {set ::continue 1}
    98     99       vwait ::continue
    99    100       set ::out 
   100    101     } {}
   101    102   }
   102    103   
   103    104   execsql COMMIT
   104    105   
................................................................................
   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 test/with1.test.

   823    823   do_execsql_test 12.1 {
   824    824   WITH RECURSIVE
   825    825     t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<20),
   826    826     t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<20)
   827    827   SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1;
   828    828   } {2 4 8 10 14 16 20}
   829    829   
          830  +# 2015-03-21
          831  +# Column wildcards on the LHS of a recursive table expression
          832  +#
          833  +do_catchsql_test 13.1 {
          834  +  WITH RECURSIVE c(i) AS (SELECT * UNION ALL SELECT i+1 FROM c WHERE i<10)
          835  +  SELECT i FROM c;
          836  +} {1 {no tables specified}}
          837  +do_catchsql_test 13.2 {
          838  +  WITH RECURSIVE c(i) AS (SELECT 5,* UNION ALL SELECT i+1 FROM c WHERE i<10)
          839  +  SELECT i FROM c;
          840  +} {1 {no tables specified}}
          841  +do_catchsql_test 13.3 {
          842  +  WITH RECURSIVE c(i,j) AS (SELECT 5,* UNION ALL SELECT i+1,11 FROM c WHERE i<10)
          843  +  SELECT i FROM c;
          844  +} {1 {table c has 1 values for 2 columns}}
   830    845   
   831    846   finish_test

Changes to test/without_rowid6.test.

    32     32   } {x123y}
    33     33   do_execsql_test without_rowid6-130 {
    34     34     SELECT c FROM t1 ORDER BY a DESC LIMIT 5;
    35     35   } {x1000y x999y x998y x997y x996y}
    36     36   do_execsql_test without_rowid6-140 {
    37     37     SELECT c FROM t1 ORDER BY b LIMIT 5;
    38     38   } {x1y x2y x3y x4y x5y}
           39  +
           40  +# Column t1.b starts out as a unique index, but that index is
           41  +# subsequently converted into a PRIMARY KEY.
           42  +#
           43  +do_execsql_test without_rowid6-200 {
           44  +  DROP TABLE IF EXISTS t1;
           45  +  CREATE TABLE t1(
           46  +    a UNIQUE,
           47  +    b UNIQUE,
           48  +    c UNIQUE,
           49  +    PRIMARY KEY(b)
           50  +  ) WITHOUT ROWID;
           51  +  INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9);
           52  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
           53  +} {4 1}
           54  +do_execsql_test without_rowid6-210 {
           55  +  EXPLAIN QUERY PLAN
           56  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
           57  +} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../}
           58  +do_execsql_test without_rowid6-220 {
           59  +  PRAGMA index_list(t1);
           60  +} {/sqlite_autoindex_t1_2 1 pk/}
           61  +
           62  +do_execsql_test without_rowid6-300 {
           63  +  DROP TABLE IF EXISTS t1;
           64  +  CREATE TABLE t1(
           65  +    a UNIQUE,
           66  +    b PRIMARY KEY,
           67  +    c UNIQUE,
           68  +    UNIQUE(b)
           69  +  ) WITHOUT ROWID;
           70  +  INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9);
           71  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
           72  +} {4 1}
           73  +do_execsql_test without_rowid6-310 {
           74  +  EXPLAIN QUERY PLAN
           75  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
           76  +} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../}
           77  +do_execsql_test without_rowid6-320 {
           78  +  PRAGMA index_list(t1);
           79  +} {/sqlite_autoindex_t1_2 1 pk/}
           80  +
           81  +do_execsql_test without_rowid6-400 {
           82  +  DROP TABLE IF EXISTS t1;
           83  +  CREATE TABLE t1(
           84  +    a UNIQUE,
           85  +    b UNIQUE PRIMARY KEY,
           86  +    c UNIQUE
           87  +  ) WITHOUT ROWID;
           88  +  INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9);
           89  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
           90  +} {4 1}
           91  +do_execsql_test without_rowid6-410 {
           92  +  EXPLAIN QUERY PLAN
           93  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
           94  +} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../}
           95  +do_execsql_test without_rowid6-420 {
           96  +  PRAGMA index_list(t1);
           97  +} {/sqlite_autoindex_t1_2 1 pk/}
           98  +
           99  +do_execsql_test without_rowid6-500 {
          100  +  DROP TABLE IF EXISTS t1;
          101  +  CREATE TABLE t1(a,b,c,
          102  +    UNIQUE(b,c),
          103  +    PRIMARY KEY(b,c)
          104  +  ) WITHOUT ROWID;
          105  +  INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9);
          106  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
          107  +} {4 1}
          108  +do_execsql_test without_rowid6-510 {
          109  +  EXPLAIN QUERY PLAN
          110  +  SELECT a FROM t1 WHERE b>3 ORDER BY b;
          111  +} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../}
          112  +do_execsql_test without_rowid6-520 {
          113  +  PRAGMA index_list(t1);
          114  +} {/sqlite_autoindex_t1_1 1 pk/}
    39    115   
    40    116   
    41    117   finish_test

Changes to tool/build-all-msvc.bat.

   317    317         REM NOTE: When preparing the debug build, set the DEBUG and MEMDEBUG
   318    318         REM       environment variables to be picked up by the MSVC makefile
   319    319         REM       itself.
   320    320         REM
   321    321         %_AECHO% Building the %%B configuration for platform %%P with name %%D...
   322    322   
   323    323         IF /I "%%B" == "Debug" (
   324         -        SET DEBUG=2
          324  +        REM
          325  +        REM NOTE: Using this level for the DEBUG environment variable should
          326  +        REM       disable all compiler optimizations and prevent use of the
          327  +        REM       NDEBUG define.  Additionally, both SQLITE_ENABLE_API_ARMOR
          328  +        REM       and SQLITE_DEBUG defines should be enabled.
          329  +        REM
          330  +        SET DEBUG=3
          331  +
          332  +        REM
          333  +        REM NOTE: Setting this to non-zero should enable the SQLITE_MEMDEBUG
          334  +        REM       define.
          335  +        REM
   325    336           SET MEMDEBUG=1
   326    337         ) ELSE (
   327    338           CALL :fn_UnsetVariable DEBUG
   328    339           CALL :fn_UnsetVariable MEMDEBUG
   329    340         )
   330    341   
   331    342         REM

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

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  +}