/ Check-in [99c02aee]
Login

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

Overview
Comment:Merge session fixes from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | memdb
Files: files | file ages | folders
SHA3-256: 99c02aeecf47bfa5be5ce38a7ec20818ea0f9fb960b136184e1cb6f8fb8b70e5
User & Date: drh 2018-03-05 18:20:35
Context
2018-03-05
21:19
Merge the session fix from trunk. check-in: 6274cf1f user: drh tags: memdb
18:20
Merge session fixes from trunk. check-in: 99c02aee user: drh tags: memdb
2018-03-02
20:00
In sqlite3_checker, wrap the call to "SELECT checkfreelist()" in a transaction. check-in: 02906e55 user: dan tags: trunk
2018-03-01
22:18
Allow the zSchema argument to sqlite3_serialize() to be NULL to mean the main database. check-in: 5b01b991 user: drh tags: memdb
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/repair/sqlite3_checker.tcl.

   216    216     puts stderr "Cannot open datababase $root_filename: $res"
   217    217     exit 1
   218    218   }
   219    219   
   220    220   if {$bFreelistCheck || $bAll} {
   221    221     puts -nonewline "freelist-check: "
   222    222     flush stdout
          223  +  db eval BEGIN
   223    224     puts [db one {SELECT checkfreelist('main')}]
          225  +  db eval END
   224    226   }
   225    227   if {$bSummary} {
   226    228     set scale 0
   227    229     set pgsz [db one {PRAGMA page_size}]
   228    230     db eval {SELECT nPage*$pgsz AS sz, name, tbl_name
   229    231                FROM sqlite_btreeinfo
   230    232               WHERE type='index'

Changes to ext/session/session4.test.

    70     70     CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
    71     71     CREATE TABLE t2(e TEXT PRIMARY KEY NOT NULL,f,g);
    72     72     CREATE TABLE t3(w REAL PRIMARY KEY NOT NULL,x,y);
    73     73     CREATE TABLE t4(z PRIMARY KEY) WITHOUT ROWID;
    74     74   }
    75     75   
    76     76   foreach {tn blob} {
    77         -  1 {54010174340012000000}
    78         -  2 {54fefe8bcb0012000300}
    79         -  3 {5480809280808001017434001200fb}
    80         -  4 {50af9c939c9c9cb09c9c6400b09c9c6400}
    81         -  5 {12000300}
    82         -  6 {09847304}
    83         -  7 {5401017434001208}
    84         -} {
    85         -if {$tn==7} breakpoint
           77  +  1 54010174340012000000
           78  +  2 54fefe8bcb0012000300
           79  +  3 5480809280808001017434001200fb
           80  +  4 50af9c939c9c9cb09c9c6400b09c9c6400
           81  +  5 12000300
           82  +  6 09847304
           83  +  7 5401017434001208
           84  +  8 54010174340012fc0386868600
           85  +  9 54010174340012FC0386868600
           86  +  10 548894FEFE
           87  +  11 54010171340012E703ABFA7433FD1200
           88  +  12 540101743400120003FFED00010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           89  +  13 540101743400120003001200010000000000000002120002400C0000000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FC87797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           90  +  14 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F03FC87797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           91  +  15 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003FC8738790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           92  +  16 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A00540301000074320009000303783879010000000080000000020000000000000000090003FC87327902400C0000000000000304666F7572
           93  +  17 540101743400120003FFE3000412F7010000E600000000021202120002400C0000000000005B0401000000743100171C0304646F750002400C000000000000540401000000D3310017000100000000000000050100000000000378797A405403000002F10100000100000000000004090001000100000007030378797A0100000000000D0007000001000000002300000F1B0378797A405403013900743200090003038C3879010000000000000000000002120002400C0000000000005B0401000000743117170003047C5E00FF
           94  +  18 54010174340012000300120001000000E6FF100000120002401E00000000000054040100000074310017000100040000010000000000000004FFFF7FFF0000000000010000010000001000000007030378797A01000000000000000F000000000000FA0304666F7572
           95  +  19 540101743400120003001200010000000000000002121B02400C00000000000054040000000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378817A01000000000000000F000001000000000100000F030378797A005403010000743200090003FFE809000303780000000000000304666F7572
           96  +  20 5401017D3400120003001200010000000000000002120002400CFC00000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FFFF797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378326C02400C0000000000000304666F7572
           97  +  21 5401017434001200030012000100FFE20000000002120002400C00000000000054040100E0007431001700010000E99D000000020000000003FFE70009000303783279020004000001030000000000002117000003001700012701000100000000743100000100000000008000090003037F387901000000008000000002000000000400000009005303010A00FF7FFFFF00000000000304664F6572
           98  +  22 540101743400120003FFFF7FFF0000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100010000000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           99  +  23 540101742700120100120003F5FF0300
          100  +  24 5401017434E312540101743400120003FFFC00
          101  +  25 540101743400540101743D3D3D3D3D3D3D3D3D3D3D3D3D3400120003FFED000300
          102  +  26 5401017446EA5301743D1D3D3D01743D1D3D3DCF3D3D3D1A3D3D3D3D3400120003FFFF000000
          103  +  27 540101743400540101743D3D3D3D3D3D3D3D3D3D251000120003FF81000000000000
          104  +  28 540101340012000397FF3D7F3D3400120003001200540101743D3D3D3D3D3D393D3D3D12000300
          105  +  29 500174340050010F74340012000300120003FFE5
          106  +  30 5004007233E900177FEF0054257F0002EF001200031E12000300
          107  +  31 5001015001015252525250010174340012EF039A9A0100E351525D52525252525252525252525252525252525250010174340012EF039A0100009A9A9A9A9A9BA3B200120003010040743400
          108  +  32 5401017400123400120003FFFC00
          109  +  33 540101743400120003001200010000000000004002120002400C0000000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FC87797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
          110  +  34 54040100000074310017000100000002000015050100000000000000030100000000140000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A0054030100007432000900030378387901000000008E000000020000000000000000090003FFFF000002400C0000000000000304666F7572
          111  +  35 540101743400120003001200010000000000000002120002400C00000000000050060100000074310017000100000000000000050100000000000000030100000003001700010000666F7572
          112  +  36 540101743400120003001200010000000000000002120002400C00000000000050050100000074310017000100000000000000050100000000000000030100000003001700010000666F7572
          113  +  37 540101743400120003001200010000000000000002120002400C00000000000050040100008074310017000100000000000000050100000000000000030100000003001700010000666F7572
          114  +  38 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000000000000000000050100000000000000030100000003001700010000666F7572
          115  +  39 540101743400120003001200010000000000000002120002400C00000000000050040100018074310017000100000000000000050100000000000000030100000003001700010000666F7572
          116  +  40 540101743400120003001200010000000000000002120002400C0000000000005004FEFFFFFF74310017000100000000000000050100000000000000030100000003001700010000666F7572
          117  +  41 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000004000000000000050100000000000000030100000003001700010000666F7572
          118  +  42 540101743400120003001200010000000000000002120002400C0000000000005005FFFF050074310017000100000000000000050100000000000000030100000003001700010000666F7572
          119  +  43 540101743400120003001200010000000000000002120002400C000000000000500401006E0074310017000300000000001221050100000000000000030100000003001700010000666F7572
          120  +  44 540101743400120003001200010000000000020000120002400C00000000000050050100000074310017000100000000000000050100004000000000030100000025001700010000666F7572
          121  +  45 540101743400120003001200010000000000ECFF02120002400C000000000000500401F9FF00743100170001000000000000000500E1000000000000030100000003000000000000666F7572
          122  +  46 54010174340B0B0B0B0B0B0B0B0B0B0B0B0B0B0B00120003001200010000000000000002120002400C00000000000050040100000074310017010000000000000000050100FFE900000000030100000003007F00000000666F7572
          123  +  47 54010103001200010000000000020002120002400C0000000000005004010000F374310017000100000000000000050100000000000000030100000003001700010000666F8E72
          124  +  48 540101743400120003001200010000000000000002120002400C00000000000050030012000174310017000700000000000000050100002000000001000000000003001700010000666F7572
          125  +  49 540101743400120004001200010000000000000002120002400C0000000000005004010000FC733100170001000000000000000501000000000000000301000000F6FF17000100007C6F7572
          126  +  50 54010174FFDDFF8003001200010000100000000002120002400C000000000000500401000000743100170000000005010000000000000000000003010072
          127  +  51 540101743200120003001200010000000000000002120002400C00000000000050040100001074310017000000000003010000120300170100000000000000050100000000000000030100000003001700010000666F7572
          128  +  52 540101745401017434001200010000000000001702120002400C00000000000050040100001A74310017000100000000000100000100000000000000030100000003001700010000666F7572
          129  +  53 540101743400120003001200010000000000000002120002400C000000000000500401000000743100170001000002400C00000000000050040110000074310017000000000000050100000000000000030100000003001700010000666F7572
          130  +  54 540101743400120003001200010000000000000002120002400C000000000002120002400C00000000000050040100000074310017FF0050040100000074310017FF7F00000000000000050100000000000000030100000003001700010000666F7572
          131  +  55 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000100010080000001000000020003010100000300170100000003001700010000666F7572
          132  +} {
    86    133     do_test 2.$tn {
    87    134       set changeset [binary decode hex $blob]
          135  +#set fd [open x.change w+]
          136  +#fconfigure $fd -encoding binary -translation binary
          137  +#puts -nonewline $fd $changeset
          138  +#close $fd
    88    139       list [catch { sqlite3changeset_apply db $changeset xConflict } msg] $msg
    89    140     } {1 SQLITE_CORRUPT}
    90    141   }
    91    142   
    92    143   finish_test

Changes to ext/session/sqlite3session.c.

  2714   2714   ){
  2715   2715     int i;                          /* Used to iterate through columns */
  2716   2716     int rc = SQLITE_OK;
  2717   2717   
  2718   2718     for(i=0; i<nCol && rc==SQLITE_OK; i++){
  2719   2719       int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
  2720   2720       if( abPK && abPK[i]==0 ) continue;
  2721         -    if( pIn->iNext>=pIn->nData ){
  2722         -      rc = SQLITE_CORRUPT;
  2723         -    }else{
  2724         -      rc = sessionInputBuffer(pIn, 9);
  2725         -    }
         2721  +    rc = sessionInputBuffer(pIn, 9);
  2726   2722       if( rc==SQLITE_OK ){
  2727         -      eType = pIn->aData[pIn->iNext++];
  2728         -      assert( apOut[i]==0 );
  2729         -      if( eType ){
  2730         -        apOut[i] = sqlite3ValueNew(0);
  2731         -        if( !apOut[i] ) rc = SQLITE_NOMEM;
         2723  +      if( pIn->iNext>=pIn->nData ){
         2724  +        rc = SQLITE_CORRUPT_BKPT;
         2725  +      }else{
         2726  +        eType = pIn->aData[pIn->iNext++];
         2727  +        assert( apOut[i]==0 );
         2728  +        if( eType ){
         2729  +          apOut[i] = sqlite3ValueNew(0);
         2730  +          if( !apOut[i] ) rc = SQLITE_NOMEM;
         2731  +        }
  2732   2732         }
  2733   2733       }
  2734   2734   
  2735   2735       if( rc==SQLITE_OK ){
  2736   2736         u8 *aVal = &pIn->aData[pIn->iNext];
  2737   2737         if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
  2738   2738           int nByte;
  2739   2739           pIn->iNext += sessionVarintGet(aVal, &nByte);
  2740   2740           rc = sessionInputBuffer(pIn, nByte);
  2741   2741           if( rc==SQLITE_OK ){
  2742         -          u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
  2743         -          rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
         2742  +          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
         2743  +            rc = SQLITE_CORRUPT_BKPT;
         2744  +          }else{
         2745  +            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
         2746  +            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
         2747  +            pIn->iNext += nByte;
         2748  +          }
  2744   2749           }
  2745         -        pIn->iNext += nByte;
  2746   2750         }
  2747   2751         if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
  2748   2752           sqlite3_int64 v = sessionGetI64(aVal);
  2749   2753           if( eType==SQLITE_INTEGER ){
  2750   2754             sqlite3VdbeMemSetInt64(apOut[i], v);
  2751   2755           }else{
  2752   2756             double d;
................................................................................
  2778   2782     int rc = SQLITE_OK;
  2779   2783     int nCol = 0;
  2780   2784     int nRead = 0;
  2781   2785   
  2782   2786     rc = sessionInputBuffer(pIn, 9);
  2783   2787     if( rc==SQLITE_OK ){
  2784   2788       nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
  2785         -    rc = sessionInputBuffer(pIn, nRead+nCol+100);
  2786         -    nRead += nCol;
         2789  +    if( nCol<0 ){
         2790  +      rc = SQLITE_CORRUPT_BKPT;
         2791  +    }else{
         2792  +      rc = sessionInputBuffer(pIn, nRead+nCol+100);
         2793  +      nRead += nCol;
         2794  +    }
  2787   2795     }
  2788   2796   
  2789   2797     while( rc==SQLITE_OK ){
  2790   2798       while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
  2791   2799         nRead++;
  2792   2800       }
  2793   2801       if( (pIn->iNext + nRead)<pIn->nData ) break;
................................................................................
  2863   2871       if( p->nCol>0 ){
  2864   2872         nCopy -= nVarint;
  2865   2873         p->in.iNext += nVarint;
  2866   2874         nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
  2867   2875         p->tblhdr.nBuf = 0;
  2868   2876         sessionBufferGrow(&p->tblhdr, nByte, &rc);
  2869   2877       }else{
  2870         -      rc = SQLITE_CORRUPT;
         2878  +      rc = SQLITE_CORRUPT_BKPT;
  2871   2879       }
  2872   2880     }
  2873   2881   
  2874   2882     if( rc==SQLITE_OK ){
  2875   2883       int iPK = sizeof(sqlite3_value*)*p->nCol*2;
  2876   2884       memset(p->tblhdr.aBuf, 0, iPK);
  2877   2885       memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
................................................................................
  2991   2999       if( p->bPatchset && p->op==SQLITE_UPDATE ){
  2992   3000         /* If this is an UPDATE that is part of a patchset, then all PK and
  2993   3001         ** modified fields are present in the new.* record. The old.* record
  2994   3002         ** is currently completely empty. This block shifts the PK fields from
  2995   3003         ** new.* to old.*, to accommodate the code that reads these arrays.  */
  2996   3004         for(i=0; i<p->nCol; i++){
  2997   3005           assert( p->apValue[i]==0 );
  2998         -        assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
  2999   3006           if( p->abPK[i] ){
  3000   3007             p->apValue[i] = p->apValue[i+p->nCol];
         3008  +          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
  3001   3009             p->apValue[i+p->nCol] = 0;
  3002   3010           }
  3003   3011         }
  3004   3012       }
  3005   3013     }
  3006   3014   
  3007   3015     return SQLITE_ROW;
................................................................................
  3721   3729     for(i=0; rc==SQLITE_OK && i<nCol; i++){
  3722   3730       if( !abPK || abPK[i] ){
  3723   3731         sqlite3_value *pVal;
  3724   3732         (void)xValue(pIter, i, &pVal);
  3725   3733         if( pVal==0 ){
  3726   3734           /* The value in the changeset was "undefined". This indicates a
  3727   3735           ** corrupt changeset blob.  */
  3728         -        rc = SQLITE_CORRUPT;
         3736  +        rc = SQLITE_CORRUPT_BKPT;
  3729   3737         }else{
  3730   3738           rc = sessionBindValue(pStmt, i+1, pVal);
  3731   3739         }
  3732   3740       }
  3733   3741     }
  3734   3742     return rc;
  3735   3743   }

Changes to ext/session/sqlite3session.h.

    18     18   ** record changes to a database.
    19     19   */
    20     20   typedef struct sqlite3_session sqlite3_session;
    21     21   
    22     22   /*
    23     23   ** CAPI3REF: Changeset Iterator Handle
    24     24   **
    25         -** An instance of this object is as as a cursor for iterating
           25  +** An instance of this object acts as a cursor for iterating
    26     26   ** over the elements of a [changeset] or [patchset].
    27     27   */
    28     28   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
    29     29   
    30     30   /*
    31     31   ** CAPI3REF: Create A New Session Object
    32     32   ** CONSTRUCTOR: sqlite3_session

Changes to src/vdbe.c.

  2375   2375   ** Store in register r[P3] the byte offset into the database file that is the
  2376   2376   ** start of the payload for the record at which that cursor P1 is currently
  2377   2377   ** pointing.
  2378   2378   **
  2379   2379   ** P2 is the column number for the argument to the sqlite_offset() function.
  2380   2380   ** This opcode does not use P2 itself, but the P2 value is used by the
  2381   2381   ** code generator.  The P1, P2, and P3 operands to this opcode are the
  2382         -** as as for OP_Column.
         2382  +** same as for OP_Column.
  2383   2383   **
  2384   2384   ** This opcode is only available if SQLite is compiled with the
  2385   2385   ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
  2386   2386   */
  2387   2387   case OP_Offset: {          /* out3 */
  2388   2388     VdbeCursor *pC;    /* The VDBE cursor */
  2389   2389     assert( pOp->p1>=0 && pOp->p1<p->nCursor );

Changes to src/wal.c.

  1581   1581   */
  1582   1582   static void walIteratorFree(WalIterator *p){
  1583   1583     sqlite3_free(p);
  1584   1584   }
  1585   1585   
  1586   1586   /*
  1587   1587   ** Construct a WalInterator object that can be used to loop over all 
  1588         -** pages in the WAL in ascending order. The caller must hold the checkpoint
  1589         -** lock.
         1588  +** pages in the WAL following frame nBackfill in ascending order. Frames
         1589  +** nBackfill or earlier may be included - excluding them is an optimization
         1590  +** only. The caller must hold the checkpoint lock.
  1590   1591   **
  1591   1592   ** On success, make *pp point to the newly allocated WalInterator object
  1592   1593   ** return SQLITE_OK. Otherwise, return an error code. If this routine
  1593   1594   ** returns an error, the value of *pp is undefined.
  1594   1595   **
  1595   1596   ** The calling routine should invoke walIteratorFree() to destroy the
  1596   1597   ** WalIterator object when it has finished with it.
  1597   1598   */
  1598         -static int walIteratorInit(Wal *pWal, WalIterator **pp){
         1599  +static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
  1599   1600     WalIterator *p;                 /* Return value */
  1600   1601     int nSegment;                   /* Number of segments to merge */
  1601   1602     u32 iLast;                      /* Last frame in log */
  1602   1603     int nByte;                      /* Number of bytes to allocate */
  1603   1604     int i;                          /* Iterator variable */
  1604   1605     ht_slot *aTmp;                  /* Temp space used by merge-sort */
  1605   1606     int rc = SQLITE_OK;             /* Return Code */
................................................................................
  1628   1629     aTmp = (ht_slot *)sqlite3_malloc64(
  1629   1630         sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
  1630   1631     );
  1631   1632     if( !aTmp ){
  1632   1633       rc = SQLITE_NOMEM_BKPT;
  1633   1634     }
  1634   1635   
  1635         -  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
         1636  +  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
  1636   1637       volatile ht_slot *aHash;
  1637   1638       u32 iZero;
  1638   1639       volatile u32 *aPgno;
  1639   1640   
  1640   1641       rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
  1641   1642       if( rc==SQLITE_OK ){
  1642   1643         int j;                      /* Counter variable */
................................................................................
  1784   1785   
  1785   1786     szPage = walPagesize(pWal);
  1786   1787     testcase( szPage<=32768 );
  1787   1788     testcase( szPage>=65536 );
  1788   1789     pInfo = walCkptInfo(pWal);
  1789   1790     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
  1790   1791   
  1791         -    /* Allocate the iterator */
  1792         -    rc = walIteratorInit(pWal, &pIter);
  1793         -    if( rc!=SQLITE_OK ){
  1794         -      return rc;
  1795         -    }
  1796         -    assert( pIter );
  1797         -
  1798   1792       /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
  1799   1793       ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
  1800   1794       assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
  1801   1795   
  1802   1796       /* Compute in mxSafeFrame the index of the last frame of the WAL that is
  1803   1797       ** safe to write into the database.  Frames beyond mxSafeFrame might
  1804   1798       ** overwrite database pages that are in use by active readers and thus
................................................................................
  1827   1821             xBusy = 0;
  1828   1822           }else{
  1829   1823             goto walcheckpoint_out;
  1830   1824           }
  1831   1825         }
  1832   1826       }
  1833   1827   
  1834         -    if( pInfo->nBackfill<mxSafeFrame
         1828  +    /* Allocate the iterator */
         1829  +    if( pInfo->nBackfill<mxSafeFrame ){
         1830  +      rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
         1831  +      assert( rc==SQLITE_OK || pIter==0 );
         1832  +    }
         1833  +
         1834  +    if( pIter
  1835   1835        && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
  1836   1836       ){
  1837   1837         i64 nSize;                    /* Current size of database file */
  1838   1838         u32 nBackfill = pInfo->nBackfill;
  1839   1839   
  1840   1840         pInfo->nBackfillAttempted = mxSafeFrame;
  1841   1841