/ Check-in [67ea2979]
Login

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

Overview
Comment:Merge latest trunk changes with this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1: 67ea2979d5831b6d0d55173bd9413b21644cf6a1
User & Date: dan 2014-09-18 11:31:52
Context
2014-09-18
14:48
Add an API to query an ota handle for the total number of key/value operations performed so far. check-in: e3943fa7 user: dan tags: ota-update
11:31
Merge latest trunk changes with this branch. check-in: 67ea2979 user: dan tags: ota-update
11:15
Fix an ota bug causing attempts to write to tables with more than 8 columns to fail. check-in: 7da98ca2 user: dan tags: ota-update
09:59
Modify an assert() within sqlite3PagerWalFramesize(), a function only ever used by zipvfs, to account for recent zipvfs changes. check-in: 3bd7c1b2 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/btree.c.

  4743   4743           */
  4744   4744           nCell = pCell[0];
  4745   4745           if( nCell<=pPage->max1bytePayload ){
  4746   4746             /* This branch runs if the record-size field of the cell is a
  4747   4747             ** single byte varint and the record fits entirely on the main
  4748   4748             ** b-tree page.  */
  4749   4749             testcase( pCell+nCell+1==pPage->aDataEnd );
  4750         -          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
         4750  +          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
  4751   4751           }else if( !(pCell[1] & 0x80) 
  4752   4752             && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
  4753   4753           ){
  4754   4754             /* The record-size field is a 2 byte varint and the record 
  4755   4755             ** fits entirely on the main b-tree page.  */
  4756   4756             testcase( pCell+nCell+2==pPage->aDataEnd );
  4757         -          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
         4757  +          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
  4758   4758           }else{
  4759   4759             /* The record flows over onto one or more overflow pages. In
  4760   4760             ** this case the whole cell needs to be parsed, a buffer allocated
  4761   4761             ** and accessPayload() used to retrieve the record into the
  4762   4762             ** buffer before VdbeRecordCompare() can be called. */
  4763   4763             void *pCellKey;
  4764   4764             u8 * const pCellBody = pCell - pPage->childPtrSize;
................................................................................
  4771   4771             }
  4772   4772             pCur->aiIdx[pCur->iPage] = (u16)idx;
  4773   4773             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
  4774   4774             if( rc ){
  4775   4775               sqlite3_free(pCellKey);
  4776   4776               goto moveto_finish;
  4777   4777             }
  4778         -          c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
         4778  +          c = xRecordCompare(nCell, pCellKey, pIdxKey);
  4779   4779             sqlite3_free(pCellKey);
  4780   4780           }
  4781   4781           assert( 
  4782   4782               (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
  4783   4783            && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
  4784   4784           );
  4785   4785           if( c<0 ){

Changes to src/func.c.

  1488   1488       cmp = sqlite3MemCompare(pBest, pArg, pColl);
  1489   1489       if( (max && cmp<0) || (!max && cmp>0) ){
  1490   1490         sqlite3VdbeMemCopy(pBest, pArg);
  1491   1491       }else{
  1492   1492         sqlite3SkipAccumulatorLoad(context);
  1493   1493       }
  1494   1494     }else{
         1495  +    pBest->db = sqlite3_context_db_handle(context);
  1495   1496       sqlite3VdbeMemCopy(pBest, pArg);
  1496   1497     }
  1497   1498   }
  1498   1499   static void minMaxFinalize(sqlite3_context *context){
  1499   1500     sqlite3_value *pRes;
  1500   1501     pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
  1501   1502     if( pRes ){

Changes to src/malloc.c.

   301    301       ** signed integer value might cause an integer overflow inside of the
   302    302       ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   303    303       ** 255 bytes of overhead.  SQLite itself will never use anything near
   304    304       ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   305    305       p = 0;
   306    306     }else if( sqlite3GlobalConfig.bMemstat ){
   307    307       sqlite3_mutex_enter(mem0.mutex);
   308         -    mallocWithAlarm(n, &p);
          308  +    mallocWithAlarm((int)n, &p);
   309    309       sqlite3_mutex_leave(mem0.mutex);
   310    310     }else{
   311    311       p = sqlite3GlobalConfig.m.xMalloc((int)n);
   312    312     }
   313    313     assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
   314    314     return p;
   315    315   }
................................................................................
   545    545     ** argument to xRealloc is always a value returned by a prior call to
   546    546     ** xRoundup. */
   547    547     nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
   548    548     if( nOld==nNew ){
   549    549       pNew = pOld;
   550    550     }else if( sqlite3GlobalConfig.bMemstat ){
   551    551       sqlite3_mutex_enter(mem0.mutex);
   552         -    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
          552  +    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
   553    553       nDiff = nNew - nOld;
   554    554       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
   555    555             mem0.alarmThreshold-nDiff ){
   556    556         sqlite3MallocAlarm(nDiff);
   557    557       }
   558    558       assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   559    559       assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   560    560       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   561    561       if( pNew==0 && mem0.alarmCallback ){
   562         -      sqlite3MallocAlarm(nBytes);
          562  +      sqlite3MallocAlarm((int)nBytes);
   563    563         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   564    564       }
   565    565       if( pNew ){
   566    566         nNew = sqlite3MallocSize(pNew);
   567    567         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   568    568       }
   569    569       sqlite3_mutex_leave(mem0.mutex);
................................................................................
   595    595   
   596    596   /*
   597    597   ** Allocate and zero memory.
   598    598   */ 
   599    599   void *sqlite3MallocZero(u64 n){
   600    600     void *p = sqlite3Malloc(n);
   601    601     if( p ){
   602         -    memset(p, 0, n);
          602  +    memset(p, 0, (size_t)n);
   603    603     }
   604    604     return p;
   605    605   }
   606    606   
   607    607   /*
   608    608   ** Allocate and zero memory.  If the allocation fails, make
   609    609   ** the mallocFailed flag in the connection pointer.
   610    610   */
   611    611   void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
   612    612     void *p = sqlite3DbMallocRaw(db, n);
   613    613     if( p ){
   614         -    memset(p, 0, n);
          614  +    memset(p, 0, (size_t)n);
   615    615     }
   616    616     return p;
   617    617   }
   618    618   
   619    619   /*
   620    620   ** Allocate and zero memory.  If the allocation fails, make
   621    621   ** the mallocFailed flag in the connection pointer.
................................................................................
   695    695           memcpy(pNew, p, db->lookaside.sz);
   696    696           sqlite3DbFree(db, p);
   697    697         }
   698    698       }else{
   699    699         assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   700    700         assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   701    701         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   702         -      pNew = sqlite3_realloc(p, n);
          702  +      pNew = sqlite3_realloc64(p, n);
   703    703         if( !pNew ){
   704    704           sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   705    705           db->mallocFailed = 1;
   706    706         }
   707    707         sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
   708    708               (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   709    709       }
................................................................................
   749    749     char *zNew;
   750    750     if( z==0 ){
   751    751       return 0;
   752    752     }
   753    753     assert( (n&0x7fffffff)==n );
   754    754     zNew = sqlite3DbMallocRaw(db, n+1);
   755    755     if( zNew ){
   756         -    memcpy(zNew, z, n);
          756  +    memcpy(zNew, z, (size_t)n);
   757    757       zNew[n] = 0;
   758    758     }
   759    759     return zNew;
   760    760   }
   761    761   
   762    762   /*
   763    763   ** Create a string from the zFromat argument and the va_list that follows.

Changes to src/pager.c.

  7276   7276   ** A read-lock must be held on the pager when this function is called. If
  7277   7277   ** the pager is in WAL mode and the WAL file currently contains one or more
  7278   7278   ** frames, return the size in bytes of the page images stored within the
  7279   7279   ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
  7280   7280   ** is empty, return 0.
  7281   7281   */
  7282   7282   int sqlite3PagerWalFramesize(Pager *pPager){
  7283         -  assert( pPager->eState==PAGER_READER );
         7283  +  assert( pPager->eState>=PAGER_READER );
  7284   7284     return sqlite3WalFramesize(pPager->pWal);
  7285   7285   }
  7286   7286   #endif
  7287   7287   
  7288   7288   /*
  7289   7289   ** Set or clear the "OTA mode" flag.
  7290   7290   */

Changes to src/printf.c.

     9      9   **
    10     10   ** This file contains code for a set of "printf"-like routines.  These
    11     11   ** routines format strings much like the printf() from the standard C
    12     12   ** library, though the implementation here has enhancements to support
    13     13   ** SQLlite.
    14     14   */
    15     15   #include "sqliteInt.h"
           16  +
           17  +/*
           18  +** If the strchrnul() library function is available, then set
           19  +** HAVE_STRCHRNUL.  If that routine is not available, this module
           20  +** will supply its own.  The built-in version is slower than
           21  +** the glibc version so the glibc version is definitely preferred.
           22  +*/
           23  +#if !defined(HAVE_STRCHRNUL)
           24  +# if defined(__linux__) && defined(_GNU_SOURCE)
           25  +#  define HAVE_STRCHRNUL 1
           26  +# else
           27  +#  define HAVE_STRCHRNUL 0
           28  +# endif
           29  +#endif
           30  +
    16     31   
    17     32   /*
    18     33   ** Conversion types fall into various categories as defined by the
    19     34   ** following enumeration.
    20     35   */
    21     36   #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
    22     37   #define etFLOAT       2 /* Floating point.  %f */
................................................................................
   220    235       useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
   221    236     }else{
   222    237       bArgList = useIntern = 0;
   223    238     }
   224    239     for(; (c=(*fmt))!=0; ++fmt){
   225    240       if( c!='%' ){
   226    241         bufpt = (char *)fmt;
   227         -      while( (c=(*++fmt))!='%' && c!=0 ){};
          242  +#if HAVE_STRCHRNUL
          243  +      fmt = strchrnul(fmt, '%');
          244  +#else
          245  +      do{ fmt++; }while( *fmt && *fmt != '%' );
          246  +#endif
   228    247         sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
   229         -      if( c==0 ) break;
          248  +      if( *fmt==0 ) break;
   230    249       }
   231    250       if( (c=(*++fmt))==0 ){
   232    251         sqlite3StrAccumAppend(pAccum, "%", 1);
   233    252         break;
   234    253       }
   235    254       /* Find out what flags are present */
   236    255       flag_leftjustify = flag_plussign = flag_blanksign = 

Changes to src/table.c.

    69     69     if( p->nRow==0 ){
    70     70       p->nColumn = nCol;
    71     71       for(i=0; i<nCol; i++){
    72     72         z = sqlite3_mprintf("%s", colv[i]);
    73     73         if( z==0 ) goto malloc_failed;
    74     74         p->azResult[p->nData++] = z;
    75     75       }
    76         -  }else if( p->nColumn!=nCol ){
           76  +  }else if( (int)p->nColumn!=nCol ){
    77     77       sqlite3_free(p->zErrMsg);
    78     78       p->zErrMsg = sqlite3_mprintf(
    79     79          "sqlite3_get_table() called with two or more incompatible queries"
    80     80       );
    81     81       p->rc = SQLITE_ERROR;
    82     82       return 1;
    83     83     }

Changes to src/utf.c.

   310    310         }
   311    311       }
   312    312       pMem->n = (int)(z - zOut);
   313    313     }
   314    314     *z = 0;
   315    315     assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   316    316   
          317  +  c = pMem->flags;
   317    318     sqlite3VdbeMemRelease(pMem);
   318         -  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
          319  +  pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
   319    320     pMem->enc = desiredEnc;
   320         -  pMem->flags |= (MEM_Term);
   321    321     pMem->z = (char*)zOut;
   322    322     pMem->zMalloc = pMem->z;
   323    323   
   324    324   translate_out:
   325    325   #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   326    326     {
   327    327       char zBuf[100];

Changes to src/vdbe.c.

   636    636       */
   637    637       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   638    638       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   639    639         assert( pOp->p2>0 );
   640    640         assert( pOp->p2<=(p->nMem-p->nCursor) );
   641    641         pOut = &aMem[pOp->p2];
   642    642         memAboutToChange(p, pOut);
   643         -      VdbeMemReleaseExtern(pOut);
          643  +      if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
   644    644         pOut->flags = MEM_Int;
   645    645       }
   646    646   
   647    647       /* Sanity checking on other operands */
   648    648   #ifdef SQLITE_DEBUG
   649    649       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   650    650         assert( pOp->p1>0 );
................................................................................
  1075   1075     u16 nullFlag;
  1076   1076     cnt = pOp->p3-pOp->p2;
  1077   1077     assert( pOp->p3<=(p->nMem-p->nCursor) );
  1078   1078     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  1079   1079     while( cnt>0 ){
  1080   1080       pOut++;
  1081   1081       memAboutToChange(p, pOut);
  1082         -    VdbeMemReleaseExtern(pOut);
         1082  +    sqlite3VdbeMemSetNull(pOut);
  1083   1083       pOut->flags = nullFlag;
  1084   1084       cnt--;
  1085   1085     }
  1086   1086     break;
  1087   1087   }
  1088   1088   
  1089   1089   /* Opcode: SoftNull P1 * * * *
................................................................................
  1170   1170       memcpy(pOut, pIn1, sizeof(Mem));
  1171   1171   #ifdef SQLITE_DEBUG
  1172   1172       if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  1173   1173         pOut->pScopyFrom += p1 - pOp->p2;
  1174   1174       }
  1175   1175   #endif
  1176   1176       pIn1->flags = MEM_Undefined;
  1177         -    pIn1->xDel = 0;
  1178   1177       pIn1->zMalloc = zMalloc;
  1179   1178       REGISTER_TRACE(p2++, pOut);
  1180   1179       pIn1++;
  1181   1180       pOut++;
  1182   1181     }while( --n );
  1183   1182     break;
  1184   1183   }
................................................................................
  1908   1907       if( affinity ){
  1909   1908         applyAffinity(pIn1, affinity, encoding);
  1910   1909         applyAffinity(pIn3, affinity, encoding);
  1911   1910         if( db->mallocFailed ) goto no_mem;
  1912   1911       }
  1913   1912   
  1914   1913       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
  1915         -    ExpandBlob(pIn1);
  1916         -    ExpandBlob(pIn3);
         1914  +    if( pIn1->flags & MEM_Zero ){
         1915  +      sqlite3VdbeMemExpandBlob(pIn1);
         1916  +      flags1 &= ~MEM_Zero;
         1917  +    }
         1918  +    if( pIn3->flags & MEM_Zero ){
         1919  +      sqlite3VdbeMemExpandBlob(pIn3);
         1920  +      flags3 &= ~MEM_Zero;
         1921  +    }
  1917   1922       res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
  1918   1923     }
  1919   1924     switch( pOp->opcode ){
  1920   1925       case OP_Eq:    res = res==0;     break;
  1921   1926       case OP_Ne:    res = res!=0;     break;
  1922   1927       case OP_Lt:    res = res<0;      break;
  1923   1928       case OP_Le:    res = res<=0;     break;
................................................................................
  1934   1939     }else{
  1935   1940       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
  1936   1941       if( res ){
  1937   1942         pc = pOp->p2-1;
  1938   1943       }
  1939   1944     }
  1940   1945     /* Undo any changes made by applyAffinity() to the input registers. */
  1941         -  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
  1942         -  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
         1946  +  pIn1->flags = flags1;
         1947  +  pIn3->flags = flags3;
  1943   1948     break;
  1944   1949   }
  1945   1950   
  1946   1951   /* Opcode: Permutation * * * P4 *
  1947   1952   **
  1948   1953   ** Set the permutation used by the OP_Compare operator to be the array
  1949   1954   ** of integers in P4.
................................................................................
  2103   2108   ** Interpret the value in register P1 as a boolean value.  Store the
  2104   2109   ** boolean complement in register P2.  If the value in register P1 is 
  2105   2110   ** NULL, then a NULL is stored in P2.
  2106   2111   */
  2107   2112   case OP_Not: {                /* same as TK_NOT, in1, out2 */
  2108   2113     pIn1 = &aMem[pOp->p1];
  2109   2114     pOut = &aMem[pOp->p2];
  2110         -  if( pIn1->flags & MEM_Null ){
  2111   2115       sqlite3VdbeMemSetNull(pOut);
  2112         -  }else{
  2113         -    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
         2116  +  if( (pIn1->flags & MEM_Null)==0 ){
         2117  +    pOut->flags = MEM_Int;
         2118  +    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
  2114   2119     }
  2115   2120     break;
  2116   2121   }
  2117   2122   
  2118   2123   /* Opcode: BitNot P1 P2 * * *
  2119   2124   ** Synopsis: r[P1]= ~r[P1]
  2120   2125   **
................................................................................
  2121   2126   ** Interpret the content of register P1 as an integer.  Store the
  2122   2127   ** ones-complement of the P1 value into register P2.  If P1 holds
  2123   2128   ** a NULL then store a NULL in P2.
  2124   2129   */
  2125   2130   case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
  2126   2131     pIn1 = &aMem[pOp->p1];
  2127   2132     pOut = &aMem[pOp->p2];
  2128         -  if( pIn1->flags & MEM_Null ){
  2129   2133       sqlite3VdbeMemSetNull(pOut);
  2130         -  }else{
  2131         -    sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
         2134  +  if( (pIn1->flags & MEM_Null)==0 ){
         2135  +    pOut->flags = MEM_Int;
         2136  +    pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
  2132   2137     }
  2133   2138     break;
  2134   2139   }
  2135   2140   
  2136   2141   /* Opcode: Once P1 P2 * * *
  2137   2142   **
  2138   2143   ** Check the "once" flag number P1. If it is set, jump to instruction P2. 
................................................................................
  2433   2438     /* Extract the content for the p2+1-th column.  Control can only
  2434   2439     ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
  2435   2440     ** all valid.
  2436   2441     */
  2437   2442     assert( p2<pC->nHdrParsed );
  2438   2443     assert( rc==SQLITE_OK );
  2439   2444     assert( sqlite3VdbeCheckMemInvariants(pDest) );
         2445  +  if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
  2440   2446     if( pC->szRow>=aOffset[p2+1] ){
  2441   2447       /* This is the common case where the desired content fits on the original
  2442   2448       ** page - where the content is not on an overflow page */
  2443         -    VdbeMemReleaseExtern(pDest);
  2444   2449       sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
  2445   2450     }else{
  2446   2451       /* This branch happens only when content is on overflow pages */
  2447   2452       t = aType[p2];
  2448   2453       if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  2449   2454             && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
  2450   2455        || (len = sqlite3VdbeSerialTypeLen(t))==0
  2451   2456       ){
  2452         -      /* Content is irrelevant for the typeof() function and for
  2453         -      ** the length(X) function if X is a blob.  So we might as well use
  2454         -      ** bogus content rather than reading content from disk.  NULL works
  2455         -      ** for text and blob and whatever is in the payloadSize64 variable
  2456         -      ** will work for everything else.  Content is also irrelevant if
  2457         -      ** the content length is 0. */
  2458         -      zData = t<=13 ? (u8*)&payloadSize64 : 0;
  2459         -      sMem.zMalloc = 0;
         2457  +      /* Content is irrelevant for
         2458  +      **    1. the typeof() function,
         2459  +      **    2. the length(X) function if X is a blob, and
         2460  +      **    3. if the content length is zero.
         2461  +      ** So we might as well use bogus content rather than reading
         2462  +      ** content from disk.  NULL will work for the value for strings
         2463  +      ** and blobs and whatever is in the payloadSize64 variable
         2464  +      ** will work for everything else. */
         2465  +      sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
  2460   2466       }else{
  2461         -      memset(&sMem, 0, sizeof(sMem));
  2462         -      sqlite3VdbeMemMove(&sMem, pDest);
  2463   2467         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
  2464         -                                   &sMem);
         2468  +                                   pDest);
  2465   2469         if( rc!=SQLITE_OK ){
  2466   2470           goto op_column_error;
  2467   2471         }
  2468         -      zData = (u8*)sMem.z;
  2469         -    }
  2470         -    sqlite3VdbeSerialGet(zData, t, pDest);
  2471         -    /* If we dynamically allocated space to hold the data (in the
  2472         -    ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  2473         -    ** dynamically allocated space over to the pDest structure.
  2474         -    ** This prevents a memory copy. */
  2475         -    if( sMem.zMalloc ){
  2476         -      assert( sMem.z==sMem.zMalloc );
  2477         -      assert( VdbeMemDynamic(pDest)==0 );
  2478         -      assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
  2479         -      pDest->flags &= ~(MEM_Ephem|MEM_Static);
  2480         -      pDest->flags |= MEM_Term;
  2481         -      pDest->z = sMem.z;
  2482         -      pDest->zMalloc = sMem.zMalloc;
         2472  +      sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
         2473  +      pDest->flags &= ~MEM_Ephem;
  2483   2474       }
  2484   2475     }
  2485   2476     pDest->enc = encoding;
  2486   2477   
  2487   2478   op_column_out:
  2488   2479     Deephemeralize(pDest);
  2489   2480   op_column_error:
................................................................................
  2655   2646     }while( (++pRec)<=pLast );
  2656   2647     assert( i==nHdr );
  2657   2648     assert( j==nByte );
  2658   2649   
  2659   2650     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2660   2651     pOut->n = (int)nByte;
  2661   2652     pOut->flags = MEM_Blob;
  2662         -  pOut->xDel = 0;
  2663   2653     if( nZero ){
  2664   2654       pOut->u.nZero = nZero;
  2665   2655       pOut->flags |= MEM_Zero;
  2666   2656     }
  2667   2657     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
  2668   2658     REGISTER_TRACE(pOp->p3, pOut);
  2669   2659     UPDATE_MAX_BLOBSIZE(pOut);
................................................................................
  4849   4839       r.default_rc = 0;
  4850   4840     }
  4851   4841     r.aMem = &aMem[pOp->p3];
  4852   4842   #ifdef SQLITE_DEBUG
  4853   4843     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4854   4844   #endif
  4855   4845     res = 0;  /* Not needed.  Only used to silence a warning. */
  4856         -  rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
         4846  +  rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
  4857   4847     assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
  4858   4848     if( (pOp->opcode&1)==(OP_IdxLT&1) ){
  4859   4849       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
  4860   4850       res = -res;
  4861   4851     }else{
  4862   4852       assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
  4863   4853       res++;
................................................................................
  5619   5609       apVal[i] = pRec;
  5620   5610       memAboutToChange(p, pRec);
  5621   5611     }
  5622   5612     ctx.pFunc = pOp->p4.pFunc;
  5623   5613     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5624   5614     ctx.pMem = pMem = &aMem[pOp->p3];
  5625   5615     pMem->n++;
  5626         -  t.flags = MEM_Null;
  5627         -  t.z = 0;
  5628         -  t.zMalloc = 0;
  5629         -  t.xDel = 0;
  5630         -  t.db = db;
         5616  +  sqlite3VdbeMemInit(&t, db, MEM_Null);
  5631   5617     ctx.pOut = &t;
  5632   5618     ctx.isError = 0;
  5633   5619     ctx.pColl = 0;
  5634   5620     ctx.skipFlag = 0;
  5635   5621     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5636   5622       assert( pOp>p->aOp );
  5637   5623       assert( pOp[-1].p4type==P4_COLLSEQ );

Changes to src/vdbe.h.

   208    208   void sqlite3VdbeSetVarmask(Vdbe*, int);
   209    209   #ifndef SQLITE_OMIT_TRACE
   210    210     char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   211    211   #endif
   212    212   int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   213    213   
   214    214   void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
   215         -int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
          215  +int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
   216    216   UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
   217    217   
   218         -typedef int (*RecordCompare)(int,const void*,UnpackedRecord*,int);
          218  +typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
   219    219   RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
   220    220   
   221    221   #ifndef SQLITE_OMIT_TRIGGER
   222    222   void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
   223    223   #endif
   224    224   
   225    225   /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on

Changes to src/vdbeInt.h.

   157    157   
   158    158   /*
   159    159   ** Internally, the vdbe manipulates nearly all SQL values as Mem
   160    160   ** structures. Each Mem struct may cache multiple representations (string,
   161    161   ** integer etc.) of the same value.
   162    162   */
   163    163   struct Mem {
   164         -  sqlite3 *db;        /* The associated database connection */
   165         -  char *z;            /* String or BLOB value */
   166         -  double r;           /* Real value */
   167    164     union {
   168    165       i64 i;              /* Integer value used when MEM_Int is set in flags */
   169    166       int nZero;          /* Used when bit MEM_Zero is set in flags */
   170    167       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   171    168       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   172    169       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   173    170     } u;
   174         -  int n;              /* Number of characters in string value, excluding '\0' */
   175    171     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   176    172     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
          173  +  int n;              /* Number of characters in string value, excluding '\0' */
          174  +  double r;           /* Real value */
          175  +  char *z;            /* String or BLOB value */
          176  +  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
          177  +  /* ShallowCopy only needs to copy the information above */
          178  +  sqlite3 *db;        /* The associated database connection */
          179  +  void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
   177    180   #ifdef SQLITE_DEBUG
   178    181     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   179    182     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   180    183   #endif
   181         -  void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   182         -  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
   183    184   };
   184    185   
   185    186   /* One or more of the following flags are set to indicate the validOK
   186    187   ** representations of the value stored in the Mem struct.
   187    188   **
   188    189   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
   189    190   ** No other flags may be set in this case.
................................................................................
   392    393   u32 sqlite3VdbeSerialTypeLen(u32);
   393    394   u32 sqlite3VdbeSerialType(Mem*, int);
   394    395   u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
   395    396   u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   396    397   void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
   397    398   
   398    399   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   399         -int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
          400  +int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
   400    401   int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
   401    402   int sqlite3VdbeExec(Vdbe*);
   402    403   int sqlite3VdbeList(Vdbe*);
   403    404   int sqlite3VdbeHalt(Vdbe*);
   404    405   int sqlite3VdbeChangeEncoding(Mem *, int);
   405    406   int sqlite3VdbeMemTooBig(Mem*);
   406    407   int sqlite3VdbeMemCopy(Mem*, const Mem*);
................................................................................
   410    411   int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
   411    412   void sqlite3VdbeMemSetInt64(Mem*, i64);
   412    413   #ifdef SQLITE_OMIT_FLOATING_POINT
   413    414   # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
   414    415   #else
   415    416     void sqlite3VdbeMemSetDouble(Mem*, double);
   416    417   #endif
          418  +void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
   417    419   void sqlite3VdbeMemSetNull(Mem*);
   418    420   void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   419    421   void sqlite3VdbeMemSetRowSet(Mem*);
   420    422   int sqlite3VdbeMemMakeWriteable(Mem*);
   421    423   int sqlite3VdbeMemStringify(Mem*, u8, u8);
   422    424   i64 sqlite3VdbeIntValue(Mem*);
   423    425   int sqlite3VdbeMemIntegerify(Mem*);
................................................................................
   424    426   double sqlite3VdbeRealValue(Mem*);
   425    427   void sqlite3VdbeIntegerAffinity(Mem*);
   426    428   int sqlite3VdbeMemRealify(Mem*);
   427    429   int sqlite3VdbeMemNumerify(Mem*);
   428    430   void sqlite3VdbeMemCast(Mem*,u8,u8);
   429    431   int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   430    432   void sqlite3VdbeMemRelease(Mem *p);
   431         -void sqlite3VdbeMemReleaseExternal(Mem *p);
   432    433   #define VdbeMemDynamic(X)  \
   433    434     (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
   434         -#define VdbeMemReleaseExtern(X)  \
   435         -  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   436    435   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   437    436   const char *sqlite3OpcodeName(int);
   438    437   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   439    438   int sqlite3VdbeCloseStatement(Vdbe *, int);
   440    439   void sqlite3VdbeFrameDelete(VdbeFrame*);
   441    440   int sqlite3VdbeFrameRestore(VdbeFrame *);
   442    441   int sqlite3VdbeTransferError(Vdbe *p);

Changes to src/vdbeapi.c.

   658    658   ** Create a new aggregate context for p and return a pointer to
   659    659   ** its pMem->z element.
   660    660   */
   661    661   static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
   662    662     Mem *pMem = p->pMem;
   663    663     assert( (pMem->flags & MEM_Agg)==0 );
   664    664     if( nByte<=0 ){
   665         -    sqlite3VdbeMemReleaseExternal(pMem);
   666         -    pMem->flags = MEM_Null;
          665  +    sqlite3VdbeMemSetNull(pMem);
   667    666       pMem->z = 0;
   668    667     }else{
   669    668       sqlite3VdbeMemGrow(pMem, nByte, 0);
   670    669       pMem->flags = MEM_Agg;
   671    670       pMem->u.pDef = p->pFunc;
   672    671       if( pMem->z ){
   673    672         memset(pMem->z, 0, nByte);
................................................................................
   799    798     ** these assert()s from failing, when building with SQLITE_DEBUG defined
   800    799     ** using gcc, we force nullMem to be 8-byte aligned using the magical
   801    800     ** __attribute__((aligned(8))) macro.  */
   802    801     static const Mem nullMem 
   803    802   #if defined(SQLITE_DEBUG) && defined(__GNUC__)
   804    803       __attribute__((aligned(8))) 
   805    804   #endif
   806         -    = {0, "", (double)0, {0}, 0, MEM_Null, 0,
          805  +    = {
          806  +        /* .u          = */ {0},
          807  +        /* .flags      = */ MEM_Null,
          808  +        /* .enc        = */ 0,
          809  +        /* .n          = */ 0,
          810  +        /* .r          = */ (double)0,
          811  +        /* .z          = */ 0,
          812  +        /* .zMalloc    = */ 0,
          813  +        /* .db         = */ 0,
          814  +        /* .xDel       = */ 0,
   807    815   #ifdef SQLITE_DEBUG
   808         -       0, 0,  /* pScopyFrom, pFiller */
          816  +        /* .pScopyFrom = */ 0,
          817  +        /* .pFiller    = */ 0,
   809    818   #endif
   810         -       0, 0 };
          819  +      };
   811    820     return &nullMem;
   812    821   }
   813    822   
   814    823   /*
   815    824   ** Check to see if column iCol of the given statement is valid.  If
   816    825   ** it is, return a pointer to the Mem for the value of that column.
   817    826   ** If iCol is not valid, return a pointer to a Mem which has a value
................................................................................
  1180   1189     sqlite3_uint64 nData, 
  1181   1190     void (*xDel)(void*)
  1182   1191   ){
  1183   1192     assert( xDel!=SQLITE_DYNAMIC );
  1184   1193     if( nData>0x7fffffff ){
  1185   1194       return invokeValueDestructor(zData, xDel, 0);
  1186   1195     }else{
  1187         -    return bindText(pStmt, i, zData, nData, xDel, 0);
         1196  +    return bindText(pStmt, i, zData, (int)nData, xDel, 0);
  1188   1197     }
  1189   1198   }
  1190   1199   int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
  1191   1200     int rc;
  1192   1201     Vdbe *p = (Vdbe *)pStmt;
  1193   1202     rc = vdbeUnbind(p, i);
  1194   1203     if( rc==SQLITE_OK ){
................................................................................
  1237   1246     unsigned char enc
  1238   1247   ){
  1239   1248     assert( xDel!=SQLITE_DYNAMIC );
  1240   1249     if( nData>0x7fffffff ){
  1241   1250       return invokeValueDestructor(zData, xDel, 0);
  1242   1251     }else{
  1243   1252       if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
  1244         -    return bindText(pStmt, i, zData, nData, xDel, enc);
         1253  +    return bindText(pStmt, i, zData, (int)nData, xDel, enc);
  1245   1254     }
  1246   1255   }
  1247   1256   #ifndef SQLITE_OMIT_UTF16
  1248   1257   int sqlite3_bind_text16(
  1249   1258     sqlite3_stmt *pStmt, 
  1250   1259     int i, 
  1251   1260     const void *zData, 

Changes to src/vdbeaux.c.

  3081   3081         pMem->flags = MEM_Int;
  3082   3082         return 0;
  3083   3083       }
  3084   3084       default: {
  3085   3085         static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
  3086   3086         pMem->z = (char *)buf;
  3087   3087         pMem->n = (serial_type-12)/2;
  3088         -      pMem->xDel = 0;
  3089   3088         pMem->flags = aFlag[serial_type&1];
  3090   3089         return pMem->n;
  3091   3090       }
  3092   3091     }
  3093   3092     return 0;
  3094   3093   }
  3095   3094   /*
................................................................................
  3157   3156     Mem *pMem = p->aMem;
  3158   3157   
  3159   3158     p->default_rc = 0;
  3160   3159     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  3161   3160     idx = getVarint32(aKey, szHdr);
  3162   3161     d = szHdr;
  3163   3162     u = 0;
  3164         -  while( idx<szHdr && u<p->nField && d<=nKey ){
         3163  +  while( idx<szHdr && d<=nKey ){
  3165   3164       u32 serial_type;
  3166   3165   
  3167   3166       idx += getVarint32(&aKey[idx], serial_type);
  3168   3167       pMem->enc = pKeyInfo->enc;
  3169   3168       pMem->db = pKeyInfo->db;
  3170   3169       /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
  3171   3170       pMem->zMalloc = 0;
  3172   3171       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  3173   3172       pMem++;
  3174         -    u++;
         3173  +    if( (++u)>=p->nField ) break;
  3175   3174     }
  3176   3175     assert( u<=pKeyInfo->nField + 1 );
  3177   3176     p->nField = u;
  3178   3177   }
  3179   3178   
  3180   3179   #if SQLITE_DEBUG
  3181   3180   /*
................................................................................
  3299   3298       return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
  3300   3299     }else{
  3301   3300       int rc;
  3302   3301       const void *v1, *v2;
  3303   3302       int n1, n2;
  3304   3303       Mem c1;
  3305   3304       Mem c2;
  3306         -    memset(&c1, 0, sizeof(c1));
  3307         -    memset(&c2, 0, sizeof(c2));
         3305  +    c1.db = c2.db = pMem1->db;
         3306  +    c1.flags = c2.flags = 0;
         3307  +    c1.zMalloc = c2.zMalloc = 0;
  3308   3308       sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
  3309   3309       sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
  3310   3310       v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
  3311   3311       n1 = v1==0 ? 0 : c1.n;
  3312   3312       v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
  3313   3313       n2 = v2==0 ? 0 : c2.n;
  3314   3314       rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
  3315   3315       sqlite3VdbeMemRelease(&c1);
  3316   3316       sqlite3VdbeMemRelease(&c2);
  3317   3317       if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
  3318   3318       return rc;
  3319   3319     }
  3320   3320   }
         3321  +
         3322  +/*
         3323  +** Compare two blobs.  Return negative, zero, or positive if the first
         3324  +** is less than, equal to, or greater than the second, respectively.
         3325  +** If one blob is a prefix of the other, then the shorter is the lessor.
         3326  +*/
         3327  +static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
         3328  +  int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
         3329  +  if( c ) return c;
         3330  +  return pB1->n - pB2->n;
         3331  +}
         3332  +
  3321   3333   
  3322   3334   /*
  3323   3335   ** Compare the values contained by the two memory cells, returning
  3324   3336   ** negative, zero or positive if pMem1 is less than, equal to, or greater
  3325   3337   ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
  3326   3338   ** and reals) sorted numerically, followed by text ordered by the collating
  3327   3339   ** sequence pColl and finally blob's ordered by memcmp().
  3328   3340   **
  3329   3341   ** Two NULL values are considered equal by this function.
  3330   3342   */
  3331   3343   int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
  3332         -  int rc;
  3333   3344     int f1, f2;
  3334   3345     int combined_flags;
  3335   3346   
  3336   3347     f1 = pMem1->flags;
  3337   3348     f2 = pMem2->flags;
  3338   3349     combined_flags = f1|f2;
  3339   3350     assert( (combined_flags & MEM_RowSet)==0 );
................................................................................
  3400   3411         return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
  3401   3412       }
  3402   3413       /* If a NULL pointer was passed as the collate function, fall through
  3403   3414       ** to the blob case and use memcmp().  */
  3404   3415     }
  3405   3416    
  3406   3417     /* Both values must be blobs.  Compare using memcmp().  */
  3407         -  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
  3408         -  if( rc==0 ){
  3409         -    rc = pMem1->n - pMem2->n;
  3410         -  }
  3411         -  return rc;
         3418  +  return sqlite3BlobCompare(pMem1, pMem2);
  3412   3419   }
  3413   3420   
  3414   3421   
  3415   3422   /*
  3416   3423   ** The first argument passed to this function is a serial-type that
  3417   3424   ** corresponds to an integer - all values between 1 and 9 inclusive 
  3418   3425   ** except 7. The second points to a buffer containing an integer value
................................................................................
  3470   3477   ** returned.
  3471   3478   **
  3472   3479   ** If database corruption is discovered, set pPKey2->errCode to 
  3473   3480   ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, 
  3474   3481   ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
  3475   3482   ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
  3476   3483   */
  3477         -int sqlite3VdbeRecordCompare(
         3484  +static int vdbeRecordCompareWithSkip(
  3478   3485     int nKey1, const void *pKey1,   /* Left key */
  3479   3486     UnpackedRecord *pPKey2,         /* Right key */
  3480   3487     int bSkip                       /* If true, skip the first field */
  3481   3488   ){
  3482   3489     u32 d1;                         /* Offset into aKey[] of next data element */
  3483   3490     int i;                          /* Index of next field to compare */
  3484   3491     u32 szHdr1;                     /* Size of record header in bytes */
................................................................................
  3652   3659     ** value.  */
  3653   3660     assert( CORRUPT_DB 
  3654   3661          || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) 
  3655   3662          || pKeyInfo->db->mallocFailed
  3656   3663     );
  3657   3664     return pPKey2->default_rc;
  3658   3665   }
         3666  +int sqlite3VdbeRecordCompare(
         3667  +  int nKey1, const void *pKey1,   /* Left key */
         3668  +  UnpackedRecord *pPKey2          /* Right key */
         3669  +){
         3670  +  return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
         3671  +}
         3672  +
  3659   3673   
  3660   3674   /*
  3661   3675   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
  3662   3676   ** that (a) the first field of pPKey2 is an integer, and (b) the 
  3663   3677   ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
  3664   3678   ** byte (i.e. is less than 128).
  3665   3679   **
  3666   3680   ** To avoid concerns about buffer overreads, this routine is only used
  3667   3681   ** on schemas where the maximum valid header size is 63 bytes or less.
  3668   3682   */
  3669   3683   static int vdbeRecordCompareInt(
  3670   3684     int nKey1, const void *pKey1, /* Left key */
  3671         -  UnpackedRecord *pPKey2,       /* Right key */
  3672         -  int bSkip                     /* Ignored */
         3685  +  UnpackedRecord *pPKey2        /* Right key */
  3673   3686   ){
  3674   3687     const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
  3675   3688     int serial_type = ((const u8*)pKey1)[1];
  3676   3689     int res;
  3677   3690     u32 y;
  3678   3691     u64 x;
  3679   3692     i64 v = pPKey2->aMem[0].u.i;
  3680   3693     i64 lhs;
  3681         -  UNUSED_PARAMETER(bSkip);
  3682   3694   
  3683         -  assert( bSkip==0 );
  3684   3695     assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
  3685   3696     switch( serial_type ){
  3686   3697       case 1: { /* 1-byte signed integer */
  3687   3698         lhs = ONE_BYTE_INT(aKey);
  3688   3699         testcase( lhs<0 );
  3689   3700         break;
  3690   3701       }
................................................................................
  3726   3737       /* This case could be removed without changing the results of running
  3727   3738       ** this code. Including it causes gcc to generate a faster switch 
  3728   3739       ** statement (since the range of switch targets now starts at zero and
  3729   3740       ** is contiguous) but does not cause any duplicate code to be generated
  3730   3741       ** (as gcc is clever enough to combine the two like cases). Other 
  3731   3742       ** compilers might be similar.  */ 
  3732   3743       case 0: case 7:
  3733         -      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
         3744  +      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
  3734   3745   
  3735   3746       default:
  3736         -      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
         3747  +      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
  3737   3748     }
  3738   3749   
  3739   3750     if( v>lhs ){
  3740   3751       res = pPKey2->r1;
  3741   3752     }else if( v<lhs ){
  3742   3753       res = pPKey2->r2;
  3743   3754     }else if( pPKey2->nField>1 ){
  3744   3755       /* The first fields of the two keys are equal. Compare the trailing 
  3745   3756       ** fields.  */
  3746         -    res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
         3757  +    res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  3747   3758     }else{
  3748   3759       /* The first fields of the two keys are equal and there are no trailing
  3749   3760       ** fields. Return pPKey2->default_rc in this case. */
  3750   3761       res = pPKey2->default_rc;
  3751   3762     }
  3752   3763   
  3753   3764     assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
................................................................................
  3758   3769   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
  3759   3770   ** that (a) the first field of pPKey2 is a string, that (b) the first field
  3760   3771   ** uses the collation sequence BINARY and (c) that the size-of-header varint 
  3761   3772   ** at the start of (pKey1/nKey1) fits in a single byte.
  3762   3773   */
  3763   3774   static int vdbeRecordCompareString(
  3764   3775     int nKey1, const void *pKey1, /* Left key */
  3765         -  UnpackedRecord *pPKey2,       /* Right key */
  3766         -  int bSkip
         3776  +  UnpackedRecord *pPKey2        /* Right key */
  3767   3777   ){
  3768   3778     const u8 *aKey1 = (const u8*)pKey1;
  3769   3779     int serial_type;
  3770   3780     int res;
  3771         -  UNUSED_PARAMETER(bSkip);
  3772   3781   
  3773         -  assert( bSkip==0 );
  3774   3782     getVarint32(&aKey1[1], serial_type);
  3775         -
  3776   3783     if( serial_type<12 ){
  3777   3784       res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
  3778   3785     }else if( !(serial_type & 0x01) ){ 
  3779   3786       res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
  3780   3787     }else{
  3781   3788       int nCmp;
  3782   3789       int nStr;
................................................................................
  3790   3797       nCmp = MIN( pPKey2->aMem[0].n, nStr );
  3791   3798       res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
  3792   3799   
  3793   3800       if( res==0 ){
  3794   3801         res = nStr - pPKey2->aMem[0].n;
  3795   3802         if( res==0 ){
  3796   3803           if( pPKey2->nField>1 ){
  3797         -          res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
         3804  +          res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  3798   3805           }else{
  3799   3806             res = pPKey2->default_rc;
  3800   3807           }
  3801   3808         }else if( res>0 ){
  3802   3809           res = pPKey2->r2;
  3803   3810         }else{
  3804   3811           res = pPKey2->r1;
................................................................................
  3872   3879     i64 nCellKey = 0;
  3873   3880     int rc;
  3874   3881     u32 szHdr;        /* Size of the header */
  3875   3882     u32 typeRowid;    /* Serial type of the rowid */
  3876   3883     u32 lenRowid;     /* Size of the rowid */
  3877   3884     Mem m, v;
  3878   3885   
  3879         -  UNUSED_PARAMETER(db);
  3880         -
  3881   3886     /* Get the size of the index entry.  Only indices entries of less
  3882   3887     ** than 2GiB are support - anything large must be database corruption.
  3883   3888     ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
  3884   3889     ** this code can safely assume that nCellKey is 32-bits  
  3885   3890     */
  3886   3891     assert( sqlite3BtreeCursorIsValid(pCur) );
  3887   3892     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
  3888   3893     assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
  3889   3894     assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
  3890   3895   
  3891   3896     /* Read in the complete content of the index entry */
  3892         -  memset(&m, 0, sizeof(m));
         3897  +  sqlite3VdbeMemInit(&m, db, 0);
  3893   3898     rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
  3894   3899     if( rc ){
  3895   3900       return rc;
  3896   3901     }
  3897   3902   
  3898   3903     /* The index entry must begin with a header size */
  3899   3904     (void)getVarint32((u8*)m.z, szHdr);
................................................................................
  3945   3950   **
  3946   3951   ** pUnpacked is either created without a rowid or is truncated so that it
  3947   3952   ** omits the rowid at the end.  The rowid at the end of the index entry
  3948   3953   ** is ignored as well.  Hence, this routine only compares the prefixes 
  3949   3954   ** of the keys prior to the final rowid, not the entire key.
  3950   3955   */
  3951   3956   int sqlite3VdbeIdxKeyCompare(
         3957  +  sqlite3 *db,                     /* Database connection */
  3952   3958     VdbeCursor *pC,                  /* The cursor to compare against */
  3953   3959     UnpackedRecord *pUnpacked,       /* Unpacked version of key */
  3954   3960     int *res                         /* Write the comparison result here */
  3955   3961   ){
  3956   3962     i64 nCellKey = 0;
  3957   3963     int rc;
  3958   3964     BtCursor *pCur = pC->pCursor;
................................................................................
  3963   3969     assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
  3964   3970     /* nCellKey will always be between 0 and 0xffffffff because of the way
  3965   3971     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
  3966   3972     if( nCellKey<=0 || nCellKey>0x7fffffff ){
  3967   3973       *res = 0;
  3968   3974       return SQLITE_CORRUPT_BKPT;
  3969   3975     }
  3970         -  memset(&m, 0, sizeof(m));
         3976  +  sqlite3VdbeMemInit(&m, db, 0);
  3971   3977     rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
  3972   3978     if( rc ){
  3973   3979       return rc;
  3974   3980     }
  3975         -  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
         3981  +  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
  3976   3982     sqlite3VdbeMemRelease(&m);
  3977   3983     return SQLITE_OK;
  3978   3984   }
  3979   3985   
  3980   3986   /*
  3981   3987   ** This routine sets the value to be returned by subsequent calls to
  3982   3988   ** sqlite3_changes() on the database handle 'db'. 

Changes to src/vdbemem.c.

    22     22   /*
    23     23   ** Check invariants on a Mem object.
    24     24   **
    25     25   ** This routine is intended for use inside of assert() statements, like
    26     26   ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
    27     27   */
    28     28   int sqlite3VdbeCheckMemInvariants(Mem *p){
    29         -  /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
    30         -  ** function for Mem.z 
           29  +  /* If MEM_Dyn is set then Mem.xDel!=0.  
           30  +  ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
    31     31     */
    32     32     assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
    33         -  assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
    34     33   
    35     34     /* If p holds a string or blob, the Mem.z must point to exactly
    36     35     ** one of the following:
    37     36     **
    38     37     **   (1) Memory in Mem.zMalloc and managed by the Mem object
    39     38     **   (2) Memory to be freed using Mem.xDel
    40     39     **   (3) An ephemeral string or blob
................................................................................
    44     43       assert( 
    45     44         ((p->z==p->zMalloc)? 1 : 0) +
    46     45         ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
    47     46         ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
    48     47         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
    49     48       );
    50     49     }
    51         -
    52     50     return 1;
    53     51   }
    54     52   #endif
    55     53   
    56     54   
    57     55   /*
    58     56   ** If pMem is an object with a valid string representation, this routine
................................................................................
   117    115         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   118    116         bPreserve = 0;
   119    117       }else{
   120    118         sqlite3DbFree(pMem->db, pMem->zMalloc);
   121    119         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   122    120       }
   123    121       if( pMem->zMalloc==0 ){
   124         -      VdbeMemReleaseExtern(pMem);
          122  +      sqlite3VdbeMemSetNull(pMem);
   125    123         pMem->z = 0;
   126         -      pMem->flags = MEM_Null;  
   127    124         return SQLITE_NOMEM;
   128    125       }
   129    126     }
   130    127   
   131    128     if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
   132    129       memcpy(pMem->zMalloc, pMem->z, pMem->n);
   133    130     }
................................................................................
   134    131     if( (pMem->flags&MEM_Dyn)!=0 ){
   135    132       assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
   136    133       pMem->xDel((void *)(pMem->z));
   137    134     }
   138    135   
   139    136     pMem->z = pMem->zMalloc;
   140    137     pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
   141         -  pMem->xDel = 0;
   142    138     return SQLITE_OK;
   143    139   }
   144    140   
   145    141   /*
   146    142   ** Make the given Mem object MEM_Dyn.  In other words, make it so
   147    143   ** that any TEXT or BLOB content is stored in memory obtained from
   148    144   ** malloc().  In this way, we know that the memory is safe to be
................................................................................
   297    293       memset(&t, 0, sizeof(t));
   298    294       t.flags = MEM_Null;
   299    295       t.db = pMem->db;
   300    296       ctx.pOut = &t;
   301    297       ctx.pMem = pMem;
   302    298       ctx.pFunc = pFunc;
   303    299       pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   304         -    assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
          300  +    assert( (pMem->flags & MEM_Dyn)==0 );
   305    301       sqlite3DbFree(pMem->db, pMem->zMalloc);
   306    302       memcpy(pMem, &t, sizeof(t));
   307    303       rc = ctx.isError;
   308    304     }
   309    305     return rc;
   310    306   }
   311    307   
   312    308   /*
   313         -** If the memory cell contains a string value that must be freed by
   314         -** invoking an external callback, free it now. Calling this function
   315         -** does not free any Mem.zMalloc buffer.
          309  +** If the memory cell contains a value that must be freed by
          310  +** invoking the external callback in Mem.xDel, then this routine
          311  +** will free that value.  It also sets Mem.flags to MEM_Null.
   316    312   **
   317         -** The VdbeMemReleaseExtern() macro invokes this routine if only if there
   318         -** is work for this routine to do.
          313  +** This is a helper routine for sqlite3VdbeMemSetNull() and
          314  +** for sqlite3VdbeMemRelease().  Use those other routines as the
          315  +** entry point for releasing Mem resources.
   319    316   */
   320         -void sqlite3VdbeMemReleaseExternal(Mem *p){
          317  +static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
   321    318     assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
          319  +  assert( VdbeMemDynamic(p) );
   322    320     if( p->flags&MEM_Agg ){
   323    321       sqlite3VdbeMemFinalize(p, p->u.pDef);
   324    322       assert( (p->flags & MEM_Agg)==0 );
   325         -    sqlite3VdbeMemRelease(p);
   326         -  }else if( p->flags&MEM_Dyn ){
          323  +    testcase( p->flags & MEM_Dyn );
          324  +  }
          325  +  if( p->flags&MEM_Dyn ){
   327    326       assert( (p->flags&MEM_RowSet)==0 );
   328    327       assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
   329    328       p->xDel((void *)p->z);
   330         -    p->xDel = 0;
   331    329     }else if( p->flags&MEM_RowSet ){
   332    330       sqlite3RowSetClear(p->u.pRowSet);
   333    331     }else if( p->flags&MEM_Frame ){
   334         -    sqlite3VdbeMemSetNull(p);
          332  +    VdbeFrame *pFrame = p->u.pFrame;
          333  +    pFrame->pParent = pFrame->v->pDelFrame;
          334  +    pFrame->v->pDelFrame = pFrame;
   335    335     }
          336  +  p->flags = MEM_Null;
   336    337   }
   337    338   
   338    339   /*
   339    340   ** Release memory held by the Mem p, both external memory cleared
   340    341   ** by p->xDel and memory in p->zMalloc.
   341    342   **
   342    343   ** This is a helper routine invoked by sqlite3VdbeMemRelease() in
   343         -** the uncommon case when there really is memory in p that is
   344         -** need of freeing.
          344  +** the unusual case where there really is memory in p that needs
          345  +** to be freed.
   345    346   */
   346         -static SQLITE_NOINLINE void vdbeMemRelease(Mem *p){
          347  +static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
   347    348     if( VdbeMemDynamic(p) ){
   348         -    sqlite3VdbeMemReleaseExternal(p);
          349  +    vdbeMemClearExternAndSetNull(p);
   349    350     }
   350    351     if( p->zMalloc ){
   351    352       sqlite3DbFree(p->db, p->zMalloc);
   352    353       p->zMalloc = 0;
   353    354     }
   354    355     p->z = 0;
   355    356   }
   356    357   
   357    358   /*
   358         -** Release any memory held by the Mem. This may leave the Mem in an
   359         -** inconsistent state, for example with (Mem.z==0) and
   360         -** (Mem.flags==MEM_Str).
          359  +** Release any memory resources held by the Mem.  Both the memory that is
          360  +** free by Mem.xDel and the Mem.zMalloc allocation are freed.
          361  +**
          362  +** Use this routine prior to clean up prior to abandoning a Mem, or to
          363  +** reset a Mem back to its minimum memory utilization.
          364  +**
          365  +** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
          366  +** prior to inserting new content into the Mem.
   361    367   */
   362    368   void sqlite3VdbeMemRelease(Mem *p){
   363    369     assert( sqlite3VdbeCheckMemInvariants(p) );
   364    370     if( VdbeMemDynamic(p) || p->zMalloc ){
   365         -    vdbeMemRelease(p);
   366         -  }else{
   367         -    p->z = 0;
          371  +    vdbeMemClear(p);
   368    372     }
   369         -  assert( p->xDel==0 );
   370    373   }
   371    374   
   372    375   /*
   373    376   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   374    377   ** If the double is out of range of a 64-bit signed integer then
   375    378   ** return the closest available 64-bit signed integer.
   376    379   */
................................................................................
   574    577         assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
   575    578         pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
   576    579         break;
   577    580       }
   578    581     }
   579    582   }
   580    583   
          584  +/*
          585  +** Initialize bulk memory to be a consistent Mem object.
          586  +**
          587  +** The minimum amount of initialization feasible is performed.
          588  +*/
          589  +void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
          590  +  assert( (flags & ~MEM_TypeMask)==0 );
          591  +  pMem->flags = flags;
          592  +  pMem->db = db;
          593  +  pMem->zMalloc = 0;
          594  +}
          595  +
   581    596   
   582    597   /*
   583    598   ** Delete any previous value and set the value stored in *pMem to NULL.
          599  +**
          600  +** This routine calls the Mem.xDel destructor to dispose of values that
          601  +** require the destructor.  But it preserves the Mem.zMalloc memory allocation.
          602  +** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
          603  +** routine to invoke the destructor and deallocates Mem.zMalloc.
          604  +**
          605  +** Use this routine to reset the Mem prior to insert a new value.
          606  +**
          607  +** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
   584    608   */
   585    609   void sqlite3VdbeMemSetNull(Mem *pMem){
   586         -  if( pMem->flags & MEM_Frame ){
   587         -    VdbeFrame *pFrame = pMem->u.pFrame;
   588         -    pFrame->pParent = pFrame->v->pDelFrame;
   589         -    pFrame->v->pDelFrame = pFrame;
          610  +  if( VdbeMemDynamic(pMem) ){
          611  +    vdbeMemClearExternAndSetNull(pMem);
          612  +  }else{
          613  +    pMem->flags = MEM_Null;
   590    614     }
   591         -  if( pMem->flags & MEM_RowSet ){
   592         -    sqlite3RowSetClear(pMem->u.pRowSet);
   593         -  }
   594         -  MemSetTypeFlag(pMem, MEM_Null);
   595    615   }
   596    616   void sqlite3ValueSetNull(sqlite3_value *p){
   597    617     sqlite3VdbeMemSetNull((Mem*)p); 
   598    618   }
   599    619   
   600    620   /*
   601    621   ** Delete any previous value and set the value to be a BLOB of length
................................................................................
   604    624   void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
   605    625     sqlite3VdbeMemRelease(pMem);
   606    626     pMem->flags = MEM_Blob|MEM_Zero;
   607    627     pMem->n = 0;
   608    628     if( n<0 ) n = 0;
   609    629     pMem->u.nZero = n;
   610    630     pMem->enc = SQLITE_UTF8;
   611         -
   612         -#ifdef SQLITE_OMIT_INCRBLOB
   613         -  sqlite3VdbeMemGrow(pMem, n, 0);
   614         -  if( pMem->z ){
   615         -    pMem->n = n;
   616         -    memset(pMem->z, 0, n);
   617         -  }
   618         -#endif
          631  +  pMem->z = 0;
   619    632   }
   620    633   
   621    634   /*
   622    635   ** The pMem is known to contain content that needs to be destroyed prior
   623    636   ** to a value change.  So invoke the destructor, then set the value to
   624    637   ** a 64-bit integer.
   625    638   */
   626    639   static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
   627         -  sqlite3VdbeMemReleaseExternal(pMem);
          640  +  sqlite3VdbeMemSetNull(pMem);
   628    641     pMem->u.i = val;
   629    642     pMem->flags = MEM_Int;
   630    643   }
   631    644   
   632    645   /*
   633    646   ** Delete any previous value and set the value stored in *pMem to val,
   634    647   ** manifest type INTEGER.
................................................................................
   644    657   
   645    658   #ifndef SQLITE_OMIT_FLOATING_POINT
   646    659   /*
   647    660   ** Delete any previous value and set the value stored in *pMem to val,
   648    661   ** manifest type REAL.
   649    662   */
   650    663   void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
   651         -  if( sqlite3IsNaN(val) ){
   652    664       sqlite3VdbeMemSetNull(pMem);
   653         -  }else{
   654         -    sqlite3VdbeMemRelease(pMem);
          665  +  if( !sqlite3IsNaN(val) ){
   655    666       pMem->r = val;
   656    667       pMem->flags = MEM_Real;
   657    668     }
   658    669   }
   659    670   #endif
   660    671   
   661    672   /*
................................................................................
   726    737   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   727    738   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   728    739   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   729    740   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   730    741   */
   731    742   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   732    743     assert( (pFrom->flags & MEM_RowSet)==0 );
   733         -  VdbeMemReleaseExtern(pTo);
          744  +  assert( pTo->db==pFrom->db );
          745  +  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
   734    746     memcpy(pTo, pFrom, MEMCELLSIZE);
   735         -  pTo->xDel = 0;
   736    747     if( (pFrom->flags&MEM_Static)==0 ){
   737    748       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   738    749       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   739    750       pTo->flags |= srcType;
   740    751     }
   741    752   }
   742    753   
................................................................................
   743    754   /*
   744    755   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   745    756   ** freed before the copy is made.
   746    757   */
   747    758   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   748    759     int rc = SQLITE_OK;
   749    760   
          761  +  assert( pTo->db==pFrom->db );
   750    762     assert( (pFrom->flags & MEM_RowSet)==0 );
   751         -  VdbeMemReleaseExtern(pTo);
          763  +  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
   752    764     memcpy(pTo, pFrom, MEMCELLSIZE);
   753    765     pTo->flags &= ~MEM_Dyn;
   754         -  pTo->xDel = 0;
   755         -
   756    766     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   757    767       if( 0==(pFrom->flags&MEM_Static) ){
   758    768         pTo->flags |= MEM_Ephem;
   759    769         rc = sqlite3VdbeMemMakeWriteable(pTo);
   760    770       }
   761    771     }
   762    772   
................................................................................
   773    783     assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
   774    784     assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
   775    785     assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
   776    786   
   777    787     sqlite3VdbeMemRelease(pTo);
   778    788     memcpy(pTo, pFrom, sizeof(Mem));
   779    789     pFrom->flags = MEM_Null;
   780         -  pFrom->xDel = 0;
   781    790     pFrom->zMalloc = 0;
   782    791   }
   783    792   
   784    793   /*
   785    794   ** Change the value of a Mem to be a string or a BLOB.
   786    795   **
   787    796   ** The memory management strategy depends on the value of the xDel
................................................................................
   821    830     }else{
   822    831       iLimit = SQLITE_MAX_LENGTH;
   823    832     }
   824    833     flags = (enc==0?MEM_Blob:MEM_Str);
   825    834     if( nByte<0 ){
   826    835       assert( enc!=0 );
   827    836       if( enc==SQLITE_UTF8 ){
   828         -      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
          837  +      nByte = sqlite3Strlen30(z);
          838  +      if( nByte>iLimit ) nByte = iLimit+1;
   829    839       }else{
   830    840         for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
   831    841       }
   832    842       flags |= MEM_Term;
   833    843     }
   834    844   
   835    845     /* The following block sets the new values of Mem.z and Mem.xDel. It
................................................................................
   847    857       if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
   848    858         return SQLITE_NOMEM;
   849    859       }
   850    860       memcpy(pMem->z, z, nAlloc);
   851    861     }else if( xDel==SQLITE_DYNAMIC ){
   852    862       sqlite3VdbeMemRelease(pMem);
   853    863       pMem->zMalloc = pMem->z = (char *)z;
   854         -    pMem->xDel = 0;
   855    864     }else{
   856    865       sqlite3VdbeMemRelease(pMem);
   857    866       pMem->z = (char *)z;
   858    867       pMem->xDel = xDel;
   859    868       flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
   860    869     }
   861    870   
................................................................................
   879    888   /*
   880    889   ** Move data out of a btree key or data field and into a Mem structure.
   881    890   ** The data or key is taken from the entry that pCur is currently pointing
   882    891   ** to.  offset and amt determine what portion of the data or key to retrieve.
   883    892   ** key is true to get the key or false to get data.  The result is written
   884    893   ** into the pMem element.
   885    894   **
   886         -** The pMem structure is assumed to be uninitialized.  Any prior content
   887         -** is overwritten without being freed.
          895  +** The pMem object must have been initialized.  This routine will use
          896  +** pMem->zMalloc to hold the content from the btree, if possible.  New
          897  +** pMem->zMalloc space will be allocated if necessary.  The calling routine
          898  +** is responsible for making sure that the pMem object is eventually
          899  +** destroyed.
   888    900   **
   889    901   ** If this routine fails for any reason (malloc returns NULL or unable
   890    902   ** to read from the disk) then the pMem is left in an inconsistent state.
   891    903   */
   892    904   int sqlite3VdbeMemFromBtree(
   893    905     BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
   894    906     u32 offset,       /* Offset from the start of data to return bytes from. */
................................................................................
   897    909     Mem *pMem         /* OUT: Return data in this Mem structure. */
   898    910   ){
   899    911     char *zData;        /* Data from the btree layer */
   900    912     u32 available = 0;  /* Number of bytes available on the local btree page */
   901    913     int rc = SQLITE_OK; /* Return code */
   902    914   
   903    915     assert( sqlite3BtreeCursorIsValid(pCur) );
          916  +  assert( !VdbeMemDynamic(pMem) );
   904    917   
   905    918     /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
   906    919     ** that both the BtShared and database handle mutexes are held. */
   907    920     assert( (pMem->flags & MEM_RowSet)==0 );
   908    921     if( key ){
   909    922       zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
   910    923     }else{
   911    924       zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
   912    925     }
   913    926     assert( zData!=0 );
   914    927   
   915    928     if( offset+amt<=available ){
   916         -    sqlite3VdbeMemRelease(pMem);
   917    929       pMem->z = &zData[offset];
   918    930       pMem->flags = MEM_Blob|MEM_Ephem;
   919    931       pMem->n = (int)amt;
   920         -  }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
          932  +  }else{
          933  +    pMem->flags = MEM_Null;
          934  +    if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
   921    935       if( key ){
   922    936         rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
   923    937       }else{
   924    938         rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
   925    939       }
   926    940       if( rc==SQLITE_OK ){
   927    941         pMem->z[amt] = 0;
................................................................................
   928    942         pMem->z[amt+1] = 0;
   929    943         pMem->flags = MEM_Blob|MEM_Term;
   930    944         pMem->n = (int)amt;
   931    945       }else{
   932    946         sqlite3VdbeMemRelease(pMem);
   933    947       }
   934    948     }
          949  +  }
   935    950   
   936    951     return rc;
   937    952   }
   938    953   
   939    954   /*
   940    955   ** The pVal argument is known to be a value other than NULL.
   941    956   ** Convert it into a string with encoding enc and return a pointer

Changes to src/vdbesort.c.

   757    757     const void *pKey1, int nKey1,   /* Left side of comparison */
   758    758     const void *pKey2, int nKey2    /* Right side of comparison */
   759    759   ){
   760    760     UnpackedRecord *r2 = pTask->pUnpacked;
   761    761     if( pKey2 ){
   762    762       sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
   763    763     }
   764         -  return sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
          764  +  return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
   765    765   }
   766    766   
   767    767   /*
   768    768   ** Initialize the temporary index cursor just opened as a sorter cursor.
   769    769   **
   770    770   ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
   771    771   ** to determine the number of fields that should be compared from the
................................................................................
  2513   2513     for(i=0; i<nKeyCol; i++){
  2514   2514       if( r2->aMem[i].flags & MEM_Null ){
  2515   2515         *pRes = -1;
  2516   2516         return SQLITE_OK;
  2517   2517       }
  2518   2518     }
  2519   2519   
  2520         -  *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2, 0);
         2520  +  *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
  2521   2521     return SQLITE_OK;
  2522   2522   }

Changes to src/where.c.

  1909   1909   #endif
  1910   1910     assert( pRec!=0 );
  1911   1911     iCol = pRec->nField - 1;
  1912   1912     assert( pIdx->nSample>0 );
  1913   1913     assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
  1914   1914     do{
  1915   1915       iTest = (iMin+i)/2;
  1916         -    res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
         1916  +    res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
  1917   1917       if( res<0 ){
  1918   1918         iMin = iTest+1;
  1919   1919       }else{
  1920   1920         i = iTest;
  1921   1921       }
  1922   1922     }while( res && iMin<i );
  1923   1923   
................................................................................
  1924   1924   #ifdef SQLITE_DEBUG
  1925   1925     /* The following assert statements check that the binary search code
  1926   1926     ** above found the right answer. This block serves no purpose other
  1927   1927     ** than to invoke the asserts.  */
  1928   1928     if( res==0 ){
  1929   1929       /* If (res==0) is true, then sample $i must be equal to pRec */
  1930   1930       assert( i<pIdx->nSample );
  1931         -    assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
         1931  +    assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
  1932   1932            || pParse->db->mallocFailed );
  1933   1933     }else{
  1934   1934       /* Otherwise, pRec must be smaller than sample $i and larger than
  1935   1935       ** sample ($i-1).  */
  1936   1936       assert( i==pIdx->nSample 
  1937         -         || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
         1937  +         || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
  1938   1938            || pParse->db->mallocFailed );
  1939   1939       assert( i==0
  1940         -         || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
         1940  +         || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
  1941   1941            || pParse->db->mallocFailed );
  1942   1942     }
  1943   1943   #endif /* ifdef SQLITE_DEBUG */
  1944   1944   
  1945   1945     /* At this point, aSample[i] is the first sample that is greater than
  1946   1946     ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
  1947   1947     ** than pVal.  If aSample[i]==pVal, then res==0.