/ Check-in [0828975d]
Login

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

Overview
Comment:Merge in various obscure bug fixes and the removal of Mem.memType from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 0828975d580d309a1c66da4b6caeb65b21a01199
User & Date: drh 2014-03-05 14:49:51
Context
2014-03-05
23:12
Merge compiler-warning fixes from trunk. check-in: a1f2b042 user: drh tags: sessions
14:49
Merge in various obscure bug fixes and the removal of Mem.memType from trunk. check-in: 0828975d user: drh tags: sessions
14:40
When converting a result type from TEXT to BLOB using the sqlite3_value_blob() interface, continue to report SQLITE_TEXT as the true type from sqlite3_value_text() as long as that text is still valid. The maintains legacy behavior from before the noMemType change. check-in: 1d134ba2 user: drh tags: trunk
2014-03-04
14:34
Merge the performance enhancements of trunk (and some obscure bug fixes) into the sessions branch. check-in: 7f51ad97 user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  4607   4607       assert( pPage->nCell>0 );
  4608   4608       assert( pPage->intKey==(pIdxKey==0) );
  4609   4609       lwr = 0;
  4610   4610       upr = pPage->nCell-1;
  4611   4611       assert( biasRight==0 || biasRight==1 );
  4612   4612       idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
  4613   4613       pCur->aiIdx[pCur->iPage] = (u16)idx;
  4614         -    if( pPage->intKey ){
         4614  +    if( xRecordCompare==0 ){
  4615   4615         for(;;){
  4616   4616           i64 nCellKey;
  4617   4617           pCell = findCell(pPage, idx) + pPage->childPtrSize;
  4618   4618           if( pPage->hasData ){
  4619   4619             while( 0x80 <= *(pCell++) ){
  4620   4620               if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
  4621   4621             }

Changes to src/select.c.

  1518   1518       */
  1519   1519       nName = sqlite3Strlen30(zName);
  1520   1520       for(j=cnt=0; j<i; j++){
  1521   1521         if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
  1522   1522           char *zNewName;
  1523   1523           int k;
  1524   1524           for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
  1525         -        if( zName[k]==':' ) nName = k;
         1525  +        if( k>=0 && zName[k]==':' ) nName = k;
  1526   1526           zName[nName] = 0;
  1527   1527           zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
  1528   1528           sqlite3DbFree(db, zName);
  1529   1529           zName = zNewName;
  1530   1530           j = -1;
  1531   1531           if( zName==0 ) break;
  1532   1532         }

Changes to src/test5.c.

    72     72     }
    73     73   
    74     74     if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR;
    75     75     if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR;
    76     76   
    77     77     val.flags = MEM_Str|MEM_Term|MEM_Static;
    78     78     val.z = "hello world";
    79         -  val.memType = MEM_Str;
    80     79     val.enc = SQLITE_UTF8;
    81     80   
    82     81     for(i=0; i<repeat_count; i++){
    83     82       if( do_calls ){
    84     83         sqlite3_value_text(&val);
    85     84       }
    86     85     }

Changes to src/test_func.c.

   495    495       Mem mem;
   496    496   
   497    497       memset(&mem, 0, sizeof(mem));
   498    498       mem.db = db;
   499    499       mem.enc = ENC(db);
   500    500       pHdr += sqlite3GetVarint(pHdr, &iSerialType);
   501    501       pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
   502         -    sqlite3VdbeMemStoreType(&mem);
   503    502   
   504    503       if( iCurrent==iIdx ){
   505    504         sqlite3_result_value(context, &mem);
   506    505       }
   507    506   
   508    507       sqlite3DbFree(db, mem.zMalloc);
   509    508     }
................................................................................
   545    544   
   546    545       memset(&mem, 0, sizeof(mem));
   547    546       mem.db = db;
   548    547       mem.enc = ENC(db);
   549    548       pHdr += sqlite3GetVarint(pHdr, &iSerialType);
   550    549       pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
   551    550   
   552         -    sqlite3VdbeMemStoreType(&mem);
   553    551       switch( sqlite3_value_type(&mem) ){
   554    552         case SQLITE_TEXT:
   555    553           pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1);
   556    554           break;
   557    555   
   558    556         case SQLITE_BLOB: {
   559    557           char hexdigit[] = {

Changes to src/vdbe.c.

   299    299   ** loss of information and return the revised type of the argument.
   300    300   */
   301    301   int sqlite3_value_numeric_type(sqlite3_value *pVal){
   302    302     int eType = sqlite3_value_type(pVal);
   303    303     if( eType==SQLITE_TEXT ){
   304    304       Mem *pMem = (Mem*)pVal;
   305    305       applyNumericAffinity(pMem);
   306         -    sqlite3VdbeMemStoreType(pMem);
   307    306       eType = sqlite3_value_type(pVal);
   308    307     }
   309    308     return eType;
   310    309   }
   311    310   
   312    311   /*
   313    312   ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
................................................................................
  1249   1248     pMem = p->pResultSet = &aMem[pOp->p1];
  1250   1249     for(i=0; i<pOp->p2; i++){
  1251   1250       assert( memIsValid(&pMem[i]) );
  1252   1251       Deephemeralize(&pMem[i]);
  1253   1252       assert( (pMem[i].flags & MEM_Ephem)==0
  1254   1253               || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
  1255   1254       sqlite3VdbeMemNulTerminate(&pMem[i]);
  1256         -    sqlite3VdbeMemStoreType(&pMem[i]);
  1257   1255       REGISTER_TRACE(pOp->p1+i, &pMem[i]);
  1258   1256     }
  1259   1257     if( db->mallocFailed ) goto no_mem;
  1260   1258   
  1261   1259     /* Return SQLITE_ROW
  1262   1260     */
  1263   1261     p->pc = pc + 1;
................................................................................
  1495   1493     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  1496   1494     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1497   1495     pArg = &aMem[pOp->p2];
  1498   1496     for(i=0; i<n; i++, pArg++){
  1499   1497       assert( memIsValid(pArg) );
  1500   1498       apVal[i] = pArg;
  1501   1499       Deephemeralize(pArg);
  1502         -    sqlite3VdbeMemStoreType(pArg);
  1503   1500       REGISTER_TRACE(pOp->p2+i, pArg);
  1504   1501     }
  1505   1502   
  1506   1503     assert( pOp->p4type==P4_FUNCDEF );
  1507   1504     ctx.pFunc = pOp->p4.pFunc;
  1508   1505     ctx.iOp = pc;
  1509   1506     ctx.pVdbe = p;
................................................................................
  5551   5548     pRec = &aMem[pOp->p2];
  5552   5549     apVal = p->apArg;
  5553   5550     assert( apVal || n==0 );
  5554   5551     for(i=0; i<n; i++, pRec++){
  5555   5552       assert( memIsValid(pRec) );
  5556   5553       apVal[i] = pRec;
  5557   5554       memAboutToChange(p, pRec);
  5558         -    sqlite3VdbeMemStoreType(pRec);
  5559   5555     }
  5560   5556     ctx.pFunc = pOp->p4.pFunc;
  5561   5557     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5562   5558     ctx.pMem = pMem = &aMem[pOp->p3];
  5563   5559     pMem->n++;
  5564   5560     ctx.s.flags = MEM_Null;
  5565   5561     ctx.s.z = 0;
................................................................................
  5985   5981   
  5986   5982     /* Invoke the xFilter method */
  5987   5983     {
  5988   5984       res = 0;
  5989   5985       apArg = p->apArg;
  5990   5986       for(i = 0; i<nArg; i++){
  5991   5987         apArg[i] = &pArgc[i+1];
  5992         -      sqlite3VdbeMemStoreType(apArg[i]);
  5993   5988       }
  5994   5989   
  5995   5990       p->inVtabMethod = 1;
  5996   5991       rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  5997   5992       p->inVtabMethod = 0;
  5998   5993       sqlite3VtabImportErrmsg(p, pVtab);
  5999   5994       if( rc==SQLITE_OK ){
................................................................................
  6192   6187     if( ALWAYS(pModule->xUpdate) ){
  6193   6188       u8 vtabOnConflict = db->vtabOnConflict;
  6194   6189       apArg = p->apArg;
  6195   6190       pX = &aMem[pOp->p3];
  6196   6191       for(i=0; i<nArg; i++){
  6197   6192         assert( memIsValid(pX) );
  6198   6193         memAboutToChange(p, pX);
  6199         -      sqlite3VdbeMemStoreType(pX);
  6200   6194         apArg[i] = pX;
  6201   6195         pX++;
  6202   6196       }
  6203   6197       db->vtabOnConflict = pOp->p5;
  6204   6198       rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
  6205   6199       db->vtabOnConflict = vtabOnConflict;
  6206   6200       sqlite3VtabImportErrmsg(p, pVtab);

Changes to src/vdbeInt.h.

   164    164       int nZero;          /* Used when bit MEM_Zero is set in flags */
   165    165       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   166    166       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   167    167       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   168    168     } u;
   169    169     int n;              /* Number of characters in string value, excluding '\0' */
   170    170     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   171         -  u8  memType;        /* Lower 5 bits of flags */
   172    171     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   173    172   #ifdef SQLITE_DEBUG
   174    173     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   175    174     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   176    175   #endif
   177    176     void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   178    177     char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
................................................................................
   191    190   ** flags may coexist with the MEM_Str flag.
   192    191   */
   193    192   #define MEM_Null      0x0001   /* Value is NULL */
   194    193   #define MEM_Str       0x0002   /* Value is a string */
   195    194   #define MEM_Int       0x0004   /* Value is an integer */
   196    195   #define MEM_Real      0x0008   /* Value is a real number */
   197    196   #define MEM_Blob      0x0010   /* Value is a BLOB */
          197  +#define MEM_AffMask   0x001f   /* Mask of affinity bits */
   198    198   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   199    199   #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   200    200   #define MEM_Undefined 0x0080   /* Value is undefined */
   201    201   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
   202    202   #define MEM_TypeMask  0x01ff   /* Mask of type bits */
   203    203   
   204    204   
................................................................................
   449    449     if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   450    450   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   451    451   const char *sqlite3OpcodeName(int);
   452    452   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   453    453   int sqlite3VdbeCloseStatement(Vdbe *, int);
   454    454   void sqlite3VdbeFrameDelete(VdbeFrame*);
   455    455   int sqlite3VdbeFrameRestore(VdbeFrame *);
   456         -#define sqlite3VdbeMemStoreType(X)  (X)->memType = (u8)((X)->flags&0x1f)
   457         -/* void sqlite3VdbeMemStoreType(Mem *pMem); */
   458    456   void sqlite3VdbePreUpdateHook(
   459    457       Vdbe *, VdbeCursor *, int, const char*, Table *, i64, int);
   460    458   int sqlite3VdbeTransferError(Vdbe *p);
   461    459   
   462    460   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   463    461   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   464    462   int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);

Changes to src/vdbeapi.c.

   131    131   ** The following routines extract information from a Mem or sqlite3_value
   132    132   ** structure.
   133    133   */
   134    134   const void *sqlite3_value_blob(sqlite3_value *pVal){
   135    135     Mem *p = (Mem*)pVal;
   136    136     if( p->flags & (MEM_Blob|MEM_Str) ){
   137    137       sqlite3VdbeMemExpandBlob(p);
   138         -    p->flags &= ~MEM_Str;
   139    138       p->flags |= MEM_Blob;
   140    139       return p->n ? p->z : 0;
   141    140     }else{
   142    141       return sqlite3_value_text(pVal);
   143    142     }
   144    143   }
   145    144   int sqlite3_value_bytes(sqlite3_value *pVal){
................................................................................
   202    201        SQLITE_FLOAT,    /* 0x1a */
   203    202        SQLITE_NULL,     /* 0x1b */
   204    203        SQLITE_INTEGER,  /* 0x1c */
   205    204        SQLITE_NULL,     /* 0x1d */
   206    205        SQLITE_INTEGER,  /* 0x1e */
   207    206        SQLITE_NULL,     /* 0x1f */
   208    207     };
   209         -  return aType[pVal->memType&0x1f];
          208  +  return aType[pVal->flags&MEM_AffMask];
   210    209   }
   211    210   
   212    211   /**************************** sqlite3_result_  *******************************
   213    212   ** The following routines are used by user-defined functions to specify
   214    213   ** the function result.
   215    214   **
   216    215   ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
................................................................................
   728    727   }
   729    728   
   730    729   /*
   731    730   ** Return a pointer to static memory containing an SQL NULL value.
   732    731   */
   733    732   static const Mem *columnNullValue(void){
   734    733     /* Even though the Mem structure contains an element
   735         -  ** of type i64, on certain architecture (x86) with certain compiler
          734  +  ** of type i64, on certain architectures (x86) with certain compiler
   736    735     ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
   737    736     ** instead of an 8-byte one. This all works fine, except that when
   738    737     ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
   739    738     ** that a Mem structure is located on an 8-byte boundary. To prevent
   740         -  ** this assert() from failing, when building with SQLITE_DEBUG defined
   741         -  ** using gcc, force nullMem to be 8-byte aligned using the magical
          739  +  ** these assert()s from failing, when building with SQLITE_DEBUG defined
          740  +  ** using gcc, we force nullMem to be 8-byte aligned using the magical
   742    741     ** __attribute__((aligned(8))) macro.  */
   743    742     static const Mem nullMem 
   744    743   #if defined(SQLITE_DEBUG) && defined(__GNUC__)
   745         -  __attribute__((aligned(8))) 
          744  +    __attribute__((aligned(8))) 
          745  +#endif
          746  +    = {0, "", (double)0, {0}, 0, MEM_Null, 0,
          747  +#ifdef SQLITE_DEBUG
          748  +       0, 0,  /* pScopyFrom, pFiller */
   746    749   #endif
   747         -    = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
          750  +       0, 0 };
   748    751     return &nullMem;
   749    752   }
   750    753   
   751    754   /*
   752    755   ** Check to see if column iCol of the given statement is valid.  If
   753    756   ** it is, return a pointer to the Mem for the value of that column.
   754    757   ** If iCol is not valid, return a pointer to a Mem which has a value
................................................................................
  1416   1419     if( iIdx>=p->pUnpacked->nField ){
  1417   1420       *ppValue = (sqlite3_value *)columnNullValue();
  1418   1421     }else{
  1419   1422       *ppValue = &p->pUnpacked->aMem[iIdx];
  1420   1423       if( iIdx==p->iPKey ){
  1421   1424         sqlite3VdbeMemSetInt64(*ppValue, p->iKey1);
  1422   1425       }
  1423         -    sqlite3VdbeMemStoreType(*ppValue);
  1424   1426     }
  1425   1427   
  1426   1428    preupdate_old_out:
  1427   1429     sqlite3Error(db, rc, 0);
  1428   1430     return sqlite3ApiExit(db, rc);
  1429   1431   }
  1430   1432   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
................................................................................
  1495   1497       if( iIdx>=pUnpack->nField ){
  1496   1498         pMem = (sqlite3_value *)columnNullValue();
  1497   1499       }else{
  1498   1500         pMem = &pUnpack->aMem[iIdx];
  1499   1501         if( iIdx==p->iPKey ){
  1500   1502           sqlite3VdbeMemSetInt64(pMem, p->iKey2);
  1501   1503         }
  1502         -      sqlite3VdbeMemStoreType(pMem);
  1503   1504       }
  1504   1505     }else{
  1505   1506       /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
  1506   1507       ** value. Make a copy of the cell contents and return a pointer to it.
  1507   1508       ** It is not safe to return a pointer to the memory cell itself as the
  1508   1509       ** caller may modify the value text encoding.
  1509   1510       */
................................................................................
  1520   1521       if( pMem->flags==0 ){
  1521   1522         if( iIdx==p->iPKey ){
  1522   1523           sqlite3VdbeMemSetInt64(pMem, p->iKey2);
  1523   1524         }else{
  1524   1525           rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
  1525   1526           if( rc!=SQLITE_OK ) goto preupdate_new_out;
  1526   1527         }
  1527         -      sqlite3VdbeMemStoreType(pMem);
  1528   1528       }
  1529   1529     }
  1530   1530     *ppValue = pMem;
  1531   1531   
  1532   1532    preupdate_new_out:
  1533   1533     sqlite3Error(db, rc, 0);
  1534   1534     return sqlite3ApiExit(db, rc);
  1535   1535   }
  1536   1536   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */

Changes to src/vdbeaux.c.

  1360   1360         for(j=0; i>=apSub[j]->nOp; j++){
  1361   1361           i -= apSub[j]->nOp;
  1362   1362         }
  1363   1363         pOp = &apSub[j]->aOp[i];
  1364   1364       }
  1365   1365       if( p->explain==1 ){
  1366   1366         pMem->flags = MEM_Int;
  1367         -      pMem->memType = MEM_Int;
  1368   1367         pMem->u.i = i;                                /* Program counter */
  1369   1368         pMem++;
  1370   1369     
  1371   1370         pMem->flags = MEM_Static|MEM_Str|MEM_Term;
  1372   1371         pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
  1373   1372         assert( pMem->z!=0 );
  1374   1373         pMem->n = sqlite3Strlen30(pMem->z);
  1375         -      pMem->memType = MEM_Str;
  1376   1374         pMem->enc = SQLITE_UTF8;
  1377   1375         pMem++;
  1378   1376   
  1379   1377         /* When an OP_Program opcode is encounter (the only opcode that has
  1380   1378         ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
  1381   1379         ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
  1382   1380         ** has not already been seen.
................................................................................
  1394   1392             pSub->n = nSub*sizeof(SubProgram*);
  1395   1393           }
  1396   1394         }
  1397   1395       }
  1398   1396   
  1399   1397       pMem->flags = MEM_Int;
  1400   1398       pMem->u.i = pOp->p1;                          /* P1 */
  1401         -    pMem->memType = MEM_Int;
  1402   1399       pMem++;
  1403   1400   
  1404   1401       pMem->flags = MEM_Int;
  1405   1402       pMem->u.i = pOp->p2;                          /* P2 */
  1406         -    pMem->memType = MEM_Int;
  1407   1403       pMem++;
  1408   1404   
  1409   1405       pMem->flags = MEM_Int;
  1410   1406       pMem->u.i = pOp->p3;                          /* P3 */
  1411         -    pMem->memType = MEM_Int;
  1412   1407       pMem++;
  1413   1408   
  1414   1409       if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
  1415   1410         assert( p->db->mallocFailed );
  1416   1411         return SQLITE_ERROR;
  1417   1412       }
  1418   1413       pMem->flags = MEM_Str|MEM_Term;
................................................................................
  1420   1415       if( zP4!=pMem->z ){
  1421   1416         sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
  1422   1417       }else{
  1423   1418         assert( pMem->z!=0 );
  1424   1419         pMem->n = sqlite3Strlen30(pMem->z);
  1425   1420         pMem->enc = SQLITE_UTF8;
  1426   1421       }
  1427         -    pMem->memType = MEM_Str;
  1428   1422       pMem++;
  1429   1423   
  1430   1424       if( p->explain==1 ){
  1431   1425         if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
  1432   1426           assert( p->db->mallocFailed );
  1433   1427           return SQLITE_ERROR;
  1434   1428         }
  1435   1429         pMem->flags = MEM_Str|MEM_Term;
  1436   1430         pMem->n = 2;
  1437   1431         sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
  1438         -      pMem->memType = MEM_Str;
  1439   1432         pMem->enc = SQLITE_UTF8;
  1440   1433         pMem++;
  1441   1434     
  1442   1435   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  1443   1436         if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
  1444   1437           assert( p->db->mallocFailed );
  1445   1438           return SQLITE_ERROR;
  1446   1439         }
  1447   1440         pMem->flags = MEM_Str|MEM_Term;
  1448   1441         pMem->n = displayComment(pOp, zP4, pMem->z, 500);
  1449         -      pMem->memType = MEM_Str;
  1450   1442         pMem->enc = SQLITE_UTF8;
  1451   1443   #else
  1452   1444         pMem->flags = MEM_Null;                       /* Comment */
  1453         -      pMem->memType = MEM_Null;
  1454   1445   #endif
  1455   1446       }
  1456   1447   
  1457   1448       p->nResColumn = 8 - 4*(p->explain-1);
  1458   1449       p->pResultSet = &p->aMem[1];
  1459   1450       p->rc = SQLITE_OK;
  1460   1451       rc = SQLITE_ROW;
................................................................................
  3537   3528         rc = (serial_type!=0);
  3538   3529       }
  3539   3530   
  3540   3531       if( rc!=0 ){
  3541   3532         if( pKeyInfo->aSortOrder[i] ){
  3542   3533           rc = -rc;
  3543   3534         }
  3544         -      assert( CORRUPT_DB 
         3535  +      assert( CORRUPT_DB
  3545   3536             || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
  3546   3537             || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
         3538  +          || pKeyInfo->db->mallocFailed
  3547   3539         );
  3548   3540         assert( mem1.zMalloc==0 );  /* See comment below */
  3549   3541         return rc;
  3550   3542       }
  3551   3543   
  3552   3544       i++;
  3553   3545       pRhs++;
................................................................................
  3934   3926     if( v ){
  3935   3927       Mem *pMem = &v->aVar[iVar-1];
  3936   3928       if( 0==(pMem->flags & MEM_Null) ){
  3937   3929         sqlite3_value *pRet = sqlite3ValueNew(v->db);
  3938   3930         if( pRet ){
  3939   3931           sqlite3VdbeMemCopy((Mem *)pRet, pMem);
  3940   3932           sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
  3941         -        sqlite3VdbeMemStoreType((Mem *)pRet);
  3942   3933         }
  3943   3934         return pRet;
  3944   3935       }
  3945   3936     }
  3946   3937     return 0;
  3947   3938   }
  3948   3939   

Changes to src/vdbemem.c.

   118    118         bPreserve = 0;
   119    119       }else{
   120    120         sqlite3DbFree(pMem->db, pMem->zMalloc);
   121    121         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   122    122       }
   123    123       if( pMem->zMalloc==0 ){
   124    124         VdbeMemRelease(pMem);
          125  +      pMem->z = 0;
   125    126         pMem->flags = MEM_Null;  
   126    127         return SQLITE_NOMEM;
   127    128       }
   128    129     }
   129    130   
   130    131     if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
   131    132       memcpy(pMem->zMalloc, pMem->z, pMem->n);
................................................................................
   316    317       sqlite3VdbeMemSetNull(p);
   317    318     }
   318    319   }
   319    320   
   320    321   /*
   321    322   ** Release any memory held by the Mem. This may leave the Mem in an
   322    323   ** inconsistent state, for example with (Mem.z==0) and
   323         -** (Mem.memType==MEM_Str).
          324  +** (Mem.flags==MEM_Str).
   324    325   */
   325    326   void sqlite3VdbeMemRelease(Mem *p){
   326    327     assert( sqlite3VdbeCheckMemInvariants(p) );
   327    328     VdbeMemRelease(p);
   328    329     if( p->zMalloc ){
   329    330       sqlite3DbFree(p->db, p->zMalloc);
   330    331       p->zMalloc = 0;
................................................................................
   508    509       pFrame->pParent = pFrame->v->pDelFrame;
   509    510       pFrame->v->pDelFrame = pFrame;
   510    511     }
   511    512     if( pMem->flags & MEM_RowSet ){
   512    513       sqlite3RowSetClear(pMem->u.pRowSet);
   513    514     }
   514    515     MemSetTypeFlag(pMem, MEM_Null);
   515         -  pMem->memType = MEM_Null;
   516    516   }
   517    517   void sqlite3ValueSetNull(sqlite3_value *p){
   518    518     sqlite3VdbeMemSetNull((Mem*)p); 
   519    519   }
   520    520   
   521    521   /*
   522    522   ** Delete any previous value and set the value to be a BLOB of length
   523    523   ** n containing all zeros.
   524    524   */
   525    525   void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
   526    526     sqlite3VdbeMemRelease(pMem);
   527    527     pMem->flags = MEM_Blob|MEM_Zero;
   528         -  pMem->memType = MEM_Blob;
   529    528     pMem->n = 0;
   530    529     if( n<0 ) n = 0;
   531    530     pMem->u.nZero = n;
   532    531     pMem->enc = SQLITE_UTF8;
   533    532   
   534    533   #ifdef SQLITE_OMIT_INCRBLOB
   535    534     sqlite3VdbeMemGrow(pMem, n, 0);
................................................................................
   544    543   ** Delete any previous value and set the value stored in *pMem to val,
   545    544   ** manifest type INTEGER.
   546    545   */
   547    546   void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
   548    547     sqlite3VdbeMemRelease(pMem);
   549    548     pMem->u.i = val;
   550    549     pMem->flags = MEM_Int;
   551         -  pMem->memType = MEM_Int;
   552    550   }
   553    551   
   554    552   #ifndef SQLITE_OMIT_FLOATING_POINT
   555    553   /*
   556    554   ** Delete any previous value and set the value stored in *pMem to val,
   557    555   ** manifest type REAL.
   558    556   */
................................................................................
   559    557   void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
   560    558     if( sqlite3IsNaN(val) ){
   561    559       sqlite3VdbeMemSetNull(pMem);
   562    560     }else{
   563    561       sqlite3VdbeMemRelease(pMem);
   564    562       pMem->r = val;
   565    563       pMem->flags = MEM_Real;
   566         -    pMem->memType = MEM_Real;
   567    564     }
   568    565   }
   569    566   #endif
   570    567   
   571    568   /*
   572    569   ** Delete any previous value and set the value of pMem to be an
   573    570   ** empty boolean index.
................................................................................
   768    765       pMem->xDel = xDel;
   769    766       flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
   770    767     }
   771    768   
   772    769     pMem->n = nByte;
   773    770     pMem->flags = flags;
   774    771     pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
   775         -  pMem->memType = flags&0x1f;
   776    772   
   777    773   #ifndef SQLITE_OMIT_UTF16
   778    774     if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
   779    775       return SQLITE_NOMEM;
   780    776     }
   781    777   #endif
   782    778   
................................................................................
   834    830       }else{
   835    831         rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
   836    832       }
   837    833       if( rc==SQLITE_OK ){
   838    834         pMem->z[amt] = 0;
   839    835         pMem->z[amt+1] = 0;
   840    836         pMem->flags = MEM_Blob|MEM_Term;
   841         -      pMem->memType = MEM_Blob;
   842    837         pMem->n = (int)amt;
   843    838       }else{
   844    839         sqlite3VdbeMemRelease(pMem);
   845    840       }
   846    841     }
   847    842   
   848    843     return rc;
................................................................................
   897    892   /*
   898    893   ** Create a new sqlite3_value object.
   899    894   */
   900    895   sqlite3_value *sqlite3ValueNew(sqlite3 *db){
   901    896     Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
   902    897     if( p ){
   903    898       p->flags = MEM_Null;
   904         -    p->memType = MEM_Null;
   905    899       p->db = db;
   906    900     }
   907    901     return p;
   908    902   }
   909    903   
   910    904   /*
   911    905   ** Context object passed by sqlite3Stat4ProbeSetValue() through to 
................................................................................
   946    940           pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
   947    941           if( pRec->pKeyInfo ){
   948    942             assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
   949    943             assert( pRec->pKeyInfo->enc==ENC(db) );
   950    944             pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
   951    945             for(i=0; i<nCol; i++){
   952    946               pRec->aMem[i].flags = MEM_Null;
   953         -            pRec->aMem[i].memType = MEM_Null;
   954    947               pRec->aMem[i].db = db;
   955    948             }
   956    949           }else{
   957    950             sqlite3DbFree(db, pRec);
   958    951             pRec = 0;
   959    952           }
   960    953         }
................................................................................
  1019   1012       if( pVal==0 ) goto no_mem;
  1020   1013       if( ExprHasProperty(pExpr, EP_IntValue) ){
  1021   1014         sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
  1022   1015       }else{
  1023   1016         zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
  1024   1017         if( zVal==0 ) goto no_mem;
  1025   1018         sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
  1026         -      if( op==TK_FLOAT ) pVal->memType = MEM_Real;
  1027   1019       }
  1028   1020       if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
  1029   1021         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
  1030   1022       }else{
  1031   1023         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
  1032   1024       }
  1033   1025       if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
................................................................................
  1037   1029     }else if( op==TK_UMINUS ) {
  1038   1030       /* This branch happens for multiple negative signs.  Ex: -(-5) */
  1039   1031       if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 
  1040   1032        && pVal!=0
  1041   1033       ){
  1042   1034         sqlite3VdbeMemNumerify(pVal);
  1043   1035         if( pVal->u.i==SMALLEST_INT64 ){
  1044         -        pVal->flags &= MEM_Int;
         1036  +        pVal->flags &= ~MEM_Int;
  1045   1037           pVal->flags |= MEM_Real;
  1046         -        pVal->r = (double)LARGEST_INT64;
         1038  +        pVal->r = (double)SMALLEST_INT64;
  1047   1039         }else{
  1048   1040           pVal->u.i = -pVal->u.i;
  1049   1041         }
  1050   1042         pVal->r = -pVal->r;
  1051   1043         sqlite3ValueApplyAffinity(pVal, affinity, enc);
  1052   1044       }
  1053   1045     }else if( op==TK_NULL ){
................................................................................
  1065   1057       nVal = sqlite3Strlen30(zVal)-1;
  1066   1058       assert( zVal[nVal]=='\'' );
  1067   1059       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
  1068   1060                            0, SQLITE_DYNAMIC);
  1069   1061     }
  1070   1062   #endif
  1071   1063   
  1072         -  if( pVal ){
  1073         -    sqlite3VdbeMemStoreType(pVal);
  1074         -  }
  1075   1064     *ppVal = pVal;
  1076   1065     return rc;
  1077   1066   
  1078   1067   no_mem:
  1079   1068     db->mallocFailed = 1;
  1080   1069     sqlite3DbFree(db, zVal);
  1081   1070     assert( *ppVal==0 );
................................................................................
  1231   1220         pVal = valueNew(db, &alloc);
  1232   1221         if( pVal ){
  1233   1222           rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
  1234   1223           if( rc==SQLITE_OK ){
  1235   1224             sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
  1236   1225           }
  1237   1226           pVal->db = pParse->db;
  1238         -        sqlite3VdbeMemStoreType((Mem*)pVal);
  1239   1227         }
  1240   1228       }
  1241   1229     }else{
  1242   1230       rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
  1243   1231     }
  1244   1232     *pbOk = (pVal!=0);
  1245   1233   

Changes to test/alter4.test.

   330    330   } {}
   331    331   do_test alter4-8.2 {
   332    332     execsql {
   333    333       SELECT sql FROM sqlite_temp_master WHERE name = 't4';
   334    334     }
   335    335   } [list $::sql]
   336    336   
          337  +
          338  +# Test that a default value equal to -1 multipied by the smallest possible
          339  +# 64-bit integer is correctly converted to a real.
          340  +do_execsql_test alter4-9.1 {
          341  +  CREATE TABLE t5(
          342  +    a INTEGER DEFAULT -9223372036854775808,
          343  +    b INTEGER DEFAULT (-(-9223372036854775808))
          344  +  );
          345  +  INSERT INTO t5 DEFAULT VALUES;
          346  +}
          347  +
          348  +do_execsql_test alter4-9.2 { SELECT typeof(a), a, typeof(b), b FROM t5; } {
          349  +  integer -9223372036854775808
          350  +  real     9.22337203685478e+18
          351  +}
          352  +
          353  +do_execsql_test alter4-9.3 { 
          354  +  ALTER TABLE t5 ADD COLUMN c INTEGER DEFAULT (-(-9223372036854775808));
          355  +  SELECT typeof(c), c FROM t5;
          356  +} {real 9.22337203685478e+18}
          357  +
   337    358   finish_test

Changes to test/corruptI.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   
    13     13   set testdir [file dirname $argv0]
    14     14   source $testdir/tester.tcl
    15     15   set testprefix corruptI
           16  +
           17  +if {[permutation]=="mmap"} {
           18  +  finish_test
           19  +  return
           20  +}
    16     21   
    17     22   # Do not use a codec for tests in this file, as the database file is
    18     23   # manipulated directly using tcl scripts (using the [hexio_write] command).
    19     24   #
    20     25   do_not_use_codec
    21     26   database_may_be_corrupt
    22     27   

Changes to test/view.test.

   606    606     do_test view-21.2 {
   607    607       db progress 1000 {expr 1}
   608    608       catchsql {
   609    609         SELECT * FROM v32768;
   610    610       }
   611    611     } {1 interrupted}
   612    612   }
          613  +
          614  +db close
          615  +sqlite3 db :memory:
          616  +do_execsql_test view-22.1 {
          617  +  CREATE VIEW x1 AS SELECT 123 AS '', 234 AS '', 345 AS '';
          618  +  SELECT * FROM x1;
          619  +} {123 234 345}
          620  +do_test view-22.2 {
          621  +  unset -nocomplain x
          622  +  db eval {SELECT * FROM x1} x break
          623  +  lsort [array names x]
          624  +} {{} * :1 :2}
          625  +
   613    626   
   614    627   finish_test