/ Check-in [a444633a]
Login

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

Overview
Comment:Fix up all VDBE opcodes so that they cause an immediate exit on any kind of error, and thereby eliminate the need to test "rc" at the top of the loop. Resulting code is a little smaller and faster.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | vdbe-performance
Files: files | file ages | folders
SHA1: a444633a827642bcf4d02a461e7e5f66769d39fd
User & Date: drh 2016-02-17 18:44:11
Context
2016-02-17
20:47
When VDBE opcodes fail, they cause an immediate exit from the loop, rather than depending on the rc==SQLITE_OK test at the top of the loop. This is faster and smaller. check-in: 24c7151c user: drh tags: trunk
18:44
Fix up all VDBE opcodes so that they cause an immediate exit on any kind of error, and thereby eliminate the need to test "rc" at the top of the loop. Resulting code is a little smaller and faster. Closed-Leaf check-in: a444633a user: drh tags: vdbe-performance
15:01
Bump the version number to 3.12.0. check-in: c17d7276 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbe.c.

   623    623           }
   624    624         }
   625    625       }
   626    626       if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
   627    627     }
   628    628     sqlite3EndBenignMalloc();
   629    629   #endif
   630         -  for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
          630  +  for(pOp=&aOp[p->pc]; 1; pOp++){
          631  +    /* Errors are detected by individual opcodes, with an immediate
          632  +    ** jumps to abort_due_to_error. */
          633  +    assert( rc==SQLITE_OK );
          634  +
   631    635       assert( pOp>=aOp && pOp<&aOp[p->nOp]);
   632    636   #ifdef VDBE_PROFILE
   633    637       start = sqlite3Hwtime();
   634    638   #endif
   635    639       nVmStep++;
   636    640   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
   637    641       if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
................................................................................
   770    774     ** a return code SQLITE_ABORT.
   771    775     */
   772    776     if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
   773    777       assert( db->nProgressOps!=0 );
   774    778       nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
   775    779       if( db->xProgress(db->pProgressArg) ){
   776    780         rc = SQLITE_INTERRUPT;
   777         -      goto vdbe_error_halt;
          781  +      goto abort_due_to_error;
   778    782       }
   779    783     }
   780    784   #endif
   781    785     
   782    786     break;
   783    787   }
   784    788   
................................................................................
  1049   1053     pOut = out2Prerelease(p, pOp);
  1050   1054     pOp->opcode = OP_String;
  1051   1055     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
  1052   1056   
  1053   1057   #ifndef SQLITE_OMIT_UTF16
  1054   1058     if( encoding!=SQLITE_UTF8 ){
  1055   1059       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
  1056         -    if( rc==SQLITE_TOOBIG ) goto too_big;
         1060  +    if( rc ){
         1061  +      assert( rc==SQLITE_TOOBIG ); /* This is the only possible error here */
         1062  +      goto too_big;
         1063  +    }
  1057   1064       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
  1058   1065       assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
  1059   1066       assert( VdbeMemDynamic(pOut)==0 );
  1060   1067       pOut->szMalloc = 0;
  1061   1068       pOut->flags |= MEM_Static;
  1062   1069       if( pOp->p4type==P4_DYNAMIC ){
  1063   1070         sqlite3DbFree(db, pOp->p4.z);
................................................................................
  1315   1322     /* Run the progress counter just before returning.
  1316   1323     */
  1317   1324     if( db->xProgress!=0
  1318   1325      && nVmStep>=nProgressLimit
  1319   1326      && db->xProgress(db->pProgressArg)!=0
  1320   1327     ){
  1321   1328       rc = SQLITE_INTERRUPT;
  1322         -    goto vdbe_error_halt;
         1329  +    goto abort_due_to_error;
  1323   1330     }
  1324   1331   #endif
  1325   1332   
  1326   1333     /* If this statement has violated immediate foreign key constraints, do
  1327   1334     ** not return the number of rows modified. And do not RELEASE the statement
  1328   1335     ** transaction. It needs to be rolled back.  */
  1329   1336     if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
  1330   1337       assert( db->flags&SQLITE_CountRows );
  1331   1338       assert( p->usesStmtJournal );
  1332         -    break;
         1339  +    goto abort_due_to_error;
  1333   1340     }
  1334   1341   
  1335   1342     /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then 
  1336   1343     ** DML statements invoke this opcode to return the number of rows 
  1337   1344     ** modified to the user. This is the only way that a VM that
  1338   1345     ** opens a statement transaction may invoke this opcode.
  1339   1346     **
................................................................................
  1345   1352     ** may lead to overlapping statement transactions.
  1346   1353     **
  1347   1354     ** The statement transaction is never a top-level transaction.  Hence
  1348   1355     ** the RELEASE call below can never fail.
  1349   1356     */
  1350   1357     assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
  1351   1358     rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
  1352         -  if( NEVER(rc!=SQLITE_OK) ){
  1353         -    break;
  1354         -  }
         1359  +  assert( rc==SQLITE_OK );
  1355   1360   
  1356   1361     /* Invalidate all ephemeral cursor row caches */
  1357   1362     p->cacheCtr = (p->cacheCtr + 2)|1;
  1358   1363   
  1359   1364     /* Make sure the results of the current row are \000 terminated
  1360   1365     ** and have an assigned type.  The results are de-ephemeralized as
  1361   1366     ** a side effect.
................................................................................
  1671   1676     /* If the function returned an error, throw an exception */
  1672   1677     if( pCtx->fErrorOrAux ){
  1673   1678       if( pCtx->isError ){
  1674   1679         sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
  1675   1680         rc = pCtx->isError;
  1676   1681       }
  1677   1682       sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
         1683  +    if( rc ) goto abort_due_to_error;
  1678   1684     }
  1679   1685   
  1680   1686     /* Copy the result of the function into register P3 */
  1681   1687     if( pOut->flags & (MEM_Str|MEM_Blob) ){
  1682   1688       sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
  1683   1689       if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
  1684   1690     }
................................................................................
  1854   1860     testcase( pOp->p2==SQLITE_AFF_INTEGER );
  1855   1861     testcase( pOp->p2==SQLITE_AFF_REAL );
  1856   1862     pIn1 = &aMem[pOp->p1];
  1857   1863     memAboutToChange(p, pIn1);
  1858   1864     rc = ExpandBlob(pIn1);
  1859   1865     sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
  1860   1866     UPDATE_MAX_BLOBSIZE(pIn1);
         1867  +  if( rc ) goto abort_due_to_error;
  1861   1868     break;
  1862   1869   }
  1863   1870   #endif /* SQLITE_OMIT_CAST */
  1864   1871   
  1865   1872   /* Opcode: Lt P1 P2 P3 P4 P5
  1866   1873   ** Synopsis: if r[P1]<r[P3] goto P2
  1867   1874   **
................................................................................
  2463   2470         ** types use so much data space that there can only be 4096 and 32 of
  2464   2471         ** them, respectively.  So the maximum header length results from a
  2465   2472         ** 3-byte type for each of the maximum of 32768 columns plus three
  2466   2473         ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
  2467   2474         */
  2468   2475         if( offset > 98307 || offset > pC->payloadSize ){
  2469   2476           rc = SQLITE_CORRUPT_BKPT;
  2470         -        goto op_column_error;
         2477  +        goto abort_due_to_error;
  2471   2478         }
  2472   2479       }
  2473   2480   
  2474   2481       /* The following goto is an optimization.  It can be omitted and
  2475   2482       ** everything will still work.  But OP_Column is measurably faster
  2476   2483       ** by skipping the subsequent conditional, which is always true.
  2477   2484       */
................................................................................
  2488   2495       */
  2489   2496       op_column_read_header:
  2490   2497       if( pC->iHdrOffset<aOffset[0] ){
  2491   2498         /* Make sure zData points to enough of the record to cover the header. */
  2492   2499         if( pC->aRow==0 ){
  2493   2500           memset(&sMem, 0, sizeof(sMem));
  2494   2501           rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem);
  2495         -        if( rc!=SQLITE_OK ) goto op_column_error;
         2502  +        if( rc!=SQLITE_OK ) goto abort_due_to_error;
  2496   2503           zData = (u8*)sMem.z;
  2497   2504         }else{
  2498   2505           zData = pC->aRow;
  2499   2506         }
  2500   2507     
  2501   2508         /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
  2502   2509         i = pC->nHdrParsed;
................................................................................
  2524   2531         ** (2) the entire header was used but not all data was used
  2525   2532         ** (3) the end of the data extends beyond the end of the record.
  2526   2533         */
  2527   2534         if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
  2528   2535          || (offset64 > pC->payloadSize)
  2529   2536         ){
  2530   2537           rc = SQLITE_CORRUPT_BKPT;
  2531         -        goto op_column_error;
         2538  +        goto abort_due_to_error;
  2532   2539         }
  2533   2540       }else{
  2534   2541         t = 0;
  2535   2542       }
  2536   2543   
  2537   2544       /* If after trying to extract new entries from the header, nHdrParsed is
  2538   2545       ** still not up to p2, that means that the record has fewer than p2
................................................................................
  2597   2604         ** So we might as well use bogus content rather than reading
  2598   2605         ** content from disk. */
  2599   2606         static u8 aZero[8];  /* This is the bogus content */
  2600   2607         sqlite3VdbeSerialGet(aZero, t, pDest);
  2601   2608       }else{
  2602   2609         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
  2603   2610                                      pDest);
  2604         -      if( rc==SQLITE_OK ){
  2605         -        sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
  2606         -        pDest->flags &= ~MEM_Ephem;
  2607         -      }
         2611  +      if( rc!=SQLITE_OK ) goto abort_due_to_error;
         2612  +      sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
         2613  +      pDest->flags &= ~MEM_Ephem;
  2608   2614       }
  2609   2615     }
  2610   2616   
  2611   2617   op_column_out:
  2612         -op_column_error:
  2613   2618     UPDATE_MAX_BLOBSIZE(pDest);
  2614   2619     REGISTER_TRACE(pOp->p3, pDest);
  2615   2620     break;
  2616   2621   }
  2617   2622   
  2618   2623   /* Opcode: Affinity P1 P2 * P4 *
  2619   2624   ** Synopsis: affinity(r[P1@P2])
................................................................................
  2810   2815     BtCursor *pCrsr;
  2811   2816   
  2812   2817     assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
  2813   2818     pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
  2814   2819     assert( pCrsr );
  2815   2820     nEntry = 0;  /* Not needed.  Only used to silence a warning. */
  2816   2821     rc = sqlite3BtreeCount(pCrsr, &nEntry);
         2822  +  if( rc ) goto abort_due_to_error;
  2817   2823     pOut = out2Prerelease(p, pOp);
  2818   2824     pOut->u.i = nEntry;
  2819   2825     break;
  2820   2826   }
  2821   2827   #endif
  2822   2828   
  2823   2829   /* Opcode: Savepoint P1 * * P4 *
................................................................................
  2987   2993   
  2988   2994         if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
  2989   2995           rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
  2990   2996           if( rc!=SQLITE_OK ) goto abort_due_to_error;
  2991   2997         }
  2992   2998       }
  2993   2999     }
         3000  +  if( rc ) goto abort_due_to_error;
  2994   3001   
  2995   3002     break;
  2996   3003   }
  2997   3004   
  2998   3005   /* Opcode: AutoCommit P1 P2 * * *
  2999   3006   **
  3000   3007   ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
................................................................................
  3023   3030       }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
  3024   3031         /* If this instruction implements a COMMIT and other VMs are writing
  3025   3032         ** return an error indicating that the other VMs must complete first. 
  3026   3033         */
  3027   3034         sqlite3VdbeError(p, "cannot commit transaction - "
  3028   3035                             "SQL statements in progress");
  3029   3036         rc = SQLITE_BUSY;
  3030         -      break;
         3037  +      goto abort_due_to_error;
  3031   3038       }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  3032   3039         goto vdbe_return;
  3033   3040       }else{
  3034   3041         db->autoCommit = (u8)desiredAutoCommit;
  3035   3042       }
  3036   3043       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
  3037   3044         p->pc = (int)(pOp - aOp);
................................................................................
  3050   3057     }else{
  3051   3058       sqlite3VdbeError(p,
  3052   3059           (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
  3053   3060           (iRollback)?"cannot rollback - no transaction is active":
  3054   3061                      "cannot commit - no transaction is active"));
  3055   3062            
  3056   3063       rc = SQLITE_ERROR;
         3064  +    goto abort_due_to_error;
  3057   3065     }
  3058   3066     break;
  3059   3067   }
  3060   3068   
  3061   3069   /* Opcode: Transaction P1 P2 P3 P4 P5
  3062   3070   **
  3063   3071   ** Begin a transaction on database P1 if a transaction is not already
................................................................................
  3172   3180       */
  3173   3181       if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
  3174   3182         sqlite3ResetOneSchema(db, pOp->p1);
  3175   3183       }
  3176   3184       p->expired = 1;
  3177   3185       rc = SQLITE_SCHEMA;
  3178   3186     }
         3187  +  if( rc ) goto abort_due_to_error;
  3179   3188     break;
  3180   3189   }
  3181   3190   
  3182   3191   /* Opcode: ReadCookie P1 P2 P3 * *
  3183   3192   **
  3184   3193   ** Read cookie number P3 from database P1 and write it into register P2.
  3185   3194   ** P3==1 is the schema version.  P3==2 is the database format.
................................................................................
  3241   3250     }
  3242   3251     if( pOp->p1==1 ){
  3243   3252       /* Invalidate all prepared statements whenever the TEMP database
  3244   3253       ** schema is changed.  Ticket #1644 */
  3245   3254       sqlite3ExpirePreparedStatements(db);
  3246   3255       p->expired = 0;
  3247   3256     }
         3257  +  if( rc ) goto abort_due_to_error;
  3248   3258     break;
  3249   3259   }
  3250   3260   
  3251   3261   /* Opcode: OpenRead P1 P2 P3 P4 P5
  3252   3262   ** Synopsis: root=P2 iDb=P3
  3253   3263   **
  3254   3264   ** Open a read-only cursor for the database table whose root page is
................................................................................
  3338   3348     assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
  3339   3349     assert( p->bIsReader );
  3340   3350     assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
  3341   3351             || p->readOnly==0 );
  3342   3352   
  3343   3353     if( p->expired ){
  3344   3354       rc = SQLITE_ABORT_ROLLBACK;
  3345         -    break;
         3355  +    goto abort_due_to_error;
  3346   3356     }
  3347   3357   
  3348   3358     nField = 0;
  3349   3359     pKeyInfo = 0;
  3350   3360     p2 = pOp->p2;
  3351   3361     iDb = pOp->p3;
  3352   3362     assert( iDb>=0 && iDb<db->nDb );
................................................................................
  3372   3382       assert( (pIn2->flags & MEM_Int)!=0 );
  3373   3383       sqlite3VdbeMemIntegerify(pIn2);
  3374   3384       p2 = (int)pIn2->u.i;
  3375   3385       /* The p2 value always comes from a prior OP_CreateTable opcode and
  3376   3386       ** that opcode will always set the p2 value to 2 or more or else fail.
  3377   3387       ** If there were a failure, the prepared statement would have halted
  3378   3388       ** before reaching this instruction. */
  3379         -    if( NEVER(p2<2) ) {
  3380         -      rc = SQLITE_CORRUPT_BKPT;
  3381         -      goto abort_due_to_error;
  3382         -    }
         3389  +    assert( p2>=2 );
  3383   3390     }
  3384   3391     if( pOp->p4type==P4_KEYINFO ){
  3385   3392       pKeyInfo = pOp->p4.pKeyInfo;
  3386   3393       assert( pKeyInfo->enc==ENC(db) );
  3387   3394       assert( pKeyInfo->db==db );
  3388   3395       nField = pKeyInfo->nField+pKeyInfo->nXField;
  3389   3396     }else if( pOp->p4type==P4_INT32 ){
................................................................................
  3413   3420     assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
  3414   3421     testcase( pOp->p5 & OPFLAG_BULKCSR );
  3415   3422   #ifdef SQLITE_ENABLE_CURSOR_HINTS
  3416   3423     testcase( pOp->p2 & OPFLAG_SEEKEQ );
  3417   3424   #endif
  3418   3425     sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
  3419   3426                                  (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
         3427  +  if( rc ) goto abort_due_to_error;
  3420   3428     break;
  3421   3429   }
  3422   3430   
  3423   3431   /* Opcode: OpenEphemeral P1 P2 * P4 P5
  3424   3432   ** Synopsis: nColumn=P2
  3425   3433   **
  3426   3434   ** Open a new cursor P1 to a transient table.
................................................................................
  3489   3497         pCx->isTable = 0;
  3490   3498       }else{
  3491   3499         rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR,
  3492   3500                                 0, pCx->uc.pCursor);
  3493   3501         pCx->isTable = 1;
  3494   3502       }
  3495   3503     }
         3504  +  if( rc ) goto abort_due_to_error;
  3496   3505     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3497   3506     break;
  3498   3507   }
  3499   3508   
  3500   3509   /* Opcode: SorterOpen P1 P2 P3 P4 *
  3501   3510   **
  3502   3511   ** This opcode works like OP_OpenEphemeral except that it opens
................................................................................
  3514   3523     assert( pOp->p2>=0 );
  3515   3524     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
  3516   3525     if( pCx==0 ) goto no_mem;
  3517   3526     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3518   3527     assert( pCx->pKeyInfo->db==db );
  3519   3528     assert( pCx->pKeyInfo->enc==ENC(db) );
  3520   3529     rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
         3530  +  if( rc ) goto abort_due_to_error;
  3521   3531     break;
  3522   3532   }
  3523   3533   
  3524   3534   /* Opcode: SequenceTest P1 P2 * * *
  3525   3535   ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
  3526   3536   **
  3527   3537   ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
................................................................................
  3976   3986           break;
  3977   3987         }
  3978   3988       }
  3979   3989     }
  3980   3990     rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
  3981   3991     sqlite3DbFree(db, pFree);
  3982   3992     if( rc!=SQLITE_OK ){
  3983         -    break;
         3993  +    goto abort_due_to_error;
  3984   3994     }
  3985   3995     pC->seekResult = res;
  3986   3996     alreadyExists = (res==0);
  3987   3997     pC->nullRow = 1-alreadyExists;
  3988   3998     pC->deferredMoveto = 0;
  3989   3999     pC->cacheStatus = CACHE_STALE;
  3990   4000     if( pOp->opcode==OP_Found ){
................................................................................
  4048   4058       assert( rc==SQLITE_OK );
  4049   4059       if( pOp->p2==0 ){
  4050   4060         rc = SQLITE_CORRUPT_BKPT;
  4051   4061       }else{
  4052   4062         goto jump_to_p2;
  4053   4063       }
  4054   4064     }
         4065  +  if( rc ) goto abort_due_to_error;
  4055   4066     break;
  4056   4067   }
  4057   4068   
  4058   4069   /* Opcode: Sequence P1 P2 * * *
  4059   4070   ** Synopsis: r[P2]=cursor[P1].ctr++
  4060   4071   **
  4061   4072   ** Find the next available sequence number for cursor P1.
................................................................................
  4190   4201         do{
  4191   4202           sqlite3_randomness(sizeof(v), &v);
  4192   4203           v &= (MAX_ROWID>>1); v++;  /* Ensure that v is greater than zero */
  4193   4204         }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
  4194   4205                                                    0, &res))==SQLITE_OK)
  4195   4206               && (res==0)
  4196   4207               && (++cnt<100));
  4197         -      if( rc==SQLITE_OK && res==0 ){
         4208  +      if( rc ) goto abort_due_to_error;
         4209  +      if( res==0 ){
  4198   4210           rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
  4199   4211           goto abort_due_to_error;
  4200   4212         }
  4201   4213         assert( v>0 );  /* EV: R-40812-03570 */
  4202   4214       }
  4203   4215       pC->deferredMoveto = 0;
  4204   4216       pC->cacheStatus = CACHE_STALE;
................................................................................
  4304   4316                             pData->z, pData->n, nZero,
  4305   4317                             (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
  4306   4318     );
  4307   4319     pC->deferredMoveto = 0;
  4308   4320     pC->cacheStatus = CACHE_STALE;
  4309   4321   
  4310   4322     /* Invoke the update-hook if required. */
  4311         -  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
         4323  +  if( rc ) goto abort_due_to_error;
         4324  +  if( db->xUpdateCallback && pOp->p4.z ){
  4312   4325       zDb = db->aDb[pC->iDb].zName;
  4313   4326       zTbl = pOp->p4.z;
  4314   4327       op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
  4315   4328       assert( pC->isTable );
  4316   4329       db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
  4317   4330       assert( pC->iDb>=0 );
  4318   4331     }
................................................................................
  4394   4407     }
  4395   4408   #endif
  4396   4409   
  4397   4410     rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
  4398   4411     pC->cacheStatus = CACHE_STALE;
  4399   4412   
  4400   4413     /* Invoke the update-hook if required. */
  4401         -  if( rc==SQLITE_OK && hasUpdateCallback ){
         4414  +  if( rc ) goto abort_due_to_error;
         4415  +  if( hasUpdateCallback ){
  4402   4416       db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
  4403   4417                           db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
  4404   4418       assert( pC->iDb>=0 );
  4405   4419     }
  4406   4420     if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
  4407   4421     break;
  4408   4422   }
................................................................................
  4443   4457     assert( isSorter(pC) );
  4444   4458     assert( pOp->p4type==P4_INT32 );
  4445   4459     pIn3 = &aMem[pOp->p3];
  4446   4460     nKeyCol = pOp->p4.i;
  4447   4461     res = 0;
  4448   4462     rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
  4449   4463     VdbeBranchTaken(res!=0,2);
         4464  +  if( rc ) goto abort_due_to_error;
  4450   4465     if( res ) goto jump_to_p2;
  4451   4466     break;
  4452   4467   };
  4453   4468   
  4454   4469   /* Opcode: SorterData P1 P2 P3 * *
  4455   4470   ** Synopsis: r[P2]=data
  4456   4471   **
................................................................................
  4468   4483   
  4469   4484     pOut = &aMem[pOp->p2];
  4470   4485     pC = p->apCsr[pOp->p1];
  4471   4486     assert( isSorter(pC) );
  4472   4487     rc = sqlite3VdbeSorterRowkey(pC, pOut);
  4473   4488     assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
  4474   4489     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
         4490  +  if( rc ) goto abort_due_to_error;
  4475   4491     p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
  4476   4492     break;
  4477   4493   }
  4478   4494   
  4479   4495   /* Opcode: RowData P1 P2 * * *
  4480   4496   ** Synopsis: r[P2]=data
  4481   4497   **
................................................................................
  4556   4572     pOut->n = n;
  4557   4573     MemSetTypeFlag(pOut, MEM_Blob);
  4558   4574     if( pC->isTable==0 ){
  4559   4575       rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  4560   4576     }else{
  4561   4577       rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
  4562   4578     }
         4579  +  if( rc ) goto abort_due_to_error;
  4563   4580     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
  4564   4581     UPDATE_MAX_BLOBSIZE(pOut);
  4565   4582     REGISTER_TRACE(pOp->p2, pOut);
  4566   4583     break;
  4567   4584   }
  4568   4585   
  4569   4586   /* Opcode: Rowid P1 P2 * * *
................................................................................
  4596   4613     }else if( pC->eCurType==CURTYPE_VTAB ){
  4597   4614       assert( pC->uc.pVCur!=0 );
  4598   4615       pVtab = pC->uc.pVCur->pVtab;
  4599   4616       pModule = pVtab->pModule;
  4600   4617       assert( pModule->xRowid );
  4601   4618       rc = pModule->xRowid(pC->uc.pVCur, &v);
  4602   4619       sqlite3VtabImportErrmsg(p, pVtab);
         4620  +    if( rc ) goto abort_due_to_error;
  4603   4621   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  4604   4622     }else{
  4605   4623       assert( pC->eCurType==CURTYPE_BTREE );
  4606   4624       assert( pC->uc.pCursor!=0 );
  4607   4625       rc = sqlite3VdbeCursorRestore(pC);
  4608   4626       if( rc ) goto abort_due_to_error;
  4609   4627       if( pC->nullRow ){
................................................................................
  4666   4684     pC->nullRow = (u8)res;
  4667   4685     pC->deferredMoveto = 0;
  4668   4686     pC->cacheStatus = CACHE_STALE;
  4669   4687     pC->seekResult = pOp->p3;
  4670   4688   #ifdef SQLITE_DEBUG
  4671   4689     pC->seekOp = OP_Last;
  4672   4690   #endif
         4691  +  if( rc ) goto abort_due_to_error;
  4673   4692     if( pOp->p2>0 ){
  4674   4693       VdbeBranchTaken(res!=0,2);
  4675   4694       if( res ) goto jump_to_p2;
  4676   4695     }
  4677   4696     break;
  4678   4697   }
  4679   4698   
................................................................................
  4730   4749       assert( pC->eCurType==CURTYPE_BTREE );
  4731   4750       pCrsr = pC->uc.pCursor;
  4732   4751       assert( pCrsr );
  4733   4752       rc = sqlite3BtreeFirst(pCrsr, &res);
  4734   4753       pC->deferredMoveto = 0;
  4735   4754       pC->cacheStatus = CACHE_STALE;
  4736   4755     }
         4756  +  if( rc ) goto abort_due_to_error;
  4737   4757     pC->nullRow = (u8)res;
  4738   4758     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4739   4759     VdbeBranchTaken(res!=0,2);
  4740   4760     if( res ) goto jump_to_p2;
  4741   4761     break;
  4742   4762   }
  4743   4763   
................................................................................
  4842   4862          || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
  4843   4863          || pC->seekOp==OP_Last );
  4844   4864   
  4845   4865     rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
  4846   4866   next_tail:
  4847   4867     pC->cacheStatus = CACHE_STALE;
  4848   4868     VdbeBranchTaken(res==0,2);
         4869  +  if( rc ) goto abort_due_to_error;
  4849   4870     if( res==0 ){
  4850   4871       pC->nullRow = 0;
  4851   4872       p->aCounter[pOp->p5]++;
  4852   4873   #ifdef SQLITE_TEST
  4853   4874       sqlite3_search_count++;
  4854   4875   #endif
  4855   4876       goto jump_to_p2_and_check_for_interrupt;
................................................................................
  4892   4913     assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
  4893   4914     pIn2 = &aMem[pOp->p2];
  4894   4915     assert( pIn2->flags & MEM_Blob );
  4895   4916     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  4896   4917     assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
  4897   4918     assert( pC->isTable==0 );
  4898   4919     rc = ExpandBlob(pIn2);
  4899         -  if( rc==SQLITE_OK ){
  4900         -    if( pOp->opcode==OP_SorterInsert ){
  4901         -      rc = sqlite3VdbeSorterWrite(pC, pIn2);
  4902         -    }else{
  4903         -      nKey = pIn2->n;
  4904         -      zKey = pIn2->z;
  4905         -      rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, 
  4906         -          ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
  4907         -          );
  4908         -      assert( pC->deferredMoveto==0 );
  4909         -      pC->cacheStatus = CACHE_STALE;
  4910         -    }
         4920  +  if( rc ) goto abort_due_to_error;
         4921  +  if( pOp->opcode==OP_SorterInsert ){
         4922  +    rc = sqlite3VdbeSorterWrite(pC, pIn2);
         4923  +  }else{
         4924  +    nKey = pIn2->n;
         4925  +    zKey = pIn2->z;
         4926  +    rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, 
         4927  +        ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
         4928  +        );
         4929  +    assert( pC->deferredMoveto==0 );
         4930  +    pC->cacheStatus = CACHE_STALE;
  4911   4931     }
         4932  +  if( rc) goto abort_due_to_error;
  4912   4933     break;
  4913   4934   }
  4914   4935   
  4915   4936   /* Opcode: IdxDelete P1 P2 P3 * *
  4916   4937   ** Synopsis: key=r[P2@P3]
  4917   4938   **
  4918   4939   ** The content of P3 registers starting at register P2 form
................................................................................
  4935   4956     assert( pCrsr!=0 );
  4936   4957     assert( pOp->p5==0 );
  4937   4958     r.pKeyInfo = pC->pKeyInfo;
  4938   4959     r.nField = (u16)pOp->p3;
  4939   4960     r.default_rc = 0;
  4940   4961     r.aMem = &aMem[pOp->p2];
  4941   4962     rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
  4942         -  if( rc==SQLITE_OK && res==0 ){
         4963  +  if( rc ) goto abort_due_to_error;
         4964  +  if( res==0 ){
  4943   4965       rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
         4966  +    if( rc ) goto abort_due_to_error;
  4944   4967     }
  4945   4968     assert( pC->deferredMoveto==0 );
  4946   4969     pC->cacheStatus = CACHE_STALE;
  4947   4970     break;
  4948   4971   }
  4949   4972   
  4950   4973   /* Opcode: Seek P1 * P3 P4 *
................................................................................
  5111   5134       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
  5112   5135       res = -res;
  5113   5136     }else{
  5114   5137       assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
  5115   5138       res++;
  5116   5139     }
  5117   5140     VdbeBranchTaken(res>0,2);
         5141  +  if( rc ) goto abort_due_to_error;
  5118   5142     if( res>0 ) goto jump_to_p2;
  5119   5143     break;
  5120   5144   }
  5121   5145   
  5122   5146   /* Opcode: Destroy P1 P2 P3 * *
  5123   5147   **
  5124   5148   ** Delete an entire database table or index whose root page in the database
................................................................................
  5146   5170     assert( p->readOnly==0 );
  5147   5171     assert( pOp->p1>1 );
  5148   5172     pOut = out2Prerelease(p, pOp);
  5149   5173     pOut->flags = MEM_Null;
  5150   5174     if( db->nVdbeRead > db->nVDestroy+1 ){
  5151   5175       rc = SQLITE_LOCKED;
  5152   5176       p->errorAction = OE_Abort;
         5177  +    goto abort_due_to_error;
  5153   5178     }else{
  5154   5179       iDb = pOp->p3;
  5155   5180       assert( DbMaskTest(p->btreeMask, iDb) );
  5156   5181       iMoved = 0;  /* Not needed.  Only to silence a warning. */
  5157   5182       rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
  5158   5183       pOut->flags = MEM_Int;
  5159   5184       pOut->u.i = iMoved;
         5185  +    if( rc ) goto abort_due_to_error;
  5160   5186   #ifndef SQLITE_OMIT_AUTOVACUUM
  5161         -    if( rc==SQLITE_OK && iMoved!=0 ){
         5187  +    if( iMoved!=0 ){
  5162   5188         sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
  5163   5189         /* All OP_Destroy operations occur on the same btree */
  5164   5190         assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
  5165   5191         resetSchemaOnFault = iDb+1;
  5166   5192       }
  5167   5193   #endif
  5168   5194     }
................................................................................
  5200   5226       p->nChange += nChange;
  5201   5227       if( pOp->p3>0 ){
  5202   5228         assert( memIsValid(&aMem[pOp->p3]) );
  5203   5229         memAboutToChange(p, &aMem[pOp->p3]);
  5204   5230         aMem[pOp->p3].u.i += nChange;
  5205   5231       }
  5206   5232     }
         5233  +  if( rc ) goto abort_due_to_error;
  5207   5234     break;
  5208   5235   }
  5209   5236   
  5210   5237   /* Opcode: ResetSorter P1 * * * *
  5211   5238   **
  5212   5239   ** Delete all contents from the ephemeral table or sorter
  5213   5240   ** that is open on cursor P1.
................................................................................
  5223   5250     assert( pC!=0 );
  5224   5251     if( isSorter(pC) ){
  5225   5252       sqlite3VdbeSorterReset(db, pC->uc.pSorter);
  5226   5253     }else{
  5227   5254       assert( pC->eCurType==CURTYPE_BTREE );
  5228   5255       assert( pC->isEphemeral );
  5229   5256       rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
         5257  +    if( rc ) goto abort_due_to_error;
  5230   5258     }
  5231   5259     break;
  5232   5260   }
  5233   5261   
  5234   5262   /* Opcode: CreateTable P1 P2 * * *
  5235   5263   ** Synopsis: r[P2]=root iDb=P1
  5236   5264   **
................................................................................
  5271   5299     if( pOp->opcode==OP_CreateTable ){
  5272   5300       /* flags = BTREE_INTKEY; */
  5273   5301       flags = BTREE_INTKEY;
  5274   5302     }else{
  5275   5303       flags = BTREE_BLOBKEY;
  5276   5304     }
  5277   5305     rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
         5306  +  if( rc ) goto abort_due_to_error;
  5278   5307     pOut->u.i = pgno;
  5279   5308     break;
  5280   5309   }
  5281   5310   
  5282   5311   /* Opcode: ParseSchema P1 * * P4 *
  5283   5312   **
  5284   5313   ** Read and parse all entries from the SQLITE_MASTER table of database P1
................................................................................
  5323   5352         assert( !db->mallocFailed );
  5324   5353         rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
  5325   5354         if( rc==SQLITE_OK ) rc = initData.rc;
  5326   5355         sqlite3DbFree(db, zSql);
  5327   5356         db->init.busy = 0;
  5328   5357       }
  5329   5358     }
  5330         -  if( rc ) sqlite3ResetAllSchemasOfConnection(db);
  5331         -  if( rc==SQLITE_NOMEM ){
  5332         -    goto no_mem;
         5359  +  if( rc ){
         5360  +    sqlite3ResetAllSchemasOfConnection(db);
         5361  +    if( rc==SQLITE_NOMEM ){
         5362  +      goto no_mem;
         5363  +    }
         5364  +    goto abort_due_to_error;
  5333   5365     }
  5334   5366     break;  
  5335   5367   }
  5336   5368   
  5337   5369   #if !defined(SQLITE_OMIT_ANALYZE)
  5338   5370   /* Opcode: LoadAnalysis P1 * * * *
  5339   5371   **
................................................................................
  5340   5372   ** Read the sqlite_stat1 table for database P1 and load the content
  5341   5373   ** of that table into the internal index hash table.  This will cause
  5342   5374   ** the analysis to be used when preparing all subsequent queries.
  5343   5375   */
  5344   5376   case OP_LoadAnalysis: {
  5345   5377     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5346   5378     rc = sqlite3AnalysisLoad(db, pOp->p1);
         5379  +  if( rc ) goto abort_due_to_error;
  5347   5380     break;  
  5348   5381   }
  5349   5382   #endif /* !defined(SQLITE_OMIT_ANALYZE) */
  5350   5383   
  5351   5384   /* Opcode: DropTable P1 * * P4 *
  5352   5385   **
  5353   5386   ** Remove the internal (in-memory) data structures that describe
................................................................................
  5599   5632       for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
  5600   5633       if( pFrame ) break;
  5601   5634     }
  5602   5635   
  5603   5636     if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
  5604   5637       rc = SQLITE_ERROR;
  5605   5638       sqlite3VdbeError(p, "too many levels of trigger recursion");
  5606         -    break;
         5639  +    goto abort_due_to_error;
  5607   5640     }
  5608   5641   
  5609   5642     /* Register pRt is used to store the memory required to save the state
  5610   5643     ** of the current program, and the memory required at runtime to execute
  5611   5644     ** the trigger program. If this trigger has been fired before, then pRt 
  5612   5645     ** is already allocated. Otherwise, it must be initialized.  */
  5613   5646     if( (pRt->flags&MEM_Frame)==0 ){
................................................................................
  5963   5996     (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
  5964   5997     if( pCtx->fErrorOrAux ){
  5965   5998       if( pCtx->isError ){
  5966   5999         sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
  5967   6000         rc = pCtx->isError;
  5968   6001       }
  5969   6002       sqlite3VdbeMemRelease(&t);
         6003  +    if( rc ) goto abort_due_to_error;
  5970   6004     }else{
  5971   6005       assert( t.flags==MEM_Null );
  5972   6006     }
  5973   6007     if( pCtx->skipFlag ){
  5974   6008       assert( pOp[-1].opcode==OP_CollSeq );
  5975   6009       i = pOp[-1].p1;
  5976   6010       if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
................................................................................
  5995   6029     Mem *pMem;
  5996   6030     assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
  5997   6031     pMem = &aMem[pOp->p1];
  5998   6032     assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  5999   6033     rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
  6000   6034     if( rc ){
  6001   6035       sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
         6036  +    goto abort_due_to_error;
  6002   6037     }
  6003   6038     sqlite3VdbeChangeEncoding(pMem, encoding);
  6004   6039     UPDATE_MAX_BLOBSIZE(pMem);
  6005   6040     if( sqlite3VdbeMemTooBig(pMem) ){
  6006   6041       goto too_big;
  6007   6042     }
  6008   6043     break;
................................................................................
  6030   6065     aRes[1] = aRes[2] = -1;
  6031   6066     assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
  6032   6067          || pOp->p2==SQLITE_CHECKPOINT_FULL
  6033   6068          || pOp->p2==SQLITE_CHECKPOINT_RESTART
  6034   6069          || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
  6035   6070     );
  6036   6071     rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
  6037         -  if( rc==SQLITE_BUSY ){
         6072  +  if( rc ){
         6073  +    if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
  6038   6074       rc = SQLITE_OK;
  6039   6075       aRes[0] = 1;
  6040   6076     }
  6041   6077     for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
  6042   6078       sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
  6043   6079     }    
  6044   6080     break;
................................................................................
  6103   6139     ){
  6104   6140       if( !db->autoCommit || db->nVdbeRead>1 ){
  6105   6141         rc = SQLITE_ERROR;
  6106   6142         sqlite3VdbeError(p,
  6107   6143             "cannot change %s wal mode from within a transaction",
  6108   6144             (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
  6109   6145         );
  6110         -      break;
         6146  +      goto abort_due_to_error;
  6111   6147       }else{
  6112   6148    
  6113   6149         if( eOld==PAGER_JOURNALMODE_WAL ){
  6114   6150           /* If leaving WAL mode, close the log file. If successful, the call
  6115   6151           ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 
  6116   6152           ** file. An EXCLUSIVE lock may still be held on the database file 
  6117   6153           ** after a successful return. 
................................................................................
  6133   6169         if( rc==SQLITE_OK ){
  6134   6170           rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
  6135   6171         }
  6136   6172       }
  6137   6173     }
  6138   6174   #endif /* ifndef SQLITE_OMIT_WAL */
  6139   6175   
  6140         -  if( rc ){
  6141         -    eNew = eOld;
  6142         -  }
         6176  +  if( rc ) eNew = eOld;
  6143   6177     eNew = sqlite3PagerSetJournalMode(pPager, eNew);
  6144   6178   
  6145   6179     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  6146   6180     pOut->z = (char *)sqlite3JournalModename(eNew);
  6147   6181     pOut->n = sqlite3Strlen30(pOut->z);
  6148   6182     pOut->enc = SQLITE_UTF8;
  6149   6183     sqlite3VdbeChangeEncoding(pOut, encoding);
         6184  +  if( rc ) goto abort_due_to_error;
  6150   6185     break;
  6151   6186   };
  6152   6187   #endif /* SQLITE_OMIT_PRAGMA */
  6153   6188   
  6154   6189   #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
  6155   6190   /* Opcode: Vacuum * * * * *
  6156   6191   **
................................................................................
  6157   6192   ** Vacuum the entire database.  This opcode will cause other virtual
  6158   6193   ** machines to be created and run.  It may not be called from within
  6159   6194   ** a transaction.
  6160   6195   */
  6161   6196   case OP_Vacuum: {
  6162   6197     assert( p->readOnly==0 );
  6163   6198     rc = sqlite3RunVacuum(&p->zErrMsg, db);
         6199  +  if( rc ) goto abort_due_to_error;
  6164   6200     break;
  6165   6201   }
  6166   6202   #endif
  6167   6203   
  6168   6204   #if !defined(SQLITE_OMIT_AUTOVACUUM)
  6169   6205   /* Opcode: IncrVacuum P1 P2 * * *
  6170   6206   **
................................................................................
  6177   6213   
  6178   6214     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  6179   6215     assert( DbMaskTest(p->btreeMask, pOp->p1) );
  6180   6216     assert( p->readOnly==0 );
  6181   6217     pBt = db->aDb[pOp->p1].pBt;
  6182   6218     rc = sqlite3BtreeIncrVacuum(pBt);
  6183   6219     VdbeBranchTaken(rc==SQLITE_DONE,2);
  6184         -  if( rc==SQLITE_DONE ){
         6220  +  if( rc ){
         6221  +    if( rc!=SQLITE_DONE ) goto abort_due_to_error;
  6185   6222       rc = SQLITE_OK;
  6186   6223       goto jump_to_p2;
  6187   6224     }
  6188   6225     break;
  6189   6226   }
  6190   6227   #endif
  6191   6228   
................................................................................
  6228   6265     u8 isWriteLock = (u8)pOp->p3;
  6229   6266     if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
  6230   6267       int p1 = pOp->p1; 
  6231   6268       assert( p1>=0 && p1<db->nDb );
  6232   6269       assert( DbMaskTest(p->btreeMask, p1) );
  6233   6270       assert( isWriteLock==0 || isWriteLock==1 );
  6234   6271       rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
  6235         -    if( (rc&0xFF)==SQLITE_LOCKED ){
  6236         -      const char *z = pOp->p4.z;
  6237         -      sqlite3VdbeError(p, "database table is locked: %s", z);
         6272  +    if( rc ){
         6273  +      if( (rc&0xFF)==SQLITE_LOCKED ){
         6274  +        const char *z = pOp->p4.z;
         6275  +        sqlite3VdbeError(p, "database table is locked: %s", z);
         6276  +      }
         6277  +      goto abort_due_to_error;
  6238   6278       }
  6239   6279     }
  6240   6280     break;
  6241   6281   }
  6242   6282   #endif /* SQLITE_OMIT_SHARED_CACHE */
  6243   6283   
  6244   6284   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
  6252   6292   ** code will be set to SQLITE_LOCKED.
  6253   6293   */
  6254   6294   case OP_VBegin: {
  6255   6295     VTable *pVTab;
  6256   6296     pVTab = pOp->p4.pVtab;
  6257   6297     rc = sqlite3VtabBegin(db, pVTab);
  6258   6298     if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
         6299  +  if( rc ) goto abort_due_to_error;
  6259   6300     break;
  6260   6301   }
  6261   6302   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6262   6303   
  6263   6304   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6264   6305   /* Opcode: VCreate P1 P2 * * *
  6265   6306   **
................................................................................
  6280   6321     assert( rc==SQLITE_OK );
  6281   6322     zTab = (const char*)sqlite3_value_text(&sMem);
  6282   6323     assert( zTab || db->mallocFailed );
  6283   6324     if( zTab ){
  6284   6325       rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
  6285   6326     }
  6286   6327     sqlite3VdbeMemRelease(&sMem);
         6328  +  if( rc ) goto abort_due_to_error;
  6287   6329     break;
  6288   6330   }
  6289   6331   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6290   6332   
  6291   6333   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6292   6334   /* Opcode: VDestroy P1 * * P4 *
  6293   6335   **
................................................................................
  6294   6336   ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
  6295   6337   ** of that table.
  6296   6338   */
  6297   6339   case OP_VDestroy: {
  6298   6340     db->nVDestroy++;
  6299   6341     rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
  6300   6342     db->nVDestroy--;
         6343  +  if( rc ) goto abort_due_to_error;
  6301   6344     break;
  6302   6345   }
  6303   6346   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6304   6347   
  6305   6348   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6306   6349   /* Opcode: VOpen P1 * * P4 *
  6307   6350   **
................................................................................
  6317   6360   
  6318   6361     assert( p->bIsReader );
  6319   6362     pCur = 0;
  6320   6363     pVCur = 0;
  6321   6364     pVtab = pOp->p4.pVtab->pVtab;
  6322   6365     if( pVtab==0 || NEVER(pVtab->pModule==0) ){
  6323   6366       rc = SQLITE_LOCKED;
  6324         -    break;
         6367  +    goto abort_due_to_error;
  6325   6368     }
  6326   6369     pModule = pVtab->pModule;
  6327   6370     rc = pModule->xOpen(pVtab, &pVCur);
  6328   6371     sqlite3VtabImportErrmsg(p, pVtab);
  6329         -  if( SQLITE_OK==rc ){
  6330         -    /* Initialize sqlite3_vtab_cursor base class */
  6331         -    pVCur->pVtab = pVtab;
         6372  +  if( rc ) goto abort_due_to_error;
  6332   6373   
  6333         -    /* Initialize vdbe cursor object */
  6334         -    pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
  6335         -    if( pCur ){
  6336         -      pCur->uc.pVCur = pVCur;
  6337         -      pVtab->nRef++;
  6338         -    }else{
  6339         -      assert( db->mallocFailed );
  6340         -      pModule->xClose(pVCur);
  6341         -      goto no_mem;
  6342         -    }
         6374  +  /* Initialize sqlite3_vtab_cursor base class */
         6375  +  pVCur->pVtab = pVtab;
         6376  +
         6377  +  /* Initialize vdbe cursor object */
         6378  +  pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
         6379  +  if( pCur ){
         6380  +    pCur->uc.pVCur = pVCur;
         6381  +    pVtab->nRef++;
         6382  +  }else{
         6383  +    assert( db->mallocFailed );
         6384  +    pModule->xClose(pVCur);
         6385  +    goto no_mem;
  6343   6386     }
  6344   6387     break;
  6345   6388   }
  6346   6389   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6347   6390   
  6348   6391   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6349   6392   /* Opcode: VFilter P1 P2 P3 P4 *
................................................................................
  6397   6440     res = 0;
  6398   6441     apArg = p->apArg;
  6399   6442     for(i = 0; i<nArg; i++){
  6400   6443       apArg[i] = &pArgc[i+1];
  6401   6444     }
  6402   6445     rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
  6403   6446     sqlite3VtabImportErrmsg(p, pVtab);
  6404         -  if( rc==SQLITE_OK ){
  6405         -    res = pModule->xEof(pVCur);
  6406         -  }
         6447  +  if( rc ) goto abort_due_to_error;
         6448  +  res = pModule->xEof(pVCur);
  6407   6449     pCur->nullRow = 0;
  6408   6450     VdbeBranchTaken(res!=0,2);
  6409   6451     if( res ) goto jump_to_p2;
  6410   6452     break;
  6411   6453   }
  6412   6454   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6413   6455   
................................................................................
  6448   6490     sqlite3VdbeChangeEncoding(pDest, encoding);
  6449   6491     REGISTER_TRACE(pOp->p3, pDest);
  6450   6492     UPDATE_MAX_BLOBSIZE(pDest);
  6451   6493   
  6452   6494     if( sqlite3VdbeMemTooBig(pDest) ){
  6453   6495       goto too_big;
  6454   6496     }
         6497  +  if( rc ) goto abort_due_to_error;
  6455   6498     break;
  6456   6499   }
  6457   6500   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6458   6501   
  6459   6502   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6460   6503   /* Opcode: VNext P1 P2 * * *
  6461   6504   **
................................................................................
  6483   6526     ** underlying implementation to return an error if one occurs during
  6484   6527     ** xNext(). Instead, if an error occurs, true is returned (indicating that 
  6485   6528     ** data is available) and the error code returned when xColumn or
  6486   6529     ** some other method is next invoked on the save virtual table cursor.
  6487   6530     */
  6488   6531     rc = pModule->xNext(pCur->uc.pVCur);
  6489   6532     sqlite3VtabImportErrmsg(p, pVtab);
  6490         -  if( rc==SQLITE_OK ){
  6491         -    res = pModule->xEof(pCur->uc.pVCur);
  6492         -  }
         6533  +  if( rc ) goto abort_due_to_error;
         6534  +  res = pModule->xEof(pCur->uc.pVCur);
  6493   6535     VdbeBranchTaken(!res,2);
  6494   6536     if( !res ){
  6495   6537       /* If there is data, jump to P2 */
  6496   6538       goto jump_to_p2_and_check_for_interrupt;
  6497   6539     }
  6498   6540     goto check_for_interrupt;
  6499   6541   }
................................................................................
  6517   6559     assert( p->readOnly==0 );
  6518   6560     REGISTER_TRACE(pOp->p1, pName);
  6519   6561     assert( pName->flags & MEM_Str );
  6520   6562     testcase( pName->enc==SQLITE_UTF8 );
  6521   6563     testcase( pName->enc==SQLITE_UTF16BE );
  6522   6564     testcase( pName->enc==SQLITE_UTF16LE );
  6523   6565     rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
  6524         -  if( rc==SQLITE_OK ){
  6525         -    rc = pVtab->pModule->xRename(pVtab, pName->z);
  6526         -    sqlite3VtabImportErrmsg(p, pVtab);
  6527         -    p->expired = 0;
  6528         -  }
         6566  +  if( rc ) goto abort_due_to_error;
         6567  +  rc = pVtab->pModule->xRename(pVtab, pName->z);
         6568  +  sqlite3VtabImportErrmsg(p, pVtab);
         6569  +  p->expired = 0;
         6570  +  if( rc ) goto abort_due_to_error;
  6529   6571     break;
  6530   6572   }
  6531   6573   #endif
  6532   6574   
  6533   6575   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6534   6576   /* Opcode: VUpdate P1 P2 P3 P4 P5
  6535   6577   ** Synopsis: data=r[P3@P2]
................................................................................
  6570   6612     assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback 
  6571   6613          || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
  6572   6614     );
  6573   6615     assert( p->readOnly==0 );
  6574   6616     pVtab = pOp->p4.pVtab->pVtab;
  6575   6617     if( pVtab==0 || NEVER(pVtab->pModule==0) ){
  6576   6618       rc = SQLITE_LOCKED;
  6577         -    break;
         6619  +    goto abort_due_to_error;
  6578   6620     }
  6579   6621     pModule = pVtab->pModule;
  6580   6622     nArg = pOp->p2;
  6581   6623     assert( pOp->p4type==P4_VTAB );
  6582   6624     if( ALWAYS(pModule->xUpdate) ){
  6583   6625       u8 vtabOnConflict = db->vtabOnConflict;
  6584   6626       apArg = p->apArg;
................................................................................
  6602   6644           rc = SQLITE_OK;
  6603   6645         }else{
  6604   6646           p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
  6605   6647         }
  6606   6648       }else{
  6607   6649         p->nChange++;
  6608   6650       }
         6651  +    if( rc ) goto abort_due_to_error;
  6609   6652     }
  6610   6653     break;
  6611   6654   }
  6612   6655   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6613   6656   
  6614   6657   #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
  6615   6658   /* Opcode: Pagecount P1 P2 * * *
................................................................................
  6773   6816   #endif  /* SQLITE_DEBUG */
  6774   6817   #endif  /* NDEBUG */
  6775   6818     }  /* The end of the for(;;) loop the loops through opcodes */
  6776   6819   
  6777   6820     /* If we reach this point, it means that execution is finished with
  6778   6821     ** an error of some kind.
  6779   6822     */
  6780         -vdbe_error_halt:
         6823  +abort_due_to_error:
         6824  +  if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
  6781   6825     assert( rc );
         6826  +  if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
         6827  +    sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
         6828  +  }
  6782   6829     p->rc = rc;
  6783   6830     testcase( sqlite3GlobalConfig.xLog!=0 );
  6784   6831     sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
  6785   6832                      (int)(pOp - aOp), p->zSql, p->zErrMsg);
  6786   6833     sqlite3VdbeHalt(p);
  6787   6834     if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
  6788   6835     rc = SQLITE_ERROR;
................................................................................
  6805   6852   
  6806   6853     /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
  6807   6854     ** is encountered.
  6808   6855     */
  6809   6856   too_big:
  6810   6857     sqlite3VdbeError(p, "string or blob too big");
  6811   6858     rc = SQLITE_TOOBIG;
  6812         -  goto vdbe_error_halt;
         6859  +  goto abort_due_to_error;
  6813   6860   
  6814   6861     /* Jump to here if a malloc() fails.
  6815   6862     */
  6816   6863   no_mem:
  6817   6864     sqlite3OomFault(db);
  6818   6865     sqlite3VdbeError(p, "out of memory");
  6819   6866     rc = SQLITE_NOMEM_BKPT;
  6820         -  goto vdbe_error_halt;
  6821         -
  6822         -  /* Jump to here for any other kind of fatal error.  The "rc" variable
  6823         -  ** should hold the error number.
  6824         -  */
  6825         -abort_due_to_error:
  6826         -  assert( p->zErrMsg==0 );
  6827         -  if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
  6828         -  if( rc!=SQLITE_IOERR_NOMEM ){
  6829         -    sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
  6830         -  }
  6831         -  goto vdbe_error_halt;
         6867  +  goto abort_due_to_error;
  6832   6868   
  6833   6869     /* Jump to here if the sqlite3_interrupt() API sets the interrupt
  6834   6870     ** flag.
  6835   6871     */
  6836   6872   abort_due_to_interrupt:
  6837   6873     assert( db->u1.isInterrupted );
  6838   6874     rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
  6839   6875     p->rc = rc;
  6840   6876     sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
  6841         -  goto vdbe_error_halt;
         6877  +  goto abort_due_to_error;
  6842   6878   }