1.1 --- a/extensions/sqlite/sqlite-source/vdbe.c Sun Oct 28 05:06:12 2007 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/vdbe.c Sun Oct 28 05:48:06 2007 +0000
1.3 @@ -46,7 +46,6 @@
1.4 ** $Id$
1.5 */
1.6 #include "sqliteInt.h"
1.7 -#include "os.h"
1.8 #include <ctype.h>
1.9 #include <math.h>
1.10 #include "vdbeInt.h"
1.11 @@ -111,15 +110,6 @@
1.12 { goto no_mem; }
1.13
1.14 /*
1.15 -** Convert the given stack entity into a string that has been obtained
1.16 -** from sqliteMalloc(). This is different from Stringify() above in that
1.17 -** Stringify() will use the NBFS bytes of static string space if the string
1.18 -** will fit but this routine always mallocs for space.
1.19 -** Return non-zero if we run out of memory.
1.20 -*/
1.21 -#define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P)
1.22 -
1.23 -/*
1.24 ** The header of a record consists of a sequence variable-length integers.
1.25 ** These integers are almost always small and are encoded as a single byte.
1.26 ** The following macro takes advantage this fact to provide a fast decode
1.27 @@ -206,7 +196,7 @@
1.28 if( p->apCsr[iCur] ){
1.29 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
1.30 }
1.31 - p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) );
1.32 + p->apCsr[iCur] = pCx = sqlite3MallocZero( sizeof(Cursor) );
1.33 if( pCx ){
1.34 pCx->iDb = iDb;
1.35 }
1.36 @@ -256,7 +246,11 @@
1.37 ** SQLITE_AFF_NONE:
1.38 ** No-op. pRec is unchanged.
1.39 */
1.40 -static void applyAffinity(Mem *pRec, char affinity, u8 enc){
1.41 +static void applyAffinity(
1.42 + Mem *pRec, /* The value to apply affinity to */
1.43 + char affinity, /* The affinity to be applied */
1.44 + u8 enc /* Use this text encoding */
1.45 +){
1.46 if( affinity==SQLITE_AFF_TEXT ){
1.47 /* Only attempt the conversion to TEXT if there is an integer or real
1.48 ** representation (blob and NULL do not get converted) but no string
1.49 @@ -295,7 +289,11 @@
1.50 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
1.51 ** not the internal Mem* type.
1.52 */
1.53 -void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){
1.54 +void sqlite3ValueApplyAffinity(
1.55 + sqlite3_value *pVal,
1.56 + u8 affinity,
1.57 + u8 enc
1.58 +){
1.59 applyAffinity((Mem *)pVal, affinity, enc);
1.60 }
1.61
1.62 @@ -429,7 +427,7 @@
1.63 ** return SQLITE_BUSY.
1.64 **
1.65 ** If an error occurs, an error message is written to memory obtained
1.66 -** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
1.67 +** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
1.68 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
1.69 **
1.70 ** If the callback ever returns non-zero, then the program exits
1.71 @@ -467,6 +465,7 @@
1.72 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
1.73 assert( db->magic==SQLITE_MAGIC_BUSY );
1.74 pTos = p->pTos;
1.75 + sqlite3BtreeMutexArrayEnter(&p->aMutex);
1.76 if( p->rc==SQLITE_NOMEM ){
1.77 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1.78 ** sqlite3_column_text16() failed. */
1.79 @@ -485,7 +484,7 @@
1.80 sqlite3VdbeIOTraceSql(p);
1.81 #ifdef SQLITE_DEBUG
1.82 if( (p->db->flags & SQLITE_VdbeListing)!=0
1.83 - || sqlite3OsFileExists("vdbe_explain")
1.84 + || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS)
1.85 ){
1.86 int i;
1.87 printf("VDBE Program Listing:\n");
1.88 @@ -494,14 +493,14 @@
1.89 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
1.90 }
1.91 }
1.92 - if( sqlite3OsFileExists("vdbe_trace") ){
1.93 + if( sqlite3OsAccess(db->pVfs, "vdbe_trace", SQLITE_ACCESS_EXISTS) ){
1.94 p->trace = stdout;
1.95 }
1.96 #endif
1.97 for(pc=p->pc; rc==SQLITE_OK; pc++){
1.98 assert( pc>=0 && pc<p->nOp );
1.99 assert( pTos<=&p->aStack[pc] );
1.100 - if( sqlite3MallocFailed() ) goto no_mem;
1.101 + if( db->mallocFailed ) goto no_mem;
1.102 #ifdef VDBE_PROFILE
1.103 origPc = pc;
1.104 start = hwtime();
1.105 @@ -518,7 +517,8 @@
1.106 }
1.107 sqlite3VdbePrintOp(p->trace, pc, pOp);
1.108 }
1.109 - if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){
1.110 + if( p->trace==0 && pc==0
1.111 + && sqlite3OsAccess(db->pVfs, "vdbe_sqltrace", SQLITE_ACCESS_EXISTS) ){
1.112 sqlite3VdbePrintSql(p);
1.113 }
1.114 #endif
1.115 @@ -686,10 +686,11 @@
1.116 rc = sqlite3VdbeHalt(p);
1.117 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
1.118 if( rc==SQLITE_BUSY ){
1.119 - p->rc = SQLITE_BUSY;
1.120 - return SQLITE_BUSY;
1.121 + p->rc = rc = SQLITE_BUSY;
1.122 + }else{
1.123 + rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
1.124 }
1.125 - return p->rc ? SQLITE_ERROR : SQLITE_DONE;
1.126 + goto vdbe_return;
1.127 }
1.128
1.129 /* Opcode: Integer P1 * *
1.130 @@ -757,7 +758,7 @@
1.131 pTos->flags &= ~(MEM_Dyn);
1.132 pTos->flags |= MEM_Static;
1.133 if( pOp->p3type==P3_DYNAMIC ){
1.134 - sqliteFree(pOp->p3);
1.135 + sqlite3_free(pOp->p3);
1.136 }
1.137 pOp->p3type = P3_DYNAMIC;
1.138 pOp->p3 = pTos->z;
1.139 @@ -811,16 +812,16 @@
1.140 assert( SQLITE_MAX_SQL_LENGTH < SQLITE_MAX_LENGTH );
1.141 assert( pOp->p1 < SQLITE_MAX_LENGTH );
1.142 if( pOp->p1 ){
1.143 - char *zBlob = sqlite3HexToBlob(pOp->p3);
1.144 + char *zBlob = sqlite3HexToBlob(db, pOp->p3);
1.145 if( !zBlob ) goto no_mem;
1.146 if( pOp->p3type==P3_DYNAMIC ){
1.147 - sqliteFree(pOp->p3);
1.148 + sqlite3_free(pOp->p3);
1.149 }
1.150 pOp->p3 = zBlob;
1.151 pOp->p3type = P3_DYNAMIC;
1.152 }else{
1.153 if( pOp->p3type==P3_DYNAMIC ){
1.154 - sqliteFree(pOp->p3);
1.155 + sqlite3_free(pOp->p3);
1.156 }
1.157 pOp->p3type = P3_STATIC;
1.158 pOp->p3 = "";
1.159 @@ -1004,7 +1005,8 @@
1.160 p->popStack = pOp->p1;
1.161 p->pc = pc + 1;
1.162 p->pTos = pTos;
1.163 - return SQLITE_ROW;
1.164 + rc = SQLITE_ROW;
1.165 + goto vdbe_return;
1.166 }
1.167
1.168 /* Opcode: Concat P1 P2 *
1.169 @@ -1015,7 +1017,7 @@
1.170 ** any element of the stack is NULL, then the result is NULL.
1.171 **
1.172 ** When P1==1, this routine makes a copy of the top stack element
1.173 -** into memory obtained from sqliteMalloc().
1.174 +** into memory obtained from sqlite3_malloc().
1.175 */
1.176 case OP_Concat: { /* same as TK_CONCAT */
1.177 char *zNew;
1.178 @@ -1056,7 +1058,7 @@
1.179 if( nByte+2>SQLITE_MAX_LENGTH ){
1.180 goto too_big;
1.181 }
1.182 - zNew = sqliteMallocRaw( nByte+2 );
1.183 + zNew = sqlite3DbMallocRaw(db, nByte+2 );
1.184 if( zNew==0 ) goto no_mem;
1.185 j = 0;
1.186 pTerm = &pTos[1-nField];
1.187 @@ -1279,6 +1281,7 @@
1.188 ctx.s.flags = MEM_Null;
1.189 ctx.s.z = 0;
1.190 ctx.s.xDel = 0;
1.191 + ctx.s.db = db;
1.192 ctx.isError = 0;
1.193 if( ctx.pFunc->needCollSeq ){
1.194 assert( pOp>p->aOp );
1.195 @@ -1289,7 +1292,19 @@
1.196 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.197 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
1.198 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.199 - if( sqlite3MallocFailed() ) goto no_mem;
1.200 + if( db->mallocFailed ){
1.201 + /* Even though a malloc() has failed, the implementation of the
1.202 + ** user function may have called an sqlite3_result_XXX() function
1.203 + ** to return a value. The following call releases any resources
1.204 + ** associated with such a value.
1.205 + **
1.206 + ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1.207 + ** fails also (the if(...) statement above). But if people are
1.208 + ** misusing sqlite, they have bigger problems than a leaked value.
1.209 + */
1.210 + sqlite3VdbeMemRelease(&ctx.s);
1.211 + goto no_mem;
1.212 + }
1.213 popStack(&pTos, n);
1.214
1.215 /* If any auxilary data functions have been called by this user function,
1.216 @@ -2063,7 +2078,7 @@
1.217
1.218 aType = pC->aType;
1.219 if( aType==0 ){
1.220 - pC->aType = aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
1.221 + pC->aType = aType = sqlite3DbMallocRaw(db, 2*nField*sizeof(aType) );
1.222 }
1.223 if( aType==0 ){
1.224 goto no_mem;
1.225 @@ -2159,7 +2174,7 @@
1.226 zData = &zRec[aOffset[p2]];
1.227 }else{
1.228 len = sqlite3VdbeSerialTypeLen(aType[p2]);
1.229 - rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem);
1.230 + rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
1.231 if( rc!=SQLITE_OK ){
1.232 goto op_column_out;
1.233 }
1.234 @@ -2334,7 +2349,7 @@
1.235
1.236 /* Allocate space for the new record. */
1.237 if( nByte>sizeof(zTemp) ){
1.238 - zNewRecord = sqliteMallocRaw(nByte);
1.239 + zNewRecord = sqlite3DbMallocRaw(db, nByte);
1.240 if( !zNewRecord ){
1.241 goto no_mem;
1.242 }
1.243 @@ -2406,6 +2421,7 @@
1.244 Btree *pBt;
1.245 if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0 && !(db->autoCommit) ){
1.246 assert( sqlite3BtreeIsInTrans(pBt) );
1.247 + assert( (p->btreeMask & (1<<i))!=0 );
1.248 if( !sqlite3BtreeIsInStmt(pBt) ){
1.249 rc = sqlite3BtreeBeginStmt(pBt);
1.250 p->openedStatement = 1;
1.251 @@ -2450,15 +2466,16 @@
1.252 p->pTos = pTos;
1.253 p->pc = pc;
1.254 db->autoCommit = 1-i;
1.255 - p->rc = SQLITE_BUSY;
1.256 - return SQLITE_BUSY;
1.257 + p->rc = rc = SQLITE_BUSY;
1.258 + goto vdbe_return;
1.259 }
1.260 }
1.261 if( p->rc==SQLITE_OK ){
1.262 - return SQLITE_DONE;
1.263 + rc = SQLITE_DONE;
1.264 }else{
1.265 - return SQLITE_ERROR;
1.266 + rc = SQLITE_ERROR;
1.267 }
1.268 + goto vdbe_return;
1.269 }else{
1.270 sqlite3SetString(&p->zErrMsg,
1.271 (!i)?"cannot start a transaction within a transaction":(
1.272 @@ -2495,15 +2512,16 @@
1.273 Btree *pBt;
1.274
1.275 assert( i>=0 && i<db->nDb );
1.276 + assert( (p->btreeMask & (1<<i))!=0 );
1.277 pBt = db->aDb[i].pBt;
1.278
1.279 if( pBt ){
1.280 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
1.281 if( rc==SQLITE_BUSY ){
1.282 p->pc = pc;
1.283 - p->rc = SQLITE_BUSY;
1.284 + p->rc = rc = SQLITE_BUSY;
1.285 p->pTos = pTos;
1.286 - return SQLITE_BUSY;
1.287 + goto vdbe_return;
1.288 }
1.289 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
1.290 goto abort_due_to_error;
1.291 @@ -2541,6 +2559,7 @@
1.292 }
1.293 assert( iDb>=0 && iDb<db->nDb );
1.294 assert( db->aDb[iDb].pBt!=0 );
1.295 + assert( (p->btreeMask & (1<<iDb))!=0 );
1.296 /* The indexing of meta values at the schema layer is off by one from
1.297 ** the indexing in the btree layer. The btree considers meta[0] to
1.298 ** be the number of free pages in the database (a read-only value)
1.299 @@ -2569,6 +2588,7 @@
1.300 Db *pDb;
1.301 assert( pOp->p2<SQLITE_N_BTREE_META );
1.302 assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.303 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.304 pDb = &db->aDb[pOp->p1];
1.305 assert( pDb->pBt!=0 );
1.306 assert( pTos>=p->aStack );
1.307 @@ -2613,6 +2633,7 @@
1.308 int iMeta;
1.309 Btree *pBt;
1.310 assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.311 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.312 pBt = db->aDb[pOp->p1].pBt;
1.313 if( pBt ){
1.314 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
1.315 @@ -2621,7 +2642,8 @@
1.316 iMeta = 0;
1.317 }
1.318 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
1.319 - sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
1.320 + sqlite3_free(p->zErrMsg);
1.321 + p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
1.322 /* If the schema-cookie from the database file matches the cookie
1.323 ** stored with the in-memory representation of the schema, do
1.324 ** not reload the schema from the database file.
1.325 @@ -2703,6 +2725,7 @@
1.326 assert( (pTos->flags & MEM_Dyn)==0 );
1.327 pTos--;
1.328 assert( iDb>=0 && iDb<db->nDb );
1.329 + assert( (p->btreeMask & (1<<iDb))!=0 );
1.330 pDb = &db->aDb[iDb];
1.331 pX = pDb->pBt;
1.332 assert( pX!=0 );
1.333 @@ -2743,9 +2766,9 @@
1.334 switch( rc ){
1.335 case SQLITE_BUSY: {
1.336 p->pc = pc;
1.337 - p->rc = SQLITE_BUSY;
1.338 + p->rc = rc = SQLITE_BUSY;
1.339 p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
1.340 - return SQLITE_BUSY;
1.341 + goto vdbe_return;
1.342 }
1.343 case SQLITE_OK: {
1.344 int flags = sqlite3BtreeFlags(pCur->pCursor);
1.345 @@ -2806,11 +2829,19 @@
1.346 case OP_OpenEphemeral: { /* no-push */
1.347 int i = pOp->p1;
1.348 Cursor *pCx;
1.349 + static const int openFlags =
1.350 + SQLITE_OPEN_READWRITE |
1.351 + SQLITE_OPEN_CREATE |
1.352 + SQLITE_OPEN_EXCLUSIVE |
1.353 + SQLITE_OPEN_DELETEONCLOSE |
1.354 + SQLITE_OPEN_TRANSIENT_DB;
1.355 +
1.356 assert( i>=0 );
1.357 pCx = allocateCursor(p, i, -1);
1.358 if( pCx==0 ) goto no_mem;
1.359 pCx->nullRow = 1;
1.360 - rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, &pCx->pBt);
1.361 + rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
1.362 + &pCx->pBt);
1.363 if( rc==SQLITE_OK ){
1.364 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
1.365 }
1.366 @@ -3064,12 +3095,15 @@
1.367 assert( i>=0 && i<p->nCursor );
1.368 assert( p->apCsr[i]!=0 );
1.369 if( (pC = p->apCsr[i])->pCursor!=0 ){
1.370 - int res, rx;
1.371 + int res;
1.372 assert( pC->isTable==0 );
1.373 assert( pTos->flags & MEM_Blob );
1.374 Stringify(pTos, encoding);
1.375 - rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
1.376 - alreadyExists = rx==SQLITE_OK && res==0;
1.377 + rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
1.378 + if( rc!=SQLITE_OK ){
1.379 + break;
1.380 + }
1.381 + alreadyExists = (res==0);
1.382 pC->deferredMoveto = 0;
1.383 pC->cacheStatus = CACHE_STALE;
1.384 }
1.385 @@ -3456,14 +3490,14 @@
1.386 assert( pTos->flags & (MEM_Blob|MEM_Str) );
1.387 }
1.388 if( pC->pseudoTable ){
1.389 - sqliteFree(pC->pData);
1.390 + sqlite3_free(pC->pData);
1.391 pC->iKey = iKey;
1.392 pC->nData = pTos->n;
1.393 if( pTos->flags & MEM_Dyn ){
1.394 pC->pData = pTos->z;
1.395 pTos->flags = MEM_Null;
1.396 }else{
1.397 - pC->pData = sqliteMallocRaw( pC->nData+2 );
1.398 + pC->pData = sqlite3_malloc( pC->nData+2 );
1.399 if( !pC->pData ) goto no_mem;
1.400 memcpy(pC->pData, pTos->z, pC->nData);
1.401 pC->pData[pC->nData] = 0;
1.402 @@ -3632,7 +3666,7 @@
1.403 pTos->flags = MEM_Blob | MEM_Short;
1.404 pTos->z = pTos->zShort;
1.405 }else{
1.406 - char *z = sqliteMallocRaw( n );
1.407 + char *z = sqlite3_malloc( n );
1.408 if( z==0 ) goto no_mem;
1.409 pTos->flags = MEM_Blob | MEM_Dyn;
1.410 pTos->xDel = 0;
1.411 @@ -4058,6 +4092,7 @@
1.412 rc = SQLITE_LOCKED;
1.413 }else{
1.414 assert( iCnt==1 );
1.415 + assert( (p->btreeMask & (1<<pOp->p2))!=0 );
1.416 rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved);
1.417 pTos++;
1.418 pTos->flags = MEM_Int;
1.419 @@ -4119,6 +4154,7 @@
1.420 }
1.421 }
1.422 #endif
1.423 + assert( (p->btreeMask & (1<<pOp->p2))!=0 );
1.424 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
1.425 break;
1.426 }
1.427 @@ -4149,6 +4185,7 @@
1.428 int flags;
1.429 Db *pDb;
1.430 assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.431 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.432 pDb = &db->aDb[pOp->p1];
1.433 assert( pDb->pBt!=0 );
1.434 if( pOp->opcode==OP_CreateTable ){
1.435 @@ -4194,21 +4231,20 @@
1.436 initData.db = db;
1.437 initData.iDb = pOp->p1;
1.438 initData.pzErrMsg = &p->zErrMsg;
1.439 - zSql = sqlite3MPrintf(
1.440 + zSql = sqlite3MPrintf(db,
1.441 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
1.442 db->aDb[iDb].zName, zMaster, pOp->p3);
1.443 if( zSql==0 ) goto no_mem;
1.444 sqlite3SafetyOff(db);
1.445 assert( db->init.busy==0 );
1.446 db->init.busy = 1;
1.447 - assert( !sqlite3MallocFailed() );
1.448 + assert( !db->mallocFailed );
1.449 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
1.450 if( rc==SQLITE_ABORT ) rc = initData.rc;
1.451 - sqliteFree(zSql);
1.452 + sqlite3_free(zSql);
1.453 db->init.busy = 0;
1.454 sqlite3SafetyOn(db);
1.455 if( rc==SQLITE_NOMEM ){
1.456 - sqlite3FailedMalloc();
1.457 goto no_mem;
1.458 }
1.459 break;
1.460 @@ -4299,18 +4335,20 @@
1.461 if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
1.462 }
1.463 assert( nRoot>0 );
1.464 - aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) );
1.465 + aRoot = sqlite3_malloc( sizeof(int)*(nRoot+1) );
1.466 if( aRoot==0 ) goto no_mem;
1.467 j = pOp->p1;
1.468 assert( j>=0 && j<p->nMem );
1.469 pnErr = &p->aMem[j];
1.470 assert( (pnErr->flags & MEM_Int)!=0 );
1.471 for(j=0; j<nRoot; j++){
1.472 - aRoot[j] = pTos[-j].u.i;
1.473 + aRoot[j] = (pTos-j)->u.i;
1.474 }
1.475 aRoot[j] = 0;
1.476 popStack(&pTos, nRoot);
1.477 pTos++;
1.478 + assert( pOp->p2>=0 && pOp->p2<db->nDb );
1.479 + assert( (p->btreeMask & (1<<pOp->p2))!=0 );
1.480 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot,
1.481 pnErr->u.i, &nErr);
1.482 pnErr->u.i -= nErr;
1.483 @@ -4325,7 +4363,7 @@
1.484 }
1.485 pTos->enc = SQLITE_UTF8;
1.486 sqlite3VdbeChangeEncoding(pTos, encoding);
1.487 - sqliteFree(aRoot);
1.488 + sqlite3_free(aRoot);
1.489 break;
1.490 }
1.491 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1.492 @@ -4338,7 +4376,9 @@
1.493 case OP_FifoWrite: { /* no-push */
1.494 assert( pTos>=p->aStack );
1.495 sqlite3VdbeMemIntegerify(pTos);
1.496 - sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i);
1.497 + if( sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i)==SQLITE_NOMEM ){
1.498 + goto no_mem;
1.499 + }
1.500 assert( (pTos->flags & MEM_Dyn)==0 );
1.501 pTos--;
1.502 break;
1.503 @@ -4378,7 +4418,7 @@
1.504 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
1.505 if( i>=p->contextStackDepth ){
1.506 p->contextStackDepth = i+1;
1.507 - p->contextStack = sqliteReallocOrFree(p->contextStack,
1.508 + p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
1.509 sizeof(Context)*(i+1));
1.510 if( p->contextStack==0 ) goto no_mem;
1.511 }
1.512 @@ -4610,6 +4650,7 @@
1.513 ctx.s.flags = MEM_Null;
1.514 ctx.s.z = 0;
1.515 ctx.s.xDel = 0;
1.516 + ctx.s.db = db;
1.517 ctx.isError = 0;
1.518 ctx.pColl = 0;
1.519 if( ctx.pFunc->needCollSeq ){
1.520 @@ -4682,6 +4723,7 @@
1.521 Btree *pBt;
1.522
1.523 assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.524 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.525 pBt = db->aDb[pOp->p1].pBt;
1.526 rc = sqlite3BtreeIncrVacuum(pBt);
1.527 if( rc==SQLITE_DONE ){
1.528 @@ -4733,6 +4775,8 @@
1.529 if( isWriteLock ){
1.530 p1 = (-1*p1)-1;
1.531 }
1.532 + assert( p1>=0 && p1<db->nDb );
1.533 + assert( (p->btreeMask & (1<<p1))!=0 );
1.534 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
1.535 if( rc==SQLITE_LOCKED ){
1.536 const char *z = (const char *)pOp->p3;
1.537 @@ -4808,6 +4852,7 @@
1.538 pCur->pVtabCursor = pVtabCursor;
1.539 pCur->pModule = pVtabCursor->pVtab->pModule;
1.540 }else{
1.541 + db->mallocFailed = 1;
1.542 pModule->xClose(pVtabCursor);
1.543 }
1.544 }
1.545 @@ -4932,6 +4977,7 @@
1.546 sqlite3_context sContext;
1.547 memset(&sContext, 0, sizeof(sContext));
1.548 sContext.s.flags = MEM_Null;
1.549 + sContext.s.db = db;
1.550 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.551 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
1.552
1.553 @@ -5132,6 +5178,7 @@
1.554 ** cell, so avoid calling MemSanity() in this case.
1.555 */
1.556 if( pTos>=p->aStack && pTos->flags ){
1.557 + assert( pTos->db==db );
1.558 sqlite3VdbeMemSanity(pTos);
1.559 assert( !sqlite3VdbeMemTooBig(pTos) );
1.560 }
1.561 @@ -5176,6 +5223,12 @@
1.562 }
1.563 sqlite3VdbeHalt(p);
1.564 p->pTos = pTos;
1.565 +
1.566 + /* This is the only way out of this procedure. We have to
1.567 + ** release the mutexes on btrees that were acquired at the
1.568 + ** top. */
1.569 +vdbe_return:
1.570 + sqlite3BtreeMutexArrayLeave(&p->aMutex);
1.571 return rc;
1.572
1.573 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
1.574 @@ -5189,6 +5242,7 @@
1.575 /* Jump to here if a malloc() fails.
1.576 */
1.577 no_mem:
1.578 + db->mallocFailed = 1;
1.579 sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
1.580 rc = SQLITE_NOMEM;
1.581 goto vdbe_halt;
1.582 @@ -5204,7 +5258,7 @@
1.583 */
1.584 abort_due_to_error:
1.585 if( p->zErrMsg==0 ){
1.586 - if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM;
1.587 + if( db->mallocFailed ) rc = SQLITE_NOMEM;
1.588 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
1.589 }
1.590 goto vdbe_halt;