/ Check-in [001771afd0]
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 | reuse-schema
Files: files | file ages | folders
SHA3-256: 001771afd0567c04736a778fc78fd7144492ae7c830e7f55de61869ce988f0ed
User & Date: dan 2019-02-22 17:44:58
Wiki:reuse-schema
Context
2019-02-25
17:54
Where possible, avoid loading all schemas into memory for PRAGMA statements. check-in: 64f97530ad user: dan tags: reuse-schema
2019-02-22
17:44
Merge latest trunk changes into this branch. check-in: 001771afd0 user: dan tags: reuse-schema
16:18
In sqlite3NestedParse() be sure to detect all SQLITE_NOMEM and SQLITE_TOOBIG errors and to distinguish between them. check-in: 73056b314b user: drh tags: trunk
2019-02-20
18:44
Further test cases and fixes for SQLITE_OPEN_SHARED_SCHEMA. check-in: ba0ab042f4 user: dan tags: reuse-schema
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts3/fts3_write.c.

  1344   1344         return SQLITE_OK;
  1345   1345       }
  1346   1346   
  1347   1347       fts3SegReaderSetEof(pReader);
  1348   1348   
  1349   1349       /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
  1350   1350       ** blocks have already been traversed.  */
  1351         -    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
         1351  +#ifdef CORRUPT_DB
         1352  +    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
         1353  +#endif
  1352   1354       if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
  1353   1355         return SQLITE_OK;
  1354   1356       }
  1355   1357   
  1356   1358       rc = sqlite3Fts3ReadBlock(
  1357   1359           p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, 
  1358   1360           (bIncr ? &pReader->nPopulate : 0)

Added ext/misc/fossildelta.c.

            1  +/*
            2  +** 2019-02-19
            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  +** This SQLite extension implements the delta functions used by Fossil.
           14  +*/
           15  +#include <string.h>
           16  +#include <assert.h>
           17  +#include <stdlib.h>
           18  +#include "sqlite3ext.h"
           19  +SQLITE_EXTENSION_INIT1
           20  +
           21  +/*
           22  +** The "u32" type must be an unsigned 32-bit integer.  Adjust this
           23  +*/
           24  +typedef unsigned int u32;
           25  +
           26  +/*
           27  +** Must be a 16-bit value
           28  +*/
           29  +typedef short int s16;
           30  +typedef unsigned short int u16;
           31  +
           32  +
           33  +/*
           34  +** The width of a hash window in bytes.  The algorithm only works if this
           35  +** is a power of 2.
           36  +*/
           37  +#define NHASH 16
           38  +
           39  +/*
           40  +** The current state of the rolling hash.
           41  +**
           42  +** z[] holds the values that have been hashed.  z[] is a circular buffer.
           43  +** z[i] is the first entry and z[(i+NHASH-1)%NHASH] is the last entry of
           44  +** the window.
           45  +**
           46  +** Hash.a is the sum of all elements of hash.z[].  Hash.b is a weighted
           47  +** sum.  Hash.b is z[i]*NHASH + z[i+1]*(NHASH-1) + ... + z[i+NHASH-1]*1.
           48  +** (Each index for z[] should be module NHASH, of course.  The %NHASH operator
           49  +** is omitted in the prior expression for brevity.)
           50  +*/
           51  +typedef struct hash hash;
           52  +struct hash {
           53  +  u16 a, b;         /* Hash values */
           54  +  u16 i;            /* Start of the hash window */
           55  +  char z[NHASH];    /* The values that have been hashed */
           56  +};
           57  +
           58  +/*
           59  +** Initialize the rolling hash using the first NHASH characters of z[]
           60  +*/
           61  +static void hash_init(hash *pHash, const char *z){
           62  +  u16 a, b, i;
           63  +  a = b = z[0];
           64  +  for(i=1; i<NHASH; i++){
           65  +    a += z[i];
           66  +    b += a;
           67  +  }
           68  +  memcpy(pHash->z, z, NHASH);
           69  +  pHash->a = a & 0xffff;
           70  +  pHash->b = b & 0xffff;
           71  +  pHash->i = 0;
           72  +}
           73  +
           74  +/*
           75  +** Advance the rolling hash by a single character "c"
           76  +*/
           77  +static void hash_next(hash *pHash, int c){
           78  +  u16 old = pHash->z[pHash->i];
           79  +  pHash->z[pHash->i] = c;
           80  +  pHash->i = (pHash->i+1)&(NHASH-1);
           81  +  pHash->a = pHash->a - old + c;
           82  +  pHash->b = pHash->b - NHASH*old + pHash->a;
           83  +}
           84  +
           85  +/*
           86  +** Return a 32-bit hash value
           87  +*/
           88  +static u32 hash_32bit(hash *pHash){
           89  +  return (pHash->a & 0xffff) | (((u32)(pHash->b & 0xffff))<<16);
           90  +}
           91  +
           92  +/*
           93  +** Compute a hash on NHASH bytes.
           94  +**
           95  +** This routine is intended to be equivalent to:
           96  +**    hash h;
           97  +**    hash_init(&h, zInput);
           98  +**    return hash_32bit(&h);
           99  +*/
          100  +static u32 hash_once(const char *z){
          101  +  u16 a, b, i;
          102  +  a = b = z[0];
          103  +  for(i=1; i<NHASH; i++){
          104  +    a += z[i];
          105  +    b += a;
          106  +  }
          107  +  return a | (((u32)b)<<16);
          108  +}
          109  +
          110  +/*
          111  +** Write an base-64 integer into the given buffer.
          112  +*/
          113  +static void putInt(unsigned int v, char **pz){
          114  +  static const char zDigits[] =
          115  +    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
          116  +  /*  123456789 123456789 123456789 123456789 123456789 123456789 123 */
          117  +  int i, j;
          118  +  char zBuf[20];
          119  +  if( v==0 ){
          120  +    *(*pz)++ = '0';
          121  +    return;
          122  +  }
          123  +  for(i=0; v>0; i++, v>>=6){
          124  +    zBuf[i] = zDigits[v&0x3f];
          125  +  }
          126  +  for(j=i-1; j>=0; j--){
          127  +    *(*pz)++ = zBuf[j];
          128  +  }
          129  +}
          130  +
          131  +/*
          132  +** Read bytes from *pz and convert them into a positive integer.  When
          133  +** finished, leave *pz pointing to the first character past the end of
          134  +** the integer.  The *pLen parameter holds the length of the string
          135  +** in *pz and is decremented once for each character in the integer.
          136  +*/
          137  +static unsigned int deltaGetInt(const char **pz, int *pLen){
          138  +  static const signed char zValue[] = {
          139  +    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
          140  +    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
          141  +    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
          142  +     0,  1,  2,  3,  4,  5,  6,  7,    8,  9, -1, -1, -1, -1, -1, -1,
          143  +    -1, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24,
          144  +    25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, -1, -1, -1, -1, 36,
          145  +    -1, 37, 38, 39, 40, 41, 42, 43,   44, 45, 46, 47, 48, 49, 50, 51,
          146  +    52, 53, 54, 55, 56, 57, 58, 59,   60, 61, 62, -1, -1, -1, 63, -1,
          147  +  };
          148  +  unsigned int v = 0;
          149  +  int c;
          150  +  unsigned char *z = (unsigned char*)*pz;
          151  +  unsigned char *zStart = z;
          152  +  while( (c = zValue[0x7f&*(z++)])>=0 ){
          153  +     v = (v<<6) + c;
          154  +  }
          155  +  z--;
          156  +  *pLen -= z - zStart;
          157  +  *pz = (char*)z;
          158  +  return v;
          159  +}
          160  +
          161  +/*
          162  +** Return the number digits in the base-64 representation of a positive integer
          163  +*/
          164  +static int digit_count(int v){
          165  +  unsigned int i, x;
          166  +  for(i=1, x=64; v>=x; i++, x <<= 6){}
          167  +  return i;
          168  +}
          169  +
          170  +#ifdef __GNUC__
          171  +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
          172  +#else
          173  +# define GCC_VERSION 0
          174  +#endif
          175  +
          176  +/*
          177  +** Compute a 32-bit big-endian checksum on the N-byte buffer.  If the
          178  +** buffer is not a multiple of 4 bytes length, compute the sum that would
          179  +** have occurred if the buffer was padded with zeros to the next multiple
          180  +** of four bytes.
          181  +*/
          182  +static unsigned int checksum(const char *zIn, size_t N){
          183  +  static const int byteOrderTest = 1;
          184  +  const unsigned char *z = (const unsigned char *)zIn;
          185  +  const unsigned char *zEnd = (const unsigned char*)&zIn[N&~3];
          186  +  unsigned sum = 0;
          187  +  assert( (z - (const unsigned char*)0)%4==0 );  /* Four-byte alignment */
          188  +  if( 0==*(char*)&byteOrderTest ){
          189  +    /* This is a big-endian machine */
          190  +    while( z<zEnd ){
          191  +      sum += *(unsigned*)z;
          192  +      z += 4;
          193  +    }
          194  +  }else{
          195  +    /* A little-endian machine */
          196  +#if GCC_VERSION>=4003000
          197  +    while( z<zEnd ){
          198  +      sum += __builtin_bswap32(*(unsigned*)z);
          199  +      z += 4;
          200  +    }
          201  +#elif defined(_MSC_VER) && _MSC_VER>=1300
          202  +    while( z<zEnd ){
          203  +      sum += _byteswap_ulong(*(unsigned*)z);
          204  +      z += 4;
          205  +    }
          206  +#else
          207  +    unsigned sum0 = 0;
          208  +    unsigned sum1 = 0;
          209  +    unsigned sum2 = 0;
          210  +    while(N >= 16){
          211  +      sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
          212  +      sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
          213  +      sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
          214  +      sum  += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
          215  +      z += 16;
          216  +      N -= 16;
          217  +    }
          218  +    while(N >= 4){
          219  +      sum0 += z[0];
          220  +      sum1 += z[1];
          221  +      sum2 += z[2];
          222  +      sum  += z[3];
          223  +      z += 4;
          224  +      N -= 4;
          225  +    }
          226  +    sum += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
          227  +#endif
          228  +  }
          229  +  switch(N&3){
          230  +    case 3:   sum += (z[2] << 8);
          231  +    case 2:   sum += (z[1] << 16);
          232  +    case 1:   sum += (z[0] << 24);
          233  +    default:  ;
          234  +  }
          235  +  return sum;
          236  +}
          237  +
          238  +/*
          239  +** Create a new delta.
          240  +**
          241  +** The delta is written into a preallocated buffer, zDelta, which
          242  +** should be at least 60 bytes longer than the target file, zOut.
          243  +** The delta string will be NUL-terminated, but it might also contain
          244  +** embedded NUL characters if either the zSrc or zOut files are
          245  +** binary.  This function returns the length of the delta string
          246  +** in bytes, excluding the final NUL terminator character.
          247  +**
          248  +** Output Format:
          249  +**
          250  +** The delta begins with a base64 number followed by a newline.  This
          251  +** number is the number of bytes in the TARGET file.  Thus, given a
          252  +** delta file z, a program can compute the size of the output file
          253  +** simply by reading the first line and decoding the base-64 number
          254  +** found there.  The delta_output_size() routine does exactly this.
          255  +**
          256  +** After the initial size number, the delta consists of a series of
          257  +** literal text segments and commands to copy from the SOURCE file.
          258  +** A copy command looks like this:
          259  +**
          260  +**     NNN@MMM,
          261  +**
          262  +** where NNN is the number of bytes to be copied and MMM is the offset
          263  +** into the source file of the first byte (both base-64).   If NNN is 0
          264  +** it means copy the rest of the input file.  Literal text is like this:
          265  +**
          266  +**     NNN:TTTTT
          267  +**
          268  +** where NNN is the number of bytes of text (base-64) and TTTTT is the text.
          269  +**
          270  +** The last term is of the form
          271  +**
          272  +**     NNN;
          273  +**
          274  +** In this case, NNN is a 32-bit bigendian checksum of the output file
          275  +** that can be used to verify that the delta applied correctly.  All
          276  +** numbers are in base-64.
          277  +**
          278  +** Pure text files generate a pure text delta.  Binary files generate a
          279  +** delta that may contain some binary data.
          280  +**
          281  +** Algorithm:
          282  +**
          283  +** The encoder first builds a hash table to help it find matching
          284  +** patterns in the source file.  16-byte chunks of the source file
          285  +** sampled at evenly spaced intervals are used to populate the hash
          286  +** table.
          287  +**
          288  +** Next we begin scanning the target file using a sliding 16-byte
          289  +** window.  The hash of the 16-byte window in the target is used to
          290  +** search for a matching section in the source file.  When a match
          291  +** is found, a copy command is added to the delta.  An effort is
          292  +** made to extend the matching section to regions that come before
          293  +** and after the 16-byte hash window.  A copy command is only issued
          294  +** if the result would use less space that just quoting the text
          295  +** literally. Literal text is added to the delta for sections that
          296  +** do not match or which can not be encoded efficiently using copy
          297  +** commands.
          298  +*/
          299  +static int delta_create(
          300  +  const char *zSrc,      /* The source or pattern file */
          301  +  unsigned int lenSrc,   /* Length of the source file */
          302  +  const char *zOut,      /* The target file */
          303  +  unsigned int lenOut,   /* Length of the target file */
          304  +  char *zDelta           /* Write the delta into this buffer */
          305  +){
          306  +  int i, base;
          307  +  char *zOrigDelta = zDelta;
          308  +  hash h;
          309  +  int nHash;                 /* Number of hash table entries */
          310  +  int *landmark;             /* Primary hash table */
          311  +  int *collide;              /* Collision chain */
          312  +  int lastRead = -1;         /* Last byte of zSrc read by a COPY command */
          313  +
          314  +  /* Add the target file size to the beginning of the delta
          315  +  */
          316  +  putInt(lenOut, &zDelta);
          317  +  *(zDelta++) = '\n';
          318  +
          319  +  /* If the source file is very small, it means that we have no
          320  +  ** chance of ever doing a copy command.  Just output a single
          321  +  ** literal segment for the entire target and exit.
          322  +  */
          323  +  if( lenSrc<=NHASH ){
          324  +    putInt(lenOut, &zDelta);
          325  +    *(zDelta++) = ':';
          326  +    memcpy(zDelta, zOut, lenOut);
          327  +    zDelta += lenOut;
          328  +    putInt(checksum(zOut, lenOut), &zDelta);
          329  +    *(zDelta++) = ';';
          330  +    return zDelta - zOrigDelta;
          331  +  }
          332  +
          333  +  /* Compute the hash table used to locate matching sections in the
          334  +  ** source file.
          335  +  */
          336  +  nHash = lenSrc/NHASH;
          337  +  collide = sqlite3_malloc64( (sqlite3_int64)nHash*2*sizeof(int) );
          338  +  memset(collide, -1, nHash*2*sizeof(int));
          339  +  landmark = &collide[nHash];
          340  +  for(i=0; i<lenSrc-NHASH; i+=NHASH){
          341  +    int hv = hash_once(&zSrc[i]) % nHash;
          342  +    collide[i/NHASH] = landmark[hv];
          343  +    landmark[hv] = i/NHASH;
          344  +  }
          345  +
          346  +  /* Begin scanning the target file and generating copy commands and
          347  +  ** literal sections of the delta.
          348  +  */
          349  +  base = 0;    /* We have already generated everything before zOut[base] */
          350  +  while( base+NHASH<lenOut ){
          351  +    int iSrc, iBlock;
          352  +    unsigned int bestCnt, bestOfst=0, bestLitsz=0;
          353  +    hash_init(&h, &zOut[base]);
          354  +    i = 0;     /* Trying to match a landmark against zOut[base+i] */
          355  +    bestCnt = 0;
          356  +    while( 1 ){
          357  +      int hv;
          358  +      int limit = 250;
          359  +
          360  +      hv = hash_32bit(&h) % nHash;
          361  +      iBlock = landmark[hv];
          362  +      while( iBlock>=0 && (limit--)>0 ){
          363  +        /*
          364  +        ** The hash window has identified a potential match against
          365  +        ** landmark block iBlock.  But we need to investigate further.
          366  +        **
          367  +        ** Look for a region in zOut that matches zSrc. Anchor the search
          368  +        ** at zSrc[iSrc] and zOut[base+i].  Do not include anything prior to
          369  +        ** zOut[base] or after zOut[outLen] nor anything after zSrc[srcLen].
          370  +        **
          371  +        ** Set cnt equal to the length of the match and set ofst so that
          372  +        ** zSrc[ofst] is the first element of the match.  litsz is the number
          373  +        ** of characters between zOut[base] and the beginning of the match.
          374  +        ** sz will be the overhead (in bytes) needed to encode the copy
          375  +        ** command.  Only generate copy command if the overhead of the
          376  +        ** copy command is less than the amount of literal text to be copied.
          377  +        */
          378  +        int cnt, ofst, litsz;
          379  +        int j, k, x, y;
          380  +        int sz;
          381  +        int limitX;
          382  +
          383  +        /* Beginning at iSrc, match forwards as far as we can.  j counts
          384  +        ** the number of characters that match */
          385  +        iSrc = iBlock*NHASH;
          386  +        y = base+i;
          387  +        limitX = ( lenSrc-iSrc <= lenOut-y ) ? lenSrc : iSrc + lenOut - y;
          388  +        for(x=iSrc; x<limitX; x++, y++){
          389  +          if( zSrc[x]!=zOut[y] ) break;
          390  +        }
          391  +        j = x - iSrc - 1;
          392  +
          393  +        /* Beginning at iSrc-1, match backwards as far as we can.  k counts
          394  +        ** the number of characters that match */
          395  +        for(k=1; k<iSrc && k<=i; k++){
          396  +          if( zSrc[iSrc-k]!=zOut[base+i-k] ) break;
          397  +        }
          398  +        k--;
          399  +
          400  +        /* Compute the offset and size of the matching region */
          401  +        ofst = iSrc-k;
          402  +        cnt = j+k+1;
          403  +        litsz = i-k;  /* Number of bytes of literal text before the copy */
          404  +        /* sz will hold the number of bytes needed to encode the "insert"
          405  +        ** command and the copy command, not counting the "insert" text */
          406  +        sz = digit_count(i-k)+digit_count(cnt)+digit_count(ofst)+3;
          407  +        if( cnt>=sz && cnt>bestCnt ){
          408  +          /* Remember this match only if it is the best so far and it
          409  +          ** does not increase the file size */
          410  +          bestCnt = cnt;
          411  +          bestOfst = iSrc-k;
          412  +          bestLitsz = litsz;
          413  +        }
          414  +
          415  +        /* Check the next matching block */
          416  +        iBlock = collide[iBlock];
          417  +      }
          418  +
          419  +      /* We have a copy command that does not cause the delta to be larger
          420  +      ** than a literal insert.  So add the copy command to the delta.
          421  +      */
          422  +      if( bestCnt>0 ){
          423  +        if( bestLitsz>0 ){
          424  +          /* Add an insert command before the copy */
          425  +          putInt(bestLitsz,&zDelta);
          426  +          *(zDelta++) = ':';
          427  +          memcpy(zDelta, &zOut[base], bestLitsz);
          428  +          zDelta += bestLitsz;
          429  +          base += bestLitsz;
          430  +        }
          431  +        base += bestCnt;
          432  +        putInt(bestCnt, &zDelta);
          433  +        *(zDelta++) = '@';
          434  +        putInt(bestOfst, &zDelta);
          435  +        *(zDelta++) = ',';
          436  +        if( bestOfst + bestCnt -1 > lastRead ){
          437  +          lastRead = bestOfst + bestCnt - 1;
          438  +        }
          439  +        bestCnt = 0;
          440  +        break;
          441  +      }
          442  +
          443  +      /* If we reach this point, it means no match is found so far */
          444  +      if( base+i+NHASH>=lenOut ){
          445  +        /* We have reached the end of the file and have not found any
          446  +        ** matches.  Do an "insert" for everything that does not match */
          447  +        putInt(lenOut-base, &zDelta);
          448  +        *(zDelta++) = ':';
          449  +        memcpy(zDelta, &zOut[base], lenOut-base);
          450  +        zDelta += lenOut-base;
          451  +        base = lenOut;
          452  +        break;
          453  +      }
          454  +
          455  +      /* Advance the hash by one character.  Keep looking for a match */
          456  +      hash_next(&h, zOut[base+i+NHASH]);
          457  +      i++;
          458  +    }
          459  +  }
          460  +  /* Output a final "insert" record to get all the text at the end of
          461  +  ** the file that does not match anything in the source file.
          462  +  */
          463  +  if( base<lenOut ){
          464  +    putInt(lenOut-base, &zDelta);
          465  +    *(zDelta++) = ':';
          466  +    memcpy(zDelta, &zOut[base], lenOut-base);
          467  +    zDelta += lenOut-base;
          468  +  }
          469  +  /* Output the final checksum record. */
          470  +  putInt(checksum(zOut, lenOut), &zDelta);
          471  +  *(zDelta++) = ';';
          472  +  sqlite3_free(collide);
          473  +  return zDelta - zOrigDelta;
          474  +}
          475  +
          476  +/*
          477  +** Return the size (in bytes) of the output from applying
          478  +** a delta.
          479  +**
          480  +** This routine is provided so that an procedure that is able
          481  +** to call delta_apply() can learn how much space is required
          482  +** for the output and hence allocate nor more space that is really
          483  +** needed.
          484  +*/
          485  +static int delta_output_size(const char *zDelta, int lenDelta){
          486  +  int size;
          487  +  size = deltaGetInt(&zDelta, &lenDelta);
          488  +  if( *zDelta!='\n' ){
          489  +    /* ERROR: size integer not terminated by "\n" */
          490  +    return -1;
          491  +  }
          492  +  return size;
          493  +}
          494  +
          495  +
          496  +/*
          497  +** Apply a delta.
          498  +**
          499  +** The output buffer should be big enough to hold the whole output
          500  +** file and a NUL terminator at the end.  The delta_output_size()
          501  +** routine will determine this size for you.
          502  +**
          503  +** The delta string should be null-terminated.  But the delta string
          504  +** may contain embedded NUL characters (if the input and output are
          505  +** binary files) so we also have to pass in the length of the delta in
          506  +** the lenDelta parameter.
          507  +**
          508  +** This function returns the size of the output file in bytes (excluding
          509  +** the final NUL terminator character).  Except, if the delta string is
          510  +** malformed or intended for use with a source file other than zSrc,
          511  +** then this routine returns -1.
          512  +**
          513  +** Refer to the delta_create() documentation above for a description
          514  +** of the delta file format.
          515  +*/
          516  +static int delta_apply(
          517  +  const char *zSrc,      /* The source or pattern file */
          518  +  int lenSrc,            /* Length of the source file */
          519  +  const char *zDelta,    /* Delta to apply to the pattern */
          520  +  int lenDelta,          /* Length of the delta */
          521  +  char *zOut             /* Write the output into this preallocated buffer */
          522  +){
          523  +  unsigned int limit;
          524  +  unsigned int total = 0;
          525  +#ifdef FOSSIL_ENABLE_DELTA_CKSUM_TEST
          526  +  char *zOrigOut = zOut;
          527  +#endif
          528  +
          529  +  limit = deltaGetInt(&zDelta, &lenDelta);
          530  +  if( *zDelta!='\n' ){
          531  +    /* ERROR: size integer not terminated by "\n" */
          532  +    return -1;
          533  +  }
          534  +  zDelta++; lenDelta--;
          535  +  while( *zDelta && lenDelta>0 ){
          536  +    unsigned int cnt, ofst;
          537  +    cnt = deltaGetInt(&zDelta, &lenDelta);
          538  +    switch( zDelta[0] ){
          539  +      case '@': {
          540  +        zDelta++; lenDelta--;
          541  +        ofst = deltaGetInt(&zDelta, &lenDelta);
          542  +        if( lenDelta>0 && zDelta[0]!=',' ){
          543  +          /* ERROR: copy command not terminated by ',' */
          544  +          return -1;
          545  +        }
          546  +        zDelta++; lenDelta--;
          547  +        total += cnt;
          548  +        if( total>limit ){
          549  +          /* ERROR: copy exceeds output file size */
          550  +          return -1;
          551  +        }
          552  +        if( ofst+cnt > lenSrc ){
          553  +          /* ERROR: copy extends past end of input */
          554  +          return -1;
          555  +        }
          556  +        memcpy(zOut, &zSrc[ofst], cnt);
          557  +        zOut += cnt;
          558  +        break;
          559  +      }
          560  +      case ':': {
          561  +        zDelta++; lenDelta--;
          562  +        total += cnt;
          563  +        if( total>limit ){
          564  +          /* ERROR:  insert command gives an output larger than predicted */
          565  +          return -1;
          566  +        }
          567  +        if( cnt>lenDelta ){
          568  +          /* ERROR: insert count exceeds size of delta */
          569  +          return -1;
          570  +        }
          571  +        memcpy(zOut, zDelta, cnt);
          572  +        zOut += cnt;
          573  +        zDelta += cnt;
          574  +        lenDelta -= cnt;
          575  +        break;
          576  +      }
          577  +      case ';': {
          578  +        zDelta++; lenDelta--;
          579  +        zOut[0] = 0;
          580  +#ifdef FOSSIL_ENABLE_DELTA_CKSUM_TEST
          581  +        if( cnt!=checksum(zOrigOut, total) ){
          582  +          /* ERROR:  bad checksum */
          583  +          return -1;
          584  +        }
          585  +#endif
          586  +        if( total!=limit ){
          587  +          /* ERROR: generated size does not match predicted size */
          588  +          return -1;
          589  +        }
          590  +        return total;
          591  +      }
          592  +      default: {
          593  +        /* ERROR: unknown delta operator */
          594  +        return -1;
          595  +      }
          596  +    }
          597  +  }
          598  +  /* ERROR: unterminated delta */
          599  +  return -1;
          600  +}
          601  +
          602  +/*
          603  +** SQL functions:  fossildelta_create(X,Y)
          604  +**
          605  +** Return a delta for carrying X into Y.
          606  +*/
          607  +static void deltaCreateFunc(
          608  +  sqlite3_context *context,
          609  +  int argc,
          610  +  sqlite3_value **argv
          611  +){
          612  +  const char *aOrig; int nOrig;  /* old blob */
          613  +  const char *aNew;  int nNew;   /* new blob */
          614  +  char *aOut;        int nOut;   /* output delta */
          615  +
          616  +  assert( argc==2 );
          617  +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
          618  +  if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return;
          619  +  nOrig = sqlite3_value_bytes(argv[0]);
          620  +  aOrig = (const char*)sqlite3_value_blob(argv[0]);
          621  +  nNew = sqlite3_value_bytes(argv[1]);
          622  +  aNew = (const char*)sqlite3_value_blob(argv[1]);
          623  +  aOut = sqlite3_malloc64(nNew+70);
          624  +  if( aOut==0 ){
          625  +    sqlite3_result_error_nomem(context);
          626  +  }else{
          627  +    nOut = delta_create(aOrig, nOrig, aNew, nNew, aOut);
          628  +    if( nOut<0 ){
          629  +      sqlite3_free(aOut);
          630  +      sqlite3_result_error(context, "cannot create fossil delta", -1);
          631  +    }else{
          632  +      sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
          633  +    }
          634  +  }
          635  +}
          636  +
          637  +/*
          638  +** SQL functions:  fossildelta_apply(X,D)
          639  +**
          640  +** Return the result of applying delta D to input X.
          641  +*/
          642  +static void deltaApplyFunc(
          643  +  sqlite3_context *context,
          644  +  int argc,
          645  +  sqlite3_value **argv
          646  +){
          647  +  const char *aOrig;   int nOrig;        /* The X input */
          648  +  const char *aDelta;  int nDelta;       /* The input delta (D) */
          649  +  char *aOut;          int nOut, nOut2;  /* The output */
          650  +
          651  +  assert( argc==2 );
          652  +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
          653  +  if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return;
          654  +  nOrig = sqlite3_value_bytes(argv[0]);
          655  +  aOrig = (const char*)sqlite3_value_blob(argv[0]);
          656  +  nDelta = sqlite3_value_bytes(argv[1]);
          657  +  aDelta = (const char*)sqlite3_value_blob(argv[1]);
          658  +
          659  +  /* Figure out the size of the output */
          660  +  nOut = delta_output_size(aDelta, nDelta);
          661  +  if( nOut<0 ){
          662  +    sqlite3_result_error(context, "corrupt fossil delta", -1);
          663  +    return;
          664  +  }
          665  +  aOut = sqlite3_malloc64((sqlite3_int64)nOut+1);
          666  +  if( aOut==0 ){
          667  +    sqlite3_result_error_nomem(context);
          668  +  }else{
          669  +    nOut2 = delta_apply(aOrig, nOrig, aDelta, nDelta, aOut);
          670  +    if( nOut2!=nOut ){
          671  +      sqlite3_free(aOut);
          672  +      sqlite3_result_error(context, "corrupt fossil delta", -1);
          673  +    }else{
          674  +      sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
          675  +    }
          676  +  }
          677  +}
          678  +
          679  +
          680  +/*
          681  +** SQL functions:  fossildelta_output_size(D)
          682  +**
          683  +** Return the size of the output that results from applying delta D.
          684  +*/
          685  +static void deltaOutputSizeFunc(
          686  +  sqlite3_context *context,
          687  +  int argc,
          688  +  sqlite3_value **argv
          689  +){
          690  +  const char *aDelta;  int nDelta;       /* The input delta (D) */
          691  +  int nOut;                              /* Size of output */
          692  +  assert( argc==1 );
          693  +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
          694  +  nDelta = sqlite3_value_bytes(argv[0]);
          695  +  aDelta = (const char*)sqlite3_value_blob(argv[0]);
          696  +
          697  +  /* Figure out the size of the output */
          698  +  nOut = delta_output_size(aDelta, nDelta);
          699  +  if( nOut<0 ){
          700  +    sqlite3_result_error(context, "corrupt fossil delta", -1);
          701  +    return;
          702  +  }else{
          703  +    sqlite3_result_int(context, nOut);
          704  +  }
          705  +}
          706  +
          707  +/* The deltaparse(DELTA) table-valued function parses the DELTA in
          708  +** its input and returns a table that describes that delta.
          709  +*/
          710  +typedef struct deltaparsevtab_vtab deltaparsevtab_vtab;
          711  +typedef struct deltaparsevtab_cursor deltaparsevtab_cursor;
          712  +struct deltaparsevtab_vtab {
          713  +  sqlite3_vtab base;  /* Base class - must be first */
          714  +  /* No additional information needed */
          715  +};
          716  +struct deltaparsevtab_cursor {
          717  +  sqlite3_vtab_cursor base;  /* Base class - must be first */
          718  +  char *aDelta;              /* The delta being parsed */
          719  +  int nDelta;                /* Number of bytes in the delta */
          720  +  int iCursor;               /* Current cursor location */
          721  +  int eOp;                   /* Name of current operator */
          722  +  unsigned int a1, a2;       /* Arguments to current operator */
          723  +  int iNext;                 /* Next cursor value */
          724  +};
          725  +
          726  +/* Operator names:
          727  +*/
          728  +static const char *azOp[] = {
          729  +  "SIZE", "COPY", "INSERT", "CHECKSUM", "ERROR", "EOF"
          730  +};
          731  +#define DELTAPARSE_OP_SIZE         0
          732  +#define DELTAPARSE_OP_COPY         1
          733  +#define DELTAPARSE_OP_INSERT       2
          734  +#define DELTAPARSE_OP_CHECKSUM     3
          735  +#define DELTAPARSE_OP_ERROR        4
          736  +#define DELTAPARSE_OP_EOF          5
          737  +
          738  +/*
          739  +** The deltaparsevtabConnect() method is invoked to create a new
          740  +** deltaparse virtual table.
          741  +**
          742  +** Think of this routine as the constructor for deltaparsevtab_vtab objects.
          743  +**
          744  +** All this routine needs to do is:
          745  +**
          746  +**    (1) Allocate the deltaparsevtab_vtab object and initialize all fields.
          747  +**
          748  +**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
          749  +**        result set of queries against the virtual table will look like.
          750  +*/
          751  +static int deltaparsevtabConnect(
          752  +  sqlite3 *db,
          753  +  void *pAux,
          754  +  int argc, const char *const*argv,
          755  +  sqlite3_vtab **ppVtab,
          756  +  char **pzErr
          757  +){
          758  +  deltaparsevtab_vtab *pNew;
          759  +  int rc;
          760  +
          761  +  rc = sqlite3_declare_vtab(db,
          762  +           "CREATE TABLE x(op,a1,a2,delta HIDDEN)"
          763  +       );
          764  +  /* For convenience, define symbolic names for the index to each column. */
          765  +#define DELTAPARSEVTAB_OP     0
          766  +#define DELTAPARSEVTAB_A1     1
          767  +#define DELTAPARSEVTAB_A2     2
          768  +#define DELTAPARSEVTAB_DELTA  3
          769  +  if( rc==SQLITE_OK ){
          770  +    pNew = sqlite3_malloc64( sizeof(*pNew) );
          771  +    *ppVtab = (sqlite3_vtab*)pNew;
          772  +    if( pNew==0 ) return SQLITE_NOMEM;
          773  +    memset(pNew, 0, sizeof(*pNew));
          774  +  }
          775  +  return rc;
          776  +}
          777  +
          778  +/*
          779  +** This method is the destructor for deltaparsevtab_vtab objects.
          780  +*/
          781  +static int deltaparsevtabDisconnect(sqlite3_vtab *pVtab){
          782  +  deltaparsevtab_vtab *p = (deltaparsevtab_vtab*)pVtab;
          783  +  sqlite3_free(p);
          784  +  return SQLITE_OK;
          785  +}
          786  +
          787  +/*
          788  +** Constructor for a new deltaparsevtab_cursor object.
          789  +*/
          790  +static int deltaparsevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
          791  +  deltaparsevtab_cursor *pCur;
          792  +  pCur = sqlite3_malloc( sizeof(*pCur) );
          793  +  if( pCur==0 ) return SQLITE_NOMEM;
          794  +  memset(pCur, 0, sizeof(*pCur));
          795  +  *ppCursor = &pCur->base;
          796  +  return SQLITE_OK;
          797  +}
          798  +
          799  +/*
          800  +** Destructor for a deltaparsevtab_cursor.
          801  +*/
          802  +static int deltaparsevtabClose(sqlite3_vtab_cursor *cur){
          803  +  deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur;
          804  +  sqlite3_free(pCur);
          805  +  return SQLITE_OK;
          806  +}
          807  +
          808  +
          809  +/*
          810  +** Advance a deltaparsevtab_cursor to its next row of output.
          811  +*/
          812  +static int deltaparsevtabNext(sqlite3_vtab_cursor *cur){
          813  +  deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur;
          814  +  const char *z;
          815  +  int i = 0;
          816  +
          817  +  pCur->iCursor = pCur->iNext;
          818  +  z = pCur->aDelta + pCur->iCursor;
          819  +  pCur->a1 = deltaGetInt(&z, &i);
          820  +  switch( z[0] ){
          821  +    case '@': {
          822  +      z++;
          823  +      pCur->a2 = deltaGetInt(&z, &i);
          824  +      pCur->eOp = DELTAPARSE_OP_COPY;
          825  +      pCur->iNext = (int)(&z[1] - pCur->aDelta);
          826  +      break;
          827  +    }
          828  +    case ':': {
          829  +      z++;
          830  +      pCur->a2 = (unsigned int)(z - pCur->aDelta);
          831  +      pCur->eOp = DELTAPARSE_OP_INSERT;
          832  +      pCur->iNext = (int)(&z[pCur->a1] - pCur->aDelta);
          833  +      break;
          834  +    }
          835  +    case ';': {
          836  +      pCur->eOp = DELTAPARSE_OP_CHECKSUM;
          837  +      pCur->iNext = pCur->nDelta;
          838  +      break;
          839  +    }
          840  +    default: {
          841  +      if( pCur->iNext==pCur->nDelta ){
          842  +        pCur->eOp = DELTAPARSE_OP_EOF;
          843  +      }else{
          844  +        pCur->eOp = DELTAPARSE_OP_ERROR;
          845  +        pCur->iNext = pCur->nDelta;
          846  +      }
          847  +      break;
          848  +    }
          849  +  }
          850  +  return SQLITE_OK;
          851  +}
          852  +
          853  +/*
          854  +** Return values of columns for the row at which the deltaparsevtab_cursor
          855  +** is currently pointing.
          856  +*/
          857  +static int deltaparsevtabColumn(
          858  +  sqlite3_vtab_cursor *cur,   /* The cursor */
          859  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
          860  +  int i                       /* Which column to return */
          861  +){
          862  +  deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur;
          863  +  switch( i ){
          864  +    case DELTAPARSEVTAB_OP: {
          865  +      sqlite3_result_text(ctx, azOp[pCur->eOp], -1, SQLITE_STATIC);
          866  +      break;
          867  +    }
          868  +    case DELTAPARSEVTAB_A1: {
          869  +      sqlite3_result_int(ctx, pCur->a1);
          870  +      break;
          871  +    }
          872  +    case DELTAPARSEVTAB_A2: {
          873  +      if( pCur->eOp==DELTAPARSE_OP_COPY ){
          874  +        sqlite3_result_int(ctx, pCur->a2);
          875  +      }else if( pCur->eOp==DELTAPARSE_OP_INSERT ){
          876  +        sqlite3_result_blob(ctx, pCur->aDelta+pCur->a2, pCur->a1,
          877  +                            SQLITE_TRANSIENT);
          878  +      }
          879  +      break;
          880  +    }
          881  +    case DELTAPARSEVTAB_DELTA: {
          882  +      sqlite3_result_blob(ctx, pCur->aDelta, pCur->nDelta, SQLITE_TRANSIENT);
          883  +      break;
          884  +    }
          885  +  }
          886  +  return SQLITE_OK;
          887  +}
          888  +
          889  +/*
          890  +** Return the rowid for the current row.  In this implementation, the
          891  +** rowid is the same as the output value.
          892  +*/
          893  +static int deltaparsevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
          894  +  deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur;
          895  +  *pRowid = pCur->iCursor;
          896  +  return SQLITE_OK;
          897  +}
          898  +
          899  +/*
          900  +** Return TRUE if the cursor has been moved off of the last
          901  +** row of output.
          902  +*/
          903  +static int deltaparsevtabEof(sqlite3_vtab_cursor *cur){
          904  +  deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur;
          905  +  return pCur->eOp==DELTAPARSE_OP_EOF;
          906  +}
          907  +
          908  +/*
          909  +** This method is called to "rewind" the deltaparsevtab_cursor object back
          910  +** to the first row of output.  This method is always called at least
          911  +** once prior to any call to deltaparsevtabColumn() or deltaparsevtabRowid() or 
          912  +** deltaparsevtabEof().
          913  +*/
          914  +static int deltaparsevtabFilter(
          915  +  sqlite3_vtab_cursor *pVtabCursor, 
          916  +  int idxNum, const char *idxStr,
          917  +  int argc, sqlite3_value **argv
          918  +){
          919  +  deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor *)pVtabCursor;
          920  +  const char *a;
          921  +  int i = 0;
          922  +  pCur->eOp = DELTAPARSE_OP_ERROR;
          923  +  if( idxNum!=1 ){
          924  +    return SQLITE_OK;
          925  +  }
          926  +  pCur->nDelta = sqlite3_value_bytes(argv[0]);
          927  +  a = (const char*)sqlite3_value_blob(argv[0]);
          928  +  if( pCur->nDelta==0 || a==0 ){
          929  +    return SQLITE_OK;
          930  +  }
          931  +  pCur->aDelta = sqlite3_malloc64( pCur->nDelta+1 );
          932  +  if( pCur->aDelta==0 ){
          933  +    pCur->nDelta = 0;
          934  +    return SQLITE_NOMEM;
          935  +  }
          936  +  memcpy(pCur->aDelta, a, pCur->nDelta);
          937  +  pCur->aDelta[pCur->nDelta] = 0;
          938  +  a = pCur->aDelta;
          939  +  pCur->eOp = DELTAPARSE_OP_SIZE;
          940  +  pCur->a1 = deltaGetInt(&a, &i);
          941  +  if( a[0]!='\n' ){
          942  +    pCur->eOp = DELTAPARSE_OP_ERROR;
          943  +    pCur->a1 = pCur->a2 = 0;
          944  +    pCur->iNext = pCur->nDelta;
          945  +    return SQLITE_OK;
          946  +  }
          947  +  a++;
          948  +  pCur->iNext = (unsigned int)(a - pCur->aDelta);
          949  +  return SQLITE_OK;
          950  +}
          951  +
          952  +/*
          953  +** SQLite will invoke this method one or more times while planning a query
          954  +** that uses the virtual table.  This routine needs to create
          955  +** a query plan for each invocation and compute an estimated cost for that
          956  +** plan.
          957  +*/
          958  +static int deltaparsevtabBestIndex(
          959  +  sqlite3_vtab *tab,
          960  +  sqlite3_index_info *pIdxInfo
          961  +){
          962  +  int i;
          963  +  for(i=0; i<pIdxInfo->nConstraint; i++){
          964  +    if( pIdxInfo->aConstraint[i].iColumn != DELTAPARSEVTAB_DELTA ) continue;
          965  +    if( pIdxInfo->aConstraint[i].usable==0 ) continue;
          966  +    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
          967  +    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
          968  +    pIdxInfo->aConstraintUsage[i].omit = 1;
          969  +    pIdxInfo->estimatedCost = (double)1;
          970  +    pIdxInfo->estimatedRows = 10;
          971  +    pIdxInfo->idxNum = 1;
          972  +    return SQLITE_OK;
          973  +  }
          974  +  pIdxInfo->idxNum = 0;
          975  +  pIdxInfo->estimatedCost = (double)0x7fffffff;
          976  +  pIdxInfo->estimatedRows = 0x7fffffff;
          977  +  return SQLITE_CONSTRAINT;
          978  +}
          979  +
          980  +/*
          981  +** This following structure defines all the methods for the 
          982  +** virtual table.
          983  +*/
          984  +static sqlite3_module deltaparsevtabModule = {
          985  +  /* iVersion    */ 0,
          986  +  /* xCreate     */ 0,
          987  +  /* xConnect    */ deltaparsevtabConnect,
          988  +  /* xBestIndex  */ deltaparsevtabBestIndex,
          989  +  /* xDisconnect */ deltaparsevtabDisconnect,
          990  +  /* xDestroy    */ 0,
          991  +  /* xOpen       */ deltaparsevtabOpen,
          992  +  /* xClose      */ deltaparsevtabClose,
          993  +  /* xFilter     */ deltaparsevtabFilter,
          994  +  /* xNext       */ deltaparsevtabNext,
          995  +  /* xEof        */ deltaparsevtabEof,
          996  +  /* xColumn     */ deltaparsevtabColumn,
          997  +  /* xRowid      */ deltaparsevtabRowid,
          998  +  /* xUpdate     */ 0,
          999  +  /* xBegin      */ 0,
         1000  +  /* xSync       */ 0,
         1001  +  /* xCommit     */ 0,
         1002  +  /* xRollback   */ 0,
         1003  +  /* xFindMethod */ 0,
         1004  +  /* xRename     */ 0,
         1005  +  /* xSavepoint  */ 0,
         1006  +  /* xRelease    */ 0,
         1007  +  /* xRollbackTo */ 0,
         1008  +  /* xShadowName */ 0
         1009  +};
         1010  +
         1011  +
         1012  +
         1013  +#ifdef _WIN32
         1014  +__declspec(dllexport)
         1015  +#endif
         1016  +int sqlite3_fossildelta_init(
         1017  +  sqlite3 *db, 
         1018  +  char **pzErrMsg, 
         1019  +  const sqlite3_api_routines *pApi
         1020  +){
         1021  +  int rc = SQLITE_OK;
         1022  +  SQLITE_EXTENSION_INIT2(pApi);
         1023  +  (void)pzErrMsg;  /* Unused parameter */
         1024  +  rc = sqlite3_create_function(db, "delta_create", 2, SQLITE_UTF8, 0,
         1025  +                               deltaCreateFunc, 0, 0);
         1026  +  if( rc==SQLITE_OK ){
         1027  +    rc = sqlite3_create_function(db, "delta_apply", 2, SQLITE_UTF8, 0,
         1028  +                                 deltaApplyFunc, 0, 0);
         1029  +  }
         1030  +  if( rc==SQLITE_OK ){
         1031  +    rc = sqlite3_create_function(db, "delta_output_size", 1, SQLITE_UTF8, 0,
         1032  +                                 deltaOutputSizeFunc, 0, 0);
         1033  +  }
         1034  +  if( rc==SQLITE_OK ){
         1035  +    rc = sqlite3_create_module(db, "delta_parse", &deltaparsevtabModule, 0);
         1036  +  }
         1037  +  return rc;
         1038  +}

Changes to ext/rbu/sqlite3rbu.c.

   680    680   
   681    681     aOut = sqlite3_malloc(nOut+1);
   682    682     if( aOut==0 ){
   683    683       sqlite3_result_error_nomem(context);
   684    684     }else{
   685    685       nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
   686    686       if( nOut2!=nOut ){
          687  +      sqlite3_free(aOut);
   687    688         sqlite3_result_error(context, "corrupt fossil delta", -1);
   688    689       }else{
   689    690         sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
   690    691       }
   691    692     }
   692    693   }
   693    694   

Changes to src/btree.c.

  6726   6726       }
  6727   6727       pIns = pPage->aCellIdx + i*2;
  6728   6728       memmove(pIns+2, pIns, 2*(pPage->nCell - i));
  6729   6729       put2byte(pIns, idx);
  6730   6730       pPage->nCell++;
  6731   6731       /* increment the cell count */
  6732   6732       if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
  6733         -    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
         6733  +    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
  6734   6734   #ifndef SQLITE_OMIT_AUTOVACUUM
  6735   6735       if( pPage->pBt->autoVacuum ){
  6736   6736         /* The cell may contain a pointer to an overflow page. If so, write
  6737   6737         ** the entry for the overflow page into the pointer map.
  6738   6738         */
  6739   6739         ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
  6740   6740       }
................................................................................
  8257   8257     if( rc ){
  8258   8258       *ppChild = 0;
  8259   8259       releasePage(pChild);
  8260   8260       return rc;
  8261   8261     }
  8262   8262     assert( sqlite3PagerIswriteable(pChild->pDbPage) );
  8263   8263     assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
  8264         -  assert( pChild->nCell==pRoot->nCell );
         8264  +  assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
  8265   8265   
  8266   8266     TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
  8267   8267   
  8268   8268     /* Copy the overflow cells from pRoot to pChild */
  8269   8269     memcpy(pChild->aiOvfl, pRoot->aiOvfl,
  8270   8270            pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
  8271   8271     memcpy(pChild->apOvfl, pRoot->apOvfl,

Changes to src/build.c.

   256    256   
   257    257     if( pParse->nErr ) return;
   258    258     assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
   259    259     va_start(ap, zFormat);
   260    260     zSql = sqlite3VMPrintf(db, zFormat, ap);
   261    261     va_end(ap);
   262    262     if( zSql==0 ){
   263         -    return;   /* A malloc must have failed */
          263  +    /* This can result either from an OOM or because the formatted string
          264  +    ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
          265  +    ** an error */
          266  +    if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
          267  +    return;
   264    268     }
   265    269     pParse->nested++;
   266    270     memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
   267    271     memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
   268    272     sqlite3RunParser(pParse, zSql, &zErrMsg);
   269    273     sqlite3DbFree(db, zErrMsg);
   270    274     sqlite3DbFree(db, zSql);
................................................................................
  2036   2040         sqlite3ErrorMsg(pParse, "");
  2037   2041         return;
  2038   2042       }
  2039   2043       p->tnum = db->init.newTnum;
  2040   2044       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
  2041   2045     }
  2042   2046   
         2047  +  assert( (p->tabFlags & TF_HasPrimaryKey)==0
         2048  +       || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
         2049  +  assert( (p->tabFlags & TF_HasPrimaryKey)!=0
         2050  +       || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
         2051  +
  2043   2052     /* Special processing for WITHOUT ROWID Tables */
  2044   2053     if( tabOpts & TF_WithoutRowid ){
  2045   2054       if( (p->tabFlags & TF_Autoincrement) ){
  2046   2055         sqlite3ErrorMsg(pParse,
  2047   2056             "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
  2048   2057         return;
  2049   2058       }

Changes to src/insert.c.

  1665   1665           sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
  1666   1666           VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
  1667   1667         }
  1668   1668       }
  1669   1669       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
  1670   1670       VdbeComment((v, "for %s", pIdx->zName));
  1671   1671   #ifdef SQLITE_ENABLE_NULL_TRIM
  1672         -    if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
         1672  +    if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
         1673  +      sqlite3SetMakeRecordP5(v, pIdx->pTable);
         1674  +    }
  1673   1675   #endif
  1674   1676   
  1675   1677       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
  1676   1678       ** of a WITHOUT ROWID table and there has been no change the
  1677   1679       ** primary key, then no collision is possible.  The collision detection
  1678   1680       ** logic below can all be skipped. */
  1679   1681       if( isUpdate && pPk==pIdx && pkChng==0 ){
................................................................................
  2406   2408           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
  2407   2409         }
  2408   2410         if( i==pSrcIdx->nColumn ){
  2409   2411           idxInsFlags = OPFLAG_USESEEKRESULT;
  2410   2412           sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
  2411   2413         }
  2412   2414       }
  2413         -    if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
         2415  +    if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
  2414   2416         idxInsFlags |= OPFLAG_NCHANGE;
  2415   2417       }
  2416   2418       sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
  2417   2419       sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
  2418   2420       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
  2419   2421       sqlite3VdbeJumpHere(v, addr1);
  2420   2422       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);

Changes to src/os_unix.c.

  7818   7818     ** array cannot be const.
  7819   7819     */
  7820   7820     static sqlite3_vfs aVfs[] = {
  7821   7821   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
  7822   7822       UNIXVFS("unix",          autolockIoFinder ),
  7823   7823   #elif OS_VXWORKS
  7824   7824       UNIXVFS("unix",          vxworksIoFinder ),
         7825  +#elif __Fuchsia__
         7826  +    /* We are told that Fuchsia only supports dot-file locking */
         7827  +    UNIXVFS("unix",          dotlockIoFinder ),
  7825   7828   #else
  7826   7829       UNIXVFS("unix",          posixIoFinder ),
  7827   7830   #endif
  7828   7831       UNIXVFS("unix-none",     nolockIoFinder ),
  7829   7832       UNIXVFS("unix-dotfile",  dotlockIoFinder ),
  7830   7833       UNIXVFS("unix-excl",     posixIoFinder ),
  7831   7834   #if OS_VXWORKS

Changes to src/printf.c.

   132    132   
   133    133   /*
   134    134   ** Set the StrAccum object to an error mode.
   135    135   */
   136    136   static void setStrAccumError(StrAccum *p, u8 eError){
   137    137     assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
   138    138     p->accError = eError;
   139         -  p->nAlloc = 0;
          139  +  if( p->mxAlloc ) sqlite3_str_reset(p);
   140    140   }
   141    141   
   142    142   /*
   143    143   ** Extra argument values from a PrintfArguments object
   144    144   */
   145    145   static sqlite3_int64 getIntArg(PrintfArguments *p){
   146    146     if( p->nArg<=p->nUsed ) return 0;
................................................................................
   162    162   ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
   163    163   ** Do the size check before the memory allocation to prevent rogue
   164    164   ** SQL from requesting large allocations using the precision or width
   165    165   ** field of the printf() function.
   166    166   */
   167    167   static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
   168    168     char *z;
          169  +  if( pAccum->accError ) return 0;
   169    170     if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
   170    171       setStrAccumError(pAccum, SQLITE_TOOBIG);
   171    172       return 0;
   172    173     }
   173    174     z = sqlite3DbMallocRaw(pAccum->db, n);
   174    175     if( z==0 ){
   175    176       setStrAccumError(pAccum, SQLITE_NOMEM);
................................................................................
   881    882     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
   882    883     if( p->accError ){
   883    884       testcase(p->accError==SQLITE_TOOBIG);
   884    885       testcase(p->accError==SQLITE_NOMEM);
   885    886       return 0;
   886    887     }
   887    888     if( p->mxAlloc==0 ){
   888         -    N = p->nAlloc - p->nChar - 1;
   889    889       setStrAccumError(p, SQLITE_TOOBIG);
   890         -    return N;
          890  +    return p->nAlloc - p->nChar - 1;
   891    891     }else{
   892    892       char *zOld = isMalloced(p) ? p->zText : 0;
   893    893       i64 szNew = p->nChar;
   894    894       szNew += N + 1;
   895    895       if( szNew+p->nChar<=p->mxAlloc ){
   896    896         /* Force exponential buffer size growth as long as it does not overflow,
   897    897         ** to avoid having to call this routine too often */
................................................................................
   955    955   ** Append N bytes of text from z to the StrAccum object.  Increase the
   956    956   ** size of the memory allocation for StrAccum if necessary.
   957    957   */
   958    958   void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
   959    959     assert( z!=0 || N==0 );
   960    960     assert( p->zText!=0 || p->nChar==0 || p->accError );
   961    961     assert( N>=0 );
   962         -  assert( p->accError==0 || p->nAlloc==0 );
          962  +  assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
   963    963     if( p->nChar+N >= p->nAlloc ){
   964    964       enlargeAndAppend(p,z,N);
   965    965     }else if( N ){
   966    966       assert( p->zText );
   967    967       p->nChar += N;
   968    968       memcpy(&p->zText[p->nChar-N], z, N);
   969    969     }

Changes to src/vdbe.c.

   815    815   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   816    816     /* Call the progress callback if it is configured and the required number
   817    817     ** of VDBE ops have been executed (either since this invocation of
   818    818     ** sqlite3VdbeExec() or since last time the progress callback was called).
   819    819     ** If the progress callback returns non-zero, exit the virtual machine with
   820    820     ** a return code SQLITE_ABORT.
   821    821     */
   822         -  if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
          822  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
   823    823       assert( db->nProgressOps!=0 );
   824         -    nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
          824  +    nProgressLimit += db->nProgressOps;
   825    825       if( db->xProgress(db->pProgressArg) ){
   826    826         nProgressLimit = 0xffffffff;
   827    827         rc = SQLITE_INTERRUPT;
   828    828         goto abort_due_to_error;
   829    829       }
   830    830     }
   831    831   #endif
................................................................................
  1098   1098     pOp->opcode = OP_String;
  1099   1099     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
  1100   1100   
  1101   1101   #ifndef SQLITE_OMIT_UTF16
  1102   1102     if( encoding!=SQLITE_UTF8 ){
  1103   1103       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
  1104   1104       assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
         1105  +    if( rc ) goto too_big;
  1105   1106       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
  1106   1107       assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
  1107   1108       assert( VdbeMemDynamic(pOut)==0 );
  1108   1109       pOut->szMalloc = 0;
  1109   1110       pOut->flags |= MEM_Static;
  1110   1111       if( pOp->p4type==P4_DYNAMIC ){
  1111   1112         sqlite3DbFree(db, pOp->p4.z);
  1112   1113       }
  1113   1114       pOp->p4type = P4_DYNAMIC;
  1114   1115       pOp->p4.z = pOut->z;
  1115   1116       pOp->p1 = pOut->n;
  1116   1117     }
  1117         -  testcase( rc==SQLITE_TOOBIG );
  1118   1118   #endif
  1119   1119     if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1120   1120       goto too_big;
  1121   1121     }
  1122   1122     assert( rc==SQLITE_OK );
  1123   1123     /* Fall through to the next case, OP_String */
  1124   1124   }
................................................................................
  6170   6170       for(i=0; i<p->nMem; i++){
  6171   6171         aMem[i].pScopyFrom = 0;  /* Prevent false-positive AboutToChange() errs */
  6172   6172         aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
  6173   6173       }
  6174   6174     }
  6175   6175   #endif
  6176   6176     pOp = &aOp[-1];
  6177         -
  6178         -  break;
         6177  +  goto check_for_interrupt;
  6179   6178   }
  6180   6179   
  6181   6180   /* Opcode: Param P1 P2 * * *
  6182   6181   **
  6183   6182   ** This opcode is only ever present in sub-programs called via the 
  6184   6183   ** OP_Program instruction. Copy a value currently stored in a memory 
  6185   6184   ** cell of the calling (parent) frame to cell P2 in the current frames 
................................................................................
  7581   7580     }
  7582   7581   
  7583   7582     /* This is the only way out of this procedure.  We have to
  7584   7583     ** release the mutexes on btrees that were acquired at the
  7585   7584     ** top. */
  7586   7585   vdbe_return:
  7587   7586   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  7588         -  if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
         7587  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
         7588  +    nProgressLimit += db->nProgressOps;
  7589   7589       if( db->xProgress(db->pProgressArg) ){
  7590   7590         nProgressLimit = 0xffffffff;
  7591   7591         rc = SQLITE_INTERRUPT;
  7592   7592         goto abort_due_to_error;
  7593   7593       }
  7594   7594     }
  7595   7595   #endif

Changes to src/vdbeapi.c.

  1116   1116   **    3      The name of the table that the column derives from
  1117   1117   **    4      The name of the table column that the result column derives from
  1118   1118   **
  1119   1119   ** If the result is not a simple column reference (if it is an expression
  1120   1120   ** or a constant) then useTypes 2, 3, and 4 return NULL.
  1121   1121   */
  1122   1122   static const void *columnName(
  1123         -  sqlite3_stmt *pStmt,
  1124         -  int N,
  1125         -  const void *(*xFunc)(Mem*),
  1126         -  int useType
         1123  +  sqlite3_stmt *pStmt,     /* The statement */
         1124  +  int N,                   /* Which column to get the name for */
         1125  +  int useUtf16,            /* True to return the name as UTF16 */
         1126  +  int useType              /* What type of name */
  1127   1127   ){
  1128   1128     const void *ret;
  1129   1129     Vdbe *p;
  1130   1130     int n;
  1131   1131     sqlite3 *db;
  1132   1132   #ifdef SQLITE_ENABLE_API_ARMOR
  1133   1133     if( pStmt==0 ){
................................................................................
  1140   1140     db = p->db;
  1141   1141     assert( db!=0 );
  1142   1142     n = sqlite3_column_count(pStmt);
  1143   1143     if( N<n && N>=0 ){
  1144   1144       N += useType*n;
  1145   1145       sqlite3_mutex_enter(db->mutex);
  1146   1146       assert( db->mallocFailed==0 );
  1147         -    ret = xFunc(&p->aColName[N]);
  1148         -     /* A malloc may have failed inside of the xFunc() call. If this
         1147  +    if( useUtf16 ){
         1148  +      ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
         1149  +    }else{
         1150  +      ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
         1151  +    }
         1152  +    /* A malloc may have failed inside of the _text() call. If this
  1149   1153       ** is the case, clear the mallocFailed flag and return NULL.
  1150   1154       */
  1151   1155       if( db->mallocFailed ){
  1152   1156         sqlite3OomClear(db);
  1153   1157         ret = 0;
  1154   1158       }
  1155   1159       sqlite3_mutex_leave(db->mutex);
................................................................................
  1158   1162   }
  1159   1163   
  1160   1164   /*
  1161   1165   ** Return the name of the Nth column of the result set returned by SQL
  1162   1166   ** statement pStmt.
  1163   1167   */
  1164   1168   const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
  1165         -  return columnName(
  1166         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
         1169  +  return columnName(pStmt, N, 0, COLNAME_NAME);
  1167   1170   }
  1168   1171   #ifndef SQLITE_OMIT_UTF16
  1169   1172   const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
  1170         -  return columnName(
  1171         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
         1173  +  return columnName(pStmt, N, 1, COLNAME_NAME);
  1172   1174   }
  1173   1175   #endif
  1174   1176   
  1175   1177   /*
  1176   1178   ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
  1177   1179   ** not define OMIT_DECLTYPE.
  1178   1180   */
................................................................................
  1183   1185   
  1184   1186   #ifndef SQLITE_OMIT_DECLTYPE
  1185   1187   /*
  1186   1188   ** Return the column declaration type (if applicable) of the 'i'th column
  1187   1189   ** of the result set of SQL statement pStmt.
  1188   1190   */
  1189   1191   const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
  1190         -  return columnName(
  1191         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
         1192  +  return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
  1192   1193   }
  1193   1194   #ifndef SQLITE_OMIT_UTF16
  1194   1195   const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
  1195         -  return columnName(
  1196         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
         1196  +  return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
  1197   1197   }
  1198   1198   #endif /* SQLITE_OMIT_UTF16 */
  1199   1199   #endif /* SQLITE_OMIT_DECLTYPE */
  1200   1200   
  1201   1201   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1202   1202   /*
  1203   1203   ** Return the name of the database from which a result column derives.
  1204   1204   ** NULL is returned if the result column is an expression or constant or
  1205   1205   ** anything else which is not an unambiguous reference to a database column.
  1206   1206   */
  1207   1207   const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
  1208         -  return columnName(
  1209         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
         1208  +  return columnName(pStmt, N, 0, COLNAME_DATABASE);
  1210   1209   }
  1211   1210   #ifndef SQLITE_OMIT_UTF16
  1212   1211   const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
  1213         -  return columnName(
  1214         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
         1212  +  return columnName(pStmt, N, 1, COLNAME_DATABASE);
  1215   1213   }
  1216   1214   #endif /* SQLITE_OMIT_UTF16 */
  1217   1215   
  1218   1216   /*
  1219   1217   ** Return the name of the table from which a result column derives.
  1220   1218   ** NULL is returned if the result column is an expression or constant or
  1221   1219   ** anything else which is not an unambiguous reference to a database column.
  1222   1220   */
  1223   1221   const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
  1224         -  return columnName(
  1225         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
         1222  +  return columnName(pStmt, N, 0, COLNAME_TABLE);
  1226   1223   }
  1227   1224   #ifndef SQLITE_OMIT_UTF16
  1228   1225   const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
  1229         -  return columnName(
  1230         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
         1226  +  return columnName(pStmt, N, 1, COLNAME_TABLE);
  1231   1227   }
  1232   1228   #endif /* SQLITE_OMIT_UTF16 */
  1233   1229   
  1234   1230   /*
  1235   1231   ** Return the name of the table column from which a result column derives.
  1236   1232   ** NULL is returned if the result column is an expression or constant or
  1237   1233   ** anything else which is not an unambiguous reference to a database column.
  1238   1234   */
  1239   1235   const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
  1240         -  return columnName(
  1241         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
         1236  +  return columnName(pStmt, N, 0, COLNAME_COLUMN);
  1242   1237   }
  1243   1238   #ifndef SQLITE_OMIT_UTF16
  1244   1239   const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
  1245         -  return columnName(
  1246         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
         1240  +  return columnName(pStmt, N, 1, COLNAME_COLUMN);
  1247   1241   }
  1248   1242   #endif /* SQLITE_OMIT_UTF16 */
  1249   1243   #endif /* SQLITE_ENABLE_COLUMN_METADATA */
  1250   1244   
  1251   1245   
  1252   1246   /******************************* sqlite3_bind_  ***************************
  1253   1247   ** 

Changes to src/wherecode.c.

  1340   1340       iReleaseReg = ++pParse->nMem;
  1341   1341       iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
  1342   1342       if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
  1343   1343       addrNxt = pLevel->addrNxt;
  1344   1344       sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
  1345   1345       VdbeCoverage(v);
  1346   1346       pLevel->op = OP_Noop;
         1347  +    pTerm->wtFlags |= TERM_CODED;
  1347   1348     }else if( (pLoop->wsFlags & WHERE_IPK)!=0
  1348   1349            && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
  1349   1350     ){
  1350   1351       /* Case 3:  We have an inequality comparison against the ROWID field.
  1351   1352       */
  1352   1353       int testOp = OP_Noop;
  1353   1354       int start;

Changes to test/dbfuzz2.c.

    39     39   #include <assert.h>
    40     40   #include <stdio.h>
    41     41   #include <stdlib.h>
    42     42   #include <string.h>
    43     43   #include <stdarg.h>
    44     44   #include <ctype.h>
    45     45   #include <stdint.h>
           46  +#ifndef _WIN32
    46     47   #include <sys/time.h>
    47     48   #include <sys/resource.h>
           49  +#endif
    48     50   #include "sqlite3.h"
    49     51   
    50     52   /*
    51     53   ** This is the is the SQL that is run against the database.
    52     54   */
    53     55   static const char *azSql[] = {
    54     56     "PRAGMA integrity_check;",
................................................................................
   257    259           if( i+1==argc ){
   258    260             fprintf(stderr, "missing argument to %s\n", argv[i]);
   259    261             exit(1);
   260    262           }
   261    263           szMax = strtol(argv[++i], 0, 0);
   262    264           continue;
   263    265         }
          266  +#ifndef _WIN32
   264    267         if( strcmp(z,"max-stack")==0
   265    268          || strcmp(z,"max-data")==0
   266    269          || strcmp(z,"max-as")==0
   267    270         ){
   268    271           struct rlimit x,y;
   269    272           int resource = RLIMIT_STACK;
   270    273           char *zType = "RLIMIT_STACK";
................................................................................
   287    290           setrlimit(resource, &y);
   288    291           memset(&y,0,sizeof(y));
   289    292           getrlimit(resource, &y);
   290    293           printf("%s changed from %d to %d\n", 
   291    294                  zType, (int)x.rlim_cur, (int)y.rlim_cur);
   292    295           continue;
   293    296         }
          297  +#endif /* _WIN32 */
   294    298       }
   295    299       argv[j++] = argv[i];
   296    300     }
   297    301     argv[j] = 0;
   298    302     *pArgc = j;
   299    303     return 0;
   300    304   }

Changes to test/fuzzdata8.db.

cannot compute difference between binary files

Changes to test/in.test.

   646    646   do_execsql_test in-14.0 {
   647    647     CREATE TABLE c1(a);
   648    648     INSERT INTO c1 VALUES(1), (2), (4), (3);
   649    649   }
   650    650   do_execsql_test in-14.1 {
   651    651     SELECT * FROM c1 WHERE a IN (SELECT a FROM c1) ORDER BY 1
   652    652   } {1 2 3 4}
          653  +
          654  +# 2019-02-20 Ticket https://www.sqlite.org/src/tktview/df46dfb631f75694fbb97033b69
          655  +#
          656  +do_execsql_test in-15.0 {
          657  +  DROP TABLE IF EXISTS t1;
          658  +  CREATE TABLE IF NOT EXISTS t1(id INTEGER PRIMARY KEY);
          659  +  INSERT INTO t1 VALUES(1);
          660  +  SELECT a.id FROM t1 AS a JOIN t1 AS b ON a.id=b.id WHERE a.id IN (1,2,3);
          661  +} {1}
          662  +do_execsql_test in-15.1 {
          663  +  DROP TABLE IF EXISTS t2;
          664  +  CREATE TABLE t2(a INTEGER PRIMARY KEY,b);
          665  +  INSERT INTO t2 VALUES(1,11);
          666  +  INSERT INTO t2 VALUES(2,22);
          667  +  INSERT INTO t2 VALUES(3,33);
          668  +  SELECT b, a IN (3,4,5) FROM t2 ORDER BY b;
          669  +} {11 0 22 0 33 1}
          670  +do_execsql_test in-15.2 {
          671  +  DROP TABLE IF EXISTS t3;
          672  +  CREATE TABLE t3(x INTEGER PRIMARY KEY);
          673  +  INSERT INTO t3 VALUES(8);
          674  +  SELECT CASE WHEN x NOT IN (5,6,7) THEN 'yes' ELSE 'no' END FROM t3;
          675  +  SELECT CASE WHEN x NOT IN (NULL,6,7) THEN 'yes' ELSE 'no' END FROM t3;
          676  +} {yes no}
          677  +do_execsql_test in-15.3 {
          678  +  SELECT CASE WHEN x NOT IN (5,6,7) OR x=0 THEN 'yes' ELSE 'no' END FROM t3;
          679  +  SELECT CASE WHEN x NOT IN (NULL,6,7) OR x=0 THEN 'yes' ELSE 'no' END FROM t3;
          680  +} {yes no}
          681  +do_execsql_test in-15.4 {
          682  +  DROP TABLE IF EXISTS t4;
          683  +  CREATE TABLE t4(a INTEGER PRIMARY KEY, b INT);
          684  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
          685  +    INSERT INTO t4(a,b) SELECT x, x+100 FROM c;
          686  +  SELECT b FROM t4 WHERE a IN (3,null,8) ORDER BY +b;
          687  +} {103 108}
          688  +do_execsql_test in-15.5 {
          689  +  SELECT b FROM t4 WHERE a NOT IN (3,null,8);
          690  +} {}
          691  +do_execsql_test in-15.6 {
          692  +  DROP TABLE IF EXISTS t5;
          693  +  DROP TABLE IF EXISTS t6;
          694  +  CREATE TABLE t5(id INTEGER PRIMARY KEY, name TEXT);
          695  +  CREATE TABLE t6(id INTEGER PRIMARY KEY, name TEXT, t5_id INT);
          696  +  INSERT INTO t5 VALUES(1,'Alice'),(2,'Emma');
          697  +  INSERT INTO t6 VALUES(1,'Bob',1),(2,'Cindy',1),(3,'Dave',2);
          698  +  SELECT a.*
          699  +    FROM t5 AS 'a' JOIN t5 AS 'b' ON b.id=a.id
          700  +   WHERE b.id IN (
          701  +          SELECT t6.t5_id
          702  +            FROM t6
          703  +           WHERE name='Bob'
          704  +             AND t6.t5_id IS NOT NULL
          705  +             AND t6.id IN (
          706  +                  SELECT id
          707  +                    FROM (SELECT t6.id, count(*) AS x
          708  +                            FROM t6
          709  +                           WHERE name='Bob'
          710  +                         ) AS 't'
          711  +                   WHERE x=1
          712  +                 )
          713  +             AND t6.id IN (1,id)
          714  +         );
          715  +} {1 Alice}
          716  +
   653    717   
   654    718   finish_test

Changes to test/indexfault.test.

   332    332     faultsim_restore_and_reopen
   333    333     set ::nReadCall 0
   334    334     sqlite3_soft_heap_limit 0
   335    335   } -body {
   336    336     execsql { CREATE INDEX i1 ON t1(x) }
   337    337     faultsim_test_result {0 {}} 
   338    338   }
          339  +
          340  +do_faultsim_test 5 -prep {
          341  +  reset_db
          342  +} -body {
          343  +  execsql { 
          344  + CREATE TABLE reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongname(a PRIMARY KEY) WITHOUT ROWID;
          345  +  }
          346  +} -test {
          347  +  faultsim_test_result {0 {}} 
          348  +}
   339    349   
   340    350   uninstall_custom_faultsim
   341    351   
   342    352   finish_test

Changes to test/printf.test.

   536    536     sqlite3_mprintf_double {abc: %d %d (%1.1e) :xyz} 1 1 1.0e-20
   537    537   } {abc: 1 1 (1.0e-20) :xyz}
   538    538   do_test printf-2.1.2.9 {
   539    539     sqlite3_mprintf_double {abc: %d %d (%1.1g) :xyz} 1 1 1.0e-20
   540    540   } {abc: 1 1 (1e-20) :xyz}
   541    541   do_test printf-2.1.2.10 {
   542    542     sqlite3_mprintf_double {abc: %*.*f}  2000000000 1000000000 1.0e-20
   543         -} {abc: }
          543  +} {}
   544    544   do_test printf-2.1.3.1 {
   545    545     sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 1.0
   546    546   } {abc: (1.0) :xyz}
   547    547   do_test printf-2.1.3.2 {
   548    548     sqlite3_mprintf_double {abc: (%*.*e) :xyz} 1 1 1.0
   549    549   } {abc: (1.0e+00) :xyz}
   550    550   do_test printf-2.1.3.3 {