/ Check-in [307b802e]
Login

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

Overview
Comment:Merge latest trunk changes into this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent
Files: files | file ages | folders
SHA3-256: 307b802e8627c93a51e4c54851a4fab33db5061bb80e3d327ce53b127d6d511b
User & Date: dan 2017-09-22 10:49:03
Wiki:begin-concurrent
Context
2017-09-22
11:09
Cherrypick [ec37ad6d08] into this branch. With this patch, if SQLITE_SHARED_MAPPING is defined at build-time SQLite will use a single memory mapping for multiple connections to the same database file within a single process. check-in: c7a5880d user: dan tags: begin-concurrent
10:49
Merge latest trunk changes into this branch. check-in: 307b802e user: dan tags: begin-concurrent
00:24
Update the configure script so that it looks for tclsh8.7 ahead of tclsh8.6. check-in: 0a12915b user: drh tags: trunk
2017-08-28
17:19
Merge recent enhancements from trunk. check-in: d53108e7 user: drh tags: begin-concurrent
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   424    424     $(TOP)/ext/misc/eval.c \
   425    425     $(TOP)/ext/misc/fileio.c \
   426    426     $(TOP)/ext/misc/fuzzer.c \
   427    427     $(TOP)/ext/fts5/fts5_tcl.c \
   428    428     $(TOP)/ext/fts5/fts5_test_mi.c \
   429    429     $(TOP)/ext/fts5/fts5_test_tok.c \
   430    430     $(TOP)/ext/misc/ieee754.c \
          431  +  $(TOP)/ext/misc/mmapwarm.c \
   431    432     $(TOP)/ext/misc/nextchar.c \
   432    433     $(TOP)/ext/misc/percentile.c \
   433    434     $(TOP)/ext/misc/regexp.c \
   434    435     $(TOP)/ext/misc/remember.c \
   435    436     $(TOP)/ext/misc/series.c \
   436    437     $(TOP)/ext/misc/spellfix.c \
   437    438     $(TOP)/ext/misc/totype.c \

Changes to Makefile.msc.

  1407   1407     $(TOP)\ext\misc\eval.c \
  1408   1408     $(TOP)\ext\misc\fileio.c \
  1409   1409     $(TOP)\ext\misc\fuzzer.c \
  1410   1410     $(TOP)\ext\fts5\fts5_tcl.c \
  1411   1411     $(TOP)\ext\fts5\fts5_test_mi.c \
  1412   1412     $(TOP)\ext\fts5\fts5_test_tok.c \
  1413   1413     $(TOP)\ext\misc\ieee754.c \
         1414  +  $(TOP)\ext\misc\mmapwarm.c \
  1414   1415     $(TOP)\ext\misc\nextchar.c \
  1415   1416     $(TOP)\ext\misc\percentile.c \
  1416   1417     $(TOP)\ext\misc\regexp.c \
  1417   1418     $(TOP)\ext\misc\remember.c \
  1418   1419     $(TOP)\ext\misc\series.c \
  1419   1420     $(TOP)\ext\misc\spellfix.c \
  1420   1421     $(TOP)\ext\misc\totype.c \
................................................................................
  2262   2263   	del /Q *.exp *.lo *.ilk *.lib *.obj *.ncb *.pdb *.sdf *.suo 2>NUL
  2263   2264   	del /Q *.bsc *.def *.cod *.da *.bb *.bbg *.vc gmon.out 2>NUL
  2264   2265   	del /Q $(SQLITE3EXE) $(SQLITE3DLL) Replace.exe 2>NUL
  2265   2266   # <<mark>>
  2266   2267   	del /Q sqlite3.c sqlite3.h 2>NUL
  2267   2268   	del /Q opcodes.c opcodes.h 2>NUL
  2268   2269   	del /Q lemon.* lempar.c parse.* 2>NUL
  2269         -	del /Q mkkeywordhash.* keywordhash.h 2>NUL
         2270  +	del /Q mksourceid.* mkkeywordhash.* keywordhash.h 2>NUL
  2270   2271   	del /Q notasharedlib.* 2>NUL
  2271   2272   	-rmdir /Q/S .deps 2>NUL
  2272   2273   	-rmdir /Q/S .libs 2>NUL
  2273   2274   	-rmdir /Q/S tsrc 2>NUL
  2274   2275   	del /Q .target_source 2>NUL
  2275   2276   	del /Q tclsqlite3.exe $(SQLITETCLH) $(SQLITETCLDECLSH) 2>NUL
  2276   2277   	del /Q lsm.dll lsmtest.exe 2>NUL

Changes to configure.

 10300  10300   USE_AMALGAMATION=1
 10301  10301   
 10302  10302   #########
 10303  10303   # See whether we can run specific tclsh versions known to work well;
 10304  10304   # if not, then we fall back to plain tclsh.
 10305  10305   # TODO: try other versions before falling back?
 10306  10306   #
 10307         -for ac_prog in tclsh8.6 tclsh8.5 tclsh
        10307  +for ac_prog in tclsh8.7 tclsh8.6 tclsh8.5 tclsh
 10308  10308   do
 10309  10309     # Extract the first word of "$ac_prog", so it can be a program name with args.
 10310  10310   set dummy $ac_prog; ac_word=$2
 10311  10311   { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
 10312  10312   $as_echo_n "checking for $ac_word... " >&6; }
 10313  10313   if ${ac_cv_prog_TCLSH_CMD+:} false; then :
 10314  10314     $as_echo_n "(cached) " >&6

Changes to configure.ac.

   116    116   USE_AMALGAMATION=1
   117    117   
   118    118   #########
   119    119   # See whether we can run specific tclsh versions known to work well;
   120    120   # if not, then we fall back to plain tclsh.
   121    121   # TODO: try other versions before falling back?
   122    122   # 
   123         -AC_CHECK_PROGS(TCLSH_CMD, [tclsh8.6 tclsh8.5 tclsh], none)
          123  +AC_CHECK_PROGS(TCLSH_CMD, [tclsh8.7 tclsh8.6 tclsh8.5 tclsh], none)
   124    124   if test "$TCLSH_CMD" = "none"; then
   125    125     # If we can't find a local tclsh, then building the amalgamation will fail.
   126    126     # We act as though --disable-amalgamation has been used.
   127    127     echo "Warning: can't find tclsh - defaulting to non-amalgamation build."
   128    128     USE_AMALGAMATION=0
   129    129     TCLSH_CMD="tclsh"
   130    130   fi

Changes to doc/lemon.html.

     1      1   <html>
     2      2   <head>
     3      3   <title>The Lemon Parser Generator</title>
     4      4   </head>
     5         -<body bgcolor=white>
     6         -<h1 align=center>The Lemon Parser Generator</h1>
            5  +<body bgcolor='white'>
            6  +<h1 align='center'>The Lemon Parser Generator</h1>
     7      7   
     8      8   <p>Lemon is an LALR(1) parser generator for C.
     9      9   It does the same job as "bison" and "yacc".
    10         -But lemon is not a bison or yacc clone.  Lemon
           10  +But Lemon is not a bison or yacc clone.  Lemon
    11     11   uses a different grammar syntax which is designed to
    12     12   reduce the number of coding errors.  Lemon also uses a
    13     13   parsing engine that is faster than yacc and
    14     14   bison and which is both reentrant and threadsafe.
    15     15   (Update: Since the previous sentence was written, bison
    16     16   has also been updated so that it too can generate a
    17     17   reentrant and threadsafe parser.)
    18     18   Lemon also implements features that can be used
    19         -to eliminate resource leaks, making is suitable for use
           19  +to eliminate resource leaks, making it suitable for use
    20     20   in long-running programs such as graphical user interfaces
    21     21   or embedded controllers.</p>
    22     22   
    23     23   <p>This document is an introduction to the Lemon
    24     24   parser generator.</p>
    25     25   
    26     26   <h2>Security Note</h2>
................................................................................
    54     54   <li>A parser template file.
    55     55   </ul>
    56     56   Typically, only the grammar specification is supplied by the programmer.
    57     57   Lemon comes with a default parser template which works fine for most
    58     58   applications.  But the user is free to substitute a different parser
    59     59   template if desired.</p>
    60     60   
    61         -<p>Depending on command-line options, Lemon will generate between
    62         -one and three files of outputs.
           61  +<p>Depending on command-line options, Lemon will generate up to
           62  +three output files.
    63     63   <ul>
    64     64   <li>C code to implement the parser.
    65     65   <li>A header file defining an integer ID for each terminal symbol.
    66     66   <li>An information file that describes the states of the generated parser
    67     67       automaton.
    68     68   </ul>
    69     69   By default, all three of these output files are generated.
................................................................................
    86     86   
    87     87   <h3>Command Line Options</h3>
    88     88   
    89     89   <p>The behavior of Lemon can be modified using command-line options.
    90     90   You can obtain a list of the available command-line options together
    91     91   with a brief explanation of what each does by typing
    92     92   <pre>
    93         -   lemon -?
           93  +   lemon "-?"
    94     94   </pre>
    95     95   As of this writing, the following command-line options are supported:
    96     96   <ul>
    97     97   <li><b>-b</b>
    98     98   Show only the basis for each parser state in the report file.
    99     99   <li><b>-c</b>
   100         -Do not compress the generated action tables.
          100  +Do not compress the generated action tables.  The parser will be a
          101  +little larger and slower, but it will detect syntax errors sooner.
   101    102   <li><b>-D<i>name</i></b>
   102         -Define C preprocessor macro <i>name</i>.  This macro is useable by
   103         -"%ifdef" lines in the grammar file.
          103  +Define C preprocessor macro <i>name</i>.  This macro is usable by
          104  +"<tt><a href='#pifdef'>%ifdef</a></tt>" and
          105  +"<tt><a href='#pifdef'>%ifndef</a></tt>" lines
          106  +in the grammar file.
   104    107   <li><b>-g</b>
   105    108   Do not generate a parser.  Instead write the input grammar to standard
   106    109   output with all comments, actions, and other extraneous text removed.
   107    110   <li><b>-l</b>
   108    111   Omit "#line" directives in the generated parser C code.
   109    112   <li><b>-m</b>
   110    113   Cause the output C source code to be compatible with the "makeheaders"
   111         -program. 
          114  +program.
   112    115   <li><b>-p</b>
   113         -Display all conflicts that are resolved by 
          116  +Display all conflicts that are resolved by
   114    117   <a href='#precrules'>precedence rules</a>.
   115    118   <li><b>-q</b>
   116    119   Suppress generation of the report file.
   117    120   <li><b>-r</b>
   118    121   Do not sort or renumber the parser states as part of optimization.
   119    122   <li><b>-s</b>
   120    123   Show parser statistics before existing.
................................................................................
   161    164   be parsed.  This is accomplished by calling the following function
   162    165   once for each token:
   163    166   <pre>
   164    167      Parse(pParser, hTokenID, sTokenData, pArg);
   165    168   </pre>
   166    169   The first argument to the Parse() routine is the pointer returned by
   167    170   ParseAlloc().
   168         -The second argument is a small positive integer that tells the parse the
          171  +The second argument is a small positive integer that tells the parser the
   169    172   type of the next token in the data stream.
   170    173   There is one token type for each terminal symbol in the grammar.
   171    174   The gram.h file generated by Lemon contains #define statements that
   172    175   map symbolic terminal symbol names into appropriate integer values.
   173    176   A value of 0 for the second argument is a special flag to the
   174    177   parser to indicate that the end of input has been reached.
   175    178   The third argument is the value of the given token.  By default,
   176         -the type of the third argument is integer, but the grammar will
          179  +the type of the third argument is "void*", but the grammar will
   177    180   usually redefine this type to be some kind of structure.
   178    181   Typically the second argument will be a broad category of tokens
   179    182   such as "identifier" or "number" and the third argument will
   180    183   be the name of the identifier or the value of the number.</p>
   181    184   
   182    185   <p>The Parse() function may have either three or four arguments,
   183    186   depending on the grammar.  If the grammar specification file requests
   184         -it (via the <a href='#extraarg'><tt>extra_argument</tt> directive</a>),
          187  +it (via the <tt><a href='#extraarg'>%extra_argument</a></tt> directive),
   185    188   the Parse() function will have a fourth parameter that can be
   186    189   of any type chosen by the programmer.  The parser doesn't do anything
   187    190   with this argument except to pass it through to action routines.
   188    191   This is a convenient mechanism for passing state information down
   189    192   to the action routines without having to use global variables.</p>
   190    193   
   191    194   <p>A typical use of a Lemon parser might look something like the
   192    195   following:
   193    196   <pre>
   194         -   01 ParseTree *ParseFile(const char *zFilename){
   195         -   02    Tokenizer *pTokenizer;
   196         -   03    void *pParser;
   197         -   04    Token sToken;
   198         -   05    int hTokenId;
   199         -   06    ParserState sState;
   200         -   07
   201         -   08    pTokenizer = TokenizerCreate(zFilename);
   202         -   09    pParser = ParseAlloc( malloc );
   203         -   10    InitParserState(&sState);
   204         -   11    while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){
   205         -   12       Parse(pParser, hTokenId, sToken, &sState);
          197  +    1 ParseTree *ParseFile(const char *zFilename){
          198  +    2    Tokenizer *pTokenizer;
          199  +    3    void *pParser;
          200  +    4    Token sToken;
          201  +    5    int hTokenId;
          202  +    6    ParserState sState;
          203  +    7
          204  +    8    pTokenizer = TokenizerCreate(zFilename);
          205  +    9    pParser = ParseAlloc( malloc );
          206  +   10    InitParserState(&amp;sState);
          207  +   11    while( GetNextToken(pTokenizer, &amp;hTokenId, &amp;sToken) ){
          208  +   12       Parse(pParser, hTokenId, sToken, &amp;sState);
   206    209      13    }
   207         -   14    Parse(pParser, 0, sToken, &sState);
          210  +   14    Parse(pParser, 0, sToken, &amp;sState);
   208    211      15    ParseFree(pParser, free );
   209    212      16    TokenizerFree(pTokenizer);
   210    213      17    return sState.treeRoot;
   211    214      18 }
   212    215   </pre>
   213    216   This example shows a user-written routine that parses a file of
   214    217   text and returns a pointer to the parse tree.
   215    218   (All error-handling code is omitted from this example to keep it
   216    219   simple.)
   217    220   We assume the existence of some kind of tokenizer which is created
   218    221   using TokenizerCreate() on line 8 and deleted by TokenizerFree()
   219    222   on line 16.  The GetNextToken() function on line 11 retrieves the
   220         -next token from the input file and puts its type in the 
          223  +next token from the input file and puts its type in the
   221    224   integer variable hTokenId.  The sToken variable is assumed to be
   222    225   some kind of structure that contains details about each token,
   223         -such as its complete text, what line it occurs on, etc. </p>
          226  +such as its complete text, what line it occurs on, etc.</p>
   224    227   
   225    228   <p>This example also assumes the existence of structure of type
   226    229   ParserState that holds state information about a particular parse.
   227    230   An instance of such a structure is created on line 6 and initialized
   228    231   on line 10.  A pointer to this structure is passed into the Parse()
   229    232   routine as the optional 4th argument.
   230    233   The action routine specified by the grammar for the parser can use
................................................................................
   233    236   the ParserState structure is left pointing to the root of the parse
   234    237   tree.</p>
   235    238   
   236    239   <p>The core of this example as it relates to Lemon is as follows:
   237    240   <pre>
   238    241      ParseFile(){
   239    242         pParser = ParseAlloc( malloc );
   240         -      while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){
          243  +      while( GetNextToken(pTokenizer,&amp;hTokenId, &amp;sToken) ){
   241    244            Parse(pParser, hTokenId, sToken);
   242    245         }
   243    246         Parse(pParser, 0, sToken);
   244    247         ParseFree(pParser, free );
   245    248      }
   246    249   </pre>
   247    250   Basically, what a program has to do to use a Lemon-generated parser
................................................................................
   293    296   
   294    297   <p>The main purpose of the grammar specification file for Lemon is
   295    298   to define the grammar for the parser.  But the input file also
   296    299   specifies additional information Lemon requires to do its job.
   297    300   Most of the work in using Lemon is in writing an appropriate
   298    301   grammar file.</p>
   299    302   
   300         -<p>The grammar file for lemon is, for the most part, free format.
          303  +<p>The grammar file for Lemon is, for the most part, free format.
   301    304   It does not have sections or divisions like yacc or bison.  Any
   302    305   declaration can occur at any point in the file.
   303    306   Lemon ignores whitespace (except where it is needed to separate
   304         -tokens) and it honors the same commenting conventions as C and C++.</p>
          307  +tokens), and it honors the same commenting conventions as C and C++.</p>
   305    308   
   306    309   <h3>Terminals and Nonterminals</h3>
   307    310   
   308    311   <p>A terminal symbol (token) is any string of alphanumeric
   309    312   and/or underscore characters
   310         -that begins with an upper case letter.
          313  +that begins with an uppercase letter.
   311    314   A terminal can contain lowercase letters after the first character,
   312         -but the usual convention is to make terminals all upper case.
          315  +but the usual convention is to make terminals all uppercase.
   313    316   A nonterminal, on the other hand, is any string of alphanumeric
   314         -and underscore characters than begins with a lower case letter.
   315         -Again, the usual convention is to make nonterminals use all lower
   316         -case letters.</p>
          317  +and underscore characters than begins with a lowercase letter.
          318  +Again, the usual convention is to make nonterminals use all lowercase
          319  +letters.</p>
   317    320   
   318         -<p>In Lemon, terminal and nonterminal symbols do not need to 
          321  +<p>In Lemon, terminal and nonterminal symbols do not need to
   319    322   be declared or identified in a separate section of the grammar file.
   320    323   Lemon is able to generate a list of all terminals and nonterminals
   321    324   by examining the grammar rules, and it can always distinguish a
   322    325   terminal from a nonterminal by checking the case of the first
   323    326   character of the name.</p>
   324    327   
   325    328   <p>Yacc and bison allow terminal symbols to have either alphanumeric
................................................................................
   335    338   Each grammar rule consists of a nonterminal symbol followed by
   336    339   the special symbol "::=" and then a list of terminals and/or nonterminals.
   337    340   The rule is terminated by a period.
   338    341   The list of terminals and nonterminals on the right-hand side of the
   339    342   rule can be empty.
   340    343   Rules can occur in any order, except that the left-hand side of the
   341    344   first rule is assumed to be the start symbol for the grammar (unless
   342         -specified otherwise using the <tt>%start</tt> directive described below.)
          345  +specified otherwise using the <tt><a href='#start_symbol'>%start_symbol</a></tt>
          346  +directive described below.)
   343    347   A typical sequence of grammar rules might look something like this:
   344    348   <pre>
   345    349     expr ::= expr PLUS expr.
   346    350     expr ::= expr TIMES expr.
   347    351     expr ::= LPAREN expr RPAREN.
   348    352     expr ::= VALUE.
   349    353   </pre>
................................................................................
   378    382   rule and say "$7" when you really mean "$8".</p>
   379    383   
   380    384   <p>Lemon avoids the need to count grammar symbols by assigning symbolic
   381    385   names to each symbol in a grammar rule and then using those symbolic
   382    386   names in the action.
   383    387   In yacc or bison, one would write this:
   384    388   <pre>
   385         -  expr -> expr PLUS expr  { $$ = $1 + $3; };
          389  +  expr -&gt; expr PLUS expr  { $$ = $1 + $3; };
   386    390   </pre>
   387    391   But in Lemon, the same rule becomes the following:
   388    392   <pre>
   389    393     expr(A) ::= expr(B) PLUS expr(C).  { A = B+C; }
   390    394   </pre>
   391    395   In the Lemon rule, any symbol in parentheses after a grammar rule
   392    396   symbol becomes a place holder for that symbol in the grammar rule.
................................................................................
   418    422   
   419    423   <p>Lemon resolves parsing ambiguities in exactly the same way as
   420    424   yacc and bison.  A shift-reduce conflict is resolved in favor
   421    425   of the shift, and a reduce-reduce conflict is resolved by reducing
   422    426   whichever rule comes first in the grammar file.</p>
   423    427   
   424    428   <p>Just like in
   425         -yacc and bison, Lemon allows a measure of control 
   426         -over the resolution of paring conflicts using precedence rules.
          429  +yacc and bison, Lemon allows a measure of control
          430  +over the resolution of parsing conflicts using precedence rules.
   427    431   A precedence value can be assigned to any terminal symbol
   428         -using the 
   429         -<a href='#pleft'>%left</a>,
   430         -<a href='#pright'>%right</a> or
   431         -<a href='#pnonassoc'>%nonassoc</a> directives.  Terminal symbols
   432         -mentioned in earlier directives have a lower precedence that
          432  +using the
          433  +<tt><a href='#pleft'>%left</a></tt>,
          434  +<tt><a href='#pright'>%right</a></tt> or
          435  +<tt><a href='#pnonassoc'>%nonassoc</a></tt> directives.  Terminal symbols
          436  +mentioned in earlier directives have a lower precedence than
   433    437   terminal symbols mentioned in later directives.  For example:</p>
   434    438   
   435    439   <p><pre>
   436    440      %left AND.
   437    441      %left OR.
   438    442      %nonassoc EQ NE GT GE LT LE.
   439    443      %left PLUS MINUS.
................................................................................
   501    505   <ul>
   502    506   <li> If either the token to be shifted or the rule to be reduced
   503    507        lacks precedence information, then resolve in favor of the
   504    508        shift, but report a parsing conflict.
   505    509   <li> If the precedence of the token to be shifted is greater than
   506    510        the precedence of the rule to reduce, then resolve in favor
   507    511        of the shift.  No parsing conflict is reported.
   508         -<li> If the precedence of the token it be shifted is less than the
          512  +<li> If the precedence of the token to be shifted is less than the
   509    513        precedence of the rule to reduce, then resolve in favor of the
   510    514        reduce action.  No parsing conflict is reported.
   511    515   <li> If the precedences are the same and the shift token is
   512    516        right-associative, then resolve in favor of the shift.
   513    517        No parsing conflict is reported.
   514         -<li> If the precedences are the same the shift token is
          518  +<li> If the precedences are the same and the shift token is
   515    519        left-associative, then resolve in favor of the reduce.
   516    520        No parsing conflict is reported.
   517         -<li> Otherwise, resolve the conflict by doing the shift and
   518         -     report the parsing conflict.
          521  +<li> Otherwise, resolve the conflict by doing the shift, and
          522  +     report a parsing conflict.
   519    523   </ul>
   520    524   Reduce-reduce conflicts are resolved this way:
   521    525   <ul>
   522         -<li> If either reduce rule 
          526  +<li> If either reduce rule
   523    527        lacks precedence information, then resolve in favor of the
   524         -     rule that appears first in the grammar and report a parsing
          528  +     rule that appears first in the grammar, and report a parsing
   525    529        conflict.
   526         -<li> If both rules have precedence and the precedence is different
          530  +<li> If both rules have precedence and the precedence is different,
   527    531        then resolve the dispute in favor of the rule with the highest
   528         -     precedence and do not report a conflict.
          532  +     precedence, and do not report a conflict.
   529    533   <li> Otherwise, resolve the conflict by reducing by the rule that
   530         -     appears first in the grammar and report a parsing conflict.
          534  +     appears first in the grammar, and report a parsing conflict.
   531    535   </ul>
   532    536   
   533    537   <h3>Special Directives</h3>
   534    538   
   535    539   <p>The input grammar to Lemon consists of grammar rules and special
   536    540   directives.  We've described all the grammar rules, so now we'll
   537    541   talk about the special directives.</p>
   538    542   
   539         -<p>Directives in lemon can occur in any order.  You can put them before
   540         -the grammar rules, or after the grammar rules, or in the mist of the
          543  +<p>Directives in Lemon can occur in any order.  You can put them before
          544  +the grammar rules, or after the grammar rules, or in the midst of the
   541    545   grammar rules.  It doesn't matter.  The relative order of
   542    546   directives used to assign precedence to terminals is important, but
   543    547   other than that, the order of directives in Lemon is arbitrary.</p>
   544    548   
   545    549   <p>Lemon supports the following special directives:
   546    550   <ul>
   547         -<li><tt>%code</tt>
   548         -<li><tt>%default_destructor</tt>
   549         -<li><tt>%default_type</tt>
   550         -<li><tt>%destructor</tt>
   551         -<li><tt>%endif</tt>
   552         -<li><tt>%extra_argument</tt>
   553         -<li><tt>%fallback</tt>
   554         -<li><tt>%ifdef</tt>
   555         -<li><tt>%ifndef</tt>
   556         -<li><tt>%include</tt>
   557         -<li><tt>%left</tt>
   558         -<li><tt>%name</tt>
   559         -<li><tt>%nonassoc</tt>
   560         -<li><tt>%parse_accept</tt>
   561         -<li><tt>%parse_failure </tt>
   562         -<li><tt>%right</tt>
   563         -<li><tt>%stack_overflow</tt>
   564         -<li><tt>%stack_size</tt>
   565         -<li><tt>%start_symbol</tt>
   566         -<li><tt>%syntax_error</tt>
   567         -<li><tt>%token_class</tt>
   568         -<li><tt>%token_destructor</tt>
   569         -<li><tt>%token_prefix</tt>
   570         -<li><tt>%token_type</tt>
   571         -<li><tt>%type</tt>
   572         -<li><tt>%wildcard</tt>
          551  +<li><tt><a href='#pcode'>%code</a></tt>
          552  +<li><tt><a href='#default_destructor'>%default_destructor</a></tt>
          553  +<li><tt><a href='#default_type'>%default_type</a></tt>
          554  +<li><tt><a href='#destructor'>%destructor</a></tt>
          555  +<li><tt><a href='#pifdef'>%endif</a></tt>
          556  +<li><tt><a href='#extraarg'>%extra_argument</a></tt>
          557  +<li><tt><a href='#pfallback'>%fallback</a></tt>
          558  +<li><tt><a href='#pifdef'>%ifdef</a></tt>
          559  +<li><tt><a href='#pifdef'>%ifndef</a></tt>
          560  +<li><tt><a href='#pinclude'>%include</a></tt>
          561  +<li><tt><a href='#pleft'>%left</a></tt>
          562  +<li><tt><a href='#pname'>%name</a></tt>
          563  +<li><tt><a href='#pnonassoc'>%nonassoc</a></tt>
          564  +<li><tt><a href='#parse_accept'>%parse_accept</a></tt>
          565  +<li><tt><a href='#parse_failure'>%parse_failure</a></tt>
          566  +<li><tt><a href='#pright'>%right</a></tt>
          567  +<li><tt><a href='#stack_overflow'>%stack_overflow</a></tt>
          568  +<li><tt><a href='#stack_size'>%stack_size</a></tt>
          569  +<li><tt><a href='#start_symbol'>%start_symbol</a></tt>
          570  +<li><tt><a href='#syntax_error'>%syntax_error</a></tt>
          571  +<li><tt><a href='#token_class'>%token_class</a></tt>
          572  +<li><tt><a href='#token_destructor'>%token_destructor</a></tt>
          573  +<li><tt><a href='#token_prefix'>%token_prefix</a></tt>
          574  +<li><tt><a href='#token_type'>%token_type</a></tt>
          575  +<li><tt><a href='#ptype'>%type</a></tt>
          576  +<li><tt><a href='#pwildcard'>%wildcard</a></tt>
   573    577   </ul>
   574    578   Each of these directives will be described separately in the
   575    579   following sections:</p>
   576    580   
   577    581   <a name='pcode'></a>
   578    582   <h4>The <tt>%code</tt> directive</h4>
   579    583   
   580         -<p>The %code directive is used to specify addition C code that
          584  +<p>The <tt>%code</tt> directive is used to specify additional C code that
   581    585   is added to the end of the main output file.  This is similar to
   582         -the <a href='#pinclude'>%include</a> directive except that %include
   583         -is inserted at the beginning of the main output file.</p>
          586  +the <tt><a href='#pinclude'>%include</a></tt> directive except that
          587  +<tt>%include</tt> is inserted at the beginning of the main output file.</p>
   584    588   
   585         -<p>%code is typically used to include some action routines or perhaps
   586         -a tokenizer or even the "main()" function 
          589  +<p><tt>%code</tt> is typically used to include some action routines or perhaps
          590  +a tokenizer or even the "main()" function
   587    591   as part of the output file.</p>
   588    592   
   589    593   <a name='default_destructor'></a>
   590    594   <h4>The <tt>%default_destructor</tt> directive</h4>
   591    595   
   592         -<p>The %default_destructor directive specifies a destructor to 
          596  +<p>The <tt>%default_destructor</tt> directive specifies a destructor to
   593    597   use for non-terminals that do not have their own destructor
   594         -specified by a separate %destructor directive.  See the documentation
   595         -on the <a name='#destructor'>%destructor</a> directive below for
          598  +specified by a separate <tt>%destructor</tt> directive.  See the documentation
          599  +on the <tt><a name='#destructor'>%destructor</a></tt> directive below for
   596    600   additional information.</p>
   597    601   
   598         -<p>In some grammers, many different non-terminal symbols have the
   599         -same datatype and hence the same destructor.  This directive is
   600         -a convenience way to specify the same destructor for all those
          602  +<p>In some grammars, many different non-terminal symbols have the
          603  +same data type and hence the same destructor.  This directive is
          604  +a convenient way to specify the same destructor for all those
   601    605   non-terminals using a single statement.</p>
   602    606   
   603    607   <a name='default_type'></a>
   604    608   <h4>The <tt>%default_type</tt> directive</h4>
   605    609   
   606         -<p>The %default_type directive specifies the datatype of non-terminal
   607         -symbols that do no have their own datatype defined using a separate
   608         -<a href='#ptype'>%type</a> directive.  
   609         -</p>
          610  +<p>The <tt>%default_type</tt> directive specifies the data type of non-terminal
          611  +symbols that do not have their own data type defined using a separate
          612  +<tt><a href='#ptype'>%type</a></tt> directive.</p>
   610    613   
   611    614   <a name='destructor'></a>
   612    615   <h4>The <tt>%destructor</tt> directive</h4>
   613    616   
   614         -<p>The %destructor directive is used to specify a destructor for
          617  +<p>The <tt>%destructor</tt> directive is used to specify a destructor for
   615    618   a non-terminal symbol.
   616         -(See also the <a href='#token_destructor'>%token_destructor</a>
          619  +(See also the <tt><a href='#token_destructor'>%token_destructor</a></tt>
   617    620   directive which is used to specify a destructor for terminal symbols.)</p>
   618    621   
   619    622   <p>A non-terminal's destructor is called to dispose of the
   620    623   non-terminal's value whenever the non-terminal is popped from
   621    624   the stack.  This includes all of the following circumstances:
   622    625   <ul>
   623    626   <li> When a rule reduces and the value of a non-terminal on
................................................................................
   631    634   
   632    635   <p>Consider an example:
   633    636   <pre>
   634    637      %type nt {void*}
   635    638      %destructor nt { free($$); }
   636    639      nt(A) ::= ID NUM.   { A = malloc( 100 ); }
   637    640   </pre>
   638         -This example is a bit contrived but it serves to illustrate how
          641  +This example is a bit contrived, but it serves to illustrate how
   639    642   destructors work.  The example shows a non-terminal named
   640    643   "nt" that holds values of type "void*".  When the rule for
   641    644   an "nt" reduces, it sets the value of the non-terminal to
   642    645   space obtained from malloc().  Later, when the nt non-terminal
   643    646   is popped from the stack, the destructor will fire and call
   644    647   free() on this malloced space, thus avoiding a memory leak.
   645    648   (Note that the symbol "$$" in the destructor code is replaced
................................................................................
   647    650   
   648    651   <p>It is important to note that the value of a non-terminal is passed
   649    652   to the destructor whenever the non-terminal is removed from the
   650    653   stack, unless the non-terminal is used in a C-code action.  If
   651    654   the non-terminal is used by C-code, then it is assumed that the
   652    655   C-code will take care of destroying it.
   653    656   More commonly, the value is used to build some
   654         -larger structure and we don't want to destroy it, which is why
          657  +larger structure, and we don't want to destroy it, which is why
   655    658   the destructor is not called in this circumstance.</p>
   656    659   
   657    660   <p>Destructors help avoid memory leaks by automatically freeing
   658    661   allocated objects when they go out of scope.
   659    662   To do the same using yacc or bison is much more difficult.</p>
   660    663   
   661         -<a name="extraarg"></a>
          664  +<a name='extraarg'></a>
   662    665   <h4>The <tt>%extra_argument</tt> directive</h4>
   663    666   
   664         -The %extra_argument directive instructs Lemon to add a 4th parameter
          667  +The <tt>%extra_argument</tt> directive instructs Lemon to add a 4th parameter
   665    668   to the parameter list of the Parse() function it generates.  Lemon
   666    669   doesn't do anything itself with this extra argument, but it does
   667    670   make the argument available to C-code action routines, destructors,
   668    671   and so forth.  For example, if the grammar file contains:</p>
   669    672   
   670    673   <p><pre>
   671    674       %extra_argument { MyStruct *pAbc }
................................................................................
   675    678   of type "MyStruct*" and all action routines will have access to
   676    679   a variable named "pAbc" that is the value of the 4th parameter
   677    680   in the most recent call to Parse().</p>
   678    681   
   679    682   <a name='pfallback'></a>
   680    683   <h4>The <tt>%fallback</tt> directive</h4>
   681    684   
   682         -<p>The %fallback directive specifies an alternative meaning for one
          685  +<p>The <tt>%fallback</tt> directive specifies an alternative meaning for one
   683    686   or more tokens.  The alternative meaning is tried if the original token
   684         -would have generated a syntax error.
          687  +would have generated a syntax error.</p>
   685    688   
   686         -<p>The %fallback directive was added to support robust parsing of SQL
   687         -syntax in <a href="https://www.sqlite.org/">SQLite</a>.
          689  +<p>The <tt>%fallback</tt> directive was added to support robust parsing of SQL
          690  +syntax in <a href='https://www.sqlite.org/'>SQLite</a>.
   688    691   The SQL language contains a large assortment of keywords, each of which
   689    692   appears as a different token to the language parser.  SQL contains so
   690         -many keywords, that it can be difficult for programmers to keep up with
          693  +many keywords that it can be difficult for programmers to keep up with
   691    694   them all.  Programmers will, therefore, sometimes mistakenly use an
   692         -obscure language keyword for an identifier.  The %fallback directive
          695  +obscure language keyword for an identifier.  The <tt>%fallback</tt> directive
   693    696   provides a mechanism to tell the parser:  "If you are unable to parse
   694         -this keyword, try treating it as an identifier instead."
          697  +this keyword, try treating it as an identifier instead."</p>
   695    698   
   696         -<p>The syntax of %fallback is as follows:
          699  +<p>The syntax of <tt>%fallback</tt> is as follows:
   697    700   
   698    701   <blockquote>
   699         -<tt>%fallback</tt>  <i>ID</i> <i>TOKEN...</i> <b>.</b>
   700         -</blockquote>
          702  +<tt>%fallback</tt> <i>ID</i> <i>TOKEN...</i> <b>.</b>
          703  +</blockquote></p>
   701    704   
   702         -<p>In words, the %fallback directive is followed by a list of token names
   703         -terminated by a period.  The first token name is the fallback token - the
          705  +<p>In words, the <tt>%fallback</tt> directive is followed by a list of token
          706  +names terminated by a period.
          707  +The first token name is the fallback token &mdash; the
   704    708   token to which all the other tokens fall back to.  The second and subsequent
   705    709   arguments are tokens which fall back to the token identified by the first
   706         -argument.
          710  +argument.</p>
   707    711   
   708    712   <a name='pifdef'></a>
   709         -<h4>The <tt>%ifdef</tt>, <tt>%ifndef</tt>, and <tt>%endif</tt> directives.</h4>
          713  +<h4>The <tt>%ifdef</tt>, <tt>%ifndef</tt>, and <tt>%endif</tt> directives</h4>
   710    714   
   711         -<p>The %ifdef, %ifndef, and %endif directives are similar to
   712         -#ifdef, #ifndef, and #endif in the C-preprocessor, just not as general.
          715  +<p>The <tt>%ifdef</tt>, <tt>%ifndef</tt>, and <tt>%endif</tt> directives
          716  +are similar to #ifdef, #ifndef, and #endif in the C-preprocessor,
          717  +just not as general.
   713    718   Each of these directives must begin at the left margin.  No whitespace
   714         -is allowed between the "%" and the directive name.
          719  +is allowed between the "%" and the directive name.</p>
   715    720   
   716         -<p>Grammar text in between "%ifdef MACRO" and the next nested "%endif" is
          721  +<p>Grammar text in between "<tt>%ifdef MACRO</tt>" and the next nested
          722  +"<tt>%endif</tt>" is
   717    723   ignored unless the "-DMACRO" command-line option is used.  Grammar text
   718         -betwen "%ifndef MACRO" and the next nested "%endif" is included except when
   719         -the "-DMACRO" command-line option is used.
          724  +betwen "<tt>%ifndef MACRO</tt>" and the next nested "<tt>%endif</tt>" is
          725  +included except when the "-DMACRO" command-line option is used.</p>
   720    726   
   721         -<p>Note that the argument to %ifdef and %ifndef must be a single 
   722         -preprocessor symbol name, not a general expression.  There is no "%else"
   723         -directive.
          727  +<p>Note that the argument to <tt>%ifdef</tt> and <tt>%ifndef</tt> must
          728  +be a single preprocessor symbol name, not a general expression.
          729  +There is no "<tt>%else</tt>" directive.</p>
   724    730   
   725    731   
   726    732   <a name='pinclude'></a>
   727    733   <h4>The <tt>%include</tt> directive</h4>
   728    734   
   729         -<p>The %include directive specifies C code that is included at the
   730         -top of the generated parser.  You can include any text you want --
          735  +<p>The <tt>%include</tt> directive specifies C code that is included at the
          736  +top of the generated parser.  You can include any text you want &mdash;
   731    737   the Lemon parser generator copies it blindly.  If you have multiple
   732         -%include directives in your grammar file, their values are concatenated
   733         -so that all %include code ultimately appears near the top of the
   734         -generated parser, in the same order as it appeared in the grammer.</p>
          738  +<tt>%include</tt> directives in your grammar file, their values are concatenated
          739  +so that all <tt>%include</tt> code ultimately appears near the top of the
          740  +generated parser, in the same order as it appeared in the grammar.</p>
   735    741   
   736         -<p>The %include directive is very handy for getting some extra #include
          742  +<p>The <tt>%include</tt> directive is very handy for getting some extra #include
   737    743   preprocessor statements at the beginning of the generated parser.
   738    744   For example:</p>
   739    745   
   740    746   <p><pre>
   741    747      %include {#include &lt;unistd.h&gt;}
   742    748   </pre></p>
   743    749   
   744    750   <p>This might be needed, for example, if some of the C actions in the
   745         -grammar call functions that are prototyed in unistd.h.</p>
          751  +grammar call functions that are prototyped in unistd.h.</p>
   746    752   
   747    753   <a name='pleft'></a>
   748    754   <h4>The <tt>%left</tt> directive</h4>
   749    755   
   750         -The %left directive is used (along with the <a href='#pright'>%right</a> and
   751         -<a href='#pnonassoc'>%nonassoc</a> directives) to declare precedences of 
   752         -terminal symbols.  Every terminal symbol whose name appears after
   753         -a %left directive but before the next period (".") is
          756  +The <tt>%left</tt> directive is used (along with the
          757  +<tt><a href='#pright'>%right</a></tt> and
          758  +<tt><a href='#pnonassoc'>%nonassoc</a></tt> directives) to declare
          759  +precedences of terminal symbols.
          760  +Every terminal symbol whose name appears after
          761  +a <tt>%left</tt> directive but before the next period (".") is
   754    762   given the same left-associative precedence value.  Subsequent
   755         -%left directives have higher precedence.  For example:</p>
          763  +<tt>%left</tt> directives have higher precedence.  For example:</p>
   756    764   
   757    765   <p><pre>
   758    766      %left AND.
   759    767      %left OR.
   760    768      %nonassoc EQ NE GT GE LT LE.
   761    769      %left PLUS MINUS.
   762    770      %left TIMES DIVIDE MOD.
   763    771      %right EXP NOT.
   764    772   </pre></p>
   765    773   
   766         -<p>Note the period that terminates each %left, %right or %nonassoc
          774  +<p>Note the period that terminates each <tt>%left</tt>,
          775  +<tt>%right</tt> or <tt>%nonassoc</tt>
   767    776   directive.</p>
   768    777   
   769    778   <p>LALR(1) grammars can get into a situation where they require
   770    779   a large amount of stack space if you make heavy use or right-associative
   771         -operators.  For this reason, it is recommended that you use %left
   772         -rather than %right whenever possible.</p>
          780  +operators.  For this reason, it is recommended that you use <tt>%left</tt>
          781  +rather than <tt>%right</tt> whenever possible.</p>
   773    782   
   774    783   <a name='pname'></a>
   775    784   <h4>The <tt>%name</tt> directive</h4>
   776    785   
   777    786   <p>By default, the functions generated by Lemon all begin with the
   778    787   five-character string "Parse".  You can change this string to something
   779         -different using the %name directive.  For instance:</p>
          788  +different using the <tt>%name</tt> directive.  For instance:</p>
   780    789   
   781    790   <p><pre>
   782    791      %name Abcde
   783    792   </pre></p>
   784    793   
   785    794   <p>Putting this directive in the grammar file will cause Lemon to generate
   786    795   functions named
   787    796   <ul>
   788    797   <li> AbcdeAlloc(),
   789    798   <li> AbcdeFree(),
   790    799   <li> AbcdeTrace(), and
   791    800   <li> Abcde().
   792    801   </ul>
   793         -The %name directive allows you to generator two or more different
   794         -parsers and link them all into the same executable.
   795         -</p>
          802  +The <tt>%name</tt> directive allows you to generate two or more different
          803  +parsers and link them all into the same executable.</p>
   796    804   
   797    805   <a name='pnonassoc'></a>
   798    806   <h4>The <tt>%nonassoc</tt> directive</h4>
   799    807   
   800    808   <p>This directive is used to assign non-associative precedence to
   801         -one or more terminal symbols.  See the section on 
          809  +one or more terminal symbols.  See the section on
   802    810   <a href='#precrules'>precedence rules</a>
   803         -or on the <a href='#pleft'>%left</a> directive for additional information.</p>
          811  +or on the <tt><a href='#pleft'>%left</a></tt> directive
          812  +for additional information.</p>
   804    813   
   805    814   <a name='parse_accept'></a>
   806    815   <h4>The <tt>%parse_accept</tt> directive</h4>
   807    816   
   808         -<p>The %parse_accept directive specifies a block of C code that is
          817  +<p>The <tt>%parse_accept</tt> directive specifies a block of C code that is
   809    818   executed whenever the parser accepts its input string.  To "accept"
   810    819   an input string means that the parser was able to process all tokens
   811    820   without error.</p>
   812    821   
   813    822   <p>For example:</p>
   814    823   
   815    824   <p><pre>
................................................................................
   817    826         printf("parsing complete!\n");
   818    827      }
   819    828   </pre></p>
   820    829   
   821    830   <a name='parse_failure'></a>
   822    831   <h4>The <tt>%parse_failure</tt> directive</h4>
   823    832   
   824         -<p>The %parse_failure directive specifies a block of C code that
          833  +<p>The <tt>%parse_failure</tt> directive specifies a block of C code that
   825    834   is executed whenever the parser fails complete.  This code is not
   826    835   executed until the parser has tried and failed to resolve an input
   827    836   error using is usual error recovery strategy.  The routine is
   828    837   only invoked when parsing is unable to continue.</p>
   829    838   
   830    839   <p><pre>
   831    840      %parse_failure {
................................................................................
   833    842      }
   834    843   </pre></p>
   835    844   
   836    845   <a name='pright'></a>
   837    846   <h4>The <tt>%right</tt> directive</h4>
   838    847   
   839    848   <p>This directive is used to assign right-associative precedence to
   840         -one or more terminal symbols.  See the section on 
          849  +one or more terminal symbols.  See the section on
   841    850   <a href='#precrules'>precedence rules</a>
   842    851   or on the <a href='#pleft'>%left</a> directive for additional information.</p>
   843    852   
   844    853   <a name='stack_overflow'></a>
   845    854   <h4>The <tt>%stack_overflow</tt> directive</h4>
   846    855   
   847         -<p>The %stack_overflow directive specifies a block of C code that
          856  +<p>The <tt>%stack_overflow</tt> directive specifies a block of C code that
   848    857   is executed if the parser's internal stack ever overflows.  Typically
   849    858   this just prints an error message.  After a stack overflow, the parser
   850    859   will be unable to continue and must be reset.</p>
   851    860   
   852    861   <p><pre>
   853    862      %stack_overflow {
   854    863        fprintf(stderr,"Giving up.  Parser stack overflow\n");
   855    864      }
   856    865   </pre></p>
   857    866   
   858    867   <p>You can help prevent parser stack overflows by avoiding the use
   859    868   of right recursion and right-precedence operators in your grammar.
   860         -Use left recursion and and left-precedence operators instead, to
          869  +Use left recursion and and left-precedence operators instead to
   861    870   encourage rules to reduce sooner and keep the stack size down.
   862    871   For example, do rules like this:
   863    872   <pre>
   864    873      list ::= list element.      // left-recursion.  Good!
   865    874      list ::= .
   866    875   </pre>
   867    876   Not like this:
   868    877   <pre>
   869    878      list ::= element list.      // right-recursion.  Bad!
   870    879      list ::= .
   871         -</pre>
          880  +</pre></p>
   872    881   
   873    882   <a name='stack_size'></a>
   874    883   <h4>The <tt>%stack_size</tt> directive</h4>
   875    884   
   876    885   <p>If stack overflow is a problem and you can't resolve the trouble
   877    886   by using left-recursion, then you might want to increase the size
   878    887   of the parser's stack using this directive.  Put an positive integer
   879         -after the %stack_size directive and Lemon will generate a parse
          888  +after the <tt>%stack_size</tt> directive and Lemon will generate a parse
   880    889   with a stack of the requested size.  The default value is 100.</p>
   881    890   
   882    891   <p><pre>
   883    892      %stack_size 2000
   884    893   </pre></p>
   885    894   
   886    895   <a name='start_symbol'></a>
   887    896   <h4>The <tt>%start_symbol</tt> directive</h4>
   888    897   
   889         -<p>By default, the start-symbol for the grammar that Lemon generates
          898  +<p>By default, the start symbol for the grammar that Lemon generates
   890    899   is the first non-terminal that appears in the grammar file.  But you
   891         -can choose a different start-symbol using the %start_symbol directive.</p>
          900  +can choose a different start symbol using the
          901  +<tt>%start_symbol</tt> directive.</p>
   892    902   
   893    903   <p><pre>
   894    904      %start_symbol  prog
   895    905   </pre></p>
   896    906   
          907  +<a name='syntax_error'></a>
          908  +<h4>The <tt>%syntax_error</tt> directive</h4>
          909  +
          910  +<p>See <a href='#error_processing'>Error Processing</a>.</p>
          911  +
          912  +<a name='token_class'></a>
          913  +<h4>The <tt>%token_class</tt> directive</h4>
          914  +
          915  +<p>Undocumented.  Appears to be related to the MULTITERMINAL concept.
          916  +<a href='http://sqlite.org/src/fdiff?v1=796930d5fc2036c7&v2=624b24c5dc048e09&sbs=0'>Implementation</a>.</p>
          917  +
   897    918   <a name='token_destructor'></a>
   898    919   <h4>The <tt>%token_destructor</tt> directive</h4>
   899    920   
   900         -<p>The %destructor directive assigns a destructor to a non-terminal
   901         -symbol.  (See the description of the %destructor directive above.)
   902         -This directive does the same thing for all terminal symbols.</p>
          921  +<p>The <tt>%destructor</tt> directive assigns a destructor to a non-terminal
          922  +symbol.  (See the description of the
          923  +<tt><a href='%destructor'>%destructor</a></tt> directive above.)
          924  +The <tt>%token_destructor</tt> directive does the same thing
          925  +for all terminal symbols.</p>
   903    926   
   904    927   <p>Unlike non-terminal symbols which may each have a different data type
   905    928   for their values, terminals all use the same data type (defined by
   906         -the %token_type directive) and so they use a common destructor.  Other
   907         -than that, the token destructor works just like the non-terminal
          929  +the <tt><a href='#token_type'>%token_type</a></tt> directive)
          930  +and so they use a common destructor.
          931  +Other than that, the token destructor works just like the non-terminal
   908    932   destructors.</p>
   909    933   
   910    934   <a name='token_prefix'></a>
   911    935   <h4>The <tt>%token_prefix</tt> directive</h4>
   912    936   
   913    937   <p>Lemon generates #defines that assign small integer constants
   914    938   to each terminal symbol in the grammar.  If desired, Lemon will
   915    939   add a prefix specified by this directive
   916         -to each of the #defines it generates.
   917         -So if the default output of Lemon looked like this:
          940  +to each of the #defines it generates.</p>
          941  +
          942  +<p>So if the default output of Lemon looked like this:
   918    943   <pre>
   919    944       #define AND              1
   920    945       #define MINUS            2
   921    946       #define OR               3
   922    947       #define PLUS             4
   923    948   </pre>
   924    949   You can insert a statement into the grammar like this:
................................................................................
   927    952   </pre>
   928    953   to cause Lemon to produce these symbols instead:
   929    954   <pre>
   930    955       #define TOKEN_AND        1
   931    956       #define TOKEN_MINUS      2
   932    957       #define TOKEN_OR         3
   933    958       #define TOKEN_PLUS       4
   934         -</pre>
          959  +</pre></p>
   935    960   
   936    961   <a name='token_type'></a><a name='ptype'></a>
   937    962   <h4>The <tt>%token_type</tt> and <tt>%type</tt> directives</h4>
   938    963   
   939    964   <p>These directives are used to specify the data types for values
   940    965   on the parser's stack associated with terminal and non-terminal
   941    966   symbols.  The values of all terminal symbols must be of the same
................................................................................
   948    973      %token_type    {Token*}
   949    974   </pre></p>
   950    975   
   951    976   <p>If the data type of terminals is not specified, the default value
   952    977   is "void*".</p>
   953    978   
   954    979   <p>Non-terminal symbols can each have their own data types.  Typically
   955         -the data type  of a non-terminal is a pointer to the root of a parse-tree
          980  +the data type of a non-terminal is a pointer to the root of a parse tree
   956    981   structure that contains all information about that non-terminal.
   957    982   For example:</p>
   958    983   
   959    984   <p><pre>
   960    985      %type   expr  {Expr*}
   961    986   </pre></p>
   962    987   
................................................................................
   969    994   non-terminal whose data type requires 1K of storage, then your 100
   970    995   entry parser stack will require 100K of heap space.  If you are willing
   971    996   and able to pay that price, fine.  You just need to know.</p>
   972    997   
   973    998   <a name='pwildcard'></a>
   974    999   <h4>The <tt>%wildcard</tt> directive</h4>
   975   1000   
   976         -<p>The %wildcard directive is followed by a single token name and a
   977         -period.  This directive specifies that the identified token should 
   978         -match any input token.
         1001  +<p>The <tt>%wildcard</tt> directive is followed by a single token name and a
         1002  +period.  This directive specifies that the identified token should
         1003  +match any input token.</p>
   979   1004   
   980   1005   <p>When the generated parser has the choice of matching an input against
   981   1006   the wildcard token and some other token, the other token is always used.
   982         -The wildcard token is only matched if there are no other alternatives.
         1007  +The wildcard token is only matched if there are no alternatives.</p>
   983   1008   
         1009  +<a name='error_processing'></a>
   984   1010   <h3>Error Processing</h3>
   985   1011   
   986   1012   <p>After extensive experimentation over several years, it has been
   987   1013   discovered that the error recovery strategy used by yacc is about
   988   1014   as good as it gets.  And so that is what Lemon uses.</p>
   989   1015   
   990   1016   <p>When a Lemon-generated parser encounters a syntax error, it
   991         -first invokes the code specified by the %syntax_error directive, if
         1017  +first invokes the code specified by the <tt>%syntax_error</tt> directive, if
   992   1018   any.  It then enters its error recovery strategy.  The error recovery
   993   1019   strategy is to begin popping the parsers stack until it enters a
   994   1020   state where it is permitted to shift a special non-terminal symbol
   995   1021   named "error".  It then shifts this non-terminal and continues
   996         -parsing.  But the %syntax_error routine will not be called again
         1022  +parsing.  The <tt>%syntax_error</tt> routine will not be called again
   997   1023   until at least three new tokens have been successfully shifted.</p>
   998   1024   
   999   1025   <p>If the parser pops its stack until the stack is empty, and it still
  1000         -is unable to shift the error symbol, then the %parse_failed routine
         1026  +is unable to shift the error symbol, then the
         1027  +<tt><a href='#parse_failure'>%parse_failure</a></tt> routine
  1001   1028   is invoked and the parser resets itself to its start state, ready
  1002   1029   to begin parsing a new file.  This is what will happen at the very
  1003         -first syntax error, of course, if there are no instances of the 
         1030  +first syntax error, of course, if there are no instances of the
  1004   1031   "error" non-terminal in your grammar.</p>
  1005   1032   
  1006   1033   </body>
  1007   1034   </html>

Changes to ext/icu/icu.c.

    98     98   ** false (0) if they are different.
    99     99   */
   100    100   static int icuLikeCompare(
   101    101     const uint8_t *zPattern,   /* LIKE pattern */
   102    102     const uint8_t *zString,    /* The UTF-8 string to compare against */
   103    103     const UChar32 uEsc         /* The escape character */
   104    104   ){
   105         -  static const int MATCH_ONE = (UChar32)'_';
   106         -  static const int MATCH_ALL = (UChar32)'%';
          105  +  static const uint32_t MATCH_ONE = (uint32_t)'_';
          106  +  static const uint32_t MATCH_ALL = (uint32_t)'%';
   107    107   
   108    108     int prevEscape = 0;     /* True if the previous character was uEsc */
   109    109   
   110    110     while( 1 ){
   111    111   
   112    112       /* Read (and consume) the next character from the input pattern. */
   113         -    UChar32 uPattern;
          113  +    uint32_t uPattern;
   114    114       SQLITE_ICU_READ_UTF8(zPattern, uPattern);
   115    115       if( uPattern==0 ) break;
   116    116   
   117    117       /* There are now 4 possibilities:
   118    118       **
   119    119       **     1. uPattern is an unescaped match-all character "%",
   120    120       **     2. uPattern is an unescaped match-one character "_",
................................................................................
   148    148         return 0;
   149    149   
   150    150       }else if( !prevEscape && uPattern==MATCH_ONE ){
   151    151         /* Case 2. */
   152    152         if( *zString==0 ) return 0;
   153    153         SQLITE_ICU_SKIP_UTF8(zString);
   154    154   
   155         -    }else if( !prevEscape && uPattern==uEsc){
          155  +    }else if( !prevEscape && uPattern==(uint32_t)uEsc){
   156    156         /* Case 3. */
   157    157         prevEscape = 1;
   158    158   
   159    159       }else{
   160    160         /* Case 4. */
   161         -      UChar32 uString;
          161  +      uint32_t uString;
   162    162         SQLITE_ICU_READ_UTF8(zString, uString);
   163         -      uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
   164         -      uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
          163  +      uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
          164  +      uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
   165    165         if( uString!=uPattern ){
   166    166           return 0;
   167    167         }
   168    168         prevEscape = 0;
   169    169       }
   170    170     }
   171    171   

Changes to ext/misc/csv.c.

    74     74   struct CsvReader {
    75     75     FILE *in;              /* Read the CSV text from this input stream */
    76     76     char *z;               /* Accumulated text for a field */
    77     77     int n;                 /* Number of bytes in z */
    78     78     int nAlloc;            /* Space allocated for z[] */
    79     79     int nLine;             /* Current line number */
    80     80     int bNotFirst;         /* True if prior text has been seen */
    81         -  char cTerm;            /* Character that terminated the most recent field */
           81  +  int cTerm;             /* Character that terminated the most recent field */
    82     82     size_t iIn;            /* Next unread character in the input buffer */
    83     83     size_t nIn;            /* Number of characters in the input buffer */
    84     84     char *zIn;             /* The input buffer */
    85     85     char zErr[CSV_MXERR];  /* Error message */
    86     86   };
    87     87   
    88     88   /* Initialize a CsvReader object */
................................................................................
   162    162   
   163    163   /* Return the next character of input.  Return EOF at end of input. */
   164    164   static int csv_getc(CsvReader *p){
   165    165     if( p->iIn >= p->nIn ){
   166    166       if( p->in!=0 ) return csv_getc_refill(p);
   167    167       return EOF;
   168    168     }
   169         -  return p->zIn[p->iIn++];
          169  +  return ((unsigned char*)p->zIn)[p->iIn++];
   170    170   }
   171    171   
   172    172   /* Increase the size of p->z and append character c to the end. 
   173    173   ** Return 0 on success and non-zero if there is an OOM error */
   174    174   static CSV_NOINLINE int csv_resize_and_append(CsvReader *p, char c){
   175    175     char *zNew;
   176    176     int nNew = p->nAlloc*2 + 100;

Added ext/misc/mmapwarm.c.

            1  +/*
            2  +** 2017-09-18
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +*/
           14  +
           15  +#include "sqlite3.h"
           16  +
           17  +
           18  +/*
           19  +** This function is used to touch each page of a mapping of a memory
           20  +** mapped SQLite database. Assuming that the system has sufficient free
           21  +** memory and supports sufficiently large mappings, this causes the OS 
           22  +** to cache the entire database in main memory, making subsequent 
           23  +** database accesses faster.
           24  +**
           25  +** If the second parameter to this function is not NULL, it is the name of
           26  +** the specific database to operate on (i.e. "main" or the name of an
           27  +** attached database).
           28  +**
           29  +** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
           30  +** It is not considered an error if the file is not memory-mapped, or if
           31  +** the mapping does not span the entire file. If an error does occur, a
           32  +** transaction may be left open on the database file.
           33  +**
           34  +** It is illegal to call this function when the database handle has an 
           35  +** open transaction. SQLITE_MISUSE is returned in this case.
           36  +*/
           37  +int sqlite3_mmap_warm(sqlite3 *db, const char *zDb){
           38  +  int rc = SQLITE_OK;
           39  +  char *zSql = 0;
           40  +  int pgsz = 0;
           41  +  int nTotal = 0;
           42  +
           43  +  if( 0==sqlite3_get_autocommit(db) ) return SQLITE_MISUSE;
           44  +
           45  +  /* Open a read-only transaction on the file in question */
           46  +  zSql = sqlite3_mprintf("BEGIN; SELECT * FROM %s%q%ssqlite_master", 
           47  +      (zDb ? "'" : ""), (zDb ? zDb : ""), (zDb ? "'." : "")
           48  +  );
           49  +  if( zSql==0 ) return SQLITE_NOMEM;
           50  +  rc = sqlite3_exec(db, zSql, 0, 0, 0);
           51  +  sqlite3_free(zSql);
           52  +
           53  +  /* Find the SQLite page size of the file */
           54  +  if( rc==SQLITE_OK ){
           55  +    zSql = sqlite3_mprintf("PRAGMA %s%q%spage_size", 
           56  +        (zDb ? "'" : ""), (zDb ? zDb : ""), (zDb ? "'." : "")
           57  +    );
           58  +    if( zSql==0 ){
           59  +      rc = SQLITE_NOMEM;
           60  +    }else{
           61  +      sqlite3_stmt *pPgsz = 0;
           62  +      rc = sqlite3_prepare_v2(db, zSql, -1, &pPgsz, 0);
           63  +      sqlite3_free(zSql);
           64  +      if( rc==SQLITE_OK ){
           65  +        if( sqlite3_step(pPgsz)==SQLITE_ROW ){
           66  +          pgsz = sqlite3_column_int(pPgsz, 0);
           67  +        }
           68  +        rc = sqlite3_finalize(pPgsz);
           69  +      }
           70  +      if( rc==SQLITE_OK && pgsz==0 ){
           71  +        rc = SQLITE_ERROR;
           72  +      }
           73  +    }
           74  +  }
           75  +
           76  +  /* Touch each mmap'd page of the file */
           77  +  if( rc==SQLITE_OK ){
           78  +    int rc2;
           79  +    sqlite3_file *pFd = 0;
           80  +    rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFd);
           81  +    if( rc==SQLITE_OK && pFd->pMethods->iVersion>=3 ){
           82  +      sqlite3_int64 iPg = 1;
           83  +      sqlite3_io_methods const *p = pFd->pMethods;
           84  +      while( 1 ){
           85  +        unsigned char *pMap;
           86  +        rc = p->xFetch(pFd, pgsz*iPg, pgsz, (void**)&pMap);
           87  +        if( rc!=SQLITE_OK || pMap==0 ) break;
           88  +
           89  +        nTotal += pMap[0];
           90  +        nTotal += pMap[pgsz-1];
           91  +
           92  +        rc = p->xUnfetch(pFd, pgsz*iPg, (void*)pMap);
           93  +        if( rc!=SQLITE_OK ) break;
           94  +        iPg++;
           95  +      }
           96  +      sqlite3_log(SQLITE_OK, 
           97  +          "sqlite3_mmap_warm_cache: Warmed up %d pages of %s", iPg==1?0:iPg,
           98  +          sqlite3_db_filename(db, zDb)
           99  +      );
          100  +    }
          101  +
          102  +    rc2 = sqlite3_exec(db, "END", 0, 0, 0);
          103  +    if( rc==SQLITE_OK ) rc = rc2;
          104  +  }
          105  +
          106  +  return rc;
          107  +}
          108  +

Changes to ext/misc/series.c.

   191    191       default:                   x = pCur->iValue;  break;
   192    192     }
   193    193     sqlite3_result_int64(ctx, x);
   194    194     return SQLITE_OK;
   195    195   }
   196    196   
   197    197   /*
   198         -** Return the rowid for the current row.  In this implementation, the
   199         -** rowid is the same as the output value.
          198  +** Return the rowid for the current row. In this implementation, the
          199  +** first row returned is assigned rowid value 1, and each subsequent
          200  +** row a value 1 more than that of the previous.
   200    201   */
   201    202   static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
   202    203     series_cursor *pCur = (series_cursor*)cur;
   203    204     *pRowid = pCur->iRowid;
   204    205     return SQLITE_OK;
   205    206   }
   206    207   

Added ext/rbu/rbutemplimit.test.

            1  +# 2014 August 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  +
           13  +source [file join [file dirname [info script]] rbu_common.tcl]
           14  +set ::testprefix rbutemplimit
           15  +
           16  +db close
           17  +sqlite3_shutdown
           18  +sqlite3_config_uri 1
           19  +
           20  +proc setup_databases {} {
           21  +  forcedelete test.db2
           22  +  forcedelete test.db
           23  +  sqlite3 db test.db
           24  +  execsql {
           25  +    -- Create target database schema.
           26  +    --
           27  +    CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB(100), c BLOB(100));
           28  +    CREATE TABLE t2(a INTEGER PRIMARY KEY, b BLOB(100), c BLOB(100));
           29  +    CREATE INDEX i1b ON t1(b);
           30  +    CREATE INDEX i1c ON t1(c);
           31  +    CREATE INDEX i2b ON t2(b);
           32  +    CREATE INDEX i2c ON t2(c);
           33  +  
           34  +    -- Create a large RBU database.
           35  +    --
           36  +    ATTACH 'test.db2' AS rbu;
           37  +    CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
           38  +    WITH s(i) AS (
           39  +      VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10000
           40  +    )
           41  +    INSERT INTO data_t1 SELECT i, randomblob(100), randomblob(100), 0 FROM s;
           42  +    CREATE TABLE rbu.data_t2(a, b, c, rbu_control);
           43  +    WITH s(i) AS (
           44  +      VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<15000
           45  +    )
           46  +    INSERT INTO data_t2 SELECT i, randomblob(100), randomblob(100), 0 FROM s;
           47  +  }
           48  +  db close
           49  +}
           50  +
           51  +proc run_rbu_cachesize {target rbu cachesize temp_limit} {
           52  +  sqlite3rbu rbu $target $rbu
           53  +  rbu temp_size_limit $temp_limit
           54  +  sqlite3_exec_nr [rbu db 1] "PRAGMA cache_size = $cachesize"
           55  +  while 1 {
           56  +    set rc [rbu step]
           57  +    set ::A([rbu temp_size]) 1
           58  +    if {$rc!="SQLITE_OK"} break
           59  +  }
           60  +  list [catch {rbu close} msg] $msg
           61  +}
           62  +
           63  +proc step_rbu_cachesize {target rbu stepsize cachesize temp_limit} {
           64  +  set res ""
           65  +  while 1 {
           66  +    sqlite3rbu rbu $target $rbu
           67  +    rbu temp_size_limit $temp_limit
           68  +    sqlite3_exec_nr [rbu db 1] "PRAGMA cache_size = $cachesize"
           69  +    for {set i 0} {$i < $stepsize} {incr i} {
           70  +      set rc [rbu step]
           71  +      set ::A([rbu temp_size]) 1
           72  +      if {$rc!="SQLITE_OK"} break
           73  +    }
           74  +    set res [list [catch {rbu close} msg] $msg]
           75  +    if {$res != "0 SQLITE_OK"} break
           76  +  }
           77  +  set res
           78  +}
           79  +
           80  +do_test 1.1.0 { setup_databases } {}
           81  +
           82  +do_test 1.1.1 {
           83  +  unset -nocomplain ::A
           84  +  run_rbu_cachesize test.db test.db2 10 0
           85  +} {0 SQLITE_DONE}
           86  +
           87  +do_test 1.1.2 { llength [array names ::A] } 3
           88  +
           89  +do_test 1.1.3 { 
           90  +  foreach {a0 a1 a2} [lsort -integer [array names ::A]] {}
           91  +  list [expr $a0==0]                         \
           92  +       [expr $a1>1048576] [expr $a1<1200000] \
           93  +       [expr $a2>1500000] [expr $a2<1700000]
           94  +} {1 1 1 1 1}
           95  +
           96  +do_test 1.2.1 {
           97  +  setup_databases
           98  +  run_rbu_cachesize test.db test.db2 10 1000000
           99  +} {1 SQLITE_FULL}
          100  +do_test 1.2.2 { info commands rbu } {}
          101  +
          102  +do_test 1.3.1 {
          103  +  setup_databases
          104  +  run_rbu_cachesize test.db test.db2 10 1300000
          105  +} {1 SQLITE_FULL}
          106  +do_test 1.3.2 { info commands rbu } {}
          107  +
          108  +do_test 1.4.1 {
          109  +  setup_databases
          110  +  run_rbu_cachesize test.db test.db2 10 1800000
          111  +} {0 SQLITE_DONE}
          112  +do_test 1.4.2 { info commands rbu } {}
          113  +
          114  +do_test 1.5.1 {
          115  +  setup_databases
          116  +  unset -nocomplain ::A
          117  +  step_rbu_cachesize test.db test.db2 1000 10 2400000
          118  +} {0 SQLITE_DONE}
          119  +do_test 1.5.2 { info commands rbu } {}
          120  +
          121  +do_test 1.6.1 {
          122  +  setup_databases
          123  +  unset -nocomplain ::A
          124  +  step_rbu_cachesize test.db test.db2 1000 10 1400000
          125  +} {1 SQLITE_FULL}
          126  +do_test 1.6.2 { info commands rbu } {}
          127  +
          128  +finish_test
          129  +

Changes to ext/rbu/sqlite3rbu.c.

   367    367     u32 mLock;
   368    368     int nFrame;                     /* Entries in aFrame[] array */
   369    369     int nFrameAlloc;                /* Allocated size of aFrame[] array */
   370    370     RbuFrame *aFrame;
   371    371     int pgsz;
   372    372     u8 *aBuf;
   373    373     i64 iWalCksum;
          374  +  i64 szTemp;                     /* Current size of all temp files in use */
          375  +  i64 szTempLimit;                /* Total size limit for temp files */
   374    376   
   375    377     /* Used in RBU vacuum mode only */
   376    378     int nRbu;                       /* Number of RBU VFS in the stack */
   377    379     rbu_file *pRbuFd;               /* Fd for main db of dbRbu */
   378    380   };
   379    381   
   380    382   /*
   381    383   ** An rbu VFS is implemented using an instance of this structure.
          384  +**
          385  +** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
          386  +** It is NULL for RBU VFS objects created explicitly using
          387  +** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
          388  +** space used by the RBU handle.
   382    389   */
   383    390   struct rbu_vfs {
   384    391     sqlite3_vfs base;               /* rbu VFS shim methods */
   385    392     sqlite3_vfs *pRealVfs;          /* Underlying VFS */
   386    393     sqlite3_mutex *mutex;           /* Mutex to protect pMain */
          394  +  sqlite3rbu *pRbu;               /* Owner RBU object */
   387    395     rbu_file *pMain;                /* Linked list of main db files */
   388    396   };
   389    397   
   390    398   /*
   391    399   ** Each file opened by an rbu VFS is represented by an instance of
   392    400   ** the following structure.
          401  +**
          402  +** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
          403  +** "sz" is set to the current size of the database file.
   393    404   */
   394    405   struct rbu_file {
   395    406     sqlite3_file base;              /* sqlite3_file methods */
   396    407     sqlite3_file *pReal;            /* Underlying file handle */
   397    408     rbu_vfs *pRbuVfs;               /* Pointer to the rbu_vfs object */
   398    409     sqlite3rbu *pRbu;               /* Pointer to rbu object (rbu target only) */
          410  +  i64 sz;                         /* Size of file in bytes (temp only) */
   399    411   
   400    412     int openFlags;                  /* Flags this file was opened with */
   401    413     u32 iCookie;                    /* Cookie value for main db files */
   402    414     u8 iWriteVer;                   /* "write-version" value for main db files */
   403    415     u8 bNolock;                     /* True to fail EXCLUSIVE locks */
   404    416   
   405    417     int nShm;                       /* Number of entries in apShm[] array */
................................................................................
  3405   3417     sqlite3_randomness(sizeof(int), (void*)&rnd);
  3406   3418     sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
  3407   3419     p->rc = sqlite3rbu_create_vfs(zRnd, 0);
  3408   3420     if( p->rc==SQLITE_OK ){
  3409   3421       sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
  3410   3422       assert( pVfs );
  3411   3423       p->zVfsName = pVfs->zName;
         3424  +    ((rbu_vfs*)pVfs)->pRbu = p;
  3412   3425     }
  3413   3426   }
  3414   3427   
  3415   3428   /*
  3416   3429   ** Destroy the private VFS created for the rbu handle passed as the only
  3417   3430   ** argument by an earlier call to rbuCreateVfs().
  3418   3431   */
................................................................................
  3777   3790         int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
  3778   3791         if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
  3779   3792       }
  3780   3793   
  3781   3794       /* Close the open database handle and VFS object. */
  3782   3795       sqlite3_close(p->dbRbu);
  3783   3796       sqlite3_close(p->dbMain);
         3797  +    assert( p->szTemp==0 );
  3784   3798       rbuDeleteVfs(p);
  3785   3799       sqlite3_free(p->aBuf);
  3786   3800       sqlite3_free(p->aFrame);
  3787   3801   
  3788   3802       rbuEditErrmsg(p);
  3789   3803       rc = p->rc;
  3790   3804       if( pzErrmsg ){
................................................................................
  3964   3978   **     database file are recorded. xShmLock() calls to unlock the same
  3965   3979   **     locks are no-ops (so that once obtained, these locks are never
  3966   3980   **     relinquished). Finally, calls to xSync() on the target database
  3967   3981   **     file fail with SQLITE_INTERNAL errors.
  3968   3982   */
  3969   3983   
  3970   3984   static void rbuUnlockShm(rbu_file *p){
         3985  +  assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
  3971   3986     if( p->pRbu ){
  3972   3987       int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
  3973   3988       int i;
  3974   3989       for(i=0; i<SQLITE_SHM_NLOCK;i++){
  3975   3990         if( (1<<i) & p->pRbu->mLock ){
  3976   3991           xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
  3977   3992         }
  3978   3993       }
  3979   3994       p->pRbu->mLock = 0;
  3980   3995     }
  3981   3996   }
         3997  +
         3998  +/*
         3999  +*/
         4000  +static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
         4001  +  sqlite3rbu *pRbu = pFd->pRbu;
         4002  +  i64 nDiff = nNew - pFd->sz;
         4003  +  pRbu->szTemp += nDiff;
         4004  +  pFd->sz = nNew;
         4005  +  assert( pRbu->szTemp>=0 );
         4006  +  if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
         4007  +  return SQLITE_OK;
         4008  +}
  3982   4009   
  3983   4010   /*
  3984   4011   ** Close an rbu file.
  3985   4012   */
  3986   4013   static int rbuVfsClose(sqlite3_file *pFile){
  3987   4014     rbu_file *p = (rbu_file*)pFile;
  3988   4015     int rc;
................................................................................
  4000   4027       rbu_file **pp;
  4001   4028       sqlite3_mutex_enter(p->pRbuVfs->mutex);
  4002   4029       for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
  4003   4030       *pp = p->pMainNext;
  4004   4031       sqlite3_mutex_leave(p->pRbuVfs->mutex);
  4005   4032       rbuUnlockShm(p);
  4006   4033       p->pReal->pMethods->xShmUnmap(p->pReal, 0);
         4034  +  }
         4035  +  else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
         4036  +    rbuUpdateTempSize(p, 0);
  4007   4037     }
  4008   4038   
  4009   4039     /* Close the underlying file handle */
  4010   4040     rc = p->pReal->pMethods->xClose(p->pReal);
  4011   4041     return rc;
  4012   4042   }
  4013   4043   
................................................................................
  4118   4148     sqlite3rbu *pRbu = p->pRbu;
  4119   4149     int rc;
  4120   4150   
  4121   4151     if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
  4122   4152       assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
  4123   4153       rc = rbuCaptureDbWrite(p->pRbu, iOfst);
  4124   4154     }else{
  4125         -    if( pRbu && pRbu->eStage==RBU_STAGE_OAL 
  4126         -     && (p->openFlags & SQLITE_OPEN_WAL) 
  4127         -     && iOfst>=pRbu->iOalSz
  4128         -    ){
  4129         -      pRbu->iOalSz = iAmt + iOfst;
         4155  +    if( pRbu ){
         4156  +      if( pRbu->eStage==RBU_STAGE_OAL 
         4157  +       && (p->openFlags & SQLITE_OPEN_WAL) 
         4158  +       && iOfst>=pRbu->iOalSz
         4159  +      ){
         4160  +        pRbu->iOalSz = iAmt + iOfst;
         4161  +      }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
         4162  +        i64 szNew = iAmt+iOfst;
         4163  +        if( szNew>p->sz ){
         4164  +          rc = rbuUpdateTempSize(p, szNew);
         4165  +          if( rc!=SQLITE_OK ) return rc;
         4166  +        }
         4167  +      }
  4130   4168       }
  4131   4169       rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
  4132   4170       if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
  4133   4171         /* These look like magic numbers. But they are stable, as they are part
  4134   4172         ** of the definition of the SQLite file format, which may not change. */
  4135   4173         u8 *pBuf = (u8*)zBuf;
  4136   4174         p->iCookie = rbuGetU32(&pBuf[24]);
................................................................................
  4141   4179   }
  4142   4180   
  4143   4181   /*
  4144   4182   ** Truncate an rbuVfs-file.
  4145   4183   */
  4146   4184   static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
  4147   4185     rbu_file *p = (rbu_file*)pFile;
         4186  +  if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
         4187  +    int rc = rbuUpdateTempSize(p, size);
         4188  +    if( rc!=SQLITE_OK ) return rc;
         4189  +  }
  4148   4190     return p->pReal->pMethods->xTruncate(p->pReal, size);
  4149   4191   }
  4150   4192   
  4151   4193   /*
  4152   4194   ** Sync an rbuVfs-file.
  4153   4195   */
  4154   4196   static int rbuVfsSync(sqlite3_file *pFile, int flags){
................................................................................
  4530   4572               rc = SQLITE_NOMEM;
  4531   4573             }
  4532   4574             pFd->pRbu = pDb->pRbu;
  4533   4575           }
  4534   4576           pDb->pWalFd = pFd;
  4535   4577         }
  4536   4578       }
         4579  +  }else{
         4580  +    pFd->pRbu = pRbuVfs->pRbu;
  4537   4581     }
  4538   4582   
  4539   4583     if( oflags & SQLITE_OPEN_MAIN_DB 
  4540   4584      && sqlite3_uri_boolean(zName, "rbu_memory", 0) 
  4541   4585     ){
  4542   4586       assert( oflags & SQLITE_OPEN_MAIN_DB );
  4543   4587       oflags =  SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
................................................................................
  4796   4840         sqlite3_mutex_free(pNew->mutex);
  4797   4841         sqlite3_free(pNew);
  4798   4842       }
  4799   4843     }
  4800   4844   
  4801   4845     return rc;
  4802   4846   }
         4847  +
         4848  +/*
         4849  +** Configure the aggregate temp file size limit for this RBU handle.
         4850  +*/
         4851  +sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
         4852  +  if( n>=0 ){
         4853  +    pRbu->szTempLimit = n;
         4854  +  }
         4855  +  return pRbu->szTempLimit;
         4856  +}
         4857  +
         4858  +sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
         4859  +  return pRbu->szTemp;
         4860  +}
  4803   4861   
  4804   4862   
  4805   4863   /**************************************************************************/
  4806   4864   
  4807   4865   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */

Changes to ext/rbu/sqlite3rbu.h.

   348    348   ** zipvfs databases.
   349    349   */
   350    350   SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
   351    351     const char *zTarget, 
   352    352     const char *zState
   353    353   );
   354    354   
          355  +/*
          356  +** Configure a limit for the amount of temp space that may be used by
          357  +** the RBU handle passed as the first argument. The new limit is specified
          358  +** in bytes by the second parameter. If it is positive, the limit is updated.
          359  +** If the second parameter to this function is passed zero, then the limit
          360  +** is removed entirely. If the second parameter is negative, the limit is
          361  +** not modified (this is useful for querying the current limit).
          362  +**
          363  +** In all cases the returned value is the current limit in bytes (zero 
          364  +** indicates unlimited).
          365  +**
          366  +** If the temp space limit is exceeded during operation, an SQLITE_FULL
          367  +** error is returned.
          368  +*/
          369  +SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
          370  +
          371  +/*
          372  +** Return the current amount of temp file space, in bytes, currently used by 
          373  +** the RBU handle passed as the only argument.
          374  +*/
          375  +SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
          376  +
   355    377   /*
   356    378   ** Internally, each RBU connection uses a separate SQLite database 
   357    379   ** connection to access the target and rbu update databases. This
   358    380   ** API allows the application direct access to these database handles.
   359    381   **
   360    382   ** The first argument passed to this function must be a valid, open, RBU
   361    383   ** handle. The second argument should be passed zero to access the target

Changes to ext/rbu/test_rbu.c.

    65     65     int ret = TCL_OK;
    66     66     sqlite3rbu *pRbu = (sqlite3rbu*)clientData;
    67     67     struct RbuCmd {
    68     68       const char *zName;
    69     69       int nArg;
    70     70       const char *zUsage;
    71     71     } aCmd[] = {
    72         -    {"step", 2, ""},              /* 0 */
    73         -    {"close", 2, ""},             /* 1 */
    74         -    {"create_rbu_delta", 2, ""},  /* 2 */
    75         -    {"savestate", 2, ""},         /* 3 */
    76         -    {"dbMain_eval", 3, "SQL"},    /* 4 */
    77         -    {"bp_progress", 2, ""},       /* 5 */
    78         -    {"db", 3, "RBU"},             /* 6 */
    79         -    {"state", 2, ""},             /* 7 */
    80         -    {"progress", 2, ""},          /* 8 */
    81         -    {"close_no_error", 2, ""},    /* 9 */
           72  +    {"step", 2, ""},                 /* 0 */
           73  +    {"close", 2, ""},                /* 1 */
           74  +    {"create_rbu_delta", 2, ""},     /* 2 */
           75  +    {"savestate", 2, ""},            /* 3 */
           76  +    {"dbMain_eval", 3, "SQL"},       /* 4 */
           77  +    {"bp_progress", 2, ""},          /* 5 */
           78  +    {"db", 3, "RBU"},                /* 6 */
           79  +    {"state", 2, ""},                /* 7 */
           80  +    {"progress", 2, ""},             /* 8 */
           81  +    {"close_no_error", 2, ""},       /* 9 */
           82  +    {"temp_size_limit", 3, "LIMIT"}, /* 10 */
           83  +    {"temp_size", 2, ""},            /* 11 */
    82     84       {0,0,0}
    83     85     };
    84     86     int iCmd;
    85     87   
    86     88     if( objc<2 ){
    87     89       Tcl_WrongNumArgs(interp, 1, objv, "METHOD");
    88     90       return TCL_ERROR;
................................................................................
   188    190         Tcl_SetResult(interp, (char*)aRes[eState], TCL_STATIC);
   189    191         break;
   190    192       }
   191    193       case 8: /* progress */ {
   192    194         sqlite3_int64 nStep =  sqlite3rbu_progress(pRbu);
   193    195         Tcl_SetObjResult(interp, Tcl_NewWideIntObj(nStep));
   194    196         break;
          197  +    }
          198  +                           
          199  +    case 10: /* temp_size_limit */ {
          200  +      sqlite3_int64 nLimit;
          201  +      if( Tcl_GetWideIntFromObj(interp, objv[2], &nLimit) ){
          202  +        ret = TCL_ERROR;
          203  +      }else{
          204  +        nLimit = sqlite3rbu_temp_size_limit(pRbu, nLimit);
          205  +        Tcl_SetObjResult(interp, Tcl_NewWideIntObj(nLimit));
          206  +      }
          207  +      break;
          208  +    }
          209  +    case 11: /* temp_size */ {
          210  +      sqlite3_int64 sz = sqlite3rbu_temp_size(pRbu);
          211  +      Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sz));
          212  +      break;
   195    213       }
   196    214   
   197    215       default: /* seems unlikely */
   198    216         assert( !"cannot happen" );
   199    217         break;
   200    218     }
   201    219   

Changes to ext/rtree/rtree.c.

  2849   2849   /*
  2850   2850   ** Remove the entry with rowid=iDelete from the r-tree structure.
  2851   2851   */
  2852   2852   static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
  2853   2853     int rc;                         /* Return code */
  2854   2854     RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
  2855   2855     int iCell;                      /* Index of iDelete cell in pLeaf */
  2856         -  RtreeNode *pRoot;               /* Root node of rtree structure */
         2856  +  RtreeNode *pRoot = 0;           /* Root node of rtree structure */
  2857   2857   
  2858   2858   
  2859   2859     /* Obtain a reference to the root node to initialize Rtree.iDepth */
  2860   2860     rc = nodeAcquire(pRtree, 1, 0, &pRoot);
  2861   2861   
  2862   2862     /* Obtain a reference to the leaf node that contains the entry 
  2863   2863     ** about to be deleted. 

Changes to main.mk.

   330    330     $(TOP)/ext/misc/carray.c \
   331    331     $(TOP)/ext/misc/closure.c \
   332    332     $(TOP)/ext/misc/csv.c \
   333    333     $(TOP)/ext/misc/eval.c \
   334    334     $(TOP)/ext/misc/fileio.c \
   335    335     $(TOP)/ext/misc/fuzzer.c \
   336    336     $(TOP)/ext/misc/ieee754.c \
          337  +  $(TOP)/ext/misc/mmapwarm.c \
   337    338     $(TOP)/ext/misc/nextchar.c \
   338    339     $(TOP)/ext/misc/percentile.c \
   339    340     $(TOP)/ext/misc/regexp.c \
   340    341     $(TOP)/ext/misc/remember.c \
   341    342     $(TOP)/ext/misc/series.c \
   342    343     $(TOP)/ext/misc/spellfix.c \
   343    344     $(TOP)/ext/misc/totype.c \

Changes to src/btree.c.

   632    632   # define btreePtrmapAllocate(x) SQLITE_OK
   633    633   # define btreePtrmapDelete(x) 
   634    634   # define btreePtrmapBegin(x,y)  SQLITE_OK
   635    635   # define btreePtrmapEnd(x,y,z) 
   636    636   #endif /* SQLITE_OMIT_CONCURRENT */
   637    637   
   638    638   static void releasePage(MemPage *pPage);  /* Forward reference */
          639  +static void releasePageOne(MemPage *pPage);      /* Forward reference */
   639    640   static void releasePageNotNull(MemPage *pPage);  /* Forward reference */
   640    641   
   641    642   /*
   642    643   ***** This routine is used inside of assert() only ****
   643    644   **
   644    645   ** Verify that the cursor holds the mutex on its BtShared
   645    646   */
................................................................................
   966    967   
   967    968     if( pKey ){
   968    969       assert( nKey==(i64)(int)nKey );
   969    970       pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
   970    971       if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
   971    972       sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
   972    973       if( pIdxKey->nField==0 ){
   973         -      rc = SQLITE_CORRUPT;
          974  +      rc = SQLITE_CORRUPT_BKPT;
   974    975         goto moveto_done;
   975    976       }
   976    977     }else{
   977    978       pIdxKey = 0;
   978    979     }
   979    980     rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
   980    981   moveto_done:
................................................................................
  2319   2320   #else
  2320   2321   # define setMempageRoot(x,y)
  2321   2322   #endif
  2322   2323   
  2323   2324   /*
  2324   2325   ** Release a MemPage.  This should be called once for each prior
  2325   2326   ** call to btreeGetPage.
         2327  +**
         2328  +** Page1 is a special case and must be released using releasePageOne().
  2326   2329   */
  2327   2330   static void releasePageNotNull(MemPage *pPage){
  2328   2331     assert( pPage->aData );
  2329   2332     assert( pPage->pBt );
  2330   2333     assert( pPage->pDbPage!=0 );
  2331   2334     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  2332   2335     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
  2333   2336     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  2334   2337     sqlite3PagerUnrefNotNull(pPage->pDbPage);
  2335   2338   }
  2336   2339   static void releasePage(MemPage *pPage){
  2337   2340     if( pPage ) releasePageNotNull(pPage);
         2341  +}
         2342  +static void releasePageOne(MemPage *pPage){
         2343  +  assert( pPage!=0 );
         2344  +  assert( pPage->aData );
         2345  +  assert( pPage->pBt );
         2346  +  assert( pPage->pDbPage!=0 );
         2347  +  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
         2348  +  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
         2349  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
         2350  +  sqlite3PagerUnrefPageOne(pPage->pDbPage);
  2338   2351   }
  2339   2352   
  2340   2353   /*
  2341   2354   ** Get an unused page.
  2342   2355   **
  2343   2356   ** This works just like btreeGetPage() with the addition:
  2344   2357   **
................................................................................
  3210   3223         int isOpen = 0;
  3211   3224         rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
  3212   3225         if( rc!=SQLITE_OK ){
  3213   3226           goto page1_init_failed;
  3214   3227         }else{
  3215   3228           setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
  3216   3229           if( isOpen==0 ){
  3217         -          releasePage(pPage1);
         3230  +          releasePageOne(pPage1);
  3218   3231             return SQLITE_OK;
  3219   3232           }
  3220   3233         }
  3221   3234         rc = SQLITE_NOTADB;
  3222   3235       }else{
  3223   3236         setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
  3224   3237       }
................................................................................
  3257   3270       if( (u32)pageSize!=pBt->pageSize ){
  3258   3271         /* After reading the first page of the database assuming a page size
  3259   3272         ** of BtShared.pageSize, we have discovered that the page-size is
  3260   3273         ** actually pageSize. Unlock the database, leave pBt->pPage1 at
  3261   3274         ** zero and return SQLITE_OK. The caller will call this function
  3262   3275         ** again with the correct page-size.
  3263   3276         */
  3264         -      releasePage(pPage1);
         3277  +      releasePageOne(pPage1);
  3265   3278         pBt->usableSize = usableSize;
  3266   3279         pBt->pageSize = pageSize;
  3267   3280         freeTempSpace(pBt);
  3268   3281         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
  3269   3282                                      pageSize-usableSize);
  3270   3283         return rc;
  3271   3284       }
................................................................................
  3311   3324     }
  3312   3325     assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
  3313   3326     pBt->pPage1 = pPage1;
  3314   3327     pBt->nPage = nPage;
  3315   3328     return SQLITE_OK;
  3316   3329   
  3317   3330   page1_init_failed:
  3318         -  releasePage(pPage1);
         3331  +  releasePageOne(pPage1);
  3319   3332     pBt->pPage1 = 0;
  3320   3333     return rc;
  3321   3334   }
  3322   3335   
  3323   3336   #ifndef NDEBUG
  3324   3337   /*
  3325   3338   ** Return the number of cursors open on pBt. This is for use
................................................................................
  3356   3369     assert( sqlite3_mutex_held(pBt->mutex) );
  3357   3370     assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
  3358   3371     if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
  3359   3372       MemPage *pPage1 = pBt->pPage1;
  3360   3373       assert( pPage1->aData );
  3361   3374       assert( sqlite3PagerRefcount(pBt->pPager)==1 );
  3362   3375       pBt->pPage1 = 0;
  3363         -    releasePageNotNull(pPage1);
         3376  +    releasePageOne(pPage1);
  3364   3377     }
  3365   3378   }
  3366   3379   
  3367   3380   /*
  3368   3381   ** If pBt points to an empty file then convert that empty file
  3369   3382   ** into a new empty database by initializing the first page of
  3370   3383   ** the database.
................................................................................
  4467   4480       ** sure pPage1->aData is set correctly. */
  4468   4481       if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
  4469   4482         int nPage = get4byte(28+(u8*)pPage1->aData);
  4470   4483         testcase( nPage==0 );
  4471   4484         if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
  4472   4485         testcase( pBt->nPage!=nPage );
  4473   4486         pBt->nPage = nPage;
  4474         -      releasePage(pPage1);
         4487  +      releasePageOne(pPage1);
  4475   4488       }
  4476   4489       assert( countValidCursors(pBt, 1)==0 );
  4477   4490       pBt->inTransaction = TRANS_READ;
  4478   4491       btreeClearHasContent(pBt);
  4479   4492     }
  4480   4493   
  4481   4494     btreeEndTransaction(p);
................................................................................
  5785   5798           }else if( c>0 ){
  5786   5799             upr = idx-1;
  5787   5800           }else{
  5788   5801             assert( c==0 );
  5789   5802             *pRes = 0;
  5790   5803             rc = SQLITE_OK;
  5791   5804             pCur->ix = (u16)idx;
  5792         -          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
         5805  +          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
  5793   5806             goto moveto_finish;
  5794   5807           }
  5795   5808           if( lwr>upr ) break;
  5796   5809           assert( lwr+upr>=0 );
  5797   5810           idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
  5798   5811         }
  5799   5812       }

Changes to src/build.c.

   596    596   ** contains lookaside memory.  (Table objects in the schema do not use
   597    597   ** lookaside memory, but some ephemeral Table objects do.)  Or the
   598    598   ** db parameter can be used with db->pnBytesFreed to measure the memory
   599    599   ** used by the Table object.
   600    600   */
   601    601   static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
   602    602     Index *pIndex, *pNext;
   603         -  TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
   604    603   
          604  +#ifdef SQLITE_DEBUG
   605    605     /* Record the number of outstanding lookaside allocations in schema Tables
   606    606     ** prior to doing any free() operations.  Since schema Tables do not use
   607    607     ** lookaside, this number should not change. */
   608         -  TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
   609         -                         db->lookaside.nOut : 0 );
          608  +  int nLookaside = 0;
          609  +  if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
          610  +    nLookaside = sqlite3LookasideUsed(db, 0);
          611  +  }
          612  +#endif
   610    613   
   611    614     /* Delete all indices associated with this table. */
   612    615     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
   613    616       pNext = pIndex->pNext;
   614    617       assert( pIndex->pSchema==pTable->pSchema
   615    618            || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
   616    619       if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
................................................................................
   636    639     sqlite3ExprListDelete(db, pTable->pCheck);
   637    640   #ifndef SQLITE_OMIT_VIRTUALTABLE
   638    641     sqlite3VtabClear(db, pTable);
   639    642   #endif
   640    643     sqlite3DbFree(db, pTable);
   641    644   
   642    645     /* Verify that no lookaside memory was used by schema tables */
   643         -  assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
          646  +  assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
   644    647   }
   645    648   void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
   646    649     /* Do not delete the table until the reference count reaches zero. */
   647    650     if( !pTable ) return;
   648    651     if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
   649    652     deleteTable(db, pTable);
   650    653   }

Changes to src/expr.c.

  1298   1298   ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
  1299   1299     ExprList *pNew;
  1300   1300     struct ExprList_item *pItem, *pOldItem;
  1301   1301     int i;
  1302   1302     Expr *pPriorSelectCol = 0;
  1303   1303     assert( db!=0 );
  1304   1304     if( p==0 ) return 0;
  1305         -  pNew = sqlite3DbMallocRawNN(db, 
  1306         -             sizeof(*pNew)+sizeof(pNew->a[0])*(p->nExpr-1) );
         1305  +  pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
  1307   1306     if( pNew==0 ) return 0;
  1308         -  pNew->nAlloc = pNew->nExpr = p->nExpr;
         1307  +  pNew->nExpr = p->nExpr;
  1309   1308     pItem = pNew->a;
  1310   1309     pOldItem = p->a;
  1311   1310     for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
  1312   1311       Expr *pOldExpr = pOldItem->pExpr;
  1313   1312       Expr *pNewExpr;
  1314   1313       pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
  1315   1314       if( pOldExpr 
................................................................................
  1455   1454   #endif
  1456   1455   
  1457   1456   
  1458   1457   /*
  1459   1458   ** Add a new element to the end of an expression list.  If pList is
  1460   1459   ** initially NULL, then create a new expression list.
  1461   1460   **
         1461  +** The pList argument must be either NULL or a pointer to an ExprList
         1462  +** obtained from a prior call to sqlite3ExprListAppend().  This routine
         1463  +** may not be used with an ExprList obtained from sqlite3ExprListDup().
         1464  +** Reason:  This routine assumes that the number of slots in pList->a[]
         1465  +** is a power of two.  That is true for sqlite3ExprListAppend() returns
         1466  +** but is not necessarily true from the return value of sqlite3ExprListDup().
         1467  +**
  1462   1468   ** If a memory allocation error occurs, the entire list is freed and
  1463   1469   ** NULL is returned.  If non-NULL is returned, then it is guaranteed
  1464   1470   ** that the new entry was successfully appended.
  1465   1471   */
  1466   1472   ExprList *sqlite3ExprListAppend(
  1467   1473     Parse *pParse,          /* Parsing context */
  1468   1474     ExprList *pList,        /* List to which to append. Might be NULL */
................................................................................
  1473   1479     assert( db!=0 );
  1474   1480     if( pList==0 ){
  1475   1481       pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
  1476   1482       if( pList==0 ){
  1477   1483         goto no_mem;
  1478   1484       }
  1479   1485       pList->nExpr = 0;
  1480         -    pList->nAlloc = 1;
  1481         -  }else if( pList->nExpr==pList->nAlloc ){
         1486  +  }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
  1482   1487       ExprList *pNew;
  1483   1488       pNew = sqlite3DbRealloc(db, pList, 
  1484         -             sizeof(*pList)+(2*pList->nAlloc - 1)*sizeof(pList->a[0]));
         1489  +             sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
  1485   1490       if( pNew==0 ){
  1486   1491         goto no_mem;
  1487   1492       }
  1488   1493       pList = pNew;
  1489         -    pList->nAlloc *= 2;
  1490   1494     }
  1491   1495     pItem = &pList->a[pList->nExpr++];
  1492   1496     assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
  1493   1497     assert( offsetof(struct ExprList_item,pExpr)==0 );
  1494   1498     memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
  1495   1499     pItem->pExpr = pExpr;
  1496   1500     return pList;
................................................................................
  1682   1686          Expr *pExpr = pList->a[i].pExpr;
  1683   1687          assert( pExpr!=0 );
  1684   1688          m |= pExpr->flags;
  1685   1689       }
  1686   1690     }
  1687   1691     return m;
  1688   1692   }
         1693  +
         1694  +/*
         1695  +** This is a SELECT-node callback for the expression walker that
         1696  +** always "fails".  By "fail" in this case, we mean set
         1697  +** pWalker->eCode to zero and abort.
         1698  +**
         1699  +** This callback is used by multiple expression walkers.
         1700  +*/
         1701  +int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
         1702  +  UNUSED_PARAMETER(NotUsed);
         1703  +  pWalker->eCode = 0;
         1704  +  return WRC_Abort;
         1705  +}
  1689   1706   
  1690   1707   /*
  1691   1708   ** These routines are Walker callbacks used to check expressions to
  1692   1709   ** see if they are "constant" for some definition of constant.  The
  1693   1710   ** Walker.eCode value determines the type of "constant" we are looking
  1694   1711   ** for.
  1695   1712   **
................................................................................
  1759   1776           /* A bound parameter in a CREATE statement that originates from
  1760   1777           ** sqlite3_prepare() causes an error */
  1761   1778           pWalker->eCode = 0;
  1762   1779           return WRC_Abort;
  1763   1780         }
  1764   1781         /* Fall through */
  1765   1782       default:
  1766         -      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
  1767         -      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
         1783  +      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail will disallow */
         1784  +      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail will disallow */
  1768   1785         return WRC_Continue;
  1769   1786     }
  1770   1787   }
  1771         -static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
  1772         -  UNUSED_PARAMETER(NotUsed);
  1773         -  pWalker->eCode = 0;
  1774         -  return WRC_Abort;
  1775         -}
  1776   1788   static int exprIsConst(Expr *p, int initFlag, int iCur){
  1777   1789     Walker w;
  1778   1790     w.eCode = initFlag;
  1779   1791     w.xExprCallback = exprNodeIsConstant;
  1780         -  w.xSelectCallback = selectNodeIsConstant;
         1792  +  w.xSelectCallback = sqlite3SelectWalkFail;
  1781   1793   #ifdef SQLITE_DEBUG
  1782   1794     w.xSelectCallback2 = sqlite3SelectWalkAssert2;
  1783   1795   #endif
  1784   1796     w.u.iCur = iCur;
  1785   1797     sqlite3WalkExpr(&w, p);
  1786   1798     return w.eCode;
  1787   1799   }
................................................................................
  1896   1908   ** Walk an expression tree.  Return 1 if the expression contains a
  1897   1909   ** subquery of some kind.  Return 0 if there are no subqueries.
  1898   1910   */
  1899   1911   int sqlite3ExprContainsSubquery(Expr *p){
  1900   1912     Walker w;
  1901   1913     w.eCode = 1;
  1902   1914     w.xExprCallback = sqlite3ExprWalkNoop;
  1903         -  w.xSelectCallback = selectNodeIsConstant;
         1915  +  w.xSelectCallback = sqlite3SelectWalkFail;
  1904   1916   #ifdef SQLITE_DEBUG
  1905   1917     w.xSelectCallback2 = sqlite3SelectWalkAssert2;
  1906   1918   #endif
  1907   1919     sqlite3WalkExpr(&w, p);
  1908   1920     return w.eCode==0;
  1909   1921   }
  1910   1922   #endif
................................................................................
  3060   3072       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  3061   3073     }else{
  3062   3074       int c;
  3063   3075       i64 value;
  3064   3076       const char *z = pExpr->u.zToken;
  3065   3077       assert( z!=0 );
  3066   3078       c = sqlite3DecOrHexToI64(z, &value);
  3067         -    if( c==1 || (c==2 && !negFlag) || (negFlag && value==SMALLEST_INT64)){
         3079  +    if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
  3068   3080   #ifdef SQLITE_OMIT_FLOATING_POINT
  3069   3081         sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
  3070   3082   #else
  3071   3083   #ifndef SQLITE_OMIT_HEX_INTEGER
  3072   3084         if( sqlite3_strnicmp(z,"0x",2)==0 ){
  3073   3085           sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
  3074   3086         }else
  3075   3087   #endif
  3076   3088         {
  3077   3089           codeReal(v, z, negFlag, iMem);
  3078   3090         }
  3079   3091   #endif
  3080   3092       }else{
  3081         -      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
         3093  +      if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
  3082   3094         sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
  3083   3095       }
  3084   3096     }
  3085   3097   }
  3086   3098   
  3087   3099   /*
  3088   3100   ** Erase column-cache entry number i
................................................................................
  4229   4241     exprToRegister(pExpr, iMem);
  4230   4242   }
  4231   4243   
  4232   4244   /*
  4233   4245   ** Generate code that pushes the value of every element of the given
  4234   4246   ** expression list into a sequence of registers beginning at target.
  4235   4247   **
  4236         -** Return the number of elements evaluated.
         4248  +** Return the number of elements evaluated.  The number returned will
         4249  +** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
         4250  +** is defined.
  4237   4251   **
  4238   4252   ** The SQLITE_ECEL_DUP flag prevents the arguments from being
  4239   4253   ** filled using OP_SCopy.  OP_Copy must be used instead.
  4240   4254   **
  4241   4255   ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
  4242   4256   ** factored out into initialization code.
  4243   4257   **
  4244   4258   ** The SQLITE_ECEL_REF flag means that expressions in the list with
  4245   4259   ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
  4246   4260   ** in registers at srcReg, and so the value can be copied from there.
         4261  +** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
         4262  +** are simply omitted rather than being copied from srcReg.
  4247   4263   */
  4248   4264   int sqlite3ExprCodeExprList(
  4249   4265     Parse *pParse,     /* Parsing context */
  4250   4266     ExprList *pList,   /* The expression list to be coded */
  4251   4267     int target,        /* Where to write results */
  4252   4268     int srcReg,        /* Source registers if SQLITE_ECEL_REF */
  4253   4269     u8 flags           /* SQLITE_ECEL_* flags */

Changes to src/main.c.

   654    654   ** space for the lookaside memory is obtained from sqlite3_malloc().
   655    655   ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
   656    656   ** the lookaside memory.
   657    657   */
   658    658   static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
   659    659   #ifndef SQLITE_OMIT_LOOKASIDE
   660    660     void *pStart;
   661         -  if( db->lookaside.nOut ){
          661  +  
          662  +  if( sqlite3LookasideUsed(db,0)>0 ){
   662    663       return SQLITE_BUSY;
   663    664     }
   664    665     /* Free any existing lookaside buffer for this handle before
   665    666     ** allocating a new one so we don't have to have space for 
   666    667     ** both at the same time.
   667    668     */
   668    669     if( db->lookaside.bMalloced ){
................................................................................
   682    683       pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
   683    684       sqlite3EndBenignMalloc();
   684    685       if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
   685    686     }else{
   686    687       pStart = pBuf;
   687    688     }
   688    689     db->lookaside.pStart = pStart;
          690  +  db->lookaside.pInit = 0;
   689    691     db->lookaside.pFree = 0;
   690    692     db->lookaside.sz = (u16)sz;
   691    693     if( pStart ){
   692    694       int i;
   693    695       LookasideSlot *p;
   694    696       assert( sz > (int)sizeof(LookasideSlot*) );
          697  +    db->lookaside.nSlot = cnt;
   695    698       p = (LookasideSlot*)pStart;
   696    699       for(i=cnt-1; i>=0; i--){
   697         -      p->pNext = db->lookaside.pFree;
   698         -      db->lookaside.pFree = p;
          700  +      p->pNext = db->lookaside.pInit;
          701  +      db->lookaside.pInit = p;
   699    702         p = (LookasideSlot*)&((u8*)p)[sz];
   700    703       }
   701    704       db->lookaside.pEnd = p;
   702    705       db->lookaside.bDisable = 0;
   703    706       db->lookaside.bMalloced = pBuf==0 ?1:0;
   704    707     }else{
   705    708       db->lookaside.pStart = db;
   706    709       db->lookaside.pEnd = db;
   707    710       db->lookaside.bDisable = 1;
   708    711       db->lookaside.bMalloced = 0;
          712  +    db->lookaside.nSlot = 0;
   709    713     }
   710    714   #endif /* SQLITE_OMIT_LOOKASIDE */
   711    715     return SQLITE_OK;
   712    716   }
   713    717   
   714    718   /*
   715    719   ** Return the mutex associated with a database connection.
................................................................................
  1221   1225     ** the same sqliteMalloc() as the one that allocates the database 
  1222   1226     ** structure?
  1223   1227     */
  1224   1228     sqlite3DbFree(db, db->aDb[1].pSchema);
  1225   1229     sqlite3_mutex_leave(db->mutex);
  1226   1230     db->magic = SQLITE_MAGIC_CLOSED;
  1227   1231     sqlite3_mutex_free(db->mutex);
  1228         -  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
         1232  +  assert( sqlite3LookasideUsed(db,0)==0 );
  1229   1233     if( db->lookaside.bMalloced ){
  1230   1234       sqlite3_free(db->lookaside.pStart);
  1231   1235     }
  1232   1236     sqlite3_free(db);
  1233   1237   }
  1234   1238   
  1235   1239   /*
................................................................................
  2165   2169   ** associated with the specific b-tree being checkpointed is taken by
  2166   2170   ** this function while the checkpoint is running.
  2167   2171   **
  2168   2172   ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
  2169   2173   ** checkpointed. If an error is encountered it is returned immediately -
  2170   2174   ** no attempt is made to checkpoint any remaining databases.
  2171   2175   **
  2172         -** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
         2176  +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
         2177  +** or TRUNCATE.
  2173   2178   */
  2174   2179   int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
  2175   2180     int rc = SQLITE_OK;             /* Return code */
  2176   2181     int i;                          /* Used to iterate through attached dbs */
  2177   2182     int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
  2178   2183   
  2179   2184     assert( sqlite3_mutex_held(db->mutex) );
................................................................................
  3933   3938   sqlite3_int64 sqlite3_uri_int64(
  3934   3939     const char *zFilename,    /* Filename as passed to xOpen */
  3935   3940     const char *zParam,       /* URI parameter sought */
  3936   3941     sqlite3_int64 bDflt       /* return if parameter is missing */
  3937   3942   ){
  3938   3943     const char *z = sqlite3_uri_parameter(zFilename, zParam);
  3939   3944     sqlite3_int64 v;
  3940         -  if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
         3945  +  if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
  3941   3946       bDflt = v;
  3942   3947     }
  3943   3948     return bDflt;
  3944   3949   }
  3945   3950   
  3946   3951   /*
  3947   3952   ** Return the Btree pointer identified by zDbName.  Return NULL if not found.

Changes to src/malloc.c.

   350    350         LookasideSlot *pBuf = (LookasideSlot*)p;
   351    351   #ifdef SQLITE_DEBUG
   352    352         /* Trash all content in the buffer being freed */
   353    353         memset(p, 0xaa, db->lookaside.sz);
   354    354   #endif
   355    355         pBuf->pNext = db->lookaside.pFree;
   356    356         db->lookaside.pFree = pBuf;
   357         -      db->lookaside.nOut--;
   358    357         return;
   359    358       }
   360    359     }
   361    360     assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   362    361     assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   363    362     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   364    363     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
................................................................................
   511    510     assert( db!=0 );
   512    511     assert( sqlite3_mutex_held(db->mutex) );
   513    512     assert( db->pnBytesFreed==0 );
   514    513     if( db->lookaside.bDisable==0 ){
   515    514       assert( db->mallocFailed==0 );
   516    515       if( n>db->lookaside.sz ){
   517    516         db->lookaside.anStat[1]++;
   518         -    }else if( (pBuf = db->lookaside.pFree)==0 ){
   519         -      db->lookaside.anStat[2]++;
   520         -    }else{
          517  +    }else if( (pBuf = db->lookaside.pFree)!=0 ){
   521    518         db->lookaside.pFree = pBuf->pNext;
   522         -      db->lookaside.nOut++;
          519  +      db->lookaside.anStat[0]++;
          520  +      return (void*)pBuf;
          521  +    }else if( (pBuf = db->lookaside.pInit)!=0 ){
          522  +      db->lookaside.pInit = pBuf->pNext;
   523    523         db->lookaside.anStat[0]++;
   524         -      if( db->lookaside.nOut>db->lookaside.mxOut ){
   525         -        db->lookaside.mxOut = db->lookaside.nOut;
   526         -      }
   527    524         return (void*)pBuf;
          525  +    }else{
          526  +      db->lookaside.anStat[2]++;
   528    527       }
   529    528     }else if( db->mallocFailed ){
   530    529       return 0;
   531    530     }
   532    531   #else
   533    532     assert( db!=0 );
   534    533     assert( sqlite3_mutex_held(db->mutex) );

Changes to src/os_unix.c.

  5296   5296   ){
  5297   5297     const sqlite3_io_methods *pLockingStyle;
  5298   5298     unixFile *pNew = (unixFile *)pId;
  5299   5299     int rc = SQLITE_OK;
  5300   5300   
  5301   5301     assert( pNew->pInode==NULL );
  5302   5302   
  5303         -  /* Usually the path zFilename should not be a relative pathname. The
  5304         -  ** exception is when opening the proxy "conch" file in builds that
  5305         -  ** include the special Apple locking styles.
  5306         -  */
  5307         -#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  5308         -  assert( zFilename==0 || zFilename[0]=='/' 
  5309         -    || pVfs->pAppData==(void*)&autolockIoFinder );
  5310         -#else
  5311         -  assert( zFilename==0 || zFilename[0]=='/' );
  5312         -#endif
  5313         -
  5314   5303     /* No locking occurs in temporary files */
  5315   5304     assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
  5316   5305   
  5317   5306     OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
  5318   5307     pNew->h = h;
  5319   5308     pNew->pVfs = pVfs;
  5320   5309     pNew->zPath = zFilename;
................................................................................
  5669   5658       **   "<path to db>-walNN"
  5670   5659       **
  5671   5660       ** where NN is a decimal number. The NN naming schemes are 
  5672   5661       ** used by the test_multiplex.c module.
  5673   5662       */
  5674   5663       nDb = sqlite3Strlen30(zPath) - 1; 
  5675   5664       while( zPath[nDb]!='-' ){
  5676         -#ifndef SQLITE_ENABLE_8_3_NAMES
  5677         -      /* In the normal case (8+3 filenames disabled) the journal filename
  5678         -      ** is guaranteed to contain a '-' character. */
  5679         -      assert( nDb>0 );
  5680         -      assert( sqlite3Isalnum(zPath[nDb]) );
  5681         -#else
  5682         -      /* If 8+3 names are possible, then the journal file might not contain
  5683         -      ** a '-' character.  So check for that case and return early. */
         5665  +      /* In normal operation, the journal file name will always contain
         5666  +      ** a '-' character.  However in 8+3 filename mode, or if a corrupt
         5667  +      ** rollback journal specifies a master journal with a goofy name, then
         5668  +      ** the '-' might be missing. */
  5684   5669         if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
  5685         -#endif
  5686   5670         nDb--;
  5687   5671       }
  5688   5672       memcpy(zDb, zPath, nDb);
  5689   5673       zDb[nDb] = '\0';
  5690   5674   
  5691   5675       rc = getFileMode(zDb, pMode, pUid, pGid);
  5692   5676     }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
................................................................................
  5964   5948           }
  5965   5949         }
  5966   5950         goto open_finished;
  5967   5951       }
  5968   5952     }
  5969   5953   #endif
  5970   5954     
         5955  +  assert( zPath==0 || zPath[0]=='/' 
         5956  +      || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL 
         5957  +  );
  5971   5958     rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
  5972   5959   
  5973   5960   open_finished:
  5974   5961     if( rc!=SQLITE_OK ){
  5975   5962       sqlite3_free(p->pPreallocatedUnused);
  5976   5963     }
  5977   5964     return rc;

Changes to src/os_win.c.

  4875   4875     }else{
  4876   4876       attr = osGetFileAttributesA((char*)zConverted);
  4877   4877   #endif
  4878   4878     }
  4879   4879     return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
  4880   4880   }
  4881   4881   
         4882  +/* forward reference */
         4883  +static int winAccess(
         4884  +  sqlite3_vfs *pVfs,         /* Not used on win32 */
         4885  +  const char *zFilename,     /* Name of file to check */
         4886  +  int flags,                 /* Type of test to make on this file */
         4887  +  int *pResOut               /* OUT: Result */
         4888  +);
         4889  +
  4882   4890   /*
  4883   4891   ** Open a file.
  4884   4892   */
  4885   4893   static int winOpen(
  4886   4894     sqlite3_vfs *pVfs,        /* Used to get maximum path length and AppData */
  4887   4895     const char *zName,        /* Name of the file (UTF-8) */
  4888   4896     sqlite3_file *id,         /* Write the SQLite file handle here */
................................................................................
  5060   5068                                 dwShareMode,
  5061   5069                                 dwCreationDisposition,
  5062   5070                                 &extendedParameters))==INVALID_HANDLE_VALUE &&
  5063   5071                                 winRetryIoerr(&cnt, &lastErrno) ){
  5064   5072                  /* Noop */
  5065   5073       }
  5066   5074   #else
  5067         -    while( (h = osCreateFileW((LPCWSTR)zConverted,
  5068         -                              dwDesiredAccess,
  5069         -                              dwShareMode, NULL,
  5070         -                              dwCreationDisposition,
  5071         -                              dwFlagsAndAttributes,
  5072         -                              NULL))==INVALID_HANDLE_VALUE &&
  5073         -                              winRetryIoerr(&cnt, &lastErrno) ){
  5074         -               /* Noop */
  5075         -    }
         5075  +    do{
         5076  +      h = osCreateFileW((LPCWSTR)zConverted,
         5077  +                        dwDesiredAccess,
         5078  +                        dwShareMode, NULL,
         5079  +                        dwCreationDisposition,
         5080  +                        dwFlagsAndAttributes,
         5081  +                        NULL);
         5082  +      if( h!=INVALID_HANDLE_VALUE ) break;
         5083  +      if( isReadWrite ){
         5084  +        int isRO = 0;
         5085  +        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
         5086  +        if( rc2==SQLITE_OK && isRO ) break;
         5087  +      }
         5088  +    }while( winRetryIoerr(&cnt, &lastErrno) );
  5076   5089   #endif
  5077   5090     }
  5078   5091   #ifdef SQLITE_WIN32_HAS_ANSI
  5079   5092     else{
  5080   5093       while( (h = osCreateFileA((LPCSTR)zConverted,
  5081   5094                                 dwDesiredAccess,
  5082   5095                                 dwShareMode, NULL,
................................................................................
  5090   5103   #endif
  5091   5104     winLogIoerr(cnt, __LINE__);
  5092   5105   
  5093   5106     OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
  5094   5107              dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
  5095   5108   
  5096   5109     if( h==INVALID_HANDLE_VALUE ){
  5097         -    pFile->lastErrno = lastErrno;
  5098         -    winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
  5099   5110       sqlite3_free(zConverted);
  5100   5111       sqlite3_free(zTmpname);
  5101   5112       if( isReadWrite && !isExclusive ){
  5102   5113         return winOpen(pVfs, zName, id,
  5103   5114            ((flags|SQLITE_OPEN_READONLY) &
  5104   5115                        ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  5105   5116            pOutFlags);
  5106   5117       }else{
         5118  +      pFile->lastErrno = lastErrno;
         5119  +      winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
  5107   5120         return SQLITE_CANTOPEN_BKPT;
  5108   5121       }
  5109   5122     }
  5110   5123   
  5111   5124     if( pOutFlags ){
  5112   5125       if( isReadWrite ){
  5113   5126         *pOutFlags = SQLITE_OPEN_READWRITE;
................................................................................
  5692   5705     UNUSED_PARAMETER(pVfs);
  5693   5706     memset(zBuf, 0, nBuf);
  5694   5707     return nBuf;
  5695   5708   #else
  5696   5709     EntropyGatherer e;
  5697   5710     UNUSED_PARAMETER(pVfs);
  5698   5711     memset(zBuf, 0, nBuf);
  5699         -#if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE
  5700         -  rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */
  5701         -#endif /* defined(_MSC_VER) && _MSC_VER>=1400 */
  5702   5712     e.a = (unsigned char*)zBuf;
  5703   5713     e.na = nBuf;
  5704   5714     e.nXor = 0;
  5705   5715     e.i = 0;
  5706   5716     {
  5707   5717       SYSTEMTIME x;
  5708   5718       osGetSystemTime(&x);

Changes to src/pager.c.

  1316   1316     unsigned char aMagic[8];   /* A buffer to hold the magic header */
  1317   1317     zMaster[0] = '\0';
  1318   1318   
  1319   1319     if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
  1320   1320      || szJ<16
  1321   1321      || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
  1322   1322      || len>=nMaster 
         1323  +   || len>szJ-16
  1323   1324      || len==0 
  1324   1325      || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
  1325   1326      || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
  1326   1327      || memcmp(aMagic, aJournalMagic, 8)
  1327   1328      || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
  1328   1329     ){
  1329   1330       return rc;
................................................................................
  2852   2853     u32 u;                   /* Unsigned loop counter */
  2853   2854     Pgno mxPg = 0;           /* Size of the original file in pages */
  2854   2855     int rc;                  /* Result code of a subroutine */
  2855   2856     int res = 1;             /* Value returned by sqlite3OsAccess() */
  2856   2857     char *zMaster = 0;       /* Name of master journal file if any */
  2857   2858     int needPagerReset;      /* True to reset page prior to first page rollback */
  2858   2859     int nPlayback = 0;       /* Total number of pages restored from journal */
         2860  +  u32 savedPageSize = pPager->pageSize;
  2859   2861   
  2860   2862     /* Figure out how many records are in the journal.  Abort early if
  2861   2863     ** the journal is empty.
  2862   2864     */
  2863   2865     assert( isOpen(pPager->jfd) );
  2864   2866     rc = sqlite3OsFileSize(pPager->jfd, &szJ);
  2865   2867     if( rc!=SQLITE_OK ){
................................................................................
  2981   2983         }
  2982   2984       }
  2983   2985     }
  2984   2986     /*NOTREACHED*/
  2985   2987     assert( 0 );
  2986   2988   
  2987   2989   end_playback:
         2990  +  if( rc==SQLITE_OK ){
         2991  +    rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
         2992  +  }
  2988   2993     /* Following a rollback, the database file should be back in its original
  2989   2994     ** state prior to the start of the transaction, so invoke the
  2990   2995     ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
  2991   2996     ** assertion that the transaction counter was modified.
  2992   2997     */
  2993   2998   #ifdef SQLITE_DEBUG
  2994   2999     if( pPager->fd->pMethods ){
................................................................................
  5414   5419   ** transaction and unlock the pager.
  5415   5420   **
  5416   5421   ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
  5417   5422   ** the rollback journal, the unlock is not performed and there is
  5418   5423   ** nothing to rollback, so this routine is a no-op.
  5419   5424   */ 
  5420   5425   static void pagerUnlockIfUnused(Pager *pPager){
  5421         -  if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
         5426  +  if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
         5427  +    assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
  5422   5428       pagerUnlockAndRollback(pPager);
  5423   5429     }
  5424   5430   }
  5425   5431   
  5426   5432   /*
  5427   5433   ** The page getter methods each try to acquire a reference to a
  5428   5434   ** page with page number pgno. If the requested reference is 
................................................................................
  5712   5718     if( pPage==0 ) return 0;
  5713   5719     return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
  5714   5720   }
  5715   5721   
  5716   5722   /*
  5717   5723   ** Release a page reference.
  5718   5724   **
  5719         -** If the number of references to the page drop to zero, then the
  5720         -** page is added to the LRU list.  When all references to all pages
  5721         -** are released, a rollback occurs and the lock on the database is
  5722         -** removed.
         5725  +** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
         5726  +** used if we know that the page being released is not the last page.
         5727  +** The btree layer always holds page1 open until the end, so these first
         5728  +** to routines can be used to release any page other than BtShared.pPage1.
         5729  +**
         5730  +** Use sqlite3PagerUnrefPageOne() to release page1.  This latter routine
         5731  +** checks the total number of outstanding pages and if the number of
         5732  +** pages reaches zero it drops the database lock.
  5723   5733   */
  5724   5734   void sqlite3PagerUnrefNotNull(DbPage *pPg){
  5725         -  Pager *pPager;
         5735  +  TESTONLY( Pager *pPager = pPg->pPager; )
  5726   5736     assert( pPg!=0 );
  5727         -  pPager = pPg->pPager;
  5728   5737     if( pPg->flags & PGHDR_MMAP ){
         5738  +    assert( pPg->pgno!=1 );  /* Page1 is never memory mapped */
  5729   5739       pagerReleaseMapPage(pPg);
  5730   5740     }else{
  5731   5741       sqlite3PcacheRelease(pPg);
  5732   5742     }
  5733         -  pagerUnlockIfUnused(pPager);
         5743  +  /* Do not use this routine to release the last reference to page1 */
         5744  +  assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
  5734   5745   }
  5735   5746   void sqlite3PagerUnref(DbPage *pPg){
  5736   5747     if( pPg ) sqlite3PagerUnrefNotNull(pPg);
         5748  +}
         5749  +void sqlite3PagerUnrefPageOne(DbPage *pPg){
         5750  +  Pager *pPager;
         5751  +  assert( pPg!=0 );
         5752  +  assert( pPg->pgno==1 );
         5753  +  assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
         5754  +  pPager = pPg->pPager;
         5755  +  sqlite3PcacheRelease(pPg);
         5756  +  pagerUnlockIfUnused(pPager);
  5737   5757   }
  5738   5758   
  5739   5759   /*
  5740   5760   ** This function is called at the start of every write transaction.
  5741   5761   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
  5742   5762   ** file when this routine is called.
  5743   5763   **

Changes to src/pager.h.

   147    147   
   148    148   /* Functions used to obtain and release page references. */ 
   149    149   int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
   150    150   DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
   151    151   void sqlite3PagerRef(DbPage*);
   152    152   void sqlite3PagerUnref(DbPage*);
   153    153   void sqlite3PagerUnrefNotNull(DbPage*);
          154  +void sqlite3PagerUnrefPageOne(DbPage*);
   154    155   
   155    156   /* Operations on page references. */
   156    157   int sqlite3PagerWrite(DbPage*);
   157    158   void sqlite3PagerDontWrite(DbPage*);
   158    159   int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
   159    160   int sqlite3PagerPageRefcount(DbPage*);
   160    161   void *sqlite3PagerGetData(DbPage *); 

Changes to src/pcache.c.

   187    187         p->pDirty = pPage->pDirtyNext;
   188    188         assert( p->bPurgeable || p->eCreate==2 );
   189    189         if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
   190    190           assert( p->bPurgeable==0 || p->eCreate==1 );
   191    191           p->eCreate = 2;
   192    192         }
   193    193       }
   194         -    pPage->pDirtyNext = 0;
   195         -    pPage->pDirtyPrev = 0;
   196    194     }
   197    195     if( addRemove & PCACHE_DIRTYLIST_ADD ){
   198         -    assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
   199         -  
          196  +    pPage->pDirtyPrev = 0;
   200    197       pPage->pDirtyNext = p->pDirty;
   201    198       if( pPage->pDirtyNext ){
   202    199         assert( pPage->pDirtyNext->pDirtyPrev==0 );
   203    200         pPage->pDirtyNext->pDirtyPrev = pPage;
   204    201       }else{
   205    202         p->pDirtyTail = pPage;
   206    203         if( p->bPurgeable ){
................................................................................
   509    506   */
   510    507   void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
   511    508     assert( p->nRef>0 );
   512    509     p->pCache->nRefSum--;
   513    510     if( (--p->nRef)==0 ){
   514    511       if( p->flags&PGHDR_CLEAN ){
   515    512         pcacheUnpin(p);
   516         -    }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
   517         -      /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
   518         -      ** then page p is already at the head of the dirty list and the
   519         -      ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
   520         -      ** tag above.  */
          513  +    }else{
   521    514         pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   522    515       }
   523    516     }
   524    517   }
   525    518   
   526    519   /*
   527    520   ** Increase the reference count of a supplied page by 1.

Changes to src/pcache.h.

    39     39     ** Elements above, except pCache, are public.  All that follow are 
    40     40     ** private to pcache.c and should not be accessed by other modules.
    41     41     ** pCache is grouped with the public elements for efficiency.
    42     42     */
    43     43     i16 nRef;                      /* Number of users of this page */
    44     44     PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
    45     45     PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
           46  +                          /* NB: pDirtyNext and pDirtyPrev are undefined if the
           47  +                          ** PgHdr object is not dirty */
    46     48   };
    47     49   
    48     50   /* Bit values for PgHdr.flags */
    49     51   #define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
    50     52   #define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
    51     53   #define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
    52     54   #define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before

Changes to src/pcache1.c.

   133    133   ** SQLITE_MUTEX_STATIC_LRU.
   134    134   */
   135    135   struct PGroup {
   136    136     sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
   137    137     unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
   138    138     unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
   139    139     unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
   140         -  unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
          140  +  unsigned int nPurgeable;       /* Number of purgeable pages allocated */
   141    141     PgHdr1 lru;                    /* The beginning and end of the LRU list */
   142    142   };
   143    143   
   144    144   /* Each page cache is an instance of the following object.  Every
   145    145   ** open database file (including each in-memory database and each
   146    146   ** temporary or transient database) has a single page cache which
   147    147   ** is an instance of this object.
   148    148   **
   149    149   ** Pointers to structures of this type are cast and returned as 
   150    150   ** opaque sqlite3_pcache* handles.
   151    151   */
   152    152   struct PCache1 {
   153    153     /* Cache configuration parameters. Page size (szPage) and the purgeable
   154         -  ** flag (bPurgeable) are set when the cache is created. nMax may be 
          154  +  ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
          155  +  ** cache is created and are never changed thereafter. nMax may be 
   155    156     ** modified at any time by a call to the pcache1Cachesize() method.
   156    157     ** The PGroup mutex must be held when accessing nMax.
   157    158     */
   158    159     PGroup *pGroup;                     /* PGroup this cache belongs to */
          160  +  unsigned int *pnPurgeable;          /* Pointer to pGroup->nPurgeable */
   159    161     int szPage;                         /* Size of database content section */
   160    162     int szExtra;                        /* sizeof(MemPage)+sizeof(PgHdr) */
   161    163     int szAlloc;                        /* Total size of one pcache line */
   162    164     int bPurgeable;                     /* True if cache is purgeable */
   163    165     unsigned int nMin;                  /* Minimum number of pages reserved */
   164    166     unsigned int nMax;                  /* Configured "cache_size" value */
   165    167     unsigned int n90pct;                /* nMax*9/10 */
................................................................................
   439    441   #endif
   440    442       if( pPg==0 ) return 0;
   441    443       p->page.pBuf = pPg;
   442    444       p->page.pExtra = &p[1];
   443    445       p->isBulkLocal = 0;
   444    446       p->isAnchor = 0;
   445    447     }
   446         -  if( pCache->bPurgeable ){
   447         -    pCache->pGroup->nCurrentPage++;
   448         -  }
          448  +  (*pCache->pnPurgeable)++;
   449    449     return p;
   450    450   }
   451    451   
   452    452   /*
   453    453   ** Free a page object allocated by pcache1AllocPage().
   454    454   */
   455    455   static void pcache1FreePage(PgHdr1 *p){
................................................................................
   462    462       pCache->pFree = p;
   463    463     }else{
   464    464       pcache1Free(p->page.pBuf);
   465    465   #ifdef SQLITE_PCACHE_SEPARATE_HEADER
   466    466       sqlite3_free(p);
   467    467   #endif
   468    468     }
   469         -  if( pCache->bPurgeable ){
   470         -    pCache->pGroup->nCurrentPage--;
   471         -  }
          469  +  (*pCache->pnPurgeable)--;
   472    470   }
   473    471   
   474    472   /*
   475    473   ** Malloc function used by SQLite to obtain space from the buffer configured
   476    474   ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
   477    475   ** exists, this function falls back to sqlite3Malloc().
   478    476   */
................................................................................
   604    602   ** If there are currently more than nMaxPage pages allocated, try
   605    603   ** to recycle pages to reduce the number allocated to nMaxPage.
   606    604   */
   607    605   static void pcache1EnforceMaxPage(PCache1 *pCache){
   608    606     PGroup *pGroup = pCache->pGroup;
   609    607     PgHdr1 *p;
   610    608     assert( sqlite3_mutex_held(pGroup->mutex) );
   611         -  while( pGroup->nCurrentPage>pGroup->nMaxPage
          609  +  while( pGroup->nPurgeable>pGroup->nMaxPage
   612    610         && (p=pGroup->lru.pLruPrev)->isAnchor==0
   613    611     ){
   614    612       assert( p->pCache->pGroup==pGroup );
   615    613       assert( PAGE_IS_UNPINNED(p) );
   616    614       pcache1PinPage(p);
   617    615       pcache1RemoveFromHash(p, 1);
   618    616     }
................................................................................
   775    773       pCache->bPurgeable = (bPurgeable ? 1 : 0);
   776    774       pcache1EnterMutex(pGroup);
   777    775       pcache1ResizeHash(pCache);
   778    776       if( bPurgeable ){
   779    777         pCache->nMin = 10;
   780    778         pGroup->nMinPage += pCache->nMin;
   781    779         pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
          780  +      pCache->pnPurgeable = &pGroup->nPurgeable;
          781  +    }else{
          782  +      static unsigned int dummyCurrentPage;
          783  +      pCache->pnPurgeable = &dummyCurrentPage;
   782    784       }
   783    785       pcache1LeaveMutex(pGroup);
   784    786       if( pCache->nHash==0 ){
   785    787         pcache1Destroy((sqlite3_pcache*)pCache);
   786    788         pCache = 0;
   787    789       }
   788    790     }
................................................................................
   884    886       pcache1RemoveFromHash(pPage, 0);
   885    887       pcache1PinPage(pPage);
   886    888       pOther = pPage->pCache;
   887    889       if( pOther->szAlloc != pCache->szAlloc ){
   888    890         pcache1FreePage(pPage);
   889    891         pPage = 0;
   890    892       }else{
   891         -      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
          893  +      pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
   892    894       }
   893    895     }
   894    896   
   895    897     /* Step 5. If a usable page buffer has still not been found, 
   896    898     ** attempt to allocate a new one. 
   897    899     */
   898    900     if( !pPage ){
................................................................................
  1065   1067   
  1066   1068     /* It is an error to call this function if the page is already 
  1067   1069     ** part of the PGroup LRU list.
  1068   1070     */
  1069   1071     assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
  1070   1072     assert( PAGE_IS_PINNED(pPage) );
  1071   1073   
  1072         -  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
         1074  +  if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
  1073   1075       pcache1RemoveFromHash(pPage, 1);
  1074   1076     }else{
  1075   1077       /* Add the page to the PGroup LRU list. */
  1076   1078       PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
  1077   1079       pPage->pLruPrev = &pGroup->lru;
  1078   1080       (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
  1079   1081       *ppFirst = pPage;
................................................................................
  1244   1246   ){
  1245   1247     PgHdr1 *p;
  1246   1248     int nRecyclable = 0;
  1247   1249     for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
  1248   1250       assert( PAGE_IS_UNPINNED(p) );
  1249   1251       nRecyclable++;
  1250   1252     }
  1251         -  *pnCurrent = pcache1.grp.nCurrentPage;
         1253  +  *pnCurrent = pcache1.grp.nPurgeable;
  1252   1254     *pnMax = (int)pcache1.grp.nMaxPage;
  1253   1255     *pnMin = (int)pcache1.grp.nMinPage;
  1254   1256     *pnRecyclable = nRecyclable;
  1255   1257   }
  1256   1258   #endif

Changes to src/pragma.c.

   294    294     }
   295    295     return lwr>upr ? 0 : &aPragmaName[mid];
   296    296   }
   297    297   
   298    298   /*
   299    299   ** Helper subroutine for PRAGMA integrity_check:
   300    300   **
   301         -** Generate code to output a single-column result row with the result
   302         -** held in register regResult.  Decrement the result count and halt if
   303         -** the maximum number of result rows have been issued.
          301  +** Generate code to output a single-column result row with a value of the
          302  +** string held in register 3.  Decrement the result count in register 1
          303  +** and halt if the maximum number of result rows have been issued.
   304    304   */
   305         -static int integrityCheckResultRow(Vdbe *v, int regResult){
          305  +static int integrityCheckResultRow(Vdbe *v){
   306    306     int addr;
   307         -  sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
          307  +  sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
   308    308     addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
   309    309     VdbeCoverage(v);
   310         -  sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
          310  +  sqlite3VdbeAddOp0(v, OP_Halt);
   311    311     return addr;
   312    312   }
   313    313   
   314    314   /*
   315    315   ** Process a pragma statement.  
   316    316   **
   317    317   ** Pragmas are of this form:
................................................................................
  1480   1480           mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  1481   1481         }
  1482   1482       }
  1483   1483       sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
  1484   1484   
  1485   1485       /* Do an integrity check on each database file */
  1486   1486       for(i=0; i<db->nDb; i++){
  1487         -      HashElem *x;
  1488         -      Hash *pTbls;
  1489         -      int *aRoot;
  1490         -      int cnt = 0;
  1491         -      int mxIdx = 0;
  1492         -      int nIdx;
         1487  +      HashElem *x;     /* For looping over tables in the schema */
         1488  +      Hash *pTbls;     /* Set of all tables in the schema */
         1489  +      int *aRoot;      /* Array of root page numbers of all btrees */
         1490  +      int cnt = 0;     /* Number of entries in aRoot[] */
         1491  +      int mxIdx = 0;   /* Maximum number of indexes for any table */
  1493   1492   
  1494   1493         if( OMIT_TEMPDB && i==1 ) continue;
  1495   1494         if( iDb>=0 && i!=iDb ) continue;
  1496   1495   
  1497   1496         sqlite3CodeVerifySchema(pParse, i);
  1498   1497   
  1499   1498         /* Do an integrity check of the B-Tree
................................................................................
  1500   1499         **
  1501   1500         ** Begin by finding the root pages numbers
  1502   1501         ** for all tables and indices in the database.
  1503   1502         */
  1504   1503         assert( sqlite3SchemaMutexHeld(db, i, 0) );
  1505   1504         pTbls = &db->aDb[i].pSchema->tblHash;
  1506   1505         for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1507         -        Table *pTab = sqliteHashData(x);
  1508         -        Index *pIdx;
         1506  +        Table *pTab = sqliteHashData(x);  /* Current table */
         1507  +        Index *pIdx;                      /* An index on pTab */
         1508  +        int nIdx;                         /* Number of indexes on pTab */
  1509   1509           if( HasRowid(pTab) ) cnt++;
  1510   1510           for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
  1511   1511           if( nIdx>mxIdx ) mxIdx = nIdx;
  1512   1512         }
  1513   1513         aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
  1514   1514         if( aRoot==0 ) break;
  1515   1515         for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1516   1516           Table *pTab = sqliteHashData(x);
  1517   1517           Index *pIdx;
  1518         -        if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
         1518  +        if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
  1519   1519           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1520         -          aRoot[cnt++] = pIdx->tnum;
         1520  +          aRoot[++cnt] = pIdx->tnum;
  1521   1521           }
  1522   1522         }
  1523         -      aRoot[cnt] = 0;
         1523  +      aRoot[0] = cnt;
  1524   1524   
  1525   1525         /* Make sure sufficient number of registers have been allocated */
  1526   1526         pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
  1527   1527         sqlite3ClearTempRegCache(pParse);
  1528   1528   
  1529   1529         /* Do the b-tree integrity checks */
  1530   1530         sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
  1531   1531         sqlite3VdbeChangeP5(v, (u8)i);
  1532   1532         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
  1533   1533         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1534   1534            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
  1535   1535            P4_DYNAMIC);
  1536         -      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
  1537         -      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1538         -      integrityCheckResultRow(v, 2);
         1536  +      sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
         1537  +      integrityCheckResultRow(v);
  1539   1538         sqlite3VdbeJumpHere(v, addr);
  1540   1539   
  1541   1540         /* Make sure all the indices are constructed correctly.
  1542   1541         */
  1543   1542         for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1544   1543           Table *pTab = sqliteHashData(x);
  1545   1544           Index *pIdx, *pPk;
  1546   1545           Index *pPrior = 0;
  1547   1546           int loopTop;
  1548   1547           int iDataCur, iIdxCur;
  1549   1548           int r1 = -1;
  1550   1549   
  1551   1550           if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
  1552         -        if( pTab->pCheck==0
  1553         -         && (pTab->tabFlags & TF_HasNotNull)==0
  1554         -         && (pTab->pIndex==0 || isQuick)
  1555         -        ){
  1556         -          continue;  /* No additional checks needed for this table */
  1557         -        }
  1558   1551           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1559   1552           sqlite3ExprCacheClear(pParse);
  1560   1553           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
  1561   1554                                      1, 0, &iDataCur, &iIdxCur);
         1555  +        /* reg[7] counts the number of entries in the table.
         1556  +        ** reg[8+i] counts the number of entries in the i-th index 
         1557  +        */
  1562   1558           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1563   1559           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1564   1560             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1565   1561           }
  1566   1562           assert( pParse->nMem>=8+j );
  1567   1563           assert( sqlite3NoTempsInRange(pParse,1,7+j) );
  1568   1564           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
................................................................................
  1575   1571             if( pTab->aCol[j].notNull==0 ) continue;
  1576   1572             sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
  1577   1573             sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
  1578   1574             jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
  1579   1575             zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
  1580   1576                                 pTab->aCol[j].zName);
  1581   1577             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
  1582         -          integrityCheckResultRow(v, 3);
         1578  +          integrityCheckResultRow(v);
  1583   1579             sqlite3VdbeJumpHere(v, jmp2);
  1584   1580           }
  1585   1581           /* Verify CHECK constraints */
  1586   1582           if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
  1587   1583             ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
  1588   1584             if( db->mallocFailed==0 ){
  1589   1585               int addrCkFault = sqlite3VdbeMakeLabel(v);
................................................................................
  1598   1594               sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
  1599   1595                   SQLITE_JUMPIFNULL);
  1600   1596               sqlite3VdbeResolveLabel(v, addrCkFault);
  1601   1597               pParse->iSelfTab = 0;
  1602   1598               zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
  1603   1599                   pTab->zName);
  1604   1600               sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
  1605         -            integrityCheckResultRow(v, 3);
         1601  +            integrityCheckResultRow(v);
  1606   1602               sqlite3VdbeResolveLabel(v, addrCkOk);
  1607   1603               sqlite3ExprCachePop(pParse);
  1608   1604             }
  1609   1605             sqlite3ExprListDelete(db, pCheck);
  1610   1606           }
  1611         -        /* Validate index entries for the current row */
  1612         -        for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
  1613         -          int jmp2, jmp3, jmp4, jmp5;
  1614         -          int ckUniq = sqlite3VdbeMakeLabel(v);
  1615         -          if( pPk==pIdx ) continue;
  1616         -          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
  1617         -                                       pPrior, r1);
  1618         -          pPrior = pIdx;
  1619         -          sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
  1620         -          /* Verify that an index entry exists for the current table row */
  1621         -          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
  1622         -                                      pIdx->nColumn); VdbeCoverage(v);
  1623         -          sqlite3VdbeLoadString(v, 3, "row ");
  1624         -          sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
  1625         -          sqlite3VdbeLoadString(v, 4, " missing from index ");
  1626         -          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1627         -          jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
  1628         -          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1629         -          jmp4 = integrityCheckResultRow(v, 3);
  1630         -          sqlite3VdbeJumpHere(v, jmp2);
  1631         -          /* For UNIQUE indexes, verify that only one entry exists with the
  1632         -          ** current key.  The entry is unique if (1) any column is NULL
  1633         -          ** or (2) the next entry has a different key */
  1634         -          if( IsUniqueIndex(pIdx) ){
  1635         -            int uniqOk = sqlite3VdbeMakeLabel(v);
  1636         -            int jmp6;
  1637         -            int kk;
  1638         -            for(kk=0; kk<pIdx->nKeyCol; kk++){
  1639         -              int iCol = pIdx->aiColumn[kk];
  1640         -              assert( iCol!=XN_ROWID && iCol<pTab->nCol );
  1641         -              if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
  1642         -              sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
  1643         -              VdbeCoverage(v);
         1607  +        if( !isQuick ){ /* Omit the remaining tests for quick_check */
         1608  +          /* Sanity check on record header decoding */
         1609  +          sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
         1610  +          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
         1611  +          /* Validate index entries for the current row */
         1612  +          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
         1613  +            int jmp2, jmp3, jmp4, jmp5;
         1614  +            int ckUniq = sqlite3VdbeMakeLabel(v);
         1615  +            if( pPk==pIdx ) continue;
         1616  +            r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
         1617  +                                         pPrior, r1);
         1618  +            pPrior = pIdx;
         1619  +            sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
         1620  +            /* Verify that an index entry exists for the current table row */
         1621  +            jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
         1622  +                                        pIdx->nColumn); VdbeCoverage(v);
         1623  +            sqlite3VdbeLoadString(v, 3, "row ");
         1624  +            sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
         1625  +            sqlite3VdbeLoadString(v, 4, " missing from index ");
         1626  +            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
         1627  +            jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
         1628  +            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
         1629  +            jmp4 = integrityCheckResultRow(v);
         1630  +            sqlite3VdbeJumpHere(v, jmp2);
         1631  +            /* For UNIQUE indexes, verify that only one entry exists with the
         1632  +            ** current key.  The entry is unique if (1) any column is NULL
         1633  +            ** or (2) the next entry has a different key */
         1634  +            if( IsUniqueIndex(pIdx) ){
         1635  +              int uniqOk = sqlite3VdbeMakeLabel(v);
         1636  +              int jmp6;
         1637  +              int kk;
         1638  +              for(kk=0; kk<pIdx->nKeyCol; kk++){
         1639  +                int iCol = pIdx->aiColumn[kk];
         1640  +                assert( iCol!=XN_ROWID && iCol<pTab->nCol );
         1641  +                if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
         1642  +                sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
         1643  +                VdbeCoverage(v);
         1644  +              }
         1645  +              jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
         1646  +              sqlite3VdbeGoto(v, uniqOk);
         1647  +              sqlite3VdbeJumpHere(v, jmp6);
         1648  +              sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
         1649  +                                   pIdx->nKeyCol); VdbeCoverage(v);
         1650  +              sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
         1651  +              sqlite3VdbeGoto(v, jmp5);
         1652  +              sqlite3VdbeResolveLabel(v, uniqOk);
  1644   1653               }
  1645         -            jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
  1646         -            sqlite3VdbeGoto(v, uniqOk);
  1647         -            sqlite3VdbeJumpHere(v, jmp6);
  1648         -            sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
  1649         -                                 pIdx->nKeyCol); VdbeCoverage(v);
  1650         -            sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
  1651         -            sqlite3VdbeGoto(v, jmp5);
  1652         -            sqlite3VdbeResolveLabel(v, uniqOk);
         1654  +            sqlite3VdbeJumpHere(v, jmp4);
         1655  +            sqlite3ResolvePartIdxLabel(pParse, jmp3);
  1653   1656             }
  1654         -          sqlite3VdbeJumpHere(v, jmp4);
  1655         -          sqlite3ResolvePartIdxLabel(pParse, jmp3);
  1656   1657           }
  1657   1658           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
  1658   1659           sqlite3VdbeJumpHere(v, loopTop-1);
  1659   1660   #ifndef SQLITE_OMIT_BTREECOUNT
  1660   1661           if( !isQuick ){
  1661   1662             sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
  1662   1663             for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1663   1664               if( pPk==pIdx ) continue;
  1664   1665               sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
  1665   1666               addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
  1666   1667               sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
  1667         -            sqlite3VdbeLoadString(v, 3, pIdx->zName);
  1668         -            sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1669         -            integrityCheckResultRow(v, 7);
         1668  +            sqlite3VdbeLoadString(v, 4, pIdx->zName);
         1669  +            sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
         1670  +            integrityCheckResultRow(v);
  1670   1671               sqlite3VdbeJumpHere(v, addr);
  1671   1672             }
  1672   1673           }
  1673   1674   #endif /* SQLITE_OMIT_BTREECOUNT */
  1674   1675         } 
  1675   1676       }
  1676   1677       {
  1677   1678         static const int iLn = VDBE_OFFSET_LINENO(2);
  1678   1679         static const VdbeOpList endCode[] = {
  1679   1680           { OP_AddImm,      1, 0,        0},    /* 0 */
  1680   1681           { OP_IfNotZero,   1, 4,        0},    /* 1 */
  1681   1682           { OP_String8,     0, 3,        0},    /* 2 */
  1682   1683           { OP_ResultRow,   3, 1,        0},    /* 3 */
         1684  +        { OP_Halt,        0, 0,        0},    /* 4 */
         1685  +        { OP_String8,     0, 3,        0},    /* 5 */
         1686  +        { OP_Goto,        0, 3,        0},    /* 6 */
  1683   1687         };
  1684   1688         VdbeOp *aOp;
  1685   1689   
  1686   1690         aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
  1687   1691         if( aOp ){
  1688   1692           aOp[0].p2 = 1-mxErr;
  1689   1693           aOp[2].p4type = P4_STATIC;
  1690   1694           aOp[2].p4.z = "ok";
         1695  +        aOp[5].p4type = P4_STATIC;
         1696  +        aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
  1691   1697         }
         1698  +      sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
  1692   1699       }
  1693   1700     }
  1694   1701     break;
  1695   1702   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  1696   1703   
  1697   1704   #ifndef SQLITE_OMIT_UTF16
  1698   1705     /*

Changes to src/select.c.

   660    660     sqlite3ReleaseTempReg(pParse, r1);
   661    661   }
   662    662   
   663    663   /*
   664    664   ** This routine generates the code for the inside of the inner loop
   665    665   ** of a SELECT.
   666    666   **
   667         -** If srcTab is negative, then the pEList expressions
          667  +** If srcTab is negative, then the p->pEList expressions
   668    668   ** are evaluated in order to get the data for this row.  If srcTab is
   669         -** zero or more, then data is pulled from srcTab and pEList is used only 
          669  +** zero or more, then data is pulled from srcTab and p->pEList is used only 
   670    670   ** to get the number of columns and the collation sequence for each column.
   671    671   */
   672    672   static void selectInnerLoop(
   673    673     Parse *pParse,          /* The parser context */
   674    674     Select *p,              /* The complete select statement being coded */
   675         -  ExprList *pEList,       /* List of values being extracted */
   676         -  int srcTab,             /* Pull data from this table */
          675  +  int srcTab,             /* Pull data from this table if non-negative */
   677    676     SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */
   678    677     DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
   679    678     SelectDest *pDest,      /* How to dispose of the results */
   680    679     int iContinue,          /* Jump here to continue with next row */
   681    680     int iBreak              /* Jump here to break out of the inner loop */
   682    681   ){
   683    682     Vdbe *v = pParse->pVdbe;
................................................................................
   693    692     ** same value. However, if the results are being sent to the sorter, the
   694    693     ** values for any expressions that are also part of the sort-key are omitted
   695    694     ** from this array. In this case regOrig is set to zero.  */
   696    695     int regResult;              /* Start of memory holding current results */
   697    696     int regOrig;                /* Start of memory holding full result (or 0) */
   698    697   
   699    698     assert( v );
   700         -  assert( pEList!=0 );
          699  +  assert( p->pEList!=0 );
   701    700     hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
   702    701     if( pSort && pSort->pOrderBy==0 ) pSort = 0;
   703    702     if( pSort==0 && !hasDistinct ){
   704    703       assert( iContinue!=0 );
   705    704       codeOffset(v, p->iOffset, iContinue);
   706    705     }
   707    706   
   708    707     /* Pull the requested columns.
   709    708     */
   710         -  nResultCol = pEList->nExpr;
          709  +  nResultCol = p->pEList->nExpr;
   711    710   
   712    711     if( pDest->iSdst==0 ){
   713    712       if( pSort ){
   714    713         nPrefixReg = pSort->pOrderBy->nExpr;
   715    714         if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
   716    715         pParse->nMem += nPrefixReg;
   717    716       }
................................................................................
   726    725       pParse->nMem += nResultCol;
   727    726     }
   728    727     pDest->nSdst = nResultCol;
   729    728     regOrig = regResult = pDest->iSdst;
   730    729     if( srcTab>=0 ){
   731    730       for(i=0; i<nResultCol; i++){
   732    731         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
   733         -      VdbeComment((v, "%s", pEList->a[i].zName));
          732  +      VdbeComment((v, "%s", p->pEList->a[i].zName));
   734    733       }
   735    734     }else if( eDest!=SRT_Exists ){
   736    735       /* If the destination is an EXISTS(...) expression, the actual
   737    736       ** values returned by the SELECT are not required.
   738    737       */
   739    738       u8 ecelFlags;
   740    739       if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
   741    740         ecelFlags = SQLITE_ECEL_DUP;
   742    741       }else{
   743    742         ecelFlags = 0;
   744    743       }
   745    744       if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
   746         -      /* For each expression in pEList that is a copy of an expression in
          745  +      /* For each expression in p->pEList that is a copy of an expression in
   747    746         ** the ORDER BY clause (pSort->pOrderBy), set the associated 
   748    747         ** iOrderByCol value to one more than the index of the ORDER BY 
   749    748         ** expression within the sort-key that pushOntoSorter() will generate.
   750         -      ** This allows the pEList field to be omitted from the sorted record,
          749  +      ** This allows the p->pEList field to be omitted from the sorted record,
   751    750         ** saving space and CPU cycles.  */
   752    751         ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
   753    752         for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
   754    753           int j;
   755    754           if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
   756         -          pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
          755  +          p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
   757    756           }
   758    757         }
   759    758         regOrig = 0;
   760    759         assert( eDest==SRT_Set || eDest==SRT_Mem 
   761    760              || eDest==SRT_Coroutine || eDest==SRT_Output );
   762    761       }
   763         -    nResultCol = sqlite3ExprCodeExprList(pParse,pEList,regResult,0,ecelFlags);
          762  +    nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,0,ecelFlags);
   764    763     }
   765    764   
   766    765     /* If the DISTINCT keyword was present on the SELECT statement
   767    766     ** and this row has been seen before, then do not make this row
   768    767     ** part of the result.
   769    768     */
   770    769     if( hasDistinct ){
................................................................................
   788    787           pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
   789    788           pOp->opcode = OP_Null;
   790    789           pOp->p1 = 1;
   791    790           pOp->p2 = regPrev;
   792    791   
   793    792           iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
   794    793           for(i=0; i<nResultCol; i++){
   795         -          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
          794  +          CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
   796    795             if( i<nResultCol-1 ){
   797    796               sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
   798    797               VdbeCoverage(v);
   799    798             }else{
   800    799               sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
   801    800               VdbeCoverage(v);
   802    801              }
................................................................................
  1858   1857     return pTab;
  1859   1858   }
  1860   1859   
  1861   1860   /*
  1862   1861   ** Get a VDBE for the given parser context.  Create a new one if necessary.
  1863   1862   ** If an error occurs, return NULL and leave a message in pParse.
  1864   1863   */
  1865         -static SQLITE_NOINLINE Vdbe *allocVdbe(Parse *pParse){
  1866         -  Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
  1867         -  if( v ) sqlite3VdbeAddOp2(v, OP_Init, 0, 1);
         1864  +Vdbe *sqlite3GetVdbe(Parse *pParse){
         1865  +  if( pParse->pVdbe ){
         1866  +    return pParse->pVdbe;
         1867  +  }
  1868   1868     if( pParse->pToplevel==0
  1869   1869      && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
  1870   1870     ){
  1871   1871       pParse->okConstFactor = 1;
  1872   1872     }
  1873         -  return v;
  1874         -}
  1875         -Vdbe *sqlite3GetVdbe(Parse *pParse){
  1876         -  Vdbe *v = pParse->pVdbe;
  1877         -  return v ? v : allocVdbe(pParse);
         1873  +  return sqlite3VdbeCreate(pParse);
  1878   1874   }
  1879   1875   
  1880   1876   
  1881   1877   /*
  1882   1878   ** Compute the iLimit and iOffset fields of the SELECT based on the
  1883   1879   ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
  1884   1880   ** that appear in the original SQL statement after the LIMIT and OFFSET
................................................................................
  2143   2139       sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
  2144   2140     }
  2145   2141     sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
  2146   2142   
  2147   2143     /* Output the single row in Current */
  2148   2144     addrCont = sqlite3VdbeMakeLabel(v);
  2149   2145     codeOffset(v, regOffset, addrCont);
  2150         -  selectInnerLoop(pParse, p, p->pEList, iCurrent,
         2146  +  selectInnerLoop(pParse, p, iCurrent,
  2151   2147         0, 0, pDest, addrCont, addrBreak);
  2152   2148     if( regLimit ){
  2153   2149       sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
  2154   2150       VdbeCoverage(v);
  2155   2151     }
  2156   2152     sqlite3VdbeResolveLabel(v, addrCont);
  2157   2153   
................................................................................
  2281   2277     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
  2282   2278     */
  2283   2279     assert( p && p->pPrior );  /* Calling function guarantees this much */
  2284   2280     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
  2285   2281     db = pParse->db;
  2286   2282     pPrior = p->pPrior;
  2287   2283     dest = *pDest;
  2288         -  if( pPrior->pOrderBy ){
  2289         -    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
  2290         -      selectOpName(p->op));
  2291         -    rc = 1;
  2292         -    goto multi_select_end;
  2293         -  }
  2294         -  if( pPrior->pLimit ){
  2295         -    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
  2296         -      selectOpName(p->op));
         2284  +  if( pPrior->pOrderBy || pPrior->pLimit ){
         2285  +    sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
         2286  +      pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
  2297   2287       rc = 1;
  2298   2288       goto multi_select_end;
  2299   2289     }
  2300   2290   
  2301   2291     v = sqlite3GetVdbe(pParse);
  2302   2292     assert( v!=0 );  /* The VDBE already created by calling function */
  2303   2293   
................................................................................
  2462   2452           int iCont, iBreak, iStart;
  2463   2453           assert( p->pEList );
  2464   2454           iBreak = sqlite3VdbeMakeLabel(v);
  2465   2455           iCont = sqlite3VdbeMakeLabel(v);
  2466   2456           computeLimitRegisters(pParse, p, iBreak);
  2467   2457           sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
  2468   2458           iStart = sqlite3VdbeCurrentAddr(v);
  2469         -        selectInnerLoop(pParse, p, p->pEList, unionTab,
         2459  +        selectInnerLoop(pParse, p, unionTab,
  2470   2460                           0, 0, &dest, iCont, iBreak);
  2471   2461           sqlite3VdbeResolveLabel(v, iCont);
  2472   2462           sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
  2473   2463           sqlite3VdbeResolveLabel(v, iBreak);
  2474   2464           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
  2475   2465         }
  2476   2466         break;
................................................................................
  2535   2525         iCont = sqlite3VdbeMakeLabel(v);
  2536   2526         computeLimitRegisters(pParse, p, iBreak);
  2537   2527         sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
  2538   2528         r1 = sqlite3GetTempReg(pParse);
  2539   2529         iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
  2540   2530         sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
  2541   2531         sqlite3ReleaseTempReg(pParse, r1);
  2542         -      selectInnerLoop(pParse, p, p->pEList, tab1,
         2532  +      selectInnerLoop(pParse, p, tab1,
  2543   2533                         0, 0, &dest, iCont, iBreak);
  2544   2534         sqlite3VdbeResolveLabel(v, iCont);
  2545   2535         sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
  2546   2536         sqlite3VdbeResolveLabel(v, iBreak);
  2547   2537         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
  2548   2538         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
  2549   2539         break;
................................................................................
  5541   5531       ** into an OP_Noop.
  5542   5532       */
  5543   5533       if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
  5544   5534         sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
  5545   5535       }
  5546   5536   
  5547   5537       /* Use the standard inner loop. */
  5548         -    selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
         5538  +    assert( p->pEList==pEList );
         5539  +    selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
  5549   5540                       sqlite3WhereContinueLabel(pWInfo),
  5550   5541                       sqlite3WhereBreakLabel(pWInfo));
  5551   5542   
  5552   5543       /* End the database scan loop.
  5553   5544       */
  5554   5545       sqlite3WhereEnd(pWInfo);
  5555   5546     }else{
................................................................................
  5844   5835         addrOutputRow = sqlite3VdbeCurrentAddr(v);
  5845   5836         sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
  5846   5837         VdbeCoverage(v);
  5847   5838         VdbeComment((v, "Groupby result generator entry point"));
  5848   5839         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5849   5840         finalizeAggFunctions(pParse, &sAggInfo);
  5850   5841         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  5851         -      selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
         5842  +      selectInnerLoop(pParse, p, -1, &sSort,
  5852   5843                         &sDistinct, pDest,
  5853   5844                         addrOutputRow+1, addrSetAbort);
  5854   5845         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5855   5846         VdbeComment((v, "end groupby result generator"));
  5856   5847   
  5857   5848         /* Generate a subroutine that will reset the group-by accumulator
  5858   5849         */
................................................................................
  5988   5979           }
  5989   5980           sqlite3WhereEnd(pWInfo);
  5990   5981           finalizeAggFunctions(pParse, &sAggInfo);
  5991   5982         }
  5992   5983   
  5993   5984         sSort.pOrderBy = 0;
  5994   5985         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  5995         -      selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, 
         5986  +      selectInnerLoop(pParse, p, -1, 0, 0, 
  5996   5987                         pDest, addrEnd, addrEnd);
  5997   5988         sqlite3ExprListDelete(db, pDel);
  5998   5989       }
  5999   5990       sqlite3VdbeResolveLabel(v, addrEnd);
  6000   5991       
  6001   5992     } /* endif aggregate query */
  6002   5993   

Changes to src/shell.c.

  1495   1495       sqlite3_finalize(pStmt);
  1496   1496     }
  1497   1497     sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
  1498   1498   }
  1499   1499   
  1500   1500   
  1501   1501   #ifdef _WIN32
  1502         -__declspec(dllexport)
         1502  +
  1503   1503   #endif
  1504   1504   int sqlite3_shathree_init(
  1505   1505     sqlite3 *db,
  1506   1506     char **pzErrMsg,
  1507   1507     const sqlite3_api_routines *pApi
  1508   1508   ){
  1509   1509     int rc = SQLITE_OK;
................................................................................
  1607   1607     }
  1608   1608     fclose(out);
  1609   1609     sqlite3_result_int64(context, rc);
  1610   1610   }
  1611   1611   
  1612   1612   
  1613   1613   #ifdef _WIN32
  1614         -__declspec(dllexport)
         1614  +
  1615   1615   #endif
  1616   1616   int sqlite3_fileio_init(
  1617   1617     sqlite3 *db, 
  1618   1618     char **pzErrMsg, 
  1619   1619     const sqlite3_api_routines *pApi
  1620   1620   ){
  1621   1621     int rc = SQLITE_OK;
................................................................................
  2135   2135   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2136   2136     rc = sqlite3_create_module(db, "completion", &completionModule, 0);
  2137   2137   #endif
  2138   2138     return rc;
  2139   2139   }
  2140   2140   
  2141   2141   #ifdef _WIN32
  2142         -__declspec(dllexport)
         2142  +
  2143   2143   #endif
  2144   2144   int sqlite3_completion_init(
  2145   2145     sqlite3 *db, 
  2146   2146     char **pzErrMsg, 
  2147   2147     const sqlite3_api_routines *pApi
  2148   2148   ){
  2149   2149     int rc = SQLITE_OK;
................................................................................
  2227   2227     OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
  2228   2228   #endif
  2229   2229   };
  2230   2230   
  2231   2231   /*
  2232   2232   ** These are the allowed shellFlgs values
  2233   2233   */
  2234         -#define SHFLG_Scratch        0x00000001 /* The --scratch option is used */
  2235         -#define SHFLG_Pagecache      0x00000002 /* The --pagecache option is used */
  2236         -#define SHFLG_Lookaside      0x00000004 /* Lookaside memory is used */
  2237         -#define SHFLG_Backslash      0x00000008 /* The --backslash option is used */
  2238         -#define SHFLG_PreserveRowid  0x00000010 /* .dump preserves rowid values */
  2239         -#define SHFLG_Newlines       0x00000020 /* .dump --newline flag */
  2240         -#define SHFLG_CountChanges   0x00000040 /* .changes setting */
  2241         -#define SHFLG_Echo           0x00000080 /* .echo or --echo setting */
         2234  +#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
         2235  +#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
         2236  +#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
         2237  +#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
         2238  +#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
         2239  +#define SHFLG_CountChanges   0x00000020 /* .changes setting */
         2240  +#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
  2242   2241   
  2243   2242   /*
  2244   2243   ** Macros for testing and setting shellFlgs
  2245   2244   */
  2246   2245   #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
  2247   2246   #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
  2248   2247   #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
................................................................................
  3071   3070   /*
  3072   3071   ** Set the destination table field of the ShellState structure to
  3073   3072   ** the name of the table given.  Escape any quote characters in the
  3074   3073   ** table name.
  3075   3074   */
  3076   3075   static void set_table_name(ShellState *p, const char *zName){
  3077   3076     int i, n;
  3078         -  int cQuote;
         3077  +  char cQuote;
  3079   3078     char *z;
  3080   3079   
  3081   3080     if( p->zDestTable ){
  3082   3081       free(p->zDestTable);
  3083   3082       p->zDestTable = 0;
  3084   3083     }
  3085   3084     if( zName==0 ) return;
................................................................................
  3253   3252          "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
  3254   3253       if( pArg->shellFlgs & SHFLG_Pagecache ){
  3255   3254         displayStatLine(pArg, "Number of Pcache Pages Used:",
  3256   3255            "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
  3257   3256       }
  3258   3257       displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
  3259   3258          "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
  3260         -    if( pArg->shellFlgs & SHFLG_Scratch ){
  3261         -      displayStatLine(pArg, "Number of Scratch Allocations Used:",
  3262         -         "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset);
  3263         -    }
  3264         -    displayStatLine(pArg, "Number of Scratch Overflow Bytes:",
  3265         -       "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset);
  3266   3259       displayStatLine(pArg, "Largest Allocation:",
  3267   3260          "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
  3268   3261       displayStatLine(pArg, "Largest Pcache Allocation:",
  3269   3262          "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
  3270         -    displayStatLine(pArg, "Largest Scratch Allocation:",
  3271         -       "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset);
  3272   3263   #ifdef YYTRACKMAXSTACKDEPTH
  3273   3264       displayStatLine(pArg, "Deepest Parser Stack:",
  3274   3265          "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
  3275   3266   #endif
  3276   3267     }
  3277   3268   
  3278   3269     if( pArg && pArg->out && db ){
................................................................................
  7285   7276         { "benign_malloc_hooks",   SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS    },
  7286   7277         { "pending_byte",          SQLITE_TESTCTRL_PENDING_BYTE           },
  7287   7278         { "assert",                SQLITE_TESTCTRL_ASSERT                 },
  7288   7279         { "always",                SQLITE_TESTCTRL_ALWAYS                 },
  7289   7280         { "reserve",               SQLITE_TESTCTRL_RESERVE                },
  7290   7281         { "optimizations",         SQLITE_TESTCTRL_OPTIMIZATIONS          },
  7291   7282         { "iskeyword",             SQLITE_TESTCTRL_ISKEYWORD              },
  7292         -      { "scratchmalloc",         SQLITE_TESTCTRL_SCRATCHMALLOC          },
  7293   7283         { "byteorder",             SQLITE_TESTCTRL_BYTEORDER              },
  7294   7284         { "never_corrupt",         SQLITE_TESTCTRL_NEVER_CORRUPT          },
  7295   7285         { "imposter",              SQLITE_TESTCTRL_IMPOSTER               },
  7296   7286       };
  7297   7287       int testctrl = -1;
  7298   7288       int rc2 = 0;
  7299   7289       int i, n2;
................................................................................
  7398   7388               raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
  7399   7389             }
  7400   7390             break;
  7401   7391   
  7402   7392           case SQLITE_TESTCTRL_BITVEC_TEST:
  7403   7393           case SQLITE_TESTCTRL_FAULT_INSTALL:
  7404   7394           case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
  7405         -        case SQLITE_TESTCTRL_SCRATCHMALLOC:
  7406   7395           default:
  7407   7396             utf8_printf(stderr,
  7408   7397                         "Error: CLI support for testctrl %s not implemented\n",
  7409   7398                         azArg[1]);
  7410   7399             break;
  7411   7400         }
  7412   7401       }
................................................................................
  7918   7907   #ifdef SQLITE_ENABLE_MULTIPLEX
  7919   7908     "   -multiplex           enable the multiplexor VFS\n"
  7920   7909   #endif
  7921   7910     "   -newline SEP         set output row separator. Default: '\\n'\n"
  7922   7911     "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
  7923   7912     "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
  7924   7913     "   -quote               set output mode to 'quote'\n"
  7925         -  "   -scratch SIZE N      use N slots of SZ bytes each for scratch memory\n"
  7926   7914     "   -separator SEP       set output column separator. Default: '|'\n"
  7927   7915     "   -stats               print memory stats before each finalize\n"
  7928   7916     "   -version             show SQLite version\n"
  7929   7917     "   -vfs NAME            use NAME as the default VFS\n"
  7930   7918   #ifdef SQLITE_ENABLE_VFSTRACE
  7931   7919     "   -vfstrace            enable tracing of all VFS calls\n"
  7932   7920   #endif
................................................................................
  8116   8104         zSize = cmdline_option_value(argc, argv, ++i);
  8117   8105         szHeap = integerValue(zSize);
  8118   8106         if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
  8119   8107         sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
  8120   8108   #else
  8121   8109         (void)cmdline_option_value(argc, argv, ++i);
  8122   8110   #endif
  8123         -    }else if( strcmp(z,"-scratch")==0 ){
  8124         -      int n, sz;
  8125         -      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
  8126         -      if( sz>400000 ) sz = 400000;
  8127         -      if( sz<2500 ) sz = 2500;
  8128         -      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
  8129         -      if( n>10 ) n = 10;
  8130         -      if( n<1 ) n = 1;
  8131         -      sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
  8132         -      data.shellFlgs |= SHFLG_Scratch;
  8133   8111       }else if( strcmp(z,"-pagecache")==0 ){
  8134   8112         int n, sz;
  8135   8113         sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
  8136   8114         if( sz>70000 ) sz = 70000;
  8137   8115         if( sz<0 ) sz = 0;
  8138   8116         n = (int)integerValue(cmdline_option_value(argc,argv,++i));
  8139   8117         sqlite3_config(SQLITE_CONFIG_PAGECACHE,
................................................................................
  8269   8247         return 0;
  8270   8248       }else if( strcmp(z,"-interactive")==0 ){
  8271   8249         stdin_is_interactive = 1;
  8272   8250       }else if( strcmp(z,"-batch")==0 ){
  8273   8251         stdin_is_interactive = 0;
  8274   8252       }else if( strcmp(z,"-heap")==0 ){
  8275   8253         i++;
  8276         -    }else if( strcmp(z,"-scratch")==0 ){
  8277         -      i+=2;
  8278   8254       }else if( strcmp(z,"-pagecache")==0 ){
  8279   8255         i+=2;
  8280   8256       }else if( strcmp(z,"-lookaside")==0 ){
  8281   8257         i+=2;
  8282   8258       }else if( strcmp(z,"-mmap")==0 ){
  8283   8259         i++;
  8284   8260       }else if( strcmp(z,"-vfs")==0 ){

Changes to src/shell.c.in.

  1710   1710   /*
  1711   1711   ** Set the destination table field of the ShellState structure to
  1712   1712   ** the name of the table given.  Escape any quote characters in the
  1713   1713   ** table name.
  1714   1714   */
  1715   1715   static void set_table_name(ShellState *p, const char *zName){
  1716   1716     int i, n;
  1717         -  int cQuote;
         1717  +  char cQuote;
  1718   1718     char *z;
  1719   1719   
  1720   1720     if( p->zDestTable ){
  1721   1721       free(p->zDestTable);
  1722   1722       p->zDestTable = 0;
  1723   1723     }
  1724   1724     if( zName==0 ) return;

Changes to src/sqlite.h.in.

  3815   3815   ** still make the distinction between protected and unprotected
  3816   3816   ** sqlite3_value objects even when not strictly required.
  3817   3817   **
  3818   3818   ** ^The sqlite3_value objects that are passed as parameters into the
  3819   3819   ** implementation of [application-defined SQL functions] are protected.
  3820   3820   ** ^The sqlite3_value object returned by
  3821   3821   ** [sqlite3_column_value()] is unprotected.
  3822         -** Unprotected sqlite3_value objects may only be used with
  3823         -** [sqlite3_result_value()] and [sqlite3_bind_value()].
         3822  +** Unprotected sqlite3_value objects may only be used as arguments
         3823  +** to [sqlite3_result_value()], [sqlite3_bind_value()], and
         3824  +** [sqlite3_value_dup()].
  3824   3825   ** The [sqlite3_value_blob | sqlite3_value_type()] family of
  3825   3826   ** interfaces require protected sqlite3_value objects.
  3826   3827   */
  3827   3828   typedef struct sqlite3_value sqlite3_value;
  3828   3829   
  3829   3830   /*
  3830   3831   ** CAPI3REF: SQL Function Context Object
................................................................................
  6242   6243   ** CAPI3REF: Virtual Table Constraint Operator Codes
  6243   6244   **
  6244   6245   ** These macros defined the allowed values for the
  6245   6246   ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  6246   6247   ** an operator that is part of a constraint term in the wHERE clause of
  6247   6248   ** a query that uses a [virtual table].
  6248   6249   */
  6249         -#define SQLITE_INDEX_CONSTRAINT_EQ      2
  6250         -#define SQLITE_INDEX_CONSTRAINT_GT      4
  6251         -#define SQLITE_INDEX_CONSTRAINT_LE      8
  6252         -#define SQLITE_INDEX_CONSTRAINT_LT     16
  6253         -#define SQLITE_INDEX_CONSTRAINT_GE     32
  6254         -#define SQLITE_INDEX_CONSTRAINT_MATCH  64
  6255         -#define SQLITE_INDEX_CONSTRAINT_LIKE   65
  6256         -#define SQLITE_INDEX_CONSTRAINT_GLOB   66
  6257         -#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
         6250  +#define SQLITE_INDEX_CONSTRAINT_EQ         2
         6251  +#define SQLITE_INDEX_CONSTRAINT_GT         4
         6252  +#define SQLITE_INDEX_CONSTRAINT_LE         8
         6253  +#define SQLITE_INDEX_CONSTRAINT_LT        16
         6254  +#define SQLITE_INDEX_CONSTRAINT_GE        32
         6255  +#define SQLITE_INDEX_CONSTRAINT_MATCH     64
         6256  +#define SQLITE_INDEX_CONSTRAINT_LIKE      65
         6257  +#define SQLITE_INDEX_CONSTRAINT_GLOB      66
         6258  +#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
         6259  +#define SQLITE_INDEX_CONSTRAINT_NE        68
         6260  +#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
         6261  +#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
         6262  +#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
         6263  +#define SQLITE_INDEX_CONSTRAINT_IS        72
  6258   6264   
  6259   6265   /*
  6260   6266   ** CAPI3REF: Register A Virtual Table Implementation
  6261   6267   ** METHOD: sqlite3
  6262   6268   **
  6263   6269   ** ^These routines are used to register a new [virtual table module] name.
  6264   6270   ** ^Module names must be registered before

Changes to src/sqliteInt.h.

    46     46   ** Make sure the Tcl calling convention macro is defined.  This macro is
    47     47   ** only used by test code and Tcl integration code.
    48     48   */
    49     49   #ifndef SQLITE_TCLAPI
    50     50   #  define SQLITE_TCLAPI
    51     51   #endif
    52     52   
    53         -/*
    54         -** Make sure that rand_s() is available on Windows systems with MSVC 2005
    55         -** or higher.
    56         -*/
    57         -#if defined(_MSC_VER) && _MSC_VER>=1400
    58         -#  define _CRT_RAND_S
    59         -#endif
    60         -
    61     53   /*
    62     54   ** Include the header file used to customize the compiler options for MSVC.
    63     55   ** This should be done first so that it can successfully prevent spurious
    64     56   ** compiler warnings due to subsequent content in this file and other files
    65     57   ** that are included by this file.
    66     58   */
    67     59   #include "msvc.h"
................................................................................
  1241   1233   ** schema information, the Lookaside.bEnabled flag is cleared so that
  1242   1234   ** lookaside allocations are not used to construct the schema objects.
  1243   1235   */
  1244   1236   struct Lookaside {
  1245   1237     u32 bDisable;           /* Only operate the lookaside when zero */
  1246   1238     u16 sz;                 /* Size of each buffer in bytes */
  1247   1239     u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
  1248         -  int nOut;               /* Number of buffers currently checked out */
  1249         -  int mxOut;              /* Highwater mark for nOut */
  1250         -  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
         1240  +  u32 nSlot;              /* Number of lookaside slots allocated */
         1241  +  u32 anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
         1242  +  LookasideSlot *pInit;   /* List of buffers not previously used */
  1251   1243     LookasideSlot *pFree;   /* List of available buffers */
  1252   1244     void *pStart;           /* First byte of available memory space */
  1253   1245     void *pEnd;             /* First byte past end of available space */
  1254   1246   };
  1255   1247   struct LookasideSlot {
  1256   1248     LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
  1257   1249   };
................................................................................
  2473   2465   ** column expression as it exists in a SELECT statement.  However, if
  2474   2466   ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
  2475   2467   ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
  2476   2468   ** form is used for name resolution with nested FROM clauses.
  2477   2469   */
  2478   2470   struct ExprList {
  2479   2471     int nExpr;             /* Number of expressions on the list */
  2480         -  int nAlloc;            /* Number of a[] slots allocated */
  2481   2472     struct ExprList_item { /* For each expression in the list */
  2482   2473       Expr *pExpr;            /* The parse tree for this expression */
  2483   2474       char *zName;            /* Token associated with this expression */
  2484   2475       char *zSpan;            /* Original text of the expression */
  2485   2476       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
  2486   2477       unsigned done :1;       /* A flag to indicate when processing is finished */
  2487   2478       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
................................................................................
  3365   3356   int sqlite3WalkExpr(Walker*, Expr*);
  3366   3357   int sqlite3WalkExprList(Walker*, ExprList*);
  3367   3358   int sqlite3WalkSelect(Walker*, Select*);
  3368   3359   int sqlite3WalkSelectExpr(Walker*, Select*);
  3369   3360   int sqlite3WalkSelectFrom(Walker*, Select*);
  3370   3361   int sqlite3ExprWalkNoop(Walker*, Expr*);
  3371   3362   int sqlite3SelectWalkNoop(Walker*, Select*);
         3363  +int sqlite3SelectWalkFail(Walker*, Select*);
  3372   3364   #ifdef SQLITE_DEBUG
  3373   3365   void sqlite3SelectWalkAssert2(Walker*, Select*);
  3374   3366   #endif
  3375   3367   
  3376   3368   /*
  3377   3369   ** Return code from the parse-tree walking primitives and their
  3378   3370   ** callbacks.
................................................................................
  3572   3564   # define sqlite3MemoryBarrier()
  3573   3565   #endif
  3574   3566   
  3575   3567   sqlite3_int64 sqlite3StatusValue(int);
  3576   3568   void sqlite3StatusUp(int, int);
  3577   3569   void sqlite3StatusDown(int, int);
  3578   3570   void sqlite3StatusHighwater(int, int);
         3571  +int sqlite3LookasideUsed(sqlite3*,int*);
  3579   3572   
  3580   3573   /* Access to mutexes used by sqlite3_status() */
  3581   3574   sqlite3_mutex *sqlite3Pcache1Mutex(void);
  3582   3575   sqlite3_mutex *sqlite3MallocMutex(void);
  3583   3576   
  3584   3577   #ifndef SQLITE_OMIT_FLOATING_POINT
  3585   3578     int sqlite3IsNaN(double);

Changes to src/status.c.

   165    165     rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
   166    166     if( rc==0 ){
   167    167       *pCurrent = (int)iCur;
   168    168       *pHighwater = (int)iHwtr;
   169    169     }
   170    170     return rc;
   171    171   }
          172  +
          173  +/*
          174  +** Return the number of LookasideSlot elements on the linked list
          175  +*/
          176  +static u32 countLookasideSlots(LookasideSlot *p){
          177  +  u32 cnt = 0;
          178  +  while( p ){
          179  +    p = p->pNext;
          180  +    cnt++;
          181  +  }
          182  +  return cnt;
          183  +}
          184  +
          185  +/*
          186  +** Count the number of slots of lookaside memory that are outstanding
          187  +*/
          188  +int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
          189  +  u32 nInit = countLookasideSlots(db->lookaside.pInit);
          190  +  u32 nFree = countLookasideSlots(db->lookaside.pFree);
          191  +  if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
          192  +  return db->lookaside.nSlot - (nInit+nFree);
          193  +}
   172    194   
   173    195   /*
   174    196   ** Query status information for a single database connection
   175    197   */
   176    198   int sqlite3_db_status(
   177    199     sqlite3 *db,          /* The database connection whose status is desired */
   178    200     int op,               /* Status verb */
................................................................................
   185    207     if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
   186    208       return SQLITE_MISUSE_BKPT;
   187    209     }
   188    210   #endif
   189    211     sqlite3_mutex_enter(db->mutex);
   190    212     switch( op ){
   191    213       case SQLITE_DBSTATUS_LOOKASIDE_USED: {
   192         -      *pCurrent = db->lookaside.nOut;
   193         -      *pHighwater = db->lookaside.mxOut;
          214  +      *pCurrent = sqlite3LookasideUsed(db, pHighwater);
   194    215         if( resetFlag ){
   195         -        db->lookaside.mxOut = db->lookaside.nOut;
          216  +        LookasideSlot *p = db->lookaside.pFree;
          217  +        if( p ){
          218  +          while( p->pNext ) p = p->pNext;
          219  +          p->pNext = db->lookaside.pInit;
          220  +          db->lookaside.pInit = db->lookaside.pFree;
          221  +          db->lookaside.pFree = 0;
          222  +        }
   196    223         }
   197    224         break;
   198    225       }
   199    226   
   200    227       case SQLITE_DBSTATUS_LOOKASIDE_HIT:
   201    228       case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
   202    229       case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {

Changes to src/test1.c.

  7445   7445       Tcl_Obj *pNew = Tcl_NewObj();
  7446   7446       Tcl_ListObjAppendElement(interp, pNew, Tcl_NewWideIntObj((i64)nPrior));
  7447   7447       Tcl_ListObjAppendElement(interp, pNew, Tcl_NewWideIntObj((i64)nFrame));
  7448   7448       Tcl_SetObjResult(interp, pNew);
  7449   7449     }
  7450   7450     return TCL_OK;
  7451   7451   }
         7452  +
         7453  +/*
         7454  +** Usage: sqlite3_mmap_warm DB DBNAME
         7455  +*/
         7456  +static int SQLITE_TCLAPI test_mmap_warm(
         7457  +  void * clientData,
         7458  +  Tcl_Interp *interp,
         7459  +  int objc,
         7460  +  Tcl_Obj *CONST objv[]
         7461  +){
         7462  +  extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
         7463  +  extern int sqlite3_mmap_warm(sqlite3 *db, const char *);
         7464  +
         7465  +  if( objc!=2 && objc!=3 ){
         7466  +    Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?");
         7467  +    return TCL_ERROR;
         7468  +  }else{
         7469  +    int rc;
         7470  +    sqlite3 *db;
         7471  +    const char *zDb = 0;
         7472  +    if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
         7473  +    if( objc==3 ){
         7474  +      zDb = Tcl_GetString(objv[2]);
         7475  +    }
         7476  +    rc = sqlite3_mmap_warm(db, zDb);
         7477  +    Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
         7478  +    return TCL_OK;
         7479  +  }
         7480  +}
  7452   7481   
  7453   7482   /*
  7454   7483   ** Register commands with the TCL interpreter.
  7455   7484   */
  7456   7485   int Sqlitetest1_Init(Tcl_Interp *interp){
  7457   7486     extern int sqlite3_search_count;
  7458   7487     extern int sqlite3_found_count;
................................................................................
  7718   7747        { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 },
  7719   7748        { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 },
  7720   7749        { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 },
  7721   7750   #endif
  7722   7751        { "sqlite3_delete_database", test_delete_database, 0 },
  7723   7752        { "sqlite3_wal_info", test_wal_info, 0 },
  7724   7753        { "atomic_batch_write",      test_atomic_batch_write,     0   },
         7754  +     { "sqlite3_mmap_warm",       test_mmap_warm,          0 },
  7725   7755     };
  7726   7756     static int bitmask_size = sizeof(Bitmask)*8;
  7727   7757     static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
  7728   7758     int i;
  7729   7759     extern int sqlite3_sync_count, sqlite3_fullsync_count;
  7730   7760     extern int sqlite3_opentemp_count;
  7731   7761     extern int sqlite3_like_count;

Changes to src/test8.c.

   893    893           case SQLITE_INDEX_CONSTRAINT_LIKE:
   894    894             zOp = "like"; break;
   895    895           case SQLITE_INDEX_CONSTRAINT_GLOB:
   896    896             zOp = "glob"; break;
   897    897           case SQLITE_INDEX_CONSTRAINT_REGEXP:
   898    898             zOp = "regexp"; break;
   899    899         }
   900         -      if( zOp[0]=='L' ){
   901         -        zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", 
   902         -                               zSep, zNewCol);
   903         -      } else {
   904         -        zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp);
          900  +      if( zOp ){
          901  +        if( zOp[0]=='L' ){
          902  +          zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", 
          903  +              zSep, zNewCol);
          904  +        } else {
          905  +          zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp);
          906  +        }
          907  +        string_concat(&zQuery, zNew, 1, &rc);
          908  +        zSep = "AND";
          909  +        pUsage->argvIndex = ++nArg;
          910  +        pUsage->omit = 1;
   905    911         }
   906         -      string_concat(&zQuery, zNew, 1, &rc);
   907         -
   908         -      zSep = "AND";
   909         -      pUsage->argvIndex = ++nArg;
   910         -      pUsage->omit = 1;
   911    912       }
   912    913     }
   913    914   
   914    915     /* If there is only one term in the ORDER BY clause, and it is
   915    916     ** on a column that this virtual table has an index for, then consume 
   916    917     ** the ORDER BY clause.
   917    918     */

Changes to src/test_bestindex.c.

   410    410           zOp = "match"; break;
   411    411         case SQLITE_INDEX_CONSTRAINT_LIKE:
   412    412           zOp = "like"; break;
   413    413         case SQLITE_INDEX_CONSTRAINT_GLOB:
   414    414           zOp = "glob"; break;
   415    415         case SQLITE_INDEX_CONSTRAINT_REGEXP:
   416    416           zOp = "regexp"; break;
          417  +      case SQLITE_INDEX_CONSTRAINT_NE:
          418  +        zOp = "ne"; break;
          419  +      case SQLITE_INDEX_CONSTRAINT_ISNOT:
          420  +        zOp = "isnot"; break;
          421  +      case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
          422  +        zOp = "isnotnull"; break;
          423  +      case SQLITE_INDEX_CONSTRAINT_ISNULL:
          424  +        zOp = "isnull"; break;
          425  +      case SQLITE_INDEX_CONSTRAINT_IS:
          426  +        zOp = "is"; break;
   417    427       }
   418    428   
   419    429       Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("op", -1));
   420    430       Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj(zOp, -1));
   421    431       Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1));
   422    432       Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->iColumn));
   423    433       Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("usable", -1));

Changes to src/util.c.

   487    487               result = s * scale;
   488    488               result *= 1.0e+308;
   489    489             }
   490    490           }else{ assert( e>=342 );
   491    491             if( esign<0 ){
   492    492               result = 0.0*s;
   493    493             }else{
          494  +#ifdef INFINITY
          495  +            result = INFINITY*s;
          496  +#else
   494    497               result = 1e308*1e308*s;  /* Infinity */
          498  +#endif
   495    499             }
   496    500           }
   497    501         }else{
   498    502           /* 1.0e+22 is the largest power of 10 than can be 
   499    503           ** represented exactly. */
   500    504           while( e%22 ) { scale *= 1.0e+1; e -= 1; }
   501    505           while( e>0 ) { scale *= 1.0e+22; e -= 22; }
................................................................................
   549    553     return c;
   550    554   }
   551    555   
   552    556   /*
   553    557   ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
   554    558   ** routine does *not* accept hexadecimal notation.
   555    559   **
   556         -** If the zNum value is representable as a 64-bit twos-complement 
   557         -** integer, then write that value into *pNum and return 0.
          560  +** Returns:
   558    561   **
   559         -** If zNum is exactly 9223372036854775808, return 2.  This special
   560         -** case is broken out because while 9223372036854775808 cannot be a 
   561         -** signed 64-bit integer, its negative -9223372036854775808 can be.
   562         -**
   563         -** If zNum is too big for a 64-bit integer and is not
   564         -** 9223372036854775808  or if zNum contains any non-numeric text,
   565         -** then return 1.
          562  +**     0    Successful transformation.  Fits in a 64-bit signed integer.
          563  +**     1    Excess text after the integer value
          564  +**     2    Integer too large for a 64-bit signed integer or is malformed
          565  +**     3    Special case of 9223372036854775808
   566    566   **
   567    567   ** length is the number of bytes in the string (bytes, not characters).
   568    568   ** The string is not necessarily zero-terminated.  The encoding is
   569    569   ** given by enc.
   570    570   */
   571    571   int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
   572    572     int incr;
   573    573     u64 u = 0;
   574    574     int neg = 0; /* assume positive */
   575    575     int i;
   576    576     int c = 0;
   577    577     int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
          578  +  int rc;          /* Baseline return code */
   578    579     const char *zStart;
   579    580     const char *zEnd = zNum + length;
   580    581     assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
   581    582     if( enc==SQLITE_UTF8 ){
   582    583       incr = 1;
   583    584     }else{
   584    585       incr = 2;
................................................................................
   610    611       *pNum = (i64)u;
   611    612     }
   612    613     testcase( i==18 );
   613    614     testcase( i==19 );
   614    615     testcase( i==20 );
   615    616     if( &zNum[i]<zEnd              /* Extra bytes at the end */
   616    617      || (i==0 && zStart==zNum)     /* No digits */
   617         -   || i>19*incr                  /* Too many digits */
   618    618      || nonNum                     /* UTF16 with high-order bytes non-zero */
   619    619     ){
          620  +    rc = 1;
          621  +  }else{
          622  +    rc = 0;
          623  +  }
          624  +  if( i>19*incr ){                /* Too many digits */
   620    625       /* zNum is empty or contains non-numeric text or is longer
   621    626       ** than 19 digits (thus guaranteeing that it is too large) */
   622         -    return 1;
          627  +    return 2;
   623    628     }else if( i<19*incr ){
   624    629       /* Less than 19 digits, so we know that it fits in 64 bits */
   625    630       assert( u<=LARGEST_INT64 );
   626         -    return 0;
          631  +    return rc;
   627    632     }else{
   628    633       /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
   629    634       c = compare2pow63(zNum, incr);
   630    635       if( c<0 ){
   631    636         /* zNum is less than 9223372036854775808 so it fits */
   632    637         assert( u<=LARGEST_INT64 );
   633         -      return 0;
          638  +      return rc;
   634    639       }else if( c>0 ){
   635    640         /* zNum is greater than 9223372036854775808 so it overflows */
   636         -      return 1;
          641  +      return 2;
   637    642       }else{
   638    643         /* zNum is exactly 9223372036854775808.  Fits if negative.  The
   639    644         ** special case 2 overflow if positive */
   640    645         assert( u-1==LARGEST_INT64 );
   641         -      return neg ? 0 : 2;
          646  +      return neg ? rc : 3;
   642    647       }
   643    648     }
   644    649   }
   645    650   
   646    651   /*
   647    652   ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
   648    653   ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
   649    654   ** whereas sqlite3Atoi64() does not.
   650    655   **
   651    656   ** Returns:
   652    657   **
   653    658   **     0    Successful transformation.  Fits in a 64-bit signed integer.
   654         -**     1    Integer too large for a 64-bit signed integer or is malformed
   655         -**     2    Special case of 9223372036854775808
          659  +**     1    Excess text after the integer value
          660  +**     2    Integer too large for a 64-bit signed integer or is malformed
          661  +**     3    Special case of 9223372036854775808
   656    662   */
   657    663   int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
   658    664   #ifndef SQLITE_OMIT_HEX_INTEGER
   659    665     if( z[0]=='0'
   660    666      && (z[1]=='x' || z[1]=='X')
   661    667     ){
   662    668       u64 u = 0;
   663    669       int i, k;
   664    670       for(i=2; z[i]=='0'; i++){}
   665    671       for(k=i; sqlite3Isxdigit(z[k]); k++){
   666    672         u = u*16 + sqlite3HexToInt(z[k]);
   667    673       }
   668    674       memcpy(pOut, &u, 8);
   669         -    return (z[k]==0 && k-i<=16) ? 0 : 1;
          675  +    return (z[k]==0 && k-i<=16) ? 0 : 2;
   670    676     }else
   671    677   #endif /* SQLITE_OMIT_HEX_INTEGER */
   672    678     {
   673    679       return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
   674    680     }
   675    681   }
   676    682   
................................................................................
  1272   1278   /*
  1273   1279   ** Attempt to add, substract, or multiply the 64-bit signed value iB against
  1274   1280   ** the other 64-bit signed integer at *pA and store the result in *pA.
  1275   1281   ** Return 0 on success.  Or if the operation would have resulted in an
  1276   1282   ** overflow, leave *pA unchanged and return 1.
  1277   1283   */
  1278   1284   int sqlite3AddInt64(i64 *pA, i64 iB){
  1279         -#if GCC_VERSION>=5004000
         1285  +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
  1280   1286     return __builtin_add_overflow(*pA, iB, pA);
  1281   1287   #else
  1282   1288     i64 iA = *pA;
  1283   1289     testcase( iA==0 ); testcase( iA==1 );
  1284   1290     testcase( iB==-1 ); testcase( iB==0 );
  1285   1291     if( iB>=0 ){
  1286   1292       testcase( iA>0 && LARGEST_INT64 - iA == iB );
................................................................................
  1292   1298       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
  1293   1299     }
  1294   1300     *pA += iB;
  1295   1301     return 0; 
  1296   1302   #endif
  1297   1303   }
  1298   1304   int sqlite3SubInt64(i64 *pA, i64 iB){
  1299         -#if GCC_VERSION>=5004000
         1305  +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
  1300   1306     return __builtin_sub_overflow(*pA, iB, pA);
  1301   1307   #else
  1302   1308     testcase( iB==SMALLEST_INT64+1 );
  1303   1309     if( iB==SMALLEST_INT64 ){
  1304   1310       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
  1305   1311       if( (*pA)>=0 ) return 1;
  1306   1312       *pA -= iB;
................................................................................
  1307   1313       return 0;
  1308   1314     }else{
  1309   1315       return sqlite3AddInt64(pA, -iB);
  1310   1316     }
  1311   1317   #endif
  1312   1318   }
  1313   1319   int sqlite3MulInt64(i64 *pA, i64 iB){
  1314         -#if GCC_VERSION>=5004000
         1320  +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
  1315   1321     return __builtin_mul_overflow(*pA, iB, pA);
  1316   1322   #else
  1317   1323     i64 iA = *pA;
  1318   1324     if( iB>0 ){
  1319   1325       if( iA>LARGEST_INT64/iB ) return 1;
  1320   1326       if( iA<SMALLEST_INT64/iB ) return 1;
  1321   1327     }else if( iB<0 ){

Changes to src/vdbe.c.

   350    350   */
   351    351   static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
   352    352     assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
   353    353     assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
   354    354     if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
   355    355       return 0;
   356    356     }
   357         -  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
          357  +  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
   358    358       return MEM_Int;
   359    359     }
   360    360     return MEM_Real;
   361    361   }
   362    362   
   363    363   /*
   364    364   ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
................................................................................
  2457   2457         ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
  2458   2458         ** types use so much data space that there can only be 4096 and 32 of
  2459   2459         ** them, respectively.  So the maximum header length results from a
  2460   2460         ** 3-byte type for each of the maximum of 32768 columns plus three
  2461   2461         ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
  2462   2462         */
  2463   2463         if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
  2464         -        rc = SQLITE_CORRUPT_BKPT;
  2465         -        goto abort_due_to_error;
         2464  +        goto op_column_corrupt;
  2466   2465         }
  2467   2466       }else{
  2468   2467         /* This is an optimization.  By skipping over the first few tests
  2469   2468         ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
  2470   2469         ** measurable performance gain.
  2471   2470         **
  2472   2471         ** This branch is taken even if aOffset[0]==0.  Such a record is never
................................................................................
  2531   2530          || (offset64 > pC->payloadSize)
  2532   2531         ){
  2533   2532           if( aOffset[0]==0 ){
  2534   2533             i = 0;
  2535   2534             zHdr = zEndHdr;
  2536   2535           }else{
  2537   2536             if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
  2538         -          rc = SQLITE_CORRUPT_BKPT;
  2539         -          goto abort_due_to_error;
         2537  +          goto op_column_corrupt;
  2540   2538           }
  2541   2539         }
  2542   2540   
  2543   2541         pC->nHdrParsed = i;
  2544   2542         pC->iHdrOffset = (u32)(zHdr - zData);
  2545   2543         if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
  2546   2544       }else{
................................................................................
  2627   2625       }
  2628   2626     }
  2629   2627   
  2630   2628   op_column_out:
  2631   2629     UPDATE_MAX_BLOBSIZE(pDest);
  2632   2630     REGISTER_TRACE(pOp->p3, pDest);
  2633   2631     break;
         2632  +
         2633  +op_column_corrupt:
         2634  +  if( aOp[0].p3>0 ){
         2635  +    pOp = &aOp[aOp[0].p3-1];
         2636  +    break;
         2637  +  }else{
         2638  +    rc = SQLITE_CORRUPT_BKPT;
         2639  +    goto abort_due_to_error;
         2640  +  }
  2634   2641   }
  2635   2642   
  2636   2643   /* Opcode: Affinity P1 P2 * P4 *
  2637   2644   ** Synopsis: affinity(r[P1@P2])
  2638   2645   **
  2639   2646   ** Apply affinities to a range of P2 registers starting with P1.
  2640   2647   **
................................................................................
  5693   5700     char *z;        /* Text of the error report */
  5694   5701     Mem *pnErr;     /* Register keeping track of errors remaining */
  5695   5702   
  5696   5703     assert( p->bIsReader );
  5697   5704     nRoot = pOp->p2;
  5698   5705     aRoot = pOp->p4.ai;
  5699   5706     assert( nRoot>0 );
  5700         -  assert( aRoot[nRoot]==0 );
         5707  +  assert( aRoot[0]==nRoot );
  5701   5708     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
  5702   5709     pnErr = &aMem[pOp->p3];
  5703   5710     assert( (pnErr->flags & MEM_Int)!=0 );
  5704   5711     assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  5705   5712     pIn1 = &aMem[pOp->p1];
  5706   5713     assert( pOp->p5<db->nDb );
  5707   5714     assert( DbMaskTest(p->btreeMask, pOp->p5) );
  5708         -  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
         5715  +  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
  5709   5716                                    (int)pnErr->u.i+1, &nErr);
  5710   5717     sqlite3VdbeMemSetNull(pIn1);
  5711   5718     if( nErr==0 ){
  5712   5719       assert( z==0 );
  5713   5720     }else if( z==0 ){
  5714   5721       goto no_mem;
  5715   5722     }else{
................................................................................
  7050   7057   
  7051   7058     REGISTER_TRACE(pOp->p3, pOut);
  7052   7059     UPDATE_MAX_BLOBSIZE(pOut);
  7053   7060     break;
  7054   7061   }
  7055   7062   
  7056   7063   
  7057         -/* Opcode: Init P1 P2 * P4 *
         7064  +/* Opcode: Init P1 P2 P3 P4 *
  7058   7065   ** Synopsis: Start at P2
  7059   7066   **
  7060   7067   ** Programs contain a single instance of this opcode as the very first
  7061   7068   ** opcode.
  7062   7069   **
  7063   7070   ** If tracing is enabled (by the sqlite3_trace()) interface, then
  7064   7071   ** the UTF-8 string contained in P4 is emitted on the trace callback.
  7065   7072   ** Or if P4 is blank, use the string returned by sqlite3_sql().
  7066   7073   **
  7067   7074   ** If P2 is not zero, jump to instruction P2.
  7068   7075   **
  7069   7076   ** Increment the value of P1 so that OP_Once opcodes will jump the
  7070   7077   ** first time they are evaluated for this run.
         7078  +**
         7079  +** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
         7080  +** error is encountered.
  7071   7081   */
  7072   7082   case OP_Init: {          /* jump */
  7073   7083     char *zTrace;
  7074   7084     int i;
  7075   7085   
  7076   7086     /* If the P4 argument is not NULL, then it must be an SQL comment string.
  7077   7087     ** The "--" string is broken up to prevent false-positives with srcck1.c.

Changes to src/vdbeaux.c.

    29     29       db->pVdbe->pPrev = p;
    30     30     }
    31     31     p->pNext = db->pVdbe;
    32     32     p->pPrev = 0;
    33     33     db->pVdbe = p;
    34     34     p->magic = VDBE_MAGIC_INIT;
    35     35     p->pParse = pParse;
           36  +  pParse->pVdbe = p;
    36     37     assert( pParse->aLabel==0 );
    37     38     assert( pParse->nLabel==0 );
    38     39     assert( pParse->nOpAlloc==0 );
    39     40     assert( pParse->szOpAlloc==0 );
           41  +  sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
    40     42     return p;
    41     43   }
    42     44   
    43     45   /*
    44     46   ** Change the error string stored in Vdbe.zErrMsg
    45     47   */
    46     48   void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
................................................................................
  1390   1392       }
  1391   1393   #endif
  1392   1394       case P4_INTARRAY: {
  1393   1395         int i;
  1394   1396         int *ai = pOp->p4.ai;
  1395   1397         int n = ai[0];   /* The first element of an INTARRAY is always the
  1396   1398                          ** count of the number of elements to follow */
  1397         -      for(i=1; i<n; i++){
         1399  +      for(i=1; i<=n; i++){
  1398   1400           sqlite3XPrintf(&x, ",%d", ai[i]);
  1399   1401         }
  1400   1402         zTemp[0] = '[';
  1401   1403         sqlite3StrAccumAppend(&x, "]", 1);
  1402   1404         break;
  1403   1405       }
  1404   1406       case P4_SUBPROGRAM: {
................................................................................
  2154   2156     }
  2155   2157   
  2156   2158     /* Delete any auxdata allocations made by the VM */
  2157   2159     if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
  2158   2160     assert( p->pAuxData==0 );
  2159   2161   }
  2160   2162   
  2161         -/*
  2162         -** Clean up the VM after a single run.
  2163         -*/
  2164         -static void Cleanup(Vdbe *p){
  2165         -  sqlite3 *db = p->db;
  2166         -
  2167         -#ifdef SQLITE_DEBUG
  2168         -  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
  2169         -  ** Vdbe.aMem[] arrays have already been cleaned up.  */
  2170         -  int i;
  2171         -  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
  2172         -  if( p->aMem ){
  2173         -    for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
  2174         -  }
  2175         -#endif
  2176         -
  2177         -  sqlite3DbFree(db, p->zErrMsg);
  2178         -  p->zErrMsg = 0;
  2179         -  p->pResultSet = 0;
  2180         -}
  2181         -
  2182   2163   /*
  2183   2164   ** Set the number of result columns that will be returned by this SQL
  2184   2165   ** statement. This is now set at compile time, rather than during
  2185   2166   ** execution of the vdbe program so that sqlite3_column_count() can
  2186   2167   ** be called on an SQL statement before sqlite3_step().
  2187   2168   */
  2188   2169   void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
................................................................................
  2920   2901     ** and error message from the VDBE into the main database structure.  But
  2921   2902     ** if the VDBE has just been set to run but has not actually executed any
  2922   2903     ** instructions yet, leave the main database error information unchanged.
  2923   2904     */
  2924   2905     if( p->pc>=0 ){
  2925   2906       vdbeInvokeSqllog(p);
  2926   2907       sqlite3VdbeTransferError(p);
  2927         -    sqlite3DbFree(db, p->zErrMsg);
  2928         -    p->zErrMsg = 0;
  2929   2908       if( p->runOnlyOnce ) p->expired = 1;
  2930   2909     }else if( p->rc && p->expired ){
  2931   2910       /* The expired flag was set on the VDBE before the first call
  2932   2911       ** to sqlite3_step(). For consistency (since sqlite3_step() was
  2933   2912       ** called), set the database error in this case as well.
  2934   2913       */
  2935   2914       sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
  2936         -    sqlite3DbFree(db, p->zErrMsg);
  2937         -    p->zErrMsg = 0;
  2938   2915     }
  2939   2916   
  2940         -  /* Reclaim all memory used by the VDBE
         2917  +  /* Reset register contents and reclaim error message memory.
  2941   2918     */
  2942         -  Cleanup(p);
         2919  +#ifdef SQLITE_DEBUG
         2920  +  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
         2921  +  ** Vdbe.aMem[] arrays have already been cleaned up.  */
         2922  +  int i;
         2923  +  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
         2924  +  if( p->aMem ){
         2925  +    for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
         2926  +  }
         2927  +#endif
         2928  +  sqlite3DbFree(db, p->zErrMsg);
         2929  +  p->zErrMsg = 0;
         2930  +  p->pResultSet = 0;
  2943   2931   
  2944   2932     /* Save profiling information from this VDBE run.
  2945   2933     */
  2946   2934   #ifdef VDBE_PROFILE
  2947   2935     {
  2948   2936       FILE *out = fopen("vdbe_profile.out", "a");
  2949   2937       if( out ){

Changes to src/vdbeblob.c.

   271    271         Vdbe *v = (Vdbe *)pBlob->pStmt;
   272    272         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   273    273         VdbeOp *aOp;
   274    274   
   275    275         sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, 
   276    276                              pTab->pSchema->schema_cookie,
   277    277                              pTab->pSchema->iGeneration);
   278         -      sqlite3VdbeChangeP5(v, 1);     
          278  +      sqlite3VdbeChangeP5(v, 1);
          279  +      assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
   279    280         aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
   280    281   
   281    282         /* Make sure a mutex is held on the table to be accessed */
   282    283         sqlite3VdbeUsesBtree(v, iDb); 
   283    284   
   284    285         if( db->mallocFailed==0 ){
   285    286           assert( aOp!=0 );
................................................................................
   286    287           /* Configure the OP_TableLock instruction */
   287    288   #ifdef SQLITE_OMIT_SHARED_CACHE
   288    289           aOp[0].opcode = OP_Noop;
   289    290   #else
   290    291           aOp[0].p1 = iDb;
   291    292           aOp[0].p2 = pTab->tnum;
   292    293           aOp[0].p3 = wrFlag;
   293         -        sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
          294  +        sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
   294    295         }
   295    296         if( db->mallocFailed==0 ){
   296    297   #endif
   297    298   
   298    299           /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
   299    300           ** parameter of the other to pTab->tnum.  */
   300    301           if( wrFlag ) aOp[1].opcode = OP_OpenWrite;

Changes to src/vdbemem.c.

   151    151     ** contain a valid string or blob value.  */
   152    152     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
   153    153     testcase( bPreserve && pMem->z==0 );
   154    154   
   155    155     assert( pMem->szMalloc==0
   156    156          || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
   157    157     if( n<32 ) n = 32;
   158         -  if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
          158  +  if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
   159    159       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   160    160       bPreserve = 0;
   161    161     }else{
   162    162       if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
   163    163       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   164    164     }
   165    165     if( pMem->zMalloc==0 ){
................................................................................
   167    167       pMem->z = 0;
   168    168       pMem->szMalloc = 0;
   169    169       return SQLITE_NOMEM_BKPT;
   170    170     }else{
   171    171       pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
   172    172     }
   173    173   
   174         -  if( bPreserve && pMem->z && ALWAYS(pMem->z!=pMem->zMalloc) ){
          174  +  if( bPreserve && pMem->z ){
          175  +    assert( pMem->z!=pMem->zMalloc );
   175    176       memcpy(pMem->zMalloc, pMem->z, pMem->n);
   176    177     }
   177    178     if( (pMem->flags&MEM_Dyn)!=0 ){
   178    179       assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
   179    180       pMem->xDel((void *)(pMem->z));
   180    181     }
   181    182   
................................................................................
   204    205       return sqlite3VdbeMemGrow(pMem, szNew, 0);
   205    206     }
   206    207     assert( (pMem->flags & MEM_Dyn)==0 );
   207    208     pMem->z = pMem->zMalloc;
   208    209     pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
   209    210     return SQLITE_OK;
   210    211   }
          212  +
          213  +/*
          214  +** It is already known that pMem contains an unterminated string.
          215  +** Add the zero terminator.
          216  +*/
          217  +static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
          218  +  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
          219  +    return SQLITE_NOMEM_BKPT;
          220  +  }
          221  +  pMem->z[pMem->n] = 0;
          222  +  pMem->z[pMem->n+1] = 0;
          223  +  pMem->flags |= MEM_Term;
          224  +  return SQLITE_OK;
          225  +}
   211    226   
   212    227   /*
   213    228   ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
   214    229   ** MEM.zMalloc, where it can be safely written.
   215    230   **
   216    231   ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
   217    232   */
   218    233   int sqlite3VdbeMemMakeWriteable(Mem *pMem){
   219    234     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   220    235     assert( (pMem->flags&MEM_RowSet)==0 );
   221    236     if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
   222    237       if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
   223    238       if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
   224         -      if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
   225         -        return SQLITE_NOMEM_BKPT;
   226         -      }
   227         -      pMem->z[pMem->n] = 0;
   228         -      pMem->z[pMem->n+1] = 0;
   229         -      pMem->flags |= MEM_Term;
          239  +      int rc = vdbeMemAddTerminator(pMem);
          240  +      if( rc ) return rc;
   230    241       }
   231    242     }
   232    243     pMem->flags &= ~MEM_Ephem;
   233    244   #ifdef SQLITE_DEBUG
   234    245     pMem->pScopyFrom = 0;
   235    246   #endif
   236    247   
................................................................................
   261    272     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
   262    273     pMem->n += pMem->u.nZero;
   263    274     pMem->flags &= ~(MEM_Zero|MEM_Term);
   264    275     return SQLITE_OK;
   265    276   }
   266    277   #endif
   267    278   
   268         -/*
   269         -** It is already known that pMem contains an unterminated string.
   270         -** Add the zero terminator.
   271         -*/
   272         -static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
   273         -  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
   274         -    return SQLITE_NOMEM_BKPT;
   275         -  }
   276         -  pMem->z[pMem->n] = 0;
   277         -  pMem->z[pMem->n+1] = 0;
   278         -  pMem->flags |= MEM_Term;
   279         -  return SQLITE_OK;
   280         -}
   281         -
   282    279   /*
   283    280   ** Make sure the given Mem is \u0000 terminated.
   284    281   */
   285    282   int sqlite3VdbeMemNulTerminate(Mem *pMem){
   286    283     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   287    284     testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
   288    285     testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
................................................................................
   593    590   **
   594    591   ** Every effort is made to force the conversion, even if the input
   595    592   ** is a string that does not look completely like a number.  Convert
   596    593   ** as much of the string as we can and ignore the rest.
   597    594   */
   598    595   int sqlite3VdbeMemNumerify(Mem *pMem){
   599    596     if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
          597  +    int rc;
   600    598       assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
   601    599       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   602         -    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
          600  +    rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
          601  +    if( rc==0 ){
   603    602         MemSetTypeFlag(pMem, MEM_Int);
   604    603       }else{
   605         -      pMem->u.r = sqlite3VdbeRealValue(pMem);
   606         -      MemSetTypeFlag(pMem, MEM_Real);
   607         -      sqlite3VdbeIntegerAffinity(pMem);
          604  +      i64 i = pMem->u.i;
          605  +      sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
          606  +      if( rc==1 && pMem->u.r==(double)i ){
          607  +        pMem->u.i = i;
          608  +        MemSetTypeFlag(pMem, MEM_Int);
          609  +      }else{
          610  +        MemSetTypeFlag(pMem, MEM_Real);
          611  +      }
   608    612       }
   609    613     }
   610    614     assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
   611    615     pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
   612    616     return SQLITE_OK;
   613    617   }
   614    618   
................................................................................
  1005   1009     BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
  1006   1010     u32 offset,       /* Offset from the start of data to return bytes from. */
  1007   1011     u32 amt,          /* Number of bytes to return. */
  1008   1012     Mem *pMem         /* OUT: Return data in this Mem structure. */
  1009   1013   ){
  1010   1014     int rc;
  1011   1015     pMem->flags = MEM_Null;
  1012         -  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
         1016  +  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt)) ){
  1013   1017       rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
  1014   1018       if( rc==SQLITE_OK ){
  1015         -      pMem->z[amt] = 0;
  1016         -      pMem->z[amt+1] = 0;
  1017         -      pMem->flags = MEM_Blob|MEM_Term;
         1019  +      pMem->flags = MEM_Blob;
  1018   1020         pMem->n = (int)amt;
  1019   1021       }else{
  1020   1022         sqlite3VdbeMemRelease(pMem);
  1021   1023       }
  1022   1024     }
  1023   1025     return rc;
  1024   1026   }

Changes to src/where.c.

   864    864       if( pTerm->leftCursor != pSrc->iCursor ) continue;
   865    865       if( pTerm->prereqRight & mUnusable ) continue;
   866    866       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
   867    867       testcase( pTerm->eOperator & WO_IN );
   868    868       testcase( pTerm->eOperator & WO_ISNULL );
   869    869       testcase( pTerm->eOperator & WO_IS );
   870    870       testcase( pTerm->eOperator & WO_ALL );
   871         -    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
          871  +    if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
   872    872       if( pTerm->wtFlags & TERM_VNULL ) continue;
   873    873       assert( pTerm->u.leftColumn>=(-1) );
   874    874       nTerm++;
   875    875     }
   876    876   
   877    877     /* If the ORDER BY clause contains only columns in the current 
   878    878     ** virtual table then allocate space for the aOrderBy part of
................................................................................
   912    912     *(int*)&pIdxInfo->nOrderBy = nOrderBy;
   913    913     *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
   914    914     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
   915    915     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
   916    916                                                                      pUsage;
   917    917   
   918    918     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
   919         -    u8 op;
          919  +    u16 op;
   920    920       if( pTerm->leftCursor != pSrc->iCursor ) continue;
   921    921       if( pTerm->prereqRight & mUnusable ) continue;
   922    922       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
   923    923       testcase( pTerm->eOperator & WO_IN );
   924    924       testcase( pTerm->eOperator & WO_IS );
   925    925       testcase( pTerm->eOperator & WO_ISNULL );
   926    926       testcase( pTerm->eOperator & WO_ALL );
   927         -    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
          927  +    if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
   928    928       if( pTerm->wtFlags & TERM_VNULL ) continue;
   929    929       assert( pTerm->u.leftColumn>=(-1) );
   930    930       pIdxCons[j].iColumn = pTerm->u.leftColumn;
   931    931       pIdxCons[j].iTermOffset = i;
   932         -    op = (u8)pTerm->eOperator & WO_ALL;
          932  +    op = pTerm->eOperator & WO_ALL;
   933    933       if( op==WO_IN ) op = WO_EQ;
   934         -    if( op==WO_MATCH ){
   935         -      op = pTerm->eMatchOp;
   936         -    }
   937         -    pIdxCons[j].op = op;
   938         -    /* The direct assignment in the previous line is possible only because
   939         -    ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
   940         -    ** following asserts verify this fact. */
   941         -    assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
   942         -    assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
   943         -    assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
   944         -    assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
   945         -    assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
   946         -    assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
   947         -    assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
   948         -
   949         -    if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
   950         -     && sqlite3ExprIsVector(pTerm->pExpr->pRight) 
   951         -    ){
   952         -      if( i<16 ) mNoOmit |= (1 << i);
   953         -      if( op==WO_LT ) pIdxCons[j].op = WO_LE;
   954         -      if( op==WO_GT ) pIdxCons[j].op = WO_GE;
          934  +    if( op==WO_AUX ){
          935  +      pIdxCons[j].op = pTerm->eMatchOp;
          936  +    }else if( op & (WO_ISNULL|WO_IS) ){
          937  +      if( op==WO_ISNULL ){
          938  +        pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
          939  +      }else{
          940  +        pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
          941  +      }
          942  +    }else{
          943  +      pIdxCons[j].op = (u8)op;
          944  +      /* The direct assignment in the previous line is possible only because
          945  +      ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
          946  +      ** following asserts verify this fact. */
          947  +      assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
          948  +      assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
          949  +      assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
          950  +      assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
          951  +      assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
          952  +      assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
          953  +
          954  +      if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
          955  +       && sqlite3ExprIsVector(pTerm->pExpr->pRight) 
          956  +      ){
          957  +        if( i<16 ) mNoOmit |= (1 << i);
          958  +        if( op==WO_LT ) pIdxCons[j].op = WO_LE;
          959  +        if( op==WO_GT ) pIdxCons[j].op = WO_GE;
          960  +      }
   955    961       }
   956    962   
   957    963       j++;
   958    964     }
   959    965     for(i=0; i<nOrderBy; i++){
   960    966       Expr *pExpr = pOrderBy->a[i].pExpr;
   961    967       pIdxOrderBy[i].iColumn = pExpr->iColumn;
................................................................................
  4324   4330   ** part of sub-select statements.
  4325   4331   */
  4326   4332   static int exprIsDeterministic(Expr *p){
  4327   4333     Walker w;
  4328   4334     memset(&w, 0, sizeof(w));
  4329   4335     w.eCode = 1;
  4330   4336     w.xExprCallback = exprNodeIsDeterministic;
         4337  +  w.xSelectCallback = sqlite3SelectWalkFail;
  4331   4338     sqlite3WalkExpr(&w, p);
  4332   4339     return w.eCode;
  4333   4340   }
  4334   4341   
  4335   4342   /*
  4336   4343   ** Generate the beginning of the loop used for WHERE clause processing.
  4337   4344   ** The return value is a pointer to an opaque structure that contains
................................................................................
  4787   4794       }else{
  4788   4795         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  4789   4796       }
  4790   4797       if( pLoop->wsFlags & WHERE_INDEXED ){
  4791   4798         Index *pIx = pLoop->u.btree.pIndex;
  4792   4799         int iIndexCur;
  4793   4800         int op = OP_OpenRead;
  4794         -      /* iAuxArg is always set if to a positive value if ONEPASS is possible */
         4801  +      /* iAuxArg is always set to a positive value if ONEPASS is possible */
  4795   4802         assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
  4796   4803         if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
  4797   4804          && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
  4798   4805         ){
  4799   4806           /* This is one term of an OR-optimization using the PRIMARY KEY of a
  4800   4807           ** WITHOUT ROWID table.  No need for a separate index */
  4801   4808           iIndexCur = pLevel->iTabCur;

Changes to src/whereInt.h.

   511    511   **
   512    512   ** Value constraints:
   513    513   **     WO_EQ    == SQLITE_INDEX_CONSTRAINT_EQ
   514    514   **     WO_LT    == SQLITE_INDEX_CONSTRAINT_LT
   515    515   **     WO_LE    == SQLITE_INDEX_CONSTRAINT_LE
   516    516   **     WO_GT    == SQLITE_INDEX_CONSTRAINT_GT
   517    517   **     WO_GE    == SQLITE_INDEX_CONSTRAINT_GE
   518         -**     WO_MATCH == SQLITE_INDEX_CONSTRAINT_MATCH
   519    518   */
   520    519   #define WO_IN     0x0001
   521    520   #define WO_EQ     0x0002
   522    521   #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
   523    522   #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
   524    523   #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
   525    524   #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
   526         -#define WO_MATCH  0x0040
          525  +#define WO_AUX    0x0040       /* Op useful to virtual tables only */
   527    526   #define WO_IS     0x0080
   528    527   #define WO_ISNULL 0x0100
   529    528   #define WO_OR     0x0200       /* Two or more OR-connected terms */
   530    529   #define WO_AND    0x0400       /* Two or more AND-connected terms */
   531    530   #define WO_EQUIV  0x0800       /* Of the form A==B, both columns */
   532    531   #define WO_NOOP   0x1000       /* This term does not restrict search space */
   533    532   

Changes to src/wherecode.c.

  1013   1013   **
  1014   1014   ** If the expression is not a vector, then nReg must be passed 1. In
  1015   1015   ** this case, generate code to evaluate the expression and leave the
  1016   1016   ** result in register iReg.
  1017   1017   */
  1018   1018   static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
  1019   1019     assert( nReg>0 );
  1020         -  if( sqlite3ExprIsVector(p) ){
         1020  +  if( p && sqlite3ExprIsVector(p) ){
  1021   1021   #ifndef SQLITE_OMIT_SUBQUERY
  1022   1022       if( (p->flags & EP_xIsSelect) ){
  1023   1023         Vdbe *v = pParse->pVdbe;
  1024   1024         int iSelect = sqlite3CodeSubselect(pParse, p, 0, 0);
  1025   1025         sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
  1026   1026       }else
  1027   1027   #endif

Changes to src/whereexpr.c.

   308    308     return rc;
   309    309   }
   310    310   #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
   311    311   
   312    312   
   313    313   #ifndef SQLITE_OMIT_VIRTUALTABLE
   314    314   /*
   315         -** Check to see if the given expression is of the form
          315  +** Check to see if the pExpr expression is a form that needs to be passed
          316  +** to the xBestIndex method of virtual tables.  Forms of interest include:
   316    317   **
   317         -**         column OP expr
          318  +**          Expression                   Virtual Table Operator
          319  +**          -----------------------      ---------------------------------
          320  +**      1.  column MATCH expr            SQLITE_INDEX_CONSTRAINT_MATCH
          321  +**      2.  column GLOB expr             SQLITE_INDEX_CONSTRAINT_GLOB
          322  +**      3.  column LIKE expr             SQLITE_INDEX_CONSTRAINT_LIKE
          323  +**      4.  column REGEXP expr           SQLITE_INDEX_CONSTRAINT_REGEXP
          324  +**      5.  column != expr               SQLITE_INDEX_CONSTRAINT_NE
          325  +**      6.  expr != column               SQLITE_INDEX_CONSTRAINT_NE
          326  +**      7.  column IS NOT expr           SQLITE_INDEX_CONSTRAINT_ISNOT
          327  +**      8.  expr IS NOT column           SQLITE_INDEX_CONSTRAINT_ISNOT
          328  +**      9.  column IS NOT NULL           SQLITE_INDEX_CONSTRAINT_ISNOTNULL
   318    329   **
   319         -** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a 
   320         -** column of a virtual table.
          330  +** In every case, "column" must be a column of a virtual table.  If there
          331  +** is a match, set *ppLeft to the "column" expression, set *ppRight to the 
          332  +** "expr" expression (even though in forms (6) and (8) the column is on the
          333  +** right and the expression is on the left).  Also set *peOp2 to the
          334  +** appropriate virtual table operator.  The return value is 1 or 2 if there
          335  +** is a match.  The usual return is 1, but if the RHS is also a column
          336  +** of virtual table in forms (5) or (7) then return 2.
   321    337   **
   322         -** If it is then return TRUE.  If not, return FALSE.
          338  +** If the expression matches none of the patterns above, return 0.
   323    339   */
   324         -static int isMatchOfColumn(
          340  +static int isAuxiliaryVtabOperator(
   325    341     Expr *pExpr,                    /* Test this expression */
   326         -  unsigned char *peOp2            /* OUT: 0 for MATCH, or else an op2 value */
          342  +  unsigned char *peOp2,           /* OUT: 0 for MATCH, or else an op2 value */
          343  +  Expr **ppLeft,                  /* Column expression to left of MATCH/op2 */
          344  +  Expr **ppRight                  /* Expression to left of MATCH/op2 */
   327    345   ){
   328         -  static const struct Op2 {
   329         -    const char *zOp;
   330         -    unsigned char eOp2;
   331         -  } aOp[] = {
   332         -    { "match",  SQLITE_INDEX_CONSTRAINT_MATCH },
   333         -    { "glob",   SQLITE_INDEX_CONSTRAINT_GLOB },
   334         -    { "like",   SQLITE_INDEX_CONSTRAINT_LIKE },
   335         -    { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
   336         -  };
   337         -  ExprList *pList;
   338         -  Expr *pCol;                     /* Column reference */
   339         -  int i;
   340         -
   341         -  if( pExpr->op!=TK_FUNCTION ){
   342         -    return 0;
   343         -  }
   344         -  pList = pExpr->x.pList;
   345         -  if( pList==0 || pList->nExpr!=2 ){
   346         -    return 0;
   347         -  }
   348         -  pCol = pList->a[1].pExpr;
   349         -  if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
   350         -    return 0;
   351         -  }
   352         -  for(i=0; i<ArraySize(aOp); i++){
   353         -    if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
   354         -      *peOp2 = aOp[i].eOp2;
   355         -      return 1;
   356         -    }
          346  +  if( pExpr->op==TK_FUNCTION ){
          347  +    static const struct Op2 {
          348  +      const char *zOp;
          349  +      unsigned char eOp2;
          350  +    } aOp[] = {
          351  +      { "match",  SQLITE_INDEX_CONSTRAINT_MATCH },
          352  +      { "glob",   SQLITE_INDEX_CONSTRAINT_GLOB },
          353  +      { "like",   SQLITE_INDEX_CONSTRAINT_LIKE },
          354  +      { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
          355  +    };
          356  +    ExprList *pList;
          357  +    Expr *pCol;                     /* Column reference */
          358  +    int i;
          359  +
          360  +    pList = pExpr->x.pList;
          361  +    if( pList==0 || pList->nExpr!=2 ){
          362  +      return 0;
          363  +    }
          364  +    pCol = pList->a[1].pExpr;
          365  +    if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
          366  +      return 0;
          367  +    }
          368  +    for(i=0; i<ArraySize(aOp); i++){
          369  +      if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
          370  +        *peOp2 = aOp[i].eOp2;
          371  +        *ppRight = pList->a[0].pExpr;
          372  +        *ppLeft = pCol;
          373  +        return 1;
          374  +      }
          375  +    }
          376  +  }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
          377  +    int res = 0;
          378  +    Expr *pLeft = pExpr->pLeft;
          379  +    Expr *pRight = pExpr->pRight;
          380  +    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
          381  +      res++;
          382  +    }
          383  +    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
          384  +      res++;
          385  +      SWAP(Expr*, pLeft, pRight);
          386  +    }
          387  +    *ppLeft = pLeft;
          388  +    *ppRight = pRight;
          389  +    if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
          390  +    if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
          391  +    if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
          392  +    return res;
   357    393     }
   358    394     return 0;
   359    395   }
   360    396   #endif /* SQLITE_OMIT_VIRTUALTABLE */
   361    397   
   362    398   /*
   363    399   ** If the pBase expression originated in the ON or USING clause of
................................................................................
   600    636           sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
   601    637           sqlite3WhereExprAnalyze(pSrc, pAndWC);
   602    638           pAndWC->pOuter = pWC;
   603    639           if( !db->mallocFailed ){
   604    640             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
   605    641               assert( pAndTerm->pExpr );
   606    642               if( allowedOp(pAndTerm->pExpr->op) 
   607         -             || pAndTerm->eOperator==WO_MATCH 
          643  +             || pAndTerm->eOperator==WO_AUX
   608    644               ){
   609    645                 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
   610    646               }
   611    647             }
   612    648           }
   613    649           indexable &= b;
   614    650         }
................................................................................
  1182   1218         markTermAsChild(pWC, idxNew1, idxTerm);
  1183   1219         markTermAsChild(pWC, idxNew2, idxTerm);
  1184   1220       }
  1185   1221     }
  1186   1222   #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
  1187   1223   
  1188   1224   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1189         -  /* Add a WO_MATCH auxiliary term to the constraint set if the
  1190         -  ** current expression is of the form:  column MATCH expr.
         1225  +  /* Add a WO_AUX auxiliary term to the constraint set if the
         1226  +  ** current expression is of the form "column OP expr" where OP
         1227  +  ** is an operator that gets passed into virtual tables but which is
         1228  +  ** not normally optimized for ordinary tables.  In other words, OP
         1229  +  ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
  1191   1230     ** This information is used by the xBestIndex methods of
  1192   1231     ** virtual tables.  The native query optimizer does not attempt
  1193   1232     ** to do anything with MATCH functions.
  1194   1233     */
  1195         -  if( pWC->op==TK_AND && isMatchOfColumn(pExpr, &eOp2) ){
  1196         -    int idxNew;
         1234  +  if( pWC->op==TK_AND ){
  1197   1235       Expr *pRight, *pLeft;
  1198         -    WhereTerm *pNewTerm;
  1199         -    Bitmask prereqColumn, prereqExpr;
         1236  +    int res = isAuxiliaryVtabOperator(pExpr, &eOp2, &pLeft, &pRight);
         1237  +    while( res-- > 0 ){
         1238  +      int idxNew;
         1239  +      WhereTerm *pNewTerm;
         1240  +      Bitmask prereqColumn, prereqExpr;
  1200   1241   
  1201         -    pRight = pExpr->x.pList->a[0].pExpr;
  1202         -    pLeft = pExpr->x.pList->a[1].pExpr;
  1203         -    prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
  1204         -    prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
  1205         -    if( (prereqExpr & prereqColumn)==0 ){
  1206         -      Expr *pNewExpr;
  1207         -      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
  1208         -                              0, sqlite3ExprDup(db, pRight, 0));
  1209         -      if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
  1210         -        ExprSetProperty(pNewExpr, EP_FromJoin);
         1242  +      prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
         1243  +      prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
         1244  +      if( (prereqExpr & prereqColumn)==0 ){
         1245  +        Expr *pNewExpr;
         1246  +        pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
         1247  +            0, sqlite3ExprDup(db, pRight, 0));
         1248  +        if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
         1249  +          ExprSetProperty(pNewExpr, EP_FromJoin);
         1250  +        }
         1251  +        idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
         1252  +        testcase( idxNew==0 );
         1253  +        pNewTerm = &pWC->a[idxNew];
         1254  +        pNewTerm->prereqRight = prereqExpr;
         1255  +        pNewTerm->leftCursor = pLeft->iTable;
         1256  +        pNewTerm->u.leftColumn = pLeft->iColumn;
         1257  +        pNewTerm->eOperator = WO_AUX;
         1258  +        pNewTerm->eMatchOp = eOp2;
         1259  +        markTermAsChild(pWC, idxNew, idxTerm);
         1260  +        pTerm = &pWC->a[idxTerm];
         1261  +        pTerm->wtFlags |= TERM_COPIED;
         1262  +        pNewTerm->prereqAll = pTerm->prereqAll;
  1211   1263         }
  1212         -      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1213         -      testcase( idxNew==0 );
  1214         -      pNewTerm = &pWC->a[idxNew];
  1215         -      pNewTerm->prereqRight = prereqExpr;
  1216         -      pNewTerm->leftCursor = pLeft->iTable;
  1217         -      pNewTerm->u.leftColumn = pLeft->iColumn;
  1218         -      pNewTerm->eOperator = WO_MATCH;
  1219         -      pNewTerm->eMatchOp = eOp2;
  1220         -      markTermAsChild(pWC, idxNew, idxTerm);
  1221         -      pTerm = &pWC->a[idxTerm];
  1222         -      pTerm->wtFlags |= TERM_COPIED;
  1223         -      pNewTerm->prereqAll = pTerm->prereqAll;
         1264  +      SWAP(Expr*, pLeft, pRight);
  1224   1265       }
  1225   1266     }
  1226   1267   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1227   1268   
  1228   1269     /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
  1229   1270     ** new terms for each component comparison - "a = ?" and "b = ?".  The
  1230   1271     ** new terms completely replace the original vector comparison, which is

Added test/bestindex5.test.

            1  +# 2017 September 10
            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  +# Test the virtual table interface. In particular the xBestIndex
           12  +# method.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix bestindex4
           18  +
           19  +ifcapable !vtab {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +#-------------------------------------------------------------------------
           25  +# Virtual table callback for a virtual table named $tbl.
           26  +#  
           27  +proc vtab_cmd {method args} {
           28  +
           29  +  set binops(ne)    !=
           30  +  set binops(eq)    =
           31  +  set binops(isnot) "IS NOT"
           32  +  set binops(is)    "IS"
           33  +
           34  +  set unops(isnotnull) "IS NOT NULL"
           35  +  set unops(isnull)    "IS NULL"
           36  +
           37  +  set cols(0) a
           38  +  set cols(1) b
           39  +  set cols(2) c
           40  +
           41  +  switch -- $method {
           42  +    xConnect {
           43  +      return "CREATE TABLE t1(a, b, c)"
           44  +    }
           45  +
           46  +    xBestIndex {
           47  +      foreach {clist orderby mask} $args {}
           48  +
           49  +      set cost 1000000.0
           50  +      set ret [list]
           51  +      set str [list]
           52  +
           53  +      set v 0
           54  +      for {set i 0} {$i < [llength $clist]} {incr i} {
           55  +        array unset C
           56  +        array set C [lindex $clist $i]
           57  +        if {$C(usable)} {
           58  +          if {[info exists binops($C(op))]} {
           59  +            lappend ret omit $i
           60  +            lappend str "$cols($C(column)) $binops($C(op)) %$v%"
           61  +            incr v
           62  +            set cost [expr $cost / 2]
           63  +          }
           64  +          if {[info exists unops($C(op))]} {
           65  +            lappend ret omit $i
           66  +            lappend str "$cols($C(column)) $unops($C(op))"
           67  +            incr v
           68  +            set cost [expr $cost / 2]
           69  +          }
           70  +        }
           71  +      }
           72  +
           73  +      lappend ret idxstr [join $str " AND "]
           74  +      lappend ret cost $cost
           75  +      return $ret
           76  +    }
           77  +
           78  +    xFilter {
           79  +      set q [lindex $args 1]
           80  +      set a [lindex $args 2]
           81  +      for {set v 0} {$v < [llength $a]} {incr v} {
           82  +        set val [lindex $a $v]
           83  +        set q [string map [list %$v% '$val'] $q]
           84  +      }
           85  +      if {$q==""} { set q 1 }
           86  +      lappend ::xFilterQueries "WHERE $q"
           87  +      return [list sql "SELECT rowid, * FROM t1x WHERE $q"]
           88  +    }
           89  +  }
           90  +  return ""
           91  +}
           92  +
           93  +proc vtab_simple {method args} {
           94  +  switch -- $method {
           95  +    xConnect {
           96  +      return "CREATE TABLE t2(x)"
           97  +    }
           98  +    xBestIndex {
           99  +      return [list cost 999999.0]
          100  +    }
          101  +    xFilter {
          102  +      return [list sql "SELECT rowid, * FROM t2x"]
          103  +    }
          104  +  }
          105  +  return ""
          106  +}
          107  +
          108  +register_tcl_module db
          109  +
          110  +proc do_vtab_query_test {tn query result} {
          111  +  set ::xFilterQueries [list]
          112  +  uplevel [list
          113  +    do_test $tn [string map [list %QUERY% $query] {
          114  +      set r [execsql {%QUERY%}]
          115  +      set r [concat $::xFilterQueries $r]
          116  +      set r
          117  +    }] [list {*}$result]
          118  +  ]
          119  +}
          120  +
          121  +do_execsql_test 1.0 {
          122  +  CREATE VIRTUAL TABLE t1 USING tcl('vtab_cmd');
          123  +  CREATE TABLE t1x(a INTEGER, b TEXT, c REAL);
          124  +  INSERT INTO t1x VALUES(1, 2, 3);
          125  +  INSERT INTO t1x VALUES(4, 5, 6);
          126  +  INSERT INTO t1x VALUES(7, 8, 9);
          127  +
          128  +  CREATE VIRTUAL TABLE t2 USING tcl('vtab_simple');
          129  +  CREATE TABLE t2x(x INTEGER);
          130  +  INSERT INTO t2x VALUES(1);
          131  +}
          132  +
          133  +do_vtab_query_test 1.1 { SELECT * FROM t1 WHERE a!='hello'; } {
          134  +  "WHERE a != 'hello'"
          135  +  1 2 3.0 4 5 6.0 7 8 9.0
          136  +}
          137  +
          138  +do_vtab_query_test 1.2.1 { SELECT * FROM t1 WHERE b!=8 } {
          139  +  "WHERE b != '8'"
          140  +  1 2 3.0 4 5 6.0
          141  +}
          142  +do_vtab_query_test 1.2.2 { SELECT * FROM t1 WHERE 8!=b } {
          143  +  "WHERE b != '8'"
          144  +  1 2 3.0 4 5 6.0
          145  +}
          146  +
          147  +do_vtab_query_test 1.3 { SELECT * FROM t1 WHERE c IS NOT 3 } {
          148  +  "WHERE c IS NOT '3'"
          149  +  4 5 6.0 7 8 9.0
          150  +}
          151  +do_vtab_query_test 1.3.2 { SELECT * FROM t1 WHERE 3 IS NOT c } {
          152  +  "WHERE c IS NOT '3'"
          153  +  4 5 6.0 7 8 9.0
          154  +}
          155  +
          156  +do_vtab_query_test 1.4.1 { SELECT * FROM t1, t2 WHERE x != a } {
          157  +  "WHERE a != '1'"
          158  +  4 5 6.0 1   7 8 9.0 1
          159  +}
          160  +do_vtab_query_test 1.4.2 { SELECT * FROM t1, t2 WHERE a != x } {
          161  +  "WHERE a != '1'"
          162  +  4 5 6.0 1   7 8 9.0 1
          163  +}
          164  +
          165  +do_vtab_query_test 1.5.1 { SELECT * FROM t1 WHERE a IS NOT NULL } {
          166  +  "WHERE a IS NOT NULL"
          167  +  1 2 3.0 4 5 6.0 7 8 9.0
          168  +}
          169  +do_vtab_query_test 1.5.2 { SELECT * FROM t1 WHERE NULL IS NOT a } {
          170  +  "WHERE a IS NOT ''"
          171  +  1 2 3.0 4 5 6.0 7 8 9.0
          172  +}
          173  +
          174  +do_vtab_query_test 1.6.1 { SELECT * FROM t1 WHERE a IS NULL } {
          175  +  "WHERE a IS NULL"
          176  +}
          177  +
          178  +do_vtab_query_test 1.6.2 { SELECT * FROM t1 WHERE NULL IS a } {
          179  +  "WHERE a IS ''"
          180  +}
          181  +
          182  +do_vtab_query_test 1.7.1 { SELECT * FROM t1 WHERE (a, b) IS (1, 2) } {
          183  +  "WHERE a IS '1' AND b IS '2'"
          184  +  1 2 3.0
          185  +}
          186  +do_vtab_query_test 1.7.2 { SELECT * FROM t1 WHERE (5, 4) IS (b, a) } {
          187  +  {WHERE b IS '5' AND a IS '4'} 
          188  +  4 5 6.0
          189  +}
          190  +
          191  +#---------------------------------------------------------------------
          192  +do_execsql_test 2.0.0 {
          193  +  DELETE FROM t1x;
          194  +  INSERT INTO t1x VALUES('a', 'b', 'c');
          195  +}
          196  +do_execsql_test 2.0.1 { SELECT * FROM t1 } {a b c}
          197  +do_execsql_test 2.0.2 { SELECT * FROM t1 WHERE (a, b) != ('a', 'b'); } {}
          198  +
          199  +do_execsql_test 2.1.0 {
          200  +  DELETE FROM t1x;
          201  +  INSERT INTO t1x VALUES(7, 8, 9);
          202  +}
          203  +do_execsql_test 2.1.1 { SELECT * FROM t1 } {7 8 9.0}
          204  +do_execsql_test 2.1.2 { SELECT * FROM t1 WHERE (a, b) != (7, '8') } {}
          205  +do_execsql_test 2.1.3 { SELECT * FROM t1 WHERE a!=7 OR b!='8' }
          206  +do_execsql_test 2.1.4 { SELECT * FROM t1 WHERE a!=7 OR b!='8' }
          207  +
          208  +
          209  +do_execsql_test 2.2.1 {
          210  +  CREATE TABLE t3(a INTEGER, b TEXT);
          211  +  INSERT INTO t3 VALUES(45, 46);
          212  +}
          213  +do_execsql_test 2.2.2 { SELECT * FROM t3 WHERE (a, b) != (45, 46); }
          214  +do_execsql_test 2.2.3 { SELECT * FROM t3 WHERE (a, b) != ('45', '46'); }
          215  +do_execsql_test 2.2.4 { SELECT * FROM t3 WHERE (a, b) == (45, 46); } {45 46}
          216  +do_execsql_test 2.2.5 { SELECT * FROM t3 WHERE (a, b) == ('45', '46'); } {45 46}
          217  +
          218  +#---------------------------------------------------------------------
          219  +# Test the != operator on a virtual table with column affinities.
          220  +#
          221  +proc vtab_simple_integer {method args} {
          222  +  switch -- $method {
          223  +    xConnect {
          224  +      return "CREATE TABLE t4(x INTEGER)"
          225  +    }
          226  +    xBestIndex {
          227  +      return [list cost 999999.0]
          228  +    }
          229  +    xFilter {
          230  +      return [list sql "SELECT rowid, * FROM t4x"]
          231  +    }
          232  +  }
          233  +  return ""
          234  +}
          235  +
          236  +do_execsql_test 3.0 {
          237  +  CREATE TABLE t4x(a INTEGER);
          238  +  INSERT INTO t4x VALUES(245);
          239  +  CREATE VIRTUAL TABLE t4 USING tcl('vtab_simple_integer');
          240  +}
          241  +do_execsql_test 3.1 { SELECT rowid, * FROM t4 WHERE x=245; } {1 245}
          242  +do_execsql_test 3.2 { SELECT rowid, * FROM t4 WHERE x='245'; } {1 245}
          243  +do_execsql_test 3.3 { SELECT rowid, * FROM t4 WHERE x!=245; } {}
          244  +do_execsql_test 3.4 { SELECT rowid, * FROM t4 WHERE x!='245'; } {}
          245  +
          246  +do_execsql_test 3.5 { SELECT rowid, * FROM t4 WHERE rowid!=1 OR x!='245'; } {}
          247  +
          248  +
          249  +finish_test
          250  +

Changes to test/corruptC.test.

   160    160     hexio_write test.db 3119 [format %02x 0xdf]
   161    161     hexio_write test.db 4073 [format %02x 0xbf]
   162    162   
   163    163     sqlite3 db test.db
   164    164     catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
   165    165     catchsql {PRAGMA integrity_check}
   166    166   } {0 {{*** in database main ***
   167         -On tree page 4 cell 19: Extends off end of page}}}
          167  +On tree page 4 cell 19: Extends off end of page} {database disk image is malformed}}}
   168    168   
   169    169   # {0 {{*** in database main ***
   170    170   # Corruption detected in cell 710 on page 4
   171    171   # Multiple uses for byte 661 of page 4
   172    172   # Fragmented space is 249 byte reported as 21 on page 4}}}
   173    173   
   174    174   # test that a corrupt free cell size is handled (seed 169595)

Changes to test/e_expr.test.

  1659   1659   
  1660   1660   do_expr_test e_expr-32.2.3 { 
  1661   1661     CAST(-9223372036854775808 AS NUMERIC)
  1662   1662   } integer -9223372036854775808
  1663   1663   do_expr_test e_expr-32.2.4 { 
  1664   1664     CAST(9223372036854775807 AS NUMERIC)
  1665   1665   } integer 9223372036854775807
         1666  +do_expr_test e_expr-32.2.5 { 
         1667  +  CAST('9223372036854775807 ' AS NUMERIC)
         1668  +} integer 9223372036854775807
         1669  +do_expr_test e_expr-32.2.6 { 
         1670  +  CAST('   9223372036854775807   ' AS NUMERIC)
         1671  +} integer 9223372036854775807
         1672  +do_expr_test e_expr-32.2.7 { 
         1673  +  CAST('  ' AS NUMERIC)
         1674  +} integer 0
         1675  +do_execsql_test e_expr-32.2.8 {
         1676  +  WITH t1(x) AS (VALUES
         1677  +     ('9000000000000000001'),
         1678  +     ('9000000000000000001x'),
         1679  +     ('9000000000000000001 '),
         1680  +     (' 9000000000000000001 '),
         1681  +     (' 9000000000000000001'),
         1682  +     (' 9000000000000000001.'),
         1683  +     ('9223372036854775807'),
         1684  +     ('9223372036854775807 '),
         1685  +     ('   9223372036854775807   '),
         1686  +     ('9223372036854775808'),
         1687  +     ('   9223372036854775808   '),
         1688  +     ('9223372036854775807.0'),
         1689  +     ('9223372036854775807e+0'),
         1690  +     ('-5.0'),
         1691  +     ('-5e+0'))
         1692  +  SELECT typeof(CAST(x AS NUMERIC)), CAST(x AS NUMERIC)||'' FROM t1;
         1693  +} [list \
         1694  + integer 9000000000000000001 \
         1695  + integer 9000000000000000001 \
         1696  + integer 9000000000000000001 \
         1697  + integer 9000000000000000001 \
         1698  + integer 9000000000000000001 \
         1699  + integer 9000000000000000001 \
         1700  + integer 9223372036854775807 \
         1701  + integer 9223372036854775807 \
         1702  + integer 9223372036854775807 \
         1703  + real 9.22337203685478e+18 \
         1704  + real 9.22337203685478e+18 \
         1705  + integer 9223372036854775807 \
         1706  + integer 9223372036854775807 \
         1707  + integer -5 \
         1708  + integer -5 \
         1709  +]
  1666   1710   
  1667   1711   # EVIDENCE-OF: R-64550-29191 Note that the result from casting any
  1668   1712   # non-BLOB value into a BLOB and the result from casting any BLOB value
  1669   1713   # into a non-BLOB value may be different depending on whether the
  1670   1714   # database encoding is UTF-8, UTF-16be, or UTF-16le.
  1671   1715   #
  1672   1716   ifcapable {utf16} {

Changes to test/eqp.test.

   184    184     0 0 0 {SCAN TABLE t1}
   185    185     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   186    186     1 0 0 {SCAN TABLE t1 AS sub}
   187    187   }
   188    188   do_eqp_test 3.1.2 {
   189    189     SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub);
   190    190   } {
          191  +  0 0 0 {SCAN TABLE t1}
   191    192     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   192    193     1 0 0 {SCAN TABLE t1 AS sub}
   193         -  0 0 0 {SCAN TABLE t1}
   194    194   }
   195    195   do_eqp_test 3.1.3 {
   196    196     SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub ORDER BY y);
   197    197   } {
          198  +  0 0 0 {SCAN TABLE t1}
   198    199     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   199    200     1 0 0 {SCAN TABLE t1 AS sub}
   200    201     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   201         -  0 0 0 {SCAN TABLE t1}
   202    202   }
   203    203   do_eqp_test 3.1.4 {
   204    204     SELECT * FROM t1 WHERE (SELECT x FROM t2 ORDER BY x);
   205    205   } {
          206  +  0 0 0 {SCAN TABLE t1}
   206    207     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   207    208     1 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
   208         -  0 0 0 {SCAN TABLE t1}
   209    209   }
   210    210   
   211    211   det 3.2.1 {
   212    212     SELECT * FROM (SELECT * FROM t1 ORDER BY x LIMIT 10) ORDER BY y LIMIT 5
   213    213   } {
   214    214     1 0 0 {SCAN TABLE t1} 
   215    215     1 0 0 {USE TEMP B-TREE FOR ORDER BY} 

Changes to test/fts3conf.test.

   132    132       INSERT INTO t1(docid, x) VALUES(1, 'a b c');
   133    133       REPLACE INTO t1(docid, x) VALUES('zero', 'd e f');
   134    134   } {1 {datatype mismatch}}
   135    135   do_execsql_test 2.2.2 { COMMIT }
   136    136   do_execsql_test 2.2.3 { SELECT * FROM t1 } {{a b c} {a b c}}
   137    137   fts3_integrity 2.2.4 db t1
   138    138   
   139         -do_execsql_test 3.1 {
   140         -  CREATE VIRTUAL TABLE t3 USING fts4;
   141         -  REPLACE INTO t3(docid, content) VALUES (1, 'one two');
   142         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
   143         -} {X'0100000002000000'}
   144         -
   145         -do_execsql_test 3.2 {
   146         -  REPLACE INTO t3(docid, content) VALUES (2, 'one two three four');
   147         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'four'
   148         -} {X'0200000003000000'}
   149         -
   150         -do_execsql_test 3.3 {
   151         -  REPLACE INTO t3(docid, content) VALUES (1, 'one two three four five six');
   152         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
   153         -} {X'0200000005000000'}
   154         -
   155         -do_execsql_test 3.4 {
   156         -  UPDATE OR REPLACE t3 SET docid = 2 WHERE docid=1;
   157         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
   158         -} {X'0100000006000000'}
   159         -
   160         -do_execsql_test 3.5 {
   161         -  UPDATE OR REPLACE t3 SET docid = 3 WHERE docid=2;
   162         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
   163         -} {X'0100000006000000'}
   164         -
   165         -do_execsql_test 3.6 {
   166         -  REPLACE INTO t3(docid, content) VALUES (3, 'one two');
   167         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
   168         -} {X'0100000002000000'}
   169         -
   170         -do_execsql_test 3.7 {
   171         -  REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four');
   172         -  REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four five six');
   173         -  SELECT docid FROM t3;
   174         -} {3 4 5}
   175         -
   176         -do_execsql_test 3.8 {
   177         -  UPDATE OR REPLACE t3 SET docid = 5, content='three four' WHERE docid = 4;
   178         -  SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
   179         -} {X'0200000002000000'}
          139  +if {$tcl_platform(byteOrder)=="littleEndian"} {
          140  +  do_execsql_test 3.1 {
          141  +    CREATE VIRTUAL TABLE t3 USING fts4;
          142  +    REPLACE INTO t3(docid, content) VALUES (1, 'one two');
          143  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
          144  +  } {X'0100000002000000'}
          145  +  
          146  +  do_execsql_test 3.2 {
          147  +    REPLACE INTO t3(docid, content) VALUES (2, 'one two three four');
          148  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'four'
          149  +  } {X'0200000003000000'}
          150  +  
          151  +  do_execsql_test 3.3 {
          152  +    REPLACE INTO t3(docid, content) VALUES (1, 'one two three four five six');
          153  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
          154  +  } {X'0200000005000000'}
          155  +  
          156  +  do_execsql_test 3.4 {
          157  +    UPDATE OR REPLACE t3 SET docid = 2 WHERE docid=1;
          158  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
          159  +  } {X'0100000006000000'}
          160  +  
          161  +  do_execsql_test 3.5 {
          162  +    UPDATE OR REPLACE t3 SET docid = 3 WHERE docid=2;
          163  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
          164  +  } {X'0100000006000000'}
          165  +  
          166  +  do_execsql_test 3.6 {
          167  +    REPLACE INTO t3(docid, content) VALUES (3, 'one two');
          168  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
          169  +  } {X'0100000002000000'}
          170  +  
          171  +  do_execsql_test 3.7 {
          172  +    REPLACE INTO t3(docid, content) VALUES(NULL,'one two three four');
          173  +    REPLACE INTO t3(docid, content) VALUES(NULL,'one two three four five six');
          174  +    SELECT docid FROM t3;
          175  +  } {3 4 5}
          176  +  
          177  +  do_execsql_test 3.8 {
          178  +    UPDATE OR REPLACE t3 SET docid = 5, content='three four' WHERE docid = 4;
          179  +    SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
          180  +  } {X'0200000002000000'}
          181  +}
   180    182   
   181    183   #-------------------------------------------------------------------------
   182    184   # Test that the xSavepoint is invoked correctly if the first write 
   183    185   # operation within a transaction is to a virtual table. 
   184    186   # 
   185    187   do_catchsql_test 4.1.1 {
   186    188     CREATE VIRTUAL TABLE t0 USING fts4;

Changes to test/indexexpr2.test.

    53     53     CREATE INDEX i1 ON t1(a, b);
    54     54   } {}
    55     55   
    56     56   do_eqp_test 3.1.1 {
    57     57     SELECT b FROM t1 WHERE b IS NOT NULL AND a IS NULL 
    58     58     GROUP BY b COLLATE nocase
    59     59     ORDER BY b COLLATE nocase;
    60         -} {
    61         -  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=? AND b>?)}
    62         -  0 0 0 {USE TEMP B-TREE FOR GROUP BY}
    63         -}
           60  +} {/USE TEMP B-TREE FOR GROUP BY/}
    64     61   
    65     62   do_execsql_test 3.2.0 {
    66     63     CREATE TABLE t2(x);
    67     64   
    68     65     INSERT INTO t2 VALUES('.ABC');
    69     66     INSERT INTO t2 VALUES('.abcd');
    70     67     INSERT INTO t2 VALUES('.defg');
................................................................................
    84     81     .ABC .abcd .DEF .defg
    85     82   }
    86     83   
    87     84   do_execsql_test 3.3.0 {
    88     85     CREATE TABLE t3(x);
    89     86   }
    90     87   
    91         -do_eqp_test 3.3.1 {
    92         -  SELECT json_extract(x, '$.b') FROM t2 
    93         -  WHERE json_extract(x, '$.b') IS NOT NULL AND json_extract(x, '$.a') IS NULL 
    94         -  GROUP BY json_extract(x, '$.b') COLLATE nocase
    95         -  ORDER BY json_extract(x, '$.b') COLLATE nocase;
    96         -} {
    97         -  0 0 0 {SCAN TABLE t2} 
    98         -  0 0 0 {USE TEMP B-TREE FOR GROUP BY}
    99         -}
   100         -
   101         -do_execsql_test 3.3.2 {
   102         -  CREATE INDEX i3 ON t3(json_extract(x, '$.a'), json_extract(x, '$.b'));
   103         -} {}
   104         -
   105         -do_eqp_test 3.3.3 {
   106         -  SELECT json_extract(x, '$.b') FROM t3 
   107         -  WHERE json_extract(x, '$.b') IS NOT NULL AND json_extract(x, '$.a') IS NULL 
   108         -  GROUP BY json_extract(x, '$.b') COLLATE nocase
   109         -  ORDER BY json_extract(x, '$.b') COLLATE nocase;
   110         -} {
   111         -  0 0 0 {SEARCH TABLE t3 USING INDEX i3 (<expr>=?)} 
   112         -  0 0 0 {USE TEMP B-TREE FOR GROUP BY}
           88  +ifcapable json1 {
           89  +  do_eqp_test 3.3.1 {
           90  +    SELECT json_extract(x, '$.b') FROM t2 
           91  +    WHERE json_extract(x, '$.b') IS NOT NULL AND json_extract(x, '$.a') IS NULL 
           92  +    GROUP BY json_extract(x, '$.b') COLLATE nocase
           93  +    ORDER BY json_extract(x, '$.b') COLLATE nocase;
           94  +  } {
           95  +    0 0 0 {SCAN TABLE t2} 
           96  +    0 0 0 {USE TEMP B-TREE FOR GROUP BY}
           97  +  }
           98  +  
           99  +  do_execsql_test 3.3.2 {
          100  +    CREATE INDEX i3 ON t3(json_extract(x, '$.a'), json_extract(x, '$.b'));
          101  +  } {}
          102  +  
          103  +  do_eqp_test 3.3.3 {
          104  +    SELECT json_extract(x, '$.b') FROM t3 
          105  +    WHERE json_extract(x, '$.b') IS NOT NULL AND json_extract(x, '$.a') IS NULL 
          106  +    GROUP BY json_extract(x, '$.b') COLLATE nocase
          107  +    ORDER BY json_extract(x, '$.b') COLLATE nocase;
          108  +  } {
          109  +    0 0 0 {SEARCH TABLE t3 USING INDEX i3 (<expr>=?)} 
          110  +    0 0 0 {USE TEMP B-TREE FOR GROUP BY}
          111  +  }
   113    112   }
   114    113   
   115    114   do_execsql_test 3.4.0 {
   116    115     CREATE TABLE t4(a, b);
   117    116     INSERT INTO t4 VALUES('.ABC', 1);
   118    117     INSERT INTO t4 VALUES('.abc', 2);
   119    118     INSERT INTO t4 VALUES('.ABC', 3);

Changes to test/misc1.test.

   707    707   # minutes to prepare.  This has been speeded up to about 250 milliseconds.
   708    708   #
   709    709   do_catchsql_test misc1-25.0 {
   710    710   SELECT-1 UNION  SELECT 5 UNION SELECT 0 UNION SElECT*from(SELECT-5) UNION SELECT*from(SELECT-0) UNION  SELECT:SELECT-0 UNION SELECT-1 UNION SELECT 1 UNION SELECT 1 ORDER BY S  in(WITH K AS(WITH K AS(select'CREINDERcharREADEVIRTUL5TABLECONFLICT !1 USIN'' MFtOR(b38q,eWITH K AS(selectCREATe TABLE t0(a,b,c,d,e, PRIMARY KEY(a,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,b,c,d,c,a,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d'CEIl,k'',ab, g, a,b,o11b, i'nEX/charREDE IVT LR!VABLt5SG',N  ,N in rement,l_vacuum,M&U,'te3(''5l' a,bB,b,l*e)SELECT:SELECT, *,*,*from(( SELECT
   711    711   $group,:conc ap0,1)fro,(select"",:PBAG,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d, foreign_keysc,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,a,b,d,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,bb,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,a,b,d,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,MAato_aecSELEC,+?b," "O,"i","a",""b  ,5 ))KEY)SELECT*FROM((k()reaC,k,K) eA,k '' )t ,K  M);
   712    712   } {1 {'k' is not a function}}
   713    713   
          714  +# 2017-09-17
          715  +#
          716  +# Sometimes sqlite3ExprListAppend() can be invoked on an ExprList that
          717  +# was obtained from sqlite3ExprListDup().
          718  +#
          719  +do_execsql_test misc1-26.0 {
          720  +  DROP TABLE IF EXISTS abc;
          721  +  CREATE TABLE abc(a, b, c);
          722  +  SELECT randomblob(min(max(coalesce(EXISTS (SELECT 1 FROM ( SELECT (SELECT 2147483647) NOT IN (SELECT 2147483649 UNION ALL SELECT DISTINCT -1) IN (SELECT 2147483649), 'fault', (SELECT ALL -1 INTERSECT SELECT 'experiments') IN (SELECT ALL 56.1 ORDER BY 'experiments' DESC) FROM (SELECT DISTINCT 2147483648, 'hardware' UNION ALL SELECT -2147483648, 'experiments' ORDER BY 2147483648 LIMIT 1 OFFSET 123456789.1234567899) GROUP BY (SELECT ALL 0 INTERSECT SELECT 'in') IN (SELECT DISTINCT 'experiments' ORDER BY zeroblob(1000) LIMIT 56.1 OFFSET -456) HAVING EXISTS (SELECT 'fault' EXCEPT    SELECT DISTINCT 56.1) UNION SELECT 'The', 'The', 2147483649 UNION ALL SELECT DISTINCT 'hardware', 'first', 'experiments' ORDER BY 'hardware' LIMIT 123456789.1234567899 OFFSET -2147483647)) NOT IN (SELECT (SELECT DISTINCT (SELECT 'The') FROM abc ORDER BY EXISTS (SELECT -1 INTERSECT SELECT ALL NULL) ASC) IN (SELECT DISTINCT EXISTS (SELECT ALL 123456789.1234567899 ORDER BY 1 ASC, NULL DESC) FROM sqlite_master INTERSECT SELECT 456)), (SELECT ALL 'injection' UNION ALL SELECT ALL (SELECT DISTINCT 'first' UNION     SELECT DISTINCT 'The') FROM (SELECT 456, 'in', 2147483649))),1), 500)), 'first', EXISTS (SELECT DISTINCT 456 FROM abc ORDER BY 'experiments' DESC) FROM abc;
          723  +} {}
   714    724   
   715    725   finish_test

Added test/mjournal.test.

            1  +# 2017 September 15
            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  +
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +set testprefix mjournal
           17  +
           18  +# Test that nothing bad happens if a journal file contains a pointer to
           19  +# a master journal file that does not have a "-" in the name. At one point
           20  +# this was causing a segfault on unix.
           21  +#
           22  +do_execsql_test 1.0 {
           23  +  CREATE TABLE t1(a, b);
           24  +}
           25  +
           26  +do_test 1.1 {
           27  +  forcedelete test.db2journal test.db-journal
           28  +
           29  +  close [open test.db-journal w]
           30  +  
           31  +  hexio_write test.db-journal 0 746573742e6462326a6f75726e616c00
           32  +  hexio_write test.db-journal 16 00000010
           33  +  hexio_write test.db-journal 20 000005e1
           34  +  hexio_write test.db-journal 24 d9d505f920a163d7
           35  +
           36  +  close [open test.db2journal w]
           37  +  hexio_write test.db2journal 0 abcd
           38  +} {2}
           39  +
           40  +do_execsql_test 1.2 {
           41  +  SELECT * FROM t1;
           42  +}
           43  +
           44  +do_test 1.3 {
           45  +  forcedelete test0db2journal test.db-journal
           46  +  close [open test.db-journal w]
           47  +  hexio_write test.db-journal 0 74657374306462326a6f75726e616c00
           48  +  hexio_write test.db-journal 16 00000010
           49  +  hexio_write test.db-journal 20 000005e3
           50  +  hexio_write test.db-journal 24 d9d505f920a163d7
           51  +
           52  +  close [open test0db2journal w]
           53  +  hexio_write test0db2journal 0 abcd
           54  +} {2}
           55  +
           56  +do_execsql_test 1.4 {
           57  +  SELECT * FROM t1;
           58  +}
           59  +
           60  +# And now test that nothing bad happens if a master journal contains a
           61  +# pointer to a journal file that does not have a "-" in the name. 
           62  +#
           63  +do_test 1.5 {
           64  +  forcedelete test.db2-master test.db-journal test1
           65  +  close [open test.db-journal w]
           66  +  hexio_write test.db-journal 0 746573742e6462322d6d617374657200
           67  +  hexio_write test.db-journal 16 00000010
           68  +  hexio_write test.db-journal 20 0000059f
           69  +  hexio_write test.db-journal 24 d9d505f920a163d7
           70  +
           71  +  close [open test.db2-master w]
           72  +  hexio_write test.db2-master 0 746573743100
           73  +
           74  +  close [open test1 w]
           75  +  hexio_write test1 0 abcd
           76  +} {2}
           77  +
           78  +do_execsql_test 1.6 {
           79  +  SELECT * FROM t1;
           80  +}
           81  +
           82  +  
           83  +finish_test

Changes to test/nan.test.

   362    362     db eval {
   363    363       DELETE FROM t1;
   364    364       INSERT INTO t1 VALUES('2.5e-2147483650');
   365    365       SELECT x, typeof(x) FROM t1;
   366    366     }
   367    367   } {0.0 real}
   368    368   
   369         -  
   370         -
   371         -
          369  +do_realnum_test nan-4.40 {
          370  +  db eval {
          371  +    SELECT cast('-1e999' AS real);
          372  +  }
          373  +} {-inf}
   372    374   
   373    375   finish_test

Changes to test/pragma.test.

  1928   1928       DROP TABLE t2;
  1929   1929       CREATE TABLE t2(x, y INTEGER REFERENCES t1);
  1930   1930     }
  1931   1931     db2 eval {
  1932   1932       PRAGMA foreign_key_list(t2);
  1933   1933     }
  1934   1934   } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
         1935  +db2 close
         1936  +
         1937  +reset_db
         1938  +do_execsql_test 24.0 {
         1939  +  PRAGMA page_size = 1024;
         1940  +  CREATE TABLE t1(a, b, c);
         1941  +  CREATE INDEX i1 ON t1(b);
         1942  +  INSERT INTO t1 VALUES('a', 'b', 'c');
         1943  +  PRAGMA integrity_check;
         1944  +} {ok}
         1945  +
         1946  +set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
         1947  +db close
         1948  +hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
         1949  +
         1950  +sqlite3 db test.db
         1951  +do_catchsql_test 24.1 {
         1952  +  SELECT * FROM t1;
         1953  +} {1 {database disk image is malformed}}
         1954  +do_catchsql_test 24.2 {
         1955  +  PRAGMA integrity_check;
         1956  +} {0 {{database disk image is malformed}}}
  1935   1957   
  1936   1958   database_never_corrupt
  1937   1959   finish_test

Changes to test/pragma4.test.

    76     76     4 "PRAGMA case_sensitive_like = 1"
    77     77     5 "PRAGMA case_sensitive_like"
    78     78   } {
    79     79   
    80     80     do_pragma_ncol_test 1.$tn.1 $sql 0
    81     81   }
    82     82   
           83  +# EXPLAIN on a PRAGMA integrity_check.
           84  +# Verify that that P4_INTARRAY argument to OP_IntegrityCk is rendered
           85  +# correctly.
           86  +#
           87  +db close
           88  +forcedelete test.db
           89  +sqlite3 db test.db
           90  +do_test pragma4-2.100 {
           91  +  db eval {
           92  +    PRAGMA page_size=512;
           93  +    CREATE TABLE t1(x);
           94  +    WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10000)
           95  +    INSERT INTO t1(x) SELECT zeroblob(300) FROM c;
           96  +    CREATE TABLE t2(y);
           97  +    DROP TABLE t1;
           98  +  }
           99  +  string map {\[ x \] x \173 {} \175 {}} \
          100  +    [db eval {EXPLAIN PRAGMA integrity_check}]
          101  +} {/ IntegrityCk 2 2 1 x[0-9]+,1x /}
    83    102   
    84    103   finish_test

Changes to test/releasetest.tcl.

   110    110       -DSQLITE_ENABLE_OVERSIZE_CELL_CHECK=1
   111    111       -DSQLITE_ENABLE_STAT4
   112    112       -DSQLITE_ENABLE_STMT_SCANSTATUS
   113    113       --enable-json1 --enable-fts5 --enable-session
   114    114     }
   115    115     "Debug-One" {
   116    116       --disable-shared
   117         -    -O2
          117  +    -O2 -funsigned-char
   118    118       -DSQLITE_DEBUG=1
   119    119       -DSQLITE_MEMDEBUG=1
   120    120       -DSQLITE_MUTEX_NOOP=1
   121    121       -DSQLITE_TCL_DEFAULT_FULLMUTEX=1
   122    122       -DSQLITE_ENABLE_FTS3=1
   123    123       -DSQLITE_ENABLE_RTREE=1
   124    124       -DSQLITE_ENABLE_MEMSYS5=1

Changes to test/whereF.test.

   171    171   
   172    172   do_execsql_test 5.5 {
   173    173     SELECT count(*) FROM t1, t2 WHERE (
   174    174       t2.rowid = +t1.rowid OR (t2.f2 = t1.f1 AND t1.f1!=-1)
   175    175     )
   176    176   } {4}
   177    177   do_test 5.6 { expr [db status vmstep]<200 } 1
          178  +
          179  +# 2017-09-04 ticket b899b6042f97f52d
          180  +# Segfault on correlated subquery...
          181  +#
          182  +ifcapable json1 {
          183  +  do_execsql_test 6.1 {
          184  +    CREATE TABLE t6(x);
          185  +    SELECT * FROM t6 WHERE 1 IN (SELECT value FROM json_each(x));
          186  +  } {}
          187  +
          188  +  do_execsql_test 6.2 {
          189  +    DROP TABLE t6;
          190  +    CREATE TABLE t6(a,b,c);
          191  +    INSERT INTO t6 VALUES
          192  +     (0,null,'{"a":0,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}'),
          193  +     (1,null,'{"a":1,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}'),
          194  +     (2,null,'{"a":9,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}');
          195  +    SELECT * FROM t6
          196  +     WHERE (EXISTS (SELECT 1 FROM json_each(t6.c) AS x WHERE x.value=1));
          197  +  } {1 {} {{"a":1,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}}}
          198  +    
          199  +}
   178    200   
   179    201   finish_test

Changes to test/win32heap.test.

     5      5   #
     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12         -# focus of this script is recovery from transient manditory locks
    13         -# that sometimes appear on database files due to anti-virus software.
           12  +# focus of this script is the Win32 heap implementation.
    14     13   #
    15     14   
    16     15   if {$tcl_platform(platform)!="windows"} return
    17     16   
    18     17   set testdir [file dirname $argv0]
    19     18   source $testdir/tester.tcl
    20     19   

Changes to tool/mkshellc.tcl.

    31     31     if {[regexp {^INCLUDE } $lx]} {
    32     32       set cfile [lindex $lx 1]
    33     33       puts $out "/************************* Begin $cfile ******************/"
    34     34       set in2 [open $topdir/src/$cfile rb]
    35     35       while {![eof $in2]} {
    36     36         set lx [gets $in2]
    37     37         if {[regexp {^#include "sqlite} $lx]} continue
           38  +      set lx [string map [list __declspec(dllexport) {}] $lx]
    38     39         puts $out $lx
    39     40       }
    40     41       close $in2
    41     42       puts $out "/************************* End $cfile ********************/"
    42     43       continue
    43     44     }
    44     45     puts $out $lx
    45     46   }
    46     47   close $in
    47     48   close $out

Changes to tool/mksourceid.c.

   773    773   int main(int argc, char **argv){
   774    774     const char *zManifest = 0;
   775    775     int i;
   776    776     int bVerbose = 0;
   777    777     FILE *in;
   778    778     int allValid = 1;
   779    779     int rc;
          780  +  SHA3Context ctx;
   780    781     char zDate[50];
   781    782     char zHash[100];
   782         -  char zLine[1000];
          783  +  char zLine[20000];
   783    784   
   784    785     for(i=1; i<argc; i++){
   785    786       const char *z = argv[i];
   786    787       if( z[0]=='-' ){
   787    788         if( z[1]=='-' ) z++;
   788    789         if( strcmp(z, "-v")==0 ){
   789    790           bVerbose = 1;
................................................................................
   801    802     if( zManifest==0 ) usage(argv[0]);
   802    803     zDate[0] = 0;
   803    804     in = fopen(zManifest, "rb");
   804    805     if( in==0 ){
   805    806       fprintf(stderr, "cannot open \"%s\" for reading\n", zManifest);
   806    807       exit(1);
   807    808     }
          809  +  SHA3Init(&ctx, 256);
   808    810     while( fgets(zLine, sizeof(zLine), in) ){
          811  +    if( strncmp(zLine,"# Remove this line", 18)!=0 ){
          812  +      SHA3Update(&ctx, (unsigned char*)zLine, (unsigned)strlen(zLine));
          813  +    }
   809    814       if( strncmp(zLine, "D 20", 4)==0 ){
   810    815         memcpy(zDate, &zLine[2], 10);
   811    816         zDate[10] = ' ';
   812    817         memcpy(&zDate[11], &zLine[13], 8);
   813    818         zDate[19] = 0;
   814    819         continue;
   815    820       }
................................................................................
   822    827         }else{
   823    828           rc = sha3sum_file(zFilename, 256, zHash);
   824    829         }
   825    830         if( rc ){
   826    831           allValid = 0;
   827    832           if( bVerbose ){
   828    833             printf("hash failed: %s\n", zFilename);
   829         -        }else{
   830         -          break;
   831    834           }
   832    835         }else if( strcmp(zHash, zMHash)!=0 ){
   833    836           allValid = 0;
   834    837           if( bVerbose ){
   835    838             printf("wrong hash: %s\n", zFilename);
   836    839             printf("... expected: %s\n", zMHash);
   837    840             printf("... got:      %s\n", zHash);
   838         -        }else{
   839         -          break;
   840    841           }
   841    842         }
   842    843       }
   843    844     }
   844    845     fclose(in);
   845         -  sha3sum_file(zManifest, 256, zHash);
          846  +  DigestToBase16(SHA3Final(&ctx), zHash, 256/8);
   846    847     if( !allValid ){
   847    848       printf("%s %.60salt1\n", zDate, zHash);
   848    849     }else{
   849    850       printf("%s %s\n", zDate, zHash);
   850    851     }
   851    852     return 0;
   852    853   }