System.Data.SQLite
Check-in [2e8d224477]
Not logged in

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

Overview
Comment:3.3.1 alpha
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sourceforge
Files: files | file ages | folders
SHA1: 2e8d224477a79f75e2e55e9482548dfc5d59a77e
User & Date: rmsimpson 2006-01-16 15:51:47
Context
2006-01-20
15:50
1.0.24.5 check-in: ab457a3ee7 user: rmsimpson tags: sourceforge
2006-01-16
15:51
3.3.1 alpha check-in: 2e8d224477 user: rmsimpson tags: sourceforge
15:49
1.0.24.4 check-in: aa2d5e4dc5 user: rmsimpson tags: sourceforge
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/src/alter.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that used to generate VDBE code
    13     13   ** that implements the ALTER TABLE command.
    14     14   **
    15         -** $Id: alter.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: alter.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** The code in this file only exists if we are not omitting the
    22     22   ** ALTER TABLE logic from the build.

Changes to SQLite.Interop/src/analyze.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code associated with the ANALYZE command.
    13     13   **
    14         -** @(#) $Id: analyze.c,v 1.8 2006/01/12 20:54:07 rmsimpson Exp $
           14  +** @(#) $Id: analyze.c,v 1.9 2006/01/16 15:51:47 rmsimpson Exp $
    15     15   */
    16     16   #ifndef SQLITE_OMIT_ANALYZE
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** This routine generates code that opens the sqlite_stat1 table on cursor
    21     21   ** iStatCur.

Changes to SQLite.Interop/src/attach.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the ATTACH and DETACH commands.
    13     13   **
    14         -** $Id: attach.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           14  +** $Id: attach.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /*
    19     19   ** Resolve an expression that was part of an ATTACH or DETACH statement. This
    20     20   ** is slightly different from resolving a normal SQL expression, because simple
    21     21   ** identifiers are treated as strings, not possible column names or aliases.

Changes to SQLite.Interop/src/auth.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the sqlite3_set_authorizer()
    13     13   ** API.  This facility is an optional feature of the library.  Embedded
    14     14   ** systems that do not need this facility may omit it by recompiling
    15     15   ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
    16     16   **
    17         -** $Id: auth.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           17  +** $Id: auth.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** All of the code in this file may be omitted by defining a single
    23     23   ** macro.
    24     24   */
................................................................................
   114    114     TriggerStack *pStack; /* The stack of current triggers */
   115    115     int iDb;              /* The index of the database the expression refers to */
   116    116   
   117    117     if( db->xAuth==0 ) return;
   118    118     if( pExpr->op==TK_AS ) return;
   119    119     assert( pExpr->op==TK_COLUMN );
   120    120     iDb = sqlite3SchemaToIndex(pParse->db, pExpr->pSchema);
          121  +  if( iDb<0 ){
          122  +    /* An attempt to read a column out of a subquery or other
          123  +    ** temporary table. */
          124  +    return;
          125  +  }
   121    126     for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
   122    127       if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
   123    128     }
   124    129     if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
   125    130       pTab = pTabList->a[iSrc].pTab;
   126    131     }else if( (pStack = pParse->trigStack)!=0 ){
   127    132       /* This must be an attempt to read the NEW or OLD pseudo-tables
................................................................................
   138    143       zCol = pTab->aCol[pExpr->iColumn].zName;
   139    144     }else if( pTab->iPKey>=0 ){
   140    145       assert( pTab->iPKey<pTab->nCol );
   141    146       zCol = pTab->aCol[pTab->iPKey].zName;
   142    147     }else{
   143    148       zCol = "ROWID";
   144    149     }
   145         -  assert( iDb<db->nDb );
          150  +  assert( iDb>=0 && iDb<db->nDb );
   146    151     zDBase = db->aDb[iDb].zName;
   147    152     rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, 
   148    153                    pParse->zAuthContext);
   149    154     if( rc==SQLITE_IGNORE ){
   150    155       pExpr->op = TK_NULL;
   151    156     }else if( rc==SQLITE_DENY ){
   152    157       if( db->nDb>2 || iDb!=0 ){

Changes to SQLite.Interop/src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.16 2006/01/12 20:54:07 rmsimpson Exp $
           12  +** $Id: btree.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
   407    407   **   because the table is empty or because BtreeCursorFirst() has not been
   408    408   **   called.
   409    409   **
   410    410   ** CURSOR_REQUIRESEEK:
   411    411   **   The table that this cursor was opened on still exists, but has been 
   412    412   **   modified since the cursor was last used. The cursor position is saved
   413    413   **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
   414         -**   this state, restoreCursorPosition() can be called to attempt to seek 
          414  +**   this state, restoreOrClearCursorPosition() can be called to attempt to seek 
   415    415   **   the cursor to the saved position.
   416    416   */
   417    417   #define CURSOR_INVALID           0
   418    418   #define CURSOR_VALID             1
   419    419   #define CURSOR_REQUIRESEEK       2
   420    420   
   421    421   /*
................................................................................
   497    497     ** shared-cache feature disabled, then there is only ever one user
   498    498     ** of each BtShared structure and so this locking is not necessary. 
   499    499     ** So define the lock related functions as no-ops.
   500    500     */
   501    501     #define queryTableLock(a,b,c) SQLITE_OK
   502    502     #define lockTable(a,b,c) SQLITE_OK
   503    503     #define unlockAllTables(a)
   504         -  #define restoreCursorPosition(a,b) SQLITE_OK
          504  +  #define restoreOrClearCursorPosition(a,b) SQLITE_OK
   505    505     #define saveAllCursors(a,b,c) SQLITE_OK
   506    506   
   507    507   #else
   508    508   
   509    509   /*
   510    510   ** Save the current cursor position in the variables BtCursor.nKey 
   511    511   ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
................................................................................
   570    570     return SQLITE_OK;
   571    571   }
   572    572   
   573    573   /*
   574    574   ** Restore the cursor to the position it was in (or as close to as possible)
   575    575   ** when saveCursorPosition() was called. Note that this call deletes the 
   576    576   ** saved position info stored by saveCursorPosition(), so there can be
   577         -** at most one effective restoreCursorPosition() call after each 
          577  +** at most one effective restoreOrClearCursorPosition() call after each 
   578    578   ** saveCursorPosition().
   579    579   **
   580    580   ** If the second argument argument - doSeek - is false, then instead of 
   581    581   ** returning the cursor to it's saved position, any saved position is deleted
   582    582   ** and the cursor state set to CURSOR_INVALID.
   583    583   */
   584         -static int restoreCursorPosition(BtCursor *pCur, int doSeek){
          584  +static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){
   585    585     int rc = SQLITE_OK;
   586         -  if( pCur->eState==CURSOR_REQUIRESEEK ){
   587         -    assert( sqlite3ThreadDataReadOnly()->useSharedData );
   588         -    if( doSeek ){
   589         -      rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
   590         -    }else{
   591         -      pCur->eState = CURSOR_INVALID;
   592         -    }
   593         -    if( rc==SQLITE_OK ){
   594         -      sqliteFree(pCur->pKey);
   595         -      pCur->pKey = 0;
   596         -      assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
   597         -    }
          586  +  assert( sqlite3ThreadDataReadOnly()->useSharedData );
          587  +  assert( pCur->eState==CURSOR_REQUIRESEEK );
          588  +  if( doSeek ){
          589  +    rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
          590  +  }else{
          591  +    pCur->eState = CURSOR_INVALID;
          592  +  }
          593  +  if( rc==SQLITE_OK ){
          594  +    sqliteFree(pCur->pKey);
          595  +    pCur->pKey = 0;
          596  +    assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
   598    597     }
   599    598     return rc;
   600    599   }
   601    600   
          601  +#define restoreOrClearCursorPosition(p,x) \
          602  +  (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p,x):SQLITE_OK)
          603  +
   602    604   /*
   603    605   ** Query to see if btree handle p may obtain a lock of type eLock 
   604    606   ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
   605    607   ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
   606    608   ** SQLITE_LOCKED if not.
   607    609   */
   608    610   static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
................................................................................
  1691   1693       BtCursor *pTmp = pCur;
  1692   1694       pCur = pCur->pNext;
  1693   1695       if( pTmp->pBtree==p ){
  1694   1696         sqlite3BtreeCloseCursor(pTmp);
  1695   1697       }
  1696   1698     }
  1697   1699   
         1700  +  /* Rollback any active transaction and free the handle structure */
         1701  +  sqlite3BtreeRollback(p);
  1698   1702     sqliteFree(p);
  1699   1703   
  1700   1704   #ifndef SQLITE_OMIT_SHARED_CACHE
  1701   1705     /* If there are still other outstanding references to the shared-btree
  1702   1706     ** structure, return now. The remainder of this procedure cleans 
  1703   1707     ** up the shared-btree.
  1704   1708     */
................................................................................
  2705   2709     }
  2706   2710     pCur = sqliteMalloc( sizeof(*pCur) );
  2707   2711     if( pCur==0 ){
  2708   2712       rc = SQLITE_NOMEM;
  2709   2713       goto create_cursor_exception;
  2710   2714     }
  2711   2715     pCur->pgnoRoot = (Pgno)iTable;
  2712         -  pCur->pPage = 0;  /* For exit-handler, in case getAndInitPage() fails. */
  2713   2716     if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){
  2714   2717       rc = SQLITE_EMPTY;
  2715   2718       goto create_cursor_exception;
  2716   2719     }
  2717   2720     rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
  2718   2721     if( rc!=SQLITE_OK ){
  2719   2722       goto create_cursor_exception;
................................................................................
  2723   2726     ** variables, link the cursor into the BtShared list and set *ppCur (the
  2724   2727     ** output argument to this function).
  2725   2728     */
  2726   2729     pCur->xCompare = xCmp ? xCmp : dfltCompare;
  2727   2730     pCur->pArg = pArg;
  2728   2731     pCur->pBtree = p;
  2729   2732     pCur->wrFlag = wrFlag;
  2730         -  pCur->idx = 0;
  2731         -  memset(&pCur->info, 0, sizeof(pCur->info));
  2732   2733     pCur->pNext = pBt->pCursor;
  2733   2734     if( pCur->pNext ){
  2734   2735       pCur->pNext->pPrev = pCur;
  2735   2736     }
  2736         -  pCur->pPrev = 0;
  2737   2737     pBt->pCursor = pCur;
  2738   2738     pCur->eState = CURSOR_INVALID;
  2739   2739     *ppCur = pCur;
  2740   2740   
  2741   2741     return SQLITE_OK;
  2742   2742   create_cursor_exception:
  2743   2743     if( pCur ){
................................................................................
  2764   2764   
  2765   2765   /*
  2766   2766   ** Close a cursor.  The read lock on the database file is released
  2767   2767   ** when the last cursor is closed.
  2768   2768   */
  2769   2769   int sqlite3BtreeCloseCursor(BtCursor *pCur){
  2770   2770     BtShared *pBt = pCur->pBtree->pBt;
  2771         -  restoreCursorPosition(pCur, 0);
         2771  +  restoreOrClearCursorPosition(pCur, 0);
  2772   2772     if( pCur->pPrev ){
  2773   2773       pCur->pPrev->pNext = pCur->pNext;
  2774   2774     }else{
  2775   2775       pBt->pCursor = pCur->pNext;
  2776   2776     }
  2777   2777     if( pCur->pNext ){
  2778   2778       pCur->pNext->pPrev = pCur->pPrev;
................................................................................
  2831   2831   ** the key for the current entry.  If the cursor is not pointing
  2832   2832   ** to a valid entry, *pSize is set to 0. 
  2833   2833   **
  2834   2834   ** For a table with the INTKEY flag set, this routine returns the key
  2835   2835   ** itself, not the number of bytes in the key.
  2836   2836   */
  2837   2837   int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
  2838         -  int rc = restoreCursorPosition(pCur, 1);
         2838  +  int rc = restoreOrClearCursorPosition(pCur, 1);
  2839   2839     if( rc==SQLITE_OK ){
  2840   2840       assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
  2841   2841       if( pCur->eState==CURSOR_INVALID ){
  2842   2842         *pSize = 0;
  2843   2843       }else{
  2844   2844         getCellInfo(pCur);
  2845   2845         *pSize = pCur->info.nKey;
................................................................................
  2852   2852   ** Set *pSize to the number of bytes of data in the entry the
  2853   2853   ** cursor currently points to.  Always return SQLITE_OK.
  2854   2854   ** Failure is not possible.  If the cursor is not currently
  2855   2855   ** pointing to an entry (which can happen, for example, if
  2856   2856   ** the database is empty) then *pSize is set to 0.
  2857   2857   */
  2858   2858   int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
  2859         -  int rc = restoreCursorPosition(pCur, 1);
         2859  +  int rc = restoreOrClearCursorPosition(pCur, 1);
  2860   2860     if( rc==SQLITE_OK ){
  2861   2861       assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
  2862   2862       if( pCur->eState==CURSOR_INVALID ){
  2863   2863         /* Not pointing at a valid entry - set *pSize to 0. */
  2864   2864         *pSize = 0;
  2865   2865       }else{
  2866   2866         getCellInfo(pCur);
................................................................................
  2897   2897     assert( pCur!=0 && pCur->pPage!=0 );
  2898   2898     assert( pCur->eState==CURSOR_VALID );
  2899   2899     pBt = pCur->pBtree->pBt;
  2900   2900     pPage = pCur->pPage;
  2901   2901     pageIntegrity(pPage);
  2902   2902     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
  2903   2903     getCellInfo(pCur);
  2904         -  aPayload = pCur->info.pCell;
  2905         -  aPayload += pCur->info.nHeader;
         2904  +  aPayload = pCur->info.pCell + pCur->info.nHeader;
  2906   2905     if( pPage->intKey ){
  2907   2906       nKey = 0;
  2908   2907     }else{
  2909   2908       nKey = pCur->info.nKey;
  2910   2909     }
  2911   2910     assert( offset>=0 );
  2912   2911     if( skipKey ){
................................................................................
  2967   2966   ** begins at "offset".
  2968   2967   **
  2969   2968   ** Return SQLITE_OK on success or an error code if anything goes
  2970   2969   ** wrong.  An error is returned if "offset+amt" is larger than
  2971   2970   ** the available payload.
  2972   2971   */
  2973   2972   int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
  2974         -  int rc = restoreCursorPosition(pCur, 1);
         2973  +  int rc = restoreOrClearCursorPosition(pCur, 1);
  2975   2974     if( rc==SQLITE_OK ){
  2976   2975       assert( pCur->eState==CURSOR_VALID );
  2977   2976       assert( pCur->pPage!=0 );
  2978   2977       if( pCur->pPage->intKey ){
  2979   2978         return SQLITE_CORRUPT_BKPT;
  2980   2979       }
  2981   2980       assert( pCur->pPage->intKey==0 );
................................................................................
  2991   2990   ** begins at "offset".
  2992   2991   **
  2993   2992   ** Return SQLITE_OK on success or an error code if anything goes
  2994   2993   ** wrong.  An error is returned if "offset+amt" is larger than
  2995   2994   ** the available payload.
  2996   2995   */
  2997   2996   int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
  2998         -  int rc = restoreCursorPosition(pCur, 1);
         2997  +  int rc = restoreOrClearCursorPosition(pCur, 1);
  2999   2998     if( rc==SQLITE_OK ){
  3000   2999       assert( pCur->eState==CURSOR_VALID );
  3001   3000       assert( pCur->pPage!=0 );
  3002   3001       assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
  3003   3002       rc = getPayload(pCur, offset, amt, pBuf, 1);
  3004   3003     }
  3005   3004     return rc;
................................................................................
  3161   3160   }
  3162   3161   
  3163   3162   /*
  3164   3163   ** Move the cursor to the root page
  3165   3164   */
  3166   3165   static int moveToRoot(BtCursor *pCur){
  3167   3166     MemPage *pRoot;
  3168         -  int rc;
         3167  +  int rc = SQLITE_OK;
  3169   3168     BtShared *pBt = pCur->pBtree->pBt;
  3170   3169   
  3171         -  if( 
  3172         -    SQLITE_OK!=(rc = restoreCursorPosition(pCur, 0)) ||
  3173         -    SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
  3174         -  ){
  3175         -    pCur->eState = CURSOR_INVALID;
  3176         -    return rc;
         3170  +  restoreOrClearCursorPosition(pCur, 0);
         3171  +  assert( pCur->pPage );
         3172  +  pRoot = pCur->pPage;
         3173  +  if( pRoot->pgno==pCur->pgnoRoot ){
         3174  +    assert( pRoot->isInit );
         3175  +  }else{
         3176  +    if( 
         3177  +      SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
         3178  +    ){
         3179  +      pCur->eState = CURSOR_INVALID;
         3180  +      return rc;
         3181  +    }
         3182  +    releasePage(pCur->pPage);
         3183  +    pageIntegrity(pRoot);
         3184  +    pCur->pPage = pRoot;
  3177   3185     }
  3178         -  releasePage(pCur->pPage);
  3179         -  pageIntegrity(pRoot);
  3180         -  pCur->pPage = pRoot;
  3181   3186     pCur->idx = 0;
  3182   3187     pCur->info.nSize = 0;
  3183   3188     if( pRoot->nCell==0 && !pRoot->leaf ){
  3184   3189       Pgno subpage;
  3185   3190       assert( pRoot->pgno==1 );
  3186   3191       subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
  3187   3192       assert( subpage>0 );
................................................................................
  3191   3196     pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
  3192   3197     return rc;
  3193   3198   }
  3194   3199   
  3195   3200   /*
  3196   3201   ** Move the cursor down to the left-most leaf entry beneath the
  3197   3202   ** entry to which it is currently pointing.
         3203  +**
         3204  +** The left-most leaf is the one with the smallest key - the first
         3205  +** in ascending order.
  3198   3206   */
  3199   3207   static int moveToLeftmost(BtCursor *pCur){
  3200   3208     Pgno pgno;
  3201   3209     int rc;
  3202   3210     MemPage *pPage;
  3203   3211   
  3204   3212     assert( pCur->eState==CURSOR_VALID );
................................................................................
  3213   3221   
  3214   3222   /*
  3215   3223   ** Move the cursor down to the right-most leaf entry beneath the
  3216   3224   ** page to which it is currently pointing.  Notice the difference
  3217   3225   ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
  3218   3226   ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
  3219   3227   ** finds the right-most entry beneath the *page*.
         3228  +**
         3229  +** The right-most entry is the one with the largest key - the last
         3230  +** key in ascending order.
  3220   3231   */
  3221   3232   static int moveToRightmost(BtCursor *pCur){
  3222   3233     Pgno pgno;
  3223   3234     int rc;
  3224   3235     MemPage *pPage;
  3225   3236   
  3226   3237     assert( pCur->eState==CURSOR_VALID );
................................................................................
  3298   3309   **                  exactly matches pKey.
  3299   3310   **
  3300   3311   **     *pRes>0      The cursor is left pointing at an entry that
  3301   3312   **                  is larger than pKey.
  3302   3313   */
  3303   3314   int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){
  3304   3315     int rc;
         3316  +  int tryRightmost;
  3305   3317     rc = moveToRoot(pCur);
  3306   3318     if( rc ) return rc;
  3307   3319     assert( pCur->pPage );
  3308   3320     assert( pCur->pPage->isInit );
         3321  +  tryRightmost = pCur->pPage->intKey;
  3309   3322     if( pCur->eState==CURSOR_INVALID ){
  3310   3323       *pRes = -1;
  3311   3324       assert( pCur->pPage->nCell==0 );
  3312   3325       return SQLITE_OK;
  3313   3326     }
  3314   3327      for(;;){
  3315   3328       int lwr, upr;
................................................................................
  3322   3335         return SQLITE_CORRUPT_BKPT;
  3323   3336       }
  3324   3337       pageIntegrity(pPage);
  3325   3338       while( lwr<=upr ){
  3326   3339         void *pCellKey;
  3327   3340         i64 nCellKey;
  3328   3341         pCur->idx = (lwr+upr)/2;
         3342  +      pCur->info.nSize = 0;
  3329   3343         if( pPage->intKey ){
  3330         -        u8 *pCell = findCell(pPage, pCur->idx);
  3331         -        pCell += pPage->childPtrSize;
         3344  +        u8 *pCell;
         3345  +        if( tryRightmost ){
         3346  +          pCur->idx = upr;
         3347  +        }
         3348  +        pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
  3332   3349           if( pPage->hasData ){
  3333   3350             int dummy;
  3334   3351             pCell += getVarint32(pCell, &dummy);
  3335   3352           }
  3336   3353           getVarint(pCell, &nCellKey);
  3337         -        pCur->info.nSize = 0;
  3338   3354           if( nCellKey<nKey ){
  3339   3355             c = -1;
  3340   3356           }else if( nCellKey>nKey ){
  3341   3357             c = +1;
         3358  +          tryRightmost = 0;
  3342   3359           }else{
  3343   3360             c = 0;
  3344   3361           }
  3345   3362         }else{
  3346   3363           int available;
  3347         -        parseCell(pPage, pCur->idx, &pCur->info);
  3348         -        nCellKey = pCur->info.nKey;
  3349   3364           pCellKey = (void *)fetchPayload(pCur, &available, 0);
         3365  +        nCellKey = pCur->info.nKey;
  3350   3366           if( available>=nCellKey ){
  3351   3367             c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
  3352   3368           }else{
  3353   3369             pCellKey = sqliteMallocRaw( nCellKey );
  3354   3370             if( pCellKey==0 ) return SQLITE_NOMEM;
  3355   3371             rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
  3356   3372             c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
................................................................................
  3420   3436   ** this routine was called, then set *pRes=1.
  3421   3437   */
  3422   3438   int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  3423   3439     int rc;
  3424   3440     MemPage *pPage = pCur->pPage;
  3425   3441   
  3426   3442   #ifndef SQLITE_OMIT_SHARED_CACHE
  3427         -  rc = restoreCursorPosition(pCur, 1);
         3443  +  rc = restoreOrClearCursorPosition(pCur, 1);
  3428   3444     if( rc!=SQLITE_OK ){
  3429   3445       return rc;
  3430   3446     }
  3431   3447     if( pCur->skip>0 ){
  3432   3448       pCur->skip = 0;
  3433   3449       *pRes = 0;
  3434   3450       return SQLITE_OK;
................................................................................
  3487   3503   */
  3488   3504   int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
  3489   3505     int rc;
  3490   3506     Pgno pgno;
  3491   3507     MemPage *pPage;
  3492   3508   
  3493   3509   #ifndef SQLITE_OMIT_SHARED_CACHE
  3494         -  rc = restoreCursorPosition(pCur, 1);
         3510  +  rc = restoreOrClearCursorPosition(pCur, 1);
  3495   3511     if( rc!=SQLITE_OK ){
  3496   3512       return rc;
  3497   3513     }
  3498   3514     if( pCur->skip<0 ){
  3499   3515       pCur->skip = 0;
  3500   3516       *pRes = 0;
  3501   3517       return SQLITE_OK;
................................................................................
  5157   5173       return SQLITE_PERM;   /* Cursor not open for writing */
  5158   5174     }
  5159   5175     if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
  5160   5176       return SQLITE_LOCKED; /* The table pCur points to has a read lock */
  5161   5177     }
  5162   5178   
  5163   5179     /* Save the positions of any other cursors open on this table */
         5180  +  restoreOrClearCursorPosition(pCur, 0);
  5164   5181     if( 
  5165         -    SQLITE_OK!=(rc = restoreCursorPosition(pCur, 0)) ||
  5166   5182       SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
  5167   5183       SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc))
  5168   5184     ){
  5169   5185       return rc;
  5170   5186     }
  5171   5187   
  5172   5188     pPage = pCur->pPage;
................................................................................
  5244   5260   
  5245   5261     /* Restore the current cursor position (a no-op if the cursor is not in 
  5246   5262     ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors 
  5247   5263     ** open on the same table. Then call sqlite3pager_write() on the page
  5248   5264     ** that the entry will be deleted from.
  5249   5265     */
  5250   5266     if( 
  5251         -    (rc = restoreCursorPosition(pCur, 1)) ||
         5267  +    (rc = restoreOrClearCursorPosition(pCur, 1)) ||
  5252   5268       (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
  5253   5269       (rc = sqlite3pager_write(pPage->aData))
  5254   5270     ){
  5255   5271       return rc;
  5256   5272     }
  5257   5273   
  5258   5274     /* Locate the cell within it's page and leave pCell pointing to the
................................................................................
  5726   5742   
  5727   5743   /*
  5728   5744   ** Return the flag byte at the beginning of the page that the cursor
  5729   5745   ** is currently pointing to.
  5730   5746   */
  5731   5747   int sqlite3BtreeFlags(BtCursor *pCur){
  5732   5748     /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
  5733         -  ** restoreCursorPosition() here.
         5749  +  ** restoreOrClearCursorPosition() here.
  5734   5750     */
  5735   5751     MemPage *pPage = pCur->pPage;
  5736   5752     return pPage ? pPage->aData[pPage->hdrOffset] : 0;
  5737   5753   }
  5738   5754   
  5739   5755   #ifdef SQLITE_DEBUG
  5740   5756   /*
................................................................................
  5861   5877   ** This routine is used for testing and debugging only.
  5862   5878   */
  5863   5879   int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
  5864   5880     int cnt, idx;
  5865   5881     MemPage *pPage = pCur->pPage;
  5866   5882     BtCursor tmpCur;
  5867   5883   
  5868         -  int rc = restoreCursorPosition(pCur, 1);
         5884  +  int rc = restoreOrClearCursorPosition(pCur, 1);
  5869   5885     if( rc!=SQLITE_OK ){
  5870   5886       return rc;
  5871   5887     }
  5872   5888   
  5873   5889     pageIntegrity(pPage);
  5874   5890     assert( pPage->isInit );
  5875   5891     getTempCursor(pCur, &tmpCur);

Changes to SQLite.Interop/src/btree.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite B-Tree file
    13     13   ** subsystem.  See comments in the source code for a detailed description
    14     14   ** of what each interface routine does.
    15     15   **
    16         -** @(#) $Id: btree.h,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
           16  +** @(#) $Id: btree.h,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    17     17   */
    18     18   #ifndef _BTREE_H_
    19     19   #define _BTREE_H_
    20     20   
    21     21   /* TODO: This definition is just included so other modules compile. It
    22     22   ** needs to be revisited.
    23     23   */

Changes to SQLite.Interop/src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           25  +** $Id: build.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   #include <ctype.h>
    29     29   
    30     30   /*
    31     31   ** This routine is called when a new SQL statement is beginning to
    32     32   ** be parsed.  Initialize the pParse structure as needed.
................................................................................
  1169   1169     int i;
  1170   1170   
  1171   1171     if( (p = pParse->pNewTable)==0 ) return;
  1172   1172     i = p->nCol-1;
  1173   1173   
  1174   1174     if( sqlite3LocateCollSeq(pParse, zType, nType) ){
  1175   1175       Index *pIdx;
  1176         -    p->aCol[i].zColl = sqlite3StrNDup(zType, nType);
         1176  +    p->aCol[i].zColl = sqliteStrNDup(zType, nType);
  1177   1177     
  1178   1178       /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
  1179   1179       ** then an index may have been created on this column before the
  1180   1180       ** collation type was added. Correct this if it is the case.
  1181   1181       */
  1182   1182       for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
  1183   1183         assert( pIdx->nColumn==1 );
................................................................................
  1543   1543   #endif
  1544   1544       pParse->pNewTable = 0;
  1545   1545       db->nTable++;
  1546   1546       db->flags |= SQLITE_InternChanges;
  1547   1547   
  1548   1548   #ifndef SQLITE_OMIT_ALTERTABLE
  1549   1549       if( !p->pSelect ){
         1550  +      const unsigned char *zName = pParse->sNameToken.z;
  1550   1551         assert( !pSelect && pCons && pEnd );
  1551   1552         if( pCons->z==0 ) pCons = pEnd;
  1552         -      p->addColOffset = 13 + (pCons->z - pParse->sNameToken.z);
         1553  +      p->addColOffset = 13 + sqlite3utf8CharLen(zName, pCons->z - zName);
  1553   1554       }
  1554   1555   #endif
  1555   1556     }
  1556   1557   }
  1557   1558   
  1558   1559   #ifndef SQLITE_OMIT_VIEW
  1559   1560   /*
................................................................................
  3149   3150     if( pName1==0 || pName1->z==0 ){
  3150   3151       reindexDatabases(pParse, 0);
  3151   3152       return;
  3152   3153     }else if( pName2==0 || pName2->z==0 ){
  3153   3154       assert( pName1->z );
  3154   3155       pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0);
  3155   3156       if( pColl ){
  3156         -      char *z = sqlite3StrNDup(pName1->z, pName1->n);
         3157  +      char *z = sqliteStrNDup(pName1->z, pName1->n);
  3157   3158         if( z ){
  3158   3159           reindexDatabases(pParse, z);
  3159   3160           sqliteFree(z);
  3160   3161         }
  3161   3162         return;
  3162   3163       }
  3163   3164     }

Changes to SQLite.Interop/src/callback.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains functions used to access the internal hash tables
    14     14   ** of user defined functions and collation sequences.
    15     15   **
    16         -** $Id: callback.c,v 1.10 2006/01/12 20:54:07 rmsimpson Exp $
           16  +** $Id: callback.c,v 1.11 2006/01/16 15:51:47 rmsimpson Exp $
    17     17   */
    18     18   
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** Invoke the 'collation needed' callback to request a collation sequence
    23     23   ** in the database text encoding of name zName, length nName.

Changes to SQLite.Interop/src/complete.c.

    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that implements the sqlite3_complete() API.
    15     15   ** This code used to be part of the tokenizer.c source file.  But by
    16     16   ** separating it out, the code will be automatically omitted from
    17     17   ** static links that do not use it.
    18     18   **
    19         -** $Id: complete.c,v 1.8 2006/01/12 20:54:07 rmsimpson Exp $
           19  +** $Id: complete.c,v 1.9 2006/01/16 15:51:47 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #ifndef SQLITE_OMIT_COMPLETE
    23     23   
    24     24   /*
    25     25   ** This is defined in tokenize.c.  We just have to import the definition.
    26     26   */

Changes to SQLite.Interop/src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.15 2006/01/13 03:36:39 rmsimpson Exp $
           19  +** $Id: date.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    20     20   **
    21     21   ** NOTES:
    22     22   **
    23     23   ** SQLite processes all times and dates as Julian Day numbers.  The
    24     24   ** dates and times are stored as the number of days since noon
    25     25   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    26     26   ** calendar system.
................................................................................
   101    101       min = va_arg(ap, int);
   102    102       max = va_arg(ap, int);
   103    103       nextC = va_arg(ap, int);
   104    104       pVal = va_arg(ap, int*);
   105    105       val = 0;
   106    106       while( N-- ){
   107    107         if( !isdigit(*(u8*)zDate) ){
   108         -        return cnt;
          108  +        goto end_getDigits;
   109    109         }
   110    110         val = val*10 + *zDate - '0';
   111    111         zDate++;
   112    112       }
   113    113       if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
   114         -      return cnt;
          114  +      goto end_getDigits;
   115    115       }
   116    116       *pVal = val;
   117    117       zDate++;
   118    118       cnt++;
   119    119     }while( nextC );
   120    120     va_end(ap);
          121  +end_getDigits:
   121    122     return cnt;
   122    123   }
   123    124   
   124    125   /*
   125    126   ** Read text from z[] and convert into a floating point number.  Return
   126    127   ** the number of digits converted.
   127    128   */

Changes to SQLite.Interop/src/delete.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** in order to generate code for DELETE FROM statements.
    14     14   **
    15         -** $Id: delete.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: delete.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Look up every table that is named in pSrc.  If any table is not found,
    21     21   ** add an error message to pParse->zErrMsg and return NULL.  If all tables
    22     22   ** are found, return a pointer to the last table.

Changes to SQLite.Interop/src/experimental.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are not a part of the official
    13     13   ** SQLite API.  These routines are unsupported.
    14     14   **
    15         -** $Id: experimental.c,v 1.4 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: experimental.c,v 1.5 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set all the parameters in the compiled SQL statement to NULL.
    21     21   */
    22     22   int sqlite3_clear_bindings(sqlite3_stmt *pStmt){

Changes to SQLite.Interop/src/expr.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.21 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: expr.c,v 1.22 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** Return the 'affinity' of the expression pExpr if any.
    22     22   **
................................................................................
   351    351           break;
   352    352         }
   353    353       }
   354    354       if( i>=pParse->nVarExpr ){
   355    355         pExpr->iTable = ++pParse->nVar;
   356    356         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
   357    357           pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
   358         -        sqlite3ReallocOrFree((void**)&pParse->apVarExpr,
          358  +        sqliteReallocOrFree((void**)&pParse->apVarExpr,
   359    359                          pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
   360    360         }
   361    361         if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
   362    362           assert( pParse->apVarExpr!=0 );
   363    363           pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
   364    364         }
   365    365       }

Changes to SQLite.Interop/src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.16 2006/01/13 03:36:39 rmsimpson Exp $
           19  +** $Id: func.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <ctype.h>
    23     23   /* #include <math.h> */
    24     24   #include <stdlib.h>
    25     25   #include <assert.h>
    26     26   #include "vdbeInt.h"

Changes to SQLite.Interop/src/hash.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This is the implementation of generic hash-tables
    13     13   ** used in SQLite.
    14     14   **
    15         -** $Id: hash.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: hash.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <assert.h>
    19     19   
    20     20   /* Turn bulk memory into a hash table object by initializing the
    21     21   ** fields of the Hash structure.
    22     22   **

Changes to SQLite.Interop/src/hash.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This is the header file for the generic hash-table implemenation
    13     13   ** used in SQLite.
    14     14   **
    15         -** $Id: hash.h,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: hash.h,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #ifndef _SQLITE_HASH_H_
    18     18   #define _SQLITE_HASH_H_
    19     19   
    20     20   /* Forward declarations of structures. */
    21     21   typedef struct Hash Hash;
    22     22   typedef struct HashElem HashElem;

Changes to SQLite.Interop/src/insert.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   **
    15         -** $Id: insert.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: insert.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set P3 of the most recently inserted opcode to a column affinity
    21     21   ** string for index pIdx. A column affinity string has one character
    22     22   ** for each column in the table, according to the affinity of the column:

Changes to SQLite.Interop/src/legacy.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: legacy.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           17  +** $Id: legacy.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    18     18   */
    19     19   
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   
    24     24   /*

Changes to SQLite.Interop/src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           17  +** $Id: main.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** The following constant value is used by the SQLITE_BIGENDIAN and

Changes to SQLite.Interop/src/os_common.h.

   128    128   ** sqlite3GenericRealloc
   129    129   ** sqlite3GenericOsFree
   130    130   ** sqlite3GenericAllocationSize
   131    131   **
   132    132   ** Implementation of the os level dynamic memory allocation interface in terms
   133    133   ** of the standard malloc(), realloc() and free() found in many operating
   134    134   ** systems. No rocket science here.
          135  +**
          136  +** There are two versions of these four functions here. The version
          137  +** implemented here is only used if memory-management or memory-debugging is
          138  +** enabled. This version allocates an extra 8-bytes at the beginning of each
          139  +** block and stores the size of the allocation there.
          140  +**
          141  +** If neither memory-management or debugging is enabled, the second
          142  +** set of implementations is used instead.
   135    143   */
          144  +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || defined (SQLITE_MEMDEBUG)
   136    145   void *sqlite3GenericMalloc(int n){
   137    146     char *p = (char *)malloc(n+8);
   138    147     assert(n>0);
   139    148     assert(sizeof(int)<=8);
   140    149     if( p ){
   141    150       *(int *)p = n;
          151  +    p += 8;
   142    152     }
   143         -  return (void *)(p + 8);
          153  +  return (void *)p;
   144    154   }
   145    155   void *sqlite3GenericRealloc(void *p, int n){
   146    156     char *p2 = ((char *)p - 8);
   147    157     assert(n>0);
   148    158     p2 = realloc(p2, n+8);
   149    159     if( p2 ){
   150    160       *(int *)p2 = n;
          161  +    p2 += 8;
   151    162     }
   152         -  return (void *)((char *)p2 + 8);
          163  +  return (void *)p2;
   153    164   }
   154    165   void sqlite3GenericFree(void *p){
   155    166     assert(p);
   156    167     free((void *)((char *)p - 8));
   157    168   }
   158    169   int sqlite3GenericAllocationSize(void *p){
   159    170     return p ? *(int *)((char *)p - 8) : 0;
   160    171   }
          172  +#else
          173  +void *sqlite3GenericMalloc(int n){
          174  +  char *p = (char *)malloc(n);
          175  +  return (void *)p;
          176  +}
          177  +void *sqlite3GenericRealloc(void *p, int n){
          178  +  assert(n>0);
          179  +  p = realloc(p, n);
          180  +  return p;
          181  +}
          182  +void sqlite3GenericFree(void *p){
          183  +  assert(p);
          184  +  free(p);
          185  +}
          186  +#if 0   /* Never actually invoked */
          187  +int sqlite3GenericAllocationSize(void *p){
          188  +  assert(0);
          189  +}
          190  +#endif
          191  +#endif

Changes to SQLite.Interop/src/os_unix.c.

    15     15   #include "sqliteInt.h"
    16     16   #include "os.h"
    17     17   #if OS_UNIX              /* This file is used on unix only */
    18     18   
    19     19   /*
    20     20   ** These #defines should enable >2GB file support on Posix if the
    21     21   ** underlying operating system supports it.  If the OS lacks
    22         -** large file support, or if the OS is windows, these should be no-ops.
           22  +** large file support, these should be no-ops.
    23     23   **
    24     24   ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    25     25   ** on the compiler command line.  This is necessary if you are compiling
    26     26   ** on a recent machine (ex: RedHat 7.2) but you want your code to work
    27     27   ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
    28     28   ** without this option, LFS is enable.  But LFS does not exist in the kernel
    29     29   ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
    30     30   ** portability you should omit LFS.
    31         -**
    32         -** Similar is true for MacOS.  LFS is only supported on MacOS 9 and later.
    33     31   */
    34     32   #ifndef SQLITE_DISABLE_LFS
    35     33   # define _LARGE_FILE       1
    36     34   # ifndef _FILE_OFFSET_BITS
    37     35   #   define _FILE_OFFSET_BITS 64
    38     36   # endif
    39     37   # define _LARGEFILE_SOURCE 1
................................................................................
    47     45   #include <fcntl.h>
    48     46   #include <unistd.h>
    49     47   #include <time.h>
    50     48   #include <sys/time.h>
    51     49   #include <errno.h>
    52     50   
    53     51   /*
    54         -** Macros used to determine whether or not to use threads.  The
    55         -** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for
    56         -** Posix threads and SQLITE_W32_THREADS is defined if we are
    57         -** synchronizing using Win32 threads.
           52  +** If we are to be thread-safe, include the pthreads header and define
           53  +** the SQLITE_UNIX_THREADS macro.
    58     54   */
    59     55   #if defined(THREADSAFE) && THREADSAFE
    60     56   # include <pthread.h>
    61     57   # define SQLITE_UNIX_THREADS 1
    62     58   #endif
    63     59   
    64     60   /*
................................................................................
    81     77     struct lockInfo *pLock;   /* Info about locks on this inode */
    82     78     int h;                    /* The file descriptor */
    83     79     unsigned char locktype;   /* The type of lock held on this fd */
    84     80     unsigned char isOpen;     /* True if needs to be closed */
    85     81     unsigned char fullSync;   /* Use F_FULLSYNC if available */
    86     82     int dirfd;                /* File descriptor for the directory */
    87     83   #ifdef SQLITE_UNIX_THREADS
    88         -  pthread_t tid;            /* The thread authorized to use this OsFile */
           84  +  pthread_t tid;            /* The thread that "owns" this OsFile */
    89     85   #endif
    90     86   };
    91     87   
    92     88   /*
    93     89   ** Provide the ability to override some OS-layer functions during
    94     90   ** testing.  This is used to simulate OS crashes to verify that 
    95     91   ** commits are atomic even in the event of an OS crash.
................................................................................
   109    105   /*
   110    106   ** Include code that is common to all os_*.c files
   111    107   */
   112    108   #include "os_common.h"
   113    109   
   114    110   /*
   115    111   ** Do not include any of the File I/O interface procedures if the
   116         -** SQLITE_OMIT_DISKIO macro is defined (indicating that there database
          112  +** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
   117    113   ** will be in-memory only)
   118    114   */
   119    115   #ifndef SQLITE_OMIT_DISKIO
   120    116   
   121    117   
   122    118   /*
   123    119   ** Define various macros that are missing from some systems.
................................................................................
   160    156   ** Set or check the OsFile.tid field.  This field is set when an OsFile
   161    157   ** is first opened.  All subsequent uses of the OsFile verify that the
   162    158   ** same thread is operating on the OsFile.  Some operating systems do
   163    159   ** not allow locks to be overridden by other threads and that restriction
   164    160   ** means that sqlite3* database handles cannot be moved from one thread
   165    161   ** to another.  This logic makes sure a user does not try to do that
   166    162   ** by mistake.
          163  +**
          164  +** Version 3.3.1 (2006-01-15):  OsFiles can be moved from one thread to
          165  +** another as long as we are running on a system that supports threads
          166  +** overriding each others locks (which now the most common behavior)
          167  +** or if no locks are held.  But the OsFile.pLock field needs to be
          168  +** recomputed because its key includes the thread-id.  See the 
          169  +** transferOwnership() function below for additional information
   167    170   */
   168         -#if defined(SQLITE_UNIX_THREADS) && !defined(SQLITE_ALLOW_XTHREAD_CONNECTIONS)
          171  +#if defined(SQLITE_UNIX_THREADS)
   169    172   # define SET_THREADID(X)   (X)->tid = pthread_self()
   170         -# define CHECK_THREADID(X) (!pthread_equal((X)->tid, pthread_self()))
          173  +# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
          174  +                            !pthread_equal((X)->tid, pthread_self()))
   171    175   #else
   172    176   # define SET_THREADID(X)
   173    177   # define CHECK_THREADID(X) 0
   174    178   #endif
   175    179   
   176    180   /*
   177    181   ** Here is the dirt on POSIX advisory locks:  ANSI STD 1003.1 (1996)
................................................................................
   275    279   
   276    280   /*
   277    281   ** An instance of the following structure serves as the key used
   278    282   ** to locate a particular lockInfo structure given its inode.
   279    283   **
   280    284   ** If threads cannot override each others locks, then we set the
   281    285   ** lockKey.tid field to the thread ID.  If threads can override
   282         -** each others locks then tid is always set to zero.  tid is also
   283         -** set to zero if we compile without threading support.
          286  +** each others locks then tid is always set to zero.  tid is omitted
          287  +** if we compile without threading support.
   284    288   */
   285    289   struct lockKey {
   286    290     dev_t dev;       /* Device number */
   287    291     ino_t ino;       /* Inode number */
   288    292   #ifdef SQLITE_UNIX_THREADS
   289    293     pthread_t tid;   /* Thread ID or zero if threads can override each other */
   290    294   #endif
................................................................................
   328    332     int nRef;             /* Number of pointers to this structure */
   329    333     int nLock;            /* Number of outstanding locks */
   330    334     int nPending;         /* Number of pending close() operations */
   331    335     int *aPending;        /* Malloced space holding fd's awaiting a close() */
   332    336   };
   333    337   
   334    338   /* 
   335         -** These hash table maps inodes and process IDs into lockInfo and openCnt
   336         -** structures.  Access to these hash tables must be protected by a mutex.
          339  +** These hash tables map inodes and file descriptors (really, lockKey and
          340  +** openKey structures) into lockInfo and openCnt structures.  Access to 
          341  +** these hash tables must be protected by a mutex.
   337    342   */
   338    343   static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
   339    344   static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
   340    345   
   341    346   
   342    347   #ifdef SQLITE_UNIX_THREADS
   343    348   /*
   344    349   ** This variable records whether or not threads can override each others
   345    350   ** locks.
   346    351   **
   347    352   **    0:  No.  Threads cannot override each others locks.
   348    353   **    1:  Yes.  Threads can override each others locks.
   349    354   **   -1:  We don't know yet.
          355  +**
          356  +** This variable normally has file scope only.  But during testing, we make
          357  +** it a global so that the test code can change its value in order to verify
          358  +** that the right stuff happens in either case.
   350    359   */
          360  +#ifdef SQLITE_TEST
          361  +int threadsOverrideEachOthersLocks = -1;
          362  +#else
   351    363   static int threadsOverrideEachOthersLocks = -1;
          364  +#endif
   352    365   
   353    366   /*
   354    367   ** This structure holds information passed into individual test
   355    368   ** threads by the testThreadLockingBehavior() routine.
   356    369   */
   357    370   struct threadTestData {
   358    371     int fd;                /* File to be locked */
................................................................................
   363    376   #ifdef SQLITE_LOCK_TRACE
   364    377   /*
   365    378   ** Print out information about all locking operations.
   366    379   **
   367    380   ** This routine is used for troubleshooting locks on multithreaded
   368    381   ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
   369    382   ** command-line option on the compiler.  This code is normally
   370         -** turnned off.
          383  +** turned off.
   371    384   */
   372    385   static int lockTrace(int fd, int op, struct flock *p){
   373    386     char *zOpName, *zType;
   374    387     int s;
   375    388     int savedErrno;
   376    389     if( op==F_GETLK ){
   377    390       zOpName = "GETLK";
................................................................................
   461    474   }
   462    475   #endif /* SQLITE_UNIX_THREADS */
   463    476   
   464    477   /*
   465    478   ** Release a lockInfo structure previously allocated by findLockInfo().
   466    479   */
   467    480   static void releaseLockInfo(struct lockInfo *pLock){
          481  +  assert( sqlite3OsInMutex() );
   468    482     pLock->nRef--;
   469    483     if( pLock->nRef==0 ){
   470    484       sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
   471    485       sqliteFree(pLock);
   472    486     }
   473    487   }
   474    488   
   475    489   /*
   476    490   ** Release a openCnt structure previously allocated by findLockInfo().
   477    491   */
   478    492   static void releaseOpenCnt(struct openCnt *pOpen){
          493  +  assert( sqlite3OsInMutex() );
   479    494     pOpen->nRef--;
   480    495     if( pOpen->nRef==0 ){
   481    496       sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
   482         -    sqliteFree(pOpen->aPending);
          497  +    free(pOpen->aPending);
   483    498       sqliteFree(pOpen);
   484    499     }
   485    500   }
   486    501   
   487    502   /*
   488    503   ** Given a file descriptor, locate lockInfo and openCnt structures that
   489         -** describes that file descriptor.  Create a new ones if necessary.  The
   490         -** return values might be unset if an error occurs.
          504  +** describes that file descriptor.  Create new ones if necessary.  The
          505  +** return values might be uninitialized if an error occurs.
   491    506   **
   492    507   ** Return the number of errors.
   493    508   */
   494    509   static int findLockInfo(
   495    510     int fd,                      /* The file descriptor used in the key */
   496    511     struct lockInfo **ppLock,    /* Return the lockInfo structure here */
   497    512     struct openCnt **ppOpen      /* Return the openCnt structure here */
................................................................................
   501    516     struct openKey key2;
   502    517     struct stat statbuf;
   503    518     struct lockInfo *pLock;
   504    519     struct openCnt *pOpen;
   505    520     rc = fstat(fd, &statbuf);
   506    521     if( rc!=0 ) return 1;
   507    522   
          523  +  assert( sqlite3OsInMutex() );
   508    524     memset(&key1, 0, sizeof(key1));
   509    525     key1.dev = statbuf.st_dev;
   510    526     key1.ino = statbuf.st_ino;
   511    527   #ifdef SQLITE_UNIX_THREADS
   512    528     if( threadsOverrideEachOthersLocks<0 ){
   513    529       testThreadLockingBehavior(fd);
   514    530     }
................................................................................
   536    552         rc = 1;
   537    553         goto exit_findlockinfo;
   538    554       }
   539    555     }else{
   540    556       pLock->nRef++;
   541    557     }
   542    558     *ppLock = pLock;
   543         -  pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
   544         -  if( pOpen==0 ){
   545         -    struct openCnt *pOld;
   546         -    pOpen = sqliteMallocRaw( sizeof(*pOpen) );
          559  +  if( ppOpen!=0 ){
          560  +    pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
   547    561       if( pOpen==0 ){
   548         -      releaseLockInfo(pLock);
   549         -      rc = 1;
   550         -      goto exit_findlockinfo;
   551         -    }
   552         -    pOpen->key = key2;
   553         -    pOpen->nRef = 1;
   554         -    pOpen->nLock = 0;
   555         -    pOpen->nPending = 0;
   556         -    pOpen->aPending = 0;
   557         -    pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
   558         -    if( pOld!=0 ){
   559         -      assert( pOld==pOpen );
   560         -      sqliteFree(pOpen);
   561         -      releaseLockInfo(pLock);
   562         -      rc = 1;
   563         -      goto exit_findlockinfo;
   564         -    }
   565         -  }else{
   566         -    pOpen->nRef++;
   567         -  }
   568         -  *ppOpen = pOpen;
          562  +      struct openCnt *pOld;
          563  +      pOpen = sqliteMallocRaw( sizeof(*pOpen) );
          564  +      if( pOpen==0 ){
          565  +        releaseLockInfo(pLock);
          566  +        rc = 1;
          567  +        goto exit_findlockinfo;
          568  +      }
          569  +      pOpen->key = key2;
          570  +      pOpen->nRef = 1;
          571  +      pOpen->nLock = 0;
          572  +      pOpen->nPending = 0;
          573  +      pOpen->aPending = 0;
          574  +      pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
          575  +      if( pOld!=0 ){
          576  +        assert( pOld==pOpen );
          577  +        sqliteFree(pOpen);
          578  +        releaseLockInfo(pLock);
          579  +        rc = 1;
          580  +        goto exit_findlockinfo;
          581  +      }
          582  +    }else{
          583  +      pOpen->nRef++;
          584  +    }
          585  +    *ppOpen = pOpen;
          586  +  }
   569    587   
   570    588   exit_findlockinfo:
   571    589     return rc;
   572    590   }
          591  +
          592  +#ifdef SQLITE_DEBUG
          593  +/*
          594  +** Helper function for printing out trace information from debugging
          595  +** binaries. This returns the string represetation of the supplied
          596  +** integer lock-type.
          597  +*/
          598  +static const char *locktypeName(int locktype){
          599  +  switch( locktype ){
          600  +  case NO_LOCK: return "NONE";
          601  +  case SHARED_LOCK: return "SHARED";
          602  +  case RESERVED_LOCK: return "RESERVED";
          603  +  case PENDING_LOCK: return "PENDING";
          604  +  case EXCLUSIVE_LOCK: return "EXCLUSIVE";
          605  +  }
          606  +  return "ERROR";
          607  +}
          608  +#endif
          609  +
          610  +/*
          611  +** If we are currently in a different thread than the thread that the
          612  +** unixFile argument belongs to, then transfer ownership of the unixFile
          613  +** over to the current thread.
          614  +**
          615  +** A unixFile is only owned by a thread on systems where one thread is
          616  +** unable to override locks created by a different thread.  RedHat9 is
          617  +** an example of such a system.
          618  +**
          619  +** Ownership transfer is only allowed if the unixFile is currently unlocked.
          620  +** If the unixFile is locked and an ownership is wrong, then return
          621  +** SQLITE_MISUSE.  SQLITE_OK is returned if everything works.
          622  +*/
          623  +#ifdef SQLITE_UNIX_THREADS
          624  +static int transferOwnership(unixFile *pFile){
          625  +  int rc;
          626  +  pthread_t hSelf;
          627  +  if( threadsOverrideEachOthersLocks ){
          628  +    /* Ownership transfers not needed on this system */
          629  +    return SQLITE_OK;
          630  +  }
          631  +  hSelf = pthread_self();
          632  +  if( pthread_equal(pFile->tid, hSelf) ){
          633  +    /* We are still in the same thread */
          634  +    TRACE1("No-transfer, same thread\n");
          635  +    return SQLITE_OK;
          636  +  }
          637  +  if( pFile->locktype!=NO_LOCK ){
          638  +    /* We cannot change ownership while we are holding a lock! */
          639  +    return SQLITE_MISUSE;
          640  +  }
          641  +  TRACE4("Transfer ownership of %d from %d to %d\n", pFile->h,pFile->tid,hSelf);
          642  +  pFile->tid = hSelf;
          643  +  releaseLockInfo(pFile->pLock);
          644  +  rc = findLockInfo(pFile->h, &pFile->pLock, 0);
          645  +  TRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
          646  +     locktypeName(pFile->locktype),
          647  +     locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
          648  +  return rc;
          649  +}
          650  +#else
          651  +  /* On single-threaded builds, ownership transfer is a no-op */
          652  +# define transferOwnership(X) SQLITE_OK
          653  +#endif
   573    654   
   574    655   /*
   575    656   ** Delete the named file
   576    657   */
   577    658   int sqlite3UnixDelete(const char *zFilename){
   578    659     unlink(zFilename);
   579    660     return SQLITE_OK;
................................................................................
   758    839     return SQLITE_OK;
   759    840   }
   760    841   
   761    842   /*
   762    843   ** If the following global variable points to a string which is the
   763    844   ** name of a directory, then that directory will be used to store
   764    845   ** temporary files.
          846  +**
          847  +** See also the "PRAGMA temp_store_directory" SQL command.
   765    848   */
   766    849   char *sqlite3_temp_directory = 0;
   767    850   
   768    851   /*
   769    852   ** Create a temporary file name in zBuf.  zBuf must be big enough to
   770    853   ** hold at least SQLITE_TEMPNAME_SIZE characters.
   771    854   */
................................................................................
  1043   1126   ** return zero.
  1044   1127   */
  1045   1128   static int unixCheckReservedLock(OsFile *id){
  1046   1129     int r = 0;
  1047   1130     unixFile *pFile = (unixFile*)id;
  1048   1131   
  1049   1132     assert( pFile );
  1050         -  if( CHECK_THREADID(pFile) ) return SQLITE_MISUSE;
  1051   1133     sqlite3OsEnterMutex(); /* Because pFile->pLock is shared across threads */
  1052   1134   
  1053   1135     /* Check if a thread in this process holds such a lock */
  1054   1136     if( pFile->pLock->locktype>SHARED_LOCK ){
  1055   1137       r = 1;
  1056   1138     }
  1057   1139   
................................................................................
  1071   1153     
  1072   1154     sqlite3OsLeaveMutex();
  1073   1155     TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
  1074   1156   
  1075   1157     return r;
  1076   1158   }
  1077   1159   
  1078         -#ifdef SQLITE_DEBUG
  1079         -/*
  1080         -** Helper function for printing out trace information from debugging
  1081         -** binaries. This returns the string represetation of the supplied
  1082         -** integer lock-type.
  1083         -*/
  1084         -static const char *locktypeName(int locktype){
  1085         -  switch( locktype ){
  1086         -  case NO_LOCK: return "NONE";
  1087         -  case SHARED_LOCK: return "SHARED";
  1088         -  case RESERVED_LOCK: return "RESERVED";
  1089         -  case PENDING_LOCK: return "PENDING";
  1090         -  case EXCLUSIVE_LOCK: return "EXCLUSIVE";
  1091         -  }
  1092         -  return "ERROR";
  1093         -}
  1094         -#endif
  1095         -
  1096   1160   /*
  1097   1161   ** Lock the file with the lock specified by parameter locktype - one
  1098   1162   ** of the following:
  1099   1163   **
  1100   1164   **     (1) SHARED_LOCK
  1101   1165   **     (2) RESERVED_LOCK
  1102   1166   **     (3) PENDING_LOCK
................................................................................
  1162   1226     struct flock lock;
  1163   1227     int s;
  1164   1228   
  1165   1229     assert( pFile );
  1166   1230     TRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
  1167   1231         locktypeName(locktype), locktypeName(pFile->locktype),
  1168   1232         locktypeName(pLock->locktype), pLock->cnt , getpid());
  1169         -  if( CHECK_THREADID(pFile) ) return SQLITE_MISUSE;
  1170   1233   
  1171   1234     /* If there is already a lock of this type or more restrictive on the
  1172   1235     ** OsFile, do nothing. Don't use the end_lock: exit path, as
  1173   1236     ** sqlite3OsEnterMutex() hasn't been called yet.
  1174   1237     */
  1175   1238     if( pFile->locktype>=locktype ){
  1176   1239       TRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
................................................................................
  1183   1246     assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
  1184   1247     assert( locktype!=PENDING_LOCK );
  1185   1248     assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
  1186   1249   
  1187   1250     /* This mutex is needed because pFile->pLock is shared across threads
  1188   1251     */
  1189   1252     sqlite3OsEnterMutex();
         1253  +
         1254  +  /* Make sure the current thread owns the pFile.
         1255  +  */
         1256  +  rc = transferOwnership(pFile);
         1257  +  if( rc!=SQLITE_OK ){
         1258  +    sqlite3OsLeaveMutex();
         1259  +    return rc;
         1260  +  }
         1261  +  pLock = pFile->pLock;
  1190   1262   
  1191   1263     /* If some thread using this PID has a lock via a different OsFile*
  1192   1264     ** handle that precludes the requested lock, return BUSY.
  1193   1265     */
  1194   1266     if( (pFile->locktype!=pLock->locktype && 
  1195   1267             (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
  1196   1268     ){
................................................................................
  1306   1378   
  1307   1379   /*
  1308   1380   ** Lower the locking level on file descriptor pFile to locktype.  locktype
  1309   1381   ** must be either NO_LOCK or SHARED_LOCK.
  1310   1382   **
  1311   1383   ** If the locking level of the file descriptor is already at or below
  1312   1384   ** the requested locking level, this routine is a no-op.
  1313         -**
  1314         -** It is not possible for this routine to fail if the second argument
  1315         -** is NO_LOCK.  If the second argument is SHARED_LOCK, this routine
  1316         -** might return SQLITE_IOERR instead of SQLITE_OK.
  1317   1385   */
  1318   1386   static int unixUnlock(OsFile *id, int locktype){
  1319   1387     struct lockInfo *pLock;
  1320   1388     struct flock lock;
  1321   1389     int rc = SQLITE_OK;
  1322   1390     unixFile *pFile = (unixFile*)id;
  1323   1391   
  1324   1392     assert( pFile );
  1325   1393     TRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
  1326   1394         pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1327         -  if( CHECK_THREADID(pFile) ) return SQLITE_MISUSE;
  1328   1395   
  1329   1396     assert( locktype<=SHARED_LOCK );
  1330   1397     if( pFile->locktype<=locktype ){
  1331   1398       return SQLITE_OK;
         1399  +  }
         1400  +  if( CHECK_THREADID(pFile) ){
         1401  +    return SQLITE_MISUSE;
  1332   1402     }
  1333   1403     sqlite3OsEnterMutex();
  1334   1404     pLock = pFile->pLock;
  1335   1405     assert( pLock->cnt!=0 );
  1336   1406     if( pFile->locktype>SHARED_LOCK ){
  1337   1407       assert( pLock->locktype==pFile->locktype );
  1338   1408       if( locktype==SHARED_LOCK ){
................................................................................
  1382   1452       pOpen->nLock--;
  1383   1453       assert( pOpen->nLock>=0 );
  1384   1454       if( pOpen->nLock==0 && pOpen->nPending>0 ){
  1385   1455         int i;
  1386   1456         for(i=0; i<pOpen->nPending; i++){
  1387   1457           close(pOpen->aPending[i]);
  1388   1458         }
  1389         -      sqliteFree(pOpen->aPending);
         1459  +      free(pOpen->aPending);
  1390   1460         pOpen->nPending = 0;
  1391   1461         pOpen->aPending = 0;
  1392   1462       }
  1393   1463     }
  1394   1464     sqlite3OsLeaveMutex();
  1395   1465     pFile->locktype = locktype;
  1396   1466     return rc;
................................................................................
  1397   1467   }
  1398   1468   
  1399   1469   /*
  1400   1470   ** Close a file.
  1401   1471   */
  1402   1472   static int unixClose(OsFile **pId){
  1403   1473     unixFile *id = (unixFile*)*pId;
         1474  +  int rc;
         1475  +
  1404   1476     if( !id ) return SQLITE_OK;
  1405         -  if( CHECK_THREADID(id) ) return SQLITE_MISUSE;
  1406   1477     unixUnlock(*pId, NO_LOCK);
  1407   1478     if( id->dirfd>=0 ) close(id->dirfd);
  1408   1479     id->dirfd = -1;
  1409   1480     sqlite3OsEnterMutex();
  1410   1481   
  1411   1482     if( id->pOpen->nLock ){
  1412   1483       /* If there are outstanding locks, do not actually close the file just
  1413   1484       ** yet because that would clear those locks.  Instead, add the file
  1414   1485       ** descriptor to pOpen->aPending.  It will be automatically closed when
  1415   1486       ** the last lock is cleared.
  1416   1487       */
  1417   1488       int *aNew;
  1418   1489       struct openCnt *pOpen = id->pOpen;
  1419         -    aNew = sqliteRealloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
         1490  +    aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
  1420   1491       if( aNew==0 ){
  1421   1492         /* If a malloc fails, just leak the file descriptor */
  1422   1493       }else{
  1423   1494         pOpen->aPending = aNew;
  1424   1495         pOpen->aPending[pOpen->nPending] = id->h;
  1425   1496         pOpen->nPending++;
  1426   1497       }
................................................................................
  1433   1504   
  1434   1505     sqlite3OsLeaveMutex();
  1435   1506     id->isOpen = 0;
  1436   1507     TRACE2("CLOSE   %-3d\n", id->h);
  1437   1508     OpenCounter(-1);
  1438   1509     sqliteFree(id);
  1439   1510     *pId = 0;
  1440         -  return SQLITE_OK;
         1511  +  return rc;
  1441   1512   }
  1442   1513   
  1443   1514   /*
  1444   1515   ** Turn a relative pathname into a full pathname.  Return a pointer
  1445   1516   ** to the full pathname stored in space obtained from sqliteMalloc().
  1446   1517   ** The calling function is responsible for freeing this space once it
  1447   1518   ** is no longer needed.
................................................................................
  1512   1583   ** OsFile.  If we run out of memory, close the file and return NULL.
  1513   1584   */
  1514   1585   static int allocateUnixFile(unixFile *pInit, OsFile **pId){
  1515   1586     unixFile *pNew;
  1516   1587     pNew = sqliteMalloc( sizeof(unixFile) );
  1517   1588     if( pNew==0 ){
  1518   1589       close(pInit->h);
         1590  +    sqlite3OsEnterMutex();
  1519   1591       releaseLockInfo(pInit->pLock);
  1520   1592       releaseOpenCnt(pInit->pOpen);
         1593  +    sqlite3OsLeaveMutex();
  1521   1594       *pId = 0;
  1522   1595       return SQLITE_NOMEM;
  1523   1596     }else{
  1524   1597       *pNew = *pInit;
  1525   1598       pNew->pMethod = &sqlite3UnixIoMethod;
  1526   1599       *pId = (OsFile*)pNew;
  1527   1600       OpenCounter(+1);
................................................................................
  1548   1621     ** prefer that the randomness be increased by making use of the
  1549   1622     ** uninitialized space in zBuf - but valgrind errors tend to worry
  1550   1623     ** some users.  Rather than argue, it seems easier just to initialize
  1551   1624     ** the whole array and silence valgrind, even if that means less randomness
  1552   1625     ** in the random seed.
  1553   1626     **
  1554   1627     ** When testing, initializing zBuf[] to zero is all we do.  That means
  1555         -  ** that we always use the same random number sequence.* This makes the
         1628  +  ** that we always use the same random number sequence.  This makes the
  1556   1629     ** tests repeatable.
  1557   1630     */
  1558   1631     memset(zBuf, 0, 256);
  1559   1632   #if !defined(SQLITE_TEST)
  1560   1633     {
  1561   1634       int pid, fd;
  1562   1635       fd = open("/dev/urandom", O_RDONLY);
................................................................................
  1573   1646     }
  1574   1647   #endif
  1575   1648     return SQLITE_OK;
  1576   1649   }
  1577   1650   
  1578   1651   /*
  1579   1652   ** Sleep for a little while.  Return the amount of time slept.
         1653  +** The argument is the number of milliseconds we want to sleep.
  1580   1654   */
  1581   1655   int sqlite3UnixSleep(int ms){
  1582   1656   #if defined(HAVE_USLEEP) && HAVE_USLEEP
  1583   1657     usleep(ms*1000);
  1584   1658     return ms;
  1585   1659   #else
  1586   1660     sleep((ms+999)/1000);
................................................................................
  1599   1673   /*
  1600   1674   ** The following pair of routine implement mutual exclusion for
  1601   1675   ** multi-threaded processes.  Only a single thread is allowed to
  1602   1676   ** executed code that is surrounded by EnterMutex() and LeaveMutex().
  1603   1677   **
  1604   1678   ** SQLite uses only a single Mutex.  There is not much critical
  1605   1679   ** code and what little there is executes quickly and without blocking.
         1680  +**
         1681  +** This mutex is not recursive.
  1606   1682   */
  1607   1683   void sqlite3UnixEnterMutex(){
  1608   1684   #ifdef SQLITE_UNIX_THREADS
  1609   1685     pthread_mutex_lock(&mutex);
  1610   1686   #endif
  1611   1687     assert( !inMutex );
  1612   1688     inMutex = 1;
................................................................................
  1644   1720   # endif
  1645   1721   #else
  1646   1722   # define TSD_COUNTER(N)  /* no-op */
  1647   1723   #endif
  1648   1724   
  1649   1725   
  1650   1726   /*
  1651         -** If called with allocateFlag>1, then return a pointer to thread
         1727  +** If called with allocateFlag>0, then return a pointer to thread
  1652   1728   ** specific data for the current thread.  Allocate and zero the
  1653         -** thread-specific data if it does not already exist necessary.
         1729  +** thread-specific data if it does not already exist.
  1654   1730   **
  1655   1731   ** If called with allocateFlag==0, then check the current thread
  1656   1732   ** specific data.  Return it if it exists.  If it does not exist,
  1657   1733   ** then return NULL.
  1658   1734   **
  1659   1735   ** If called with allocateFlag<0, check to see if the thread specific
  1660   1736   ** data is allocated and is all zero.  If it is then deallocate it.

Changes to SQLite.Interop/src/os_win.c.

  1218   1218     int nByte;
  1219   1219     nByte = strlen(zRelative) + MAX_PATH + 1001;
  1220   1220     zFull = sqliteMalloc( nByte );
  1221   1221     if( zFull==0 ) return 0;
  1222   1222     if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0;
  1223   1223   #elif OS_WINCE
  1224   1224     /* WinCE has no concept of a relative pathname, or so I am told. */
  1225         -  zFull = sqlite3StrDup(zRelative);
         1225  +  zFull = sqliteStrDup(zRelative);
  1226   1226   #else
  1227   1227     char *zNotUsed;
  1228   1228     WCHAR *zWide;
  1229   1229     int nByte;
  1230   1230     zWide = utf8ToUnicode(zRelative);
  1231   1231     if( zWide ){
  1232   1232       WCHAR *zTemp, *zNotUsedW;

Changes to SQLite.Interop/src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.16 2006/01/13 03:36:39 rmsimpson Exp $
           21  +** @(#) $Id: pager.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include "os.h"
    26     26   #include "pager.h"
    27     27   #include <assert.h>
    28     28   #include <string.h>
................................................................................
   221    221   /*
   222    222   ** Hash a page number
   223    223   */
   224    224   #define pager_hash(PN)  ((PN)&(N_PG_HASH-1))
   225    225   
   226    226   /*
   227    227   ** A open page cache is an instance of the following structure.
          228  +**
          229  +** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, SQLITE_PROTOCOL
          230  +** or SQLITE_FULL. Once one of the first three errors occurs, it persists
          231  +** and is returned as the result of every major pager API call.  The
          232  +** SQLITE_FULL return code is slightly different. It persists only until the
          233  +** next successful rollback is performed on the pager cache. Also,
          234  +** SQLITE_FULL does not affect the sqlite3pager_get() and sqlite3pager_lookup()
          235  +** APIs, they may still be used successfully.
   228    236   */
   229    237   struct Pager {
   230    238     u8 journalOpen;             /* True if journal file descriptors is valid */
   231    239     u8 journalStarted;          /* True if header of journal is synced */
   232    240     u8 useJournal;              /* Use a rollback journal on this file */
   233    241     u8 noReadlock;              /* Do not bother to obtain readlocks */
   234    242     u8 stmtOpen;                /* True if the statement subjournal is open */
   235    243     u8 stmtInUse;               /* True we are in a statement subtransaction */
   236    244     u8 stmtAutoopen;            /* Open stmt journal when main journal is opened*/
   237    245     u8 noSync;                  /* Do not sync the journal if true */
   238    246     u8 fullSync;                /* Do extra syncs of the journal for robustness */
   239    247     u8 state;                   /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
   240         -  u8 errMask;                 /* One of several kinds of errors */
          248  +  u8 errCode;                 /* One of several kinds of errors */
   241    249     u8 tempFile;                /* zFilename is a temporary file */
   242    250     u8 readOnly;                /* True for a read-only database */
   243    251     u8 needSync;                /* True if an fsync() is needed on the journal */
   244    252     u8 dirtyCache;              /* True if cached pages have changed */
   245    253     u8 alwaysRollback;          /* Disable dont_rollback() for all pages */
   246    254     u8 memDb;                   /* True to inhibit all file I/O */
   247    255     u8 setMaster;               /* True if a m-j name has been written to jrnl */
................................................................................
   295    303   */
   296    304   #ifdef SQLITE_TEST
   297    305   # define TEST_INCR(x)  x++
   298    306   #else
   299    307   # define TEST_INCR(x)
   300    308   #endif
   301    309   
   302         -/*
   303         -** These are bits that can be set in Pager.errMask.
   304         -**
   305         -** TODO: Maybe we just want a variable - Pager.errCode. Can we really 
   306         -**       have two simultaneous error conditions?
   307         -**
   308         -** Recovering from an SQLITE_FULL, SQLITE_LOCK, SQLITE_CORRUPT or 
   309         -** SQLITE_IOERR error is not a simple matter, particularly if the pager 
   310         -** cache is shared between multiple connections.
   311         -**
   312         -** SQLITE_FULL (PAGER_ERR_FULL):
   313         -**     Cleared when the transaction is rolled back.
   314         -**
   315         -** SQLITE_CORRUPT (PAGER_ERR_CORRUPT):
   316         -**     Cannot be cleared. The upper layer must close the current pager 
   317         -**     and open a new one on the same file to continue.
   318         -**
   319         -** SQLITE_PROTOCOL (PAGER_ERR_LOCK):
   320         -**     This error only occurs if an internal error occurs or another process 
   321         -**     is not following the sqlite locking protocol (i.e. someone is 
   322         -**     manipulating the database file using something other than sqlite).
   323         -**     This is handled in the same way as database corruption - the error 
   324         -**     cannot be cleared except by closing the current pager and opening 
   325         -**     a brand new one on the same file.
   326         -**
   327         -** SQLITE_IOERR (PAGER_ERR_DISK):
   328         -**     Cleared when the transaction is rolled back.
   329         -*/
   330         -#define PAGER_ERR_FULL     0x01  /* a write() failed */
   331         -#define PAGER_ERR_LOCK     0x02  /* error in the locking protocol */
   332         -#define PAGER_ERR_CORRUPT  0x04  /* database or journal corruption */
   333         -#define PAGER_ERR_DISK     0x08  /* general disk I/O error - bad hard drive? */
   334         -
   335    310   /*
   336    311   ** Journal files begin with the following magic string.  The data
   337    312   ** was obtained from /dev/random.  It is used only as a sanity check.
   338    313   **
   339    314   ** Since version 2.8.0, the journal format contains additional sanity
   340    315   ** checking information.  If the power fails while the journal is begin
   341    316   ** written, semi-random garbage data might appear in the journal
................................................................................
   427    402   ** Read a 32-bit integer from the given file descriptor.  Store the integer
   428    403   ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
   429    404   ** error code is something goes wrong.
   430    405   **
   431    406   ** All values are stored on disk as big-endian.
   432    407   */
   433    408   static int read32bits(OsFile *fd, u32 *pRes){
   434         -  u32 res;
   435         -  int rc;
   436         -  rc = sqlite3OsRead(fd, &res, sizeof(res));
          409  +  unsigned char ac[4];
          410  +  int rc = sqlite3OsRead(fd, ac, sizeof(ac));
   437    411     if( rc==SQLITE_OK ){
   438         -    unsigned char ac[4];
   439         -    memcpy(ac, &res, 4);
   440         -    res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
          412  +    *pRes = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
   441    413     }
   442         -  *pRes = res;
   443    414     return rc;
   444    415   }
   445    416   
   446    417   /*
   447    418   ** Write a 32-bit integer into a string buffer in big-endian byte order.
   448    419   */
   449    420   static void put32bits(char *ac, u32 val){
................................................................................
   480    451   static u32 retrieve32bits(PgHdr *p, int offset){
   481    452     unsigned char *ac;
   482    453     ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
   483    454     return (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
   484    455   }
   485    456   
   486    457   
   487         -/*
   488         -** Convert the bits in the pPager->errMask into an approprate
   489         -** return code.
   490         -*/
   491         -static int pager_errcode(Pager *pPager){
   492         -  int rc = SQLITE_OK;
   493         -  if( pPager->errMask & PAGER_ERR_LOCK )    rc = SQLITE_PROTOCOL;
   494         -  if( pPager->errMask & PAGER_ERR_DISK )    rc = SQLITE_IOERR;
   495         -  if( pPager->errMask & PAGER_ERR_FULL )    rc = SQLITE_FULL;
   496         -  if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
   497         -  return rc;
   498         -}
   499         -
   500    458   /*
   501    459   ** This function should be called when an error occurs within the pager
   502         -** code to set the appropriate bits in Pager.errMask.
          460  +** code. The first argument is a pointer to the pager structure, the
          461  +** second the error-code about to be returned by a pager API function. 
          462  +** The value returned is a copy of the second argument to this function. 
          463  +**
          464  +** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_PROTOCOL,
          465  +** the error becomes persistent. All subsequent API calls on this Pager
          466  +** will immediately return the same error code.
   503    467   */
   504    468   static int pager_error(Pager *pPager, int rc){
   505         -  switch( rc ){
   506         -    case SQLITE_PROTOCOL:
   507         -      pPager->errMask |= PAGER_ERR_LOCK;
   508         -      break;
   509         -    case SQLITE_IOERR:
   510         -      pPager->errMask |= PAGER_ERR_DISK;
   511         -      break;
   512         -    case SQLITE_FULL:
   513         -      pPager->errMask |= PAGER_ERR_FULL;
   514         -      break;
   515         -    case SQLITE_CORRUPT:
   516         -      pPager->errMask |= PAGER_ERR_CORRUPT;
   517         -      break;
          469  +  assert( pPager->errCode==SQLITE_FULL || pPager->errCode==SQLITE_OK );
          470  +  if( 
          471  +    rc==SQLITE_FULL || 
          472  +    rc==SQLITE_IOERR || 
          473  +    rc==SQLITE_CORRUPT ||
          474  +    rc==SQLITE_PROTOCOL
          475  +  ){
          476  +    pPager->errCode = rc;
   518    477     }
   519    478     return rc;
   520    479   }
   521    480   
   522    481   #ifdef SQLITE_CHECK_PAGES
   523    482   /*
   524    483   ** Return a 32-bit hash of the page data for pPage.
................................................................................
   537    496   ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
   538    497   ** is defined, and NDEBUG is not defined, an assert() statement checks
   539    498   ** that the page is either dirty or still matches the calculated page-hash.
   540    499   */
   541    500   #define CHECK_PAGE(x) checkPage(x)
   542    501   static void checkPage(PgHdr *pPg){
   543    502     Pager *pPager = pPg->pPager;
   544         -  assert( !pPg->pageHash || pPager->errMask || MEMDB || pPg->dirty || 
          503  +  assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty || 
   545    504         pPg->pageHash==pager_pagehash(pPg) );
   546    505   }
   547    506   
   548    507   #else
   549    508   #define CHECK_PAGE(x)
   550    509   #endif
   551    510   
................................................................................
   879    838   ** Unlock the database and clear the in-memory cache.  This routine
   880    839   ** sets the state of the pager back to what it was when it was first
   881    840   ** opened.  Any outstanding pages are invalidated and subsequent attempts
   882    841   ** to access those pages will likely result in a coredump.
   883    842   */
   884    843   static void pager_reset(Pager *pPager){
   885    844     PgHdr *pPg, *pNext;
   886         -  if( pPager->errMask ) return;
          845  +  if( pPager->errCode ) return;
   887    846     for(pPg=pPager->pAll; pPg; pPg=pNext){
   888    847       pNext = pPg->pNextAll;
   889    848       sqliteFree(pPg);
   890    849     }
   891    850     pPager->pFirst = 0;
   892    851     pPager->pFirstSynced = 0;
   893    852     pPager->pLast = 0;
................................................................................
  1500   1459       }
  1501   1460     }
  1502   1461   
  1503   1462     pPager->journalOff = szJ;
  1504   1463     
  1505   1464   end_stmt_playback:
  1506   1465     if( rc!=SQLITE_OK ){
  1507         -    pPager->errMask |= PAGER_ERR_CORRUPT;
  1508         -    rc = SQLITE_CORRUPT;
         1466  +    rc = pager_error(pPager, SQLITE_CORRUPT);
  1509   1467     }else{
  1510   1468       pPager->journalOff = szJ;
  1511   1469       /* pager_reload_cache(pPager); */
  1512   1470     }
  1513   1471     return rc;
  1514   1472   }
  1515   1473   
................................................................................
  1687   1645   
  1688   1646     for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
  1689   1647     if( i>0 ) pPager->zDirectory[i-1] = 0;
  1690   1648     strcpy(pPager->zJournal, zFullPathname);
  1691   1649     sqliteFree(zFullPathname);
  1692   1650     strcpy(&pPager->zJournal[nameLen], "-journal");
  1693   1651     pPager->fd = fd;
  1694         -  pPager->journalOpen = 0;
         1652  +  /* pPager->journalOpen = 0; */
  1695   1653     pPager->useJournal = useJournal && !memDb;
  1696   1654     pPager->noReadlock = noReadlock && readOnly;
  1697         -  pPager->stmtOpen = 0;
  1698         -  pPager->stmtInUse = 0;
  1699         -  pPager->nRef = 0;
         1655  +  /* pPager->stmtOpen = 0; */
         1656  +  /* pPager->stmtInUse = 0; */
         1657  +  /* pPager->nRef = 0; */
  1700   1658     pPager->dbSize = memDb-1;
  1701   1659     pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
  1702         -  pPager->stmtSize = 0;
  1703         -  pPager->stmtJSize = 0;
  1704         -  pPager->nPage = 0;
  1705         -  pPager->nMaxPage = 0;
         1660  +  /* pPager->stmtSize = 0; */
         1661  +  /* pPager->stmtJSize = 0; */
         1662  +  /* pPager->nPage = 0; */
         1663  +  /* pPager->nMaxPage = 0; */
  1706   1664     pPager->mxPage = 100;
  1707         -  pPager->state = PAGER_UNLOCK;
  1708         -  pPager->errMask = 0;
         1665  +  assert( PAGER_UNLOCK==0 );
         1666  +  /* pPager->state = PAGER_UNLOCK; */
         1667  +  /* pPager->errMask = 0; */
  1709   1668     pPager->tempFile = tempFile;
  1710   1669     pPager->memDb = memDb;
  1711   1670     pPager->readOnly = readOnly;
  1712         -  pPager->needSync = 0;
         1671  +  /* pPager->needSync = 0; */
  1713   1672     pPager->noSync = pPager->tempFile || !useJournal;
  1714   1673     pPager->fullSync = (pPager->noSync?0:1);
  1715         -  pPager->pFirst = 0;
  1716         -  pPager->pFirstSynced = 0;
  1717         -  pPager->pLast = 0;
         1674  +  /* pPager->pFirst = 0; */
         1675  +  /* pPager->pFirstSynced = 0; */
         1676  +  /* pPager->pLast = 0; */
  1718   1677     pPager->nExtra = FORCE_ALIGNMENT(nExtra);
  1719   1678     pPager->sectorSize = PAGER_SECTOR_SIZE;
  1720         -  pPager->pBusyHandler = 0;
  1721         -  memset(pPager->aHash, 0, sizeof(pPager->aHash));
         1679  +  /* pPager->pBusyHandler = 0; */
         1680  +  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  1722   1681     *ppPager = pPager;
  1723   1682   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1724   1683     if( pTsdro->useMemoryManagement ){
  1725   1684       ThreadData *pTsd = sqlite3ThreadData();
  1726   1685       pPager->pNext = pTsd->pPager;
  1727   1686       pTsd->pPager = pPager;
  1728   1687     }
................................................................................
  1832   1791   int sqlite3pager_pagecount(Pager *pPager){
  1833   1792     i64 n;
  1834   1793     assert( pPager!=0 );
  1835   1794     if( pPager->dbSize>=0 ){
  1836   1795       n = pPager->dbSize;
  1837   1796     } else {
  1838   1797       if( sqlite3OsFileSize(pPager->fd, &n)!=SQLITE_OK ){
  1839         -      pPager->errMask |= PAGER_ERR_DISK;
         1798  +      pager_error(pPager, SQLITE_IOERR);
  1840   1799         return 0;
  1841   1800       }
  1842   1801       if( n>0 && n<pPager->pageSize ){
  1843   1802         n = 1;
  1844   1803       }else{
  1845   1804         n /= pPager->pageSize;
  1846   1805       }
................................................................................
  1978   1937   
  1979   1938   /*
  1980   1939   ** Truncate the file to the number of pages specified.
  1981   1940   */
  1982   1941   int sqlite3pager_truncate(Pager *pPager, Pgno nPage){
  1983   1942     int rc;
  1984   1943     sqlite3pager_pagecount(pPager);
  1985         -  if( pPager->errMask!=0 ){
  1986         -    rc = pager_errcode(pPager);
         1944  +  if( pPager->errCode ){
         1945  +    rc = pPager->errCode;
  1987   1946       return rc;
  1988   1947     }
  1989   1948     if( nPage>=(unsigned)pPager->dbSize ){
  1990   1949       return SQLITE_OK;
  1991   1950     }
  1992   1951     if( MEMDB ){
  1993   1952       pPager->dbSize = nPage;
................................................................................
  2042   2001         */
  2043   2002         disable_simulated_io_errors();
  2044   2003         sqlite3pager_rollback(pPager);
  2045   2004         enable_simulated_io_errors();
  2046   2005         if( !MEMDB ){
  2047   2006           sqlite3OsUnlock(pPager->fd, NO_LOCK);
  2048   2007         }
  2049         -      assert( pPager->errMask || pPager->journalOpen==0 );
         2008  +      assert( pPager->errCode || pPager->journalOpen==0 );
  2050   2009         break;
  2051   2010       }
  2052   2011       case PAGER_SHARED: {
  2053   2012         if( !MEMDB ){
  2054   2013           sqlite3OsUnlock(pPager->fd, NO_LOCK);
  2055   2014         }
  2056   2015         break;
................................................................................
  2069   2028         assert( !pHist->pStmt );
  2070   2029       }
  2071   2030   #endif
  2072   2031       pNext = pPg->pNextAll;
  2073   2032       sqliteFree(pPg);
  2074   2033     }
  2075   2034     TRACE2("CLOSE %d\n", PAGERID(pPager));
  2076         -  assert( pPager->errMask || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
         2035  +  assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
  2077   2036     if( pPager->journalOpen ){
  2078   2037       sqlite3OsClose(&pPager->jfd);
  2079   2038     }
  2080   2039     sqliteFree(pPager->aInJournal);
  2081   2040     if( pPager->stmtOpen ){
  2082   2041       sqlite3OsClose(&pPager->stfd);
  2083   2042     }
................................................................................
  2369   2328     }
  2370   2329   }
  2371   2330   
  2372   2331   /*
  2373   2332   ** Try to find a page in the cache that can be recycled. 
  2374   2333   **
  2375   2334   ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It 
  2376         -** does not set the pPager->errMask variable.
         2335  +** does not set the pPager->errCode variable.
  2377   2336   */
  2378   2337   static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
  2379   2338     PgHdr *pPg;
  2380   2339     *ppPg = 0;
  2381   2340   
  2382   2341     /* Find a page to recycle.  Try to locate a page that does not
  2383   2342     ** require us to do an fsync() on the journal.
................................................................................
  2514   2473           nReleased += sqliteAllocSize(pPg);
  2515   2474           sqliteFree(pPg);
  2516   2475         }
  2517   2476   
  2518   2477         if( rc!=SQLITE_OK ){
  2519   2478           /* An error occured whilst writing to the database file or 
  2520   2479           ** journal in pager_recycle(). The error is not returned to the 
  2521         -        ** caller of this function. Instead, set the Pager.errMask variable.
         2480  +        ** caller of this function. Instead, set the Pager.errCode variable.
  2522   2481           ** The error will be returned to the user (or users, in the case 
  2523   2482           ** of a shared pager cache) of the pager for which the error occured.
  2524   2483           */
  2525   2484           assert( rc==SQLITE_IOERR || rc==SQLITE_FULL );
  2526   2485           assert( p->state>=PAGER_RESERVED );
  2527   2486           pager_error(p, rc);
  2528   2487         }
................................................................................
  2567   2526       return SQLITE_CORRUPT_BKPT;
  2568   2527     }
  2569   2528   
  2570   2529     /* Make sure we have not hit any critical errors.
  2571   2530     */ 
  2572   2531     assert( pPager!=0 );
  2573   2532     *ppPage = 0;
  2574         -  if( pPager->errMask & ~(PAGER_ERR_FULL) ){
  2575         -    return pager_errcode(pPager);
         2533  +  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
         2534  +    return pPager->errCode;
  2576   2535     }
  2577   2536   
  2578   2537     /* If this is the first page accessed, then get a SHARED lock
  2579   2538     ** on the database file.
  2580   2539     */
  2581   2540     if( pPager->nRef==0 && !MEMDB ){
  2582   2541       if( !pPager->noReadlock ){
................................................................................
  2704   2663       if( pPg->pNextHash ){
  2705   2664         assert( pPg->pNextHash->pPrevHash==0 );
  2706   2665         pPg->pNextHash->pPrevHash = pPg;
  2707   2666       }
  2708   2667       if( pPager->nExtra>0 ){
  2709   2668         memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
  2710   2669       }
  2711         -    if( pPager->errMask!=0 ){
         2670  +    if( pPager->errCode ){
  2712   2671         sqlite3pager_unref(PGHDR_TO_DATA(pPg));
  2713         -      rc = pager_errcode(pPager);
         2672  +      rc = pPager->errCode;
  2714   2673         return rc;
  2715   2674       }
  2716   2675       if( sqlite3pager_pagecount(pPager)<(int)pgno ){
  2717   2676         memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
  2718   2677       }else{
  2719   2678         int rc;
  2720   2679         assert( MEMDB==0 );
................................................................................
  2763   2722   ** has ever happened.
  2764   2723   */
  2765   2724   void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){
  2766   2725     PgHdr *pPg;
  2767   2726   
  2768   2727     assert( pPager!=0 );
  2769   2728     assert( pgno!=0 );
  2770         -  if( pPager->errMask & ~(PAGER_ERR_FULL) ){
         2729  +  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
  2771   2730       return 0;
  2772   2731     }
  2773   2732     pPg = pager_lookup(pPager, pgno);
  2774   2733     if( pPg==0 ) return 0;
  2775   2734     page_ref(pPg);
  2776   2735     return PGHDR_TO_DATA(pPg);
  2777   2736   }
................................................................................
  2861   2820     sqlite3OsSetFullSync(pPager->fd, pPager->fullSync);
  2862   2821     sqlite3OsOpenDirectory(pPager->jfd, pPager->zDirectory);
  2863   2822     pPager->journalOpen = 1;
  2864   2823     pPager->journalStarted = 0;
  2865   2824     pPager->needSync = 0;
  2866   2825     pPager->alwaysRollback = 0;
  2867   2826     pPager->nRec = 0;
  2868         -  if( pPager->errMask!=0 ){
  2869         -    rc = pager_errcode(pPager);
         2827  +  if( pPager->errCode ){
         2828  +    rc = pPager->errCode;
  2870   2829       goto failed_to_open_journal;
  2871   2830     }
  2872   2831     pPager->origDbSize = pPager->dbSize;
  2873   2832   
  2874   2833     rc = writeJournalHdr(pPager);
  2875   2834   
  2876   2835     if( pPager->stmtAutoopen && rc==SQLITE_OK ){
................................................................................
  2980   2939   int sqlite3pager_write(void *pData){
  2981   2940     PgHdr *pPg = DATA_TO_PGHDR(pData);
  2982   2941     Pager *pPager = pPg->pPager;
  2983   2942     int rc = SQLITE_OK;
  2984   2943   
  2985   2944     /* Check for errors
  2986   2945     */
  2987         -  if( pPager->errMask ){ 
  2988         -    return pager_errcode(pPager);
         2946  +  if( pPager->errCode ){ 
         2947  +    return pPager->errCode;
  2989   2948     }
  2990   2949     if( pPager->readOnly ){
  2991   2950       return SQLITE_PERM;
  2992   2951     }
  2993   2952   
  2994   2953     assert( !pPager->setMaster );
  2995   2954   
................................................................................
  3055   3014             pPager->journalOff += szPg;
  3056   3015             TRACE4("JOURNAL %d page %d needSync=%d\n",
  3057   3016                     PAGERID(pPager), pPg->pgno, pPg->needSync);
  3058   3017             CODEC(pPager, pData, pPg->pgno, 0);
  3059   3018             *(u32*)PGHDR_TO_EXTRA(pPg, pPager) = saved;
  3060   3019             if( rc!=SQLITE_OK ){
  3061   3020               sqlite3pager_rollback(pPager);
  3062         -            pPager->errMask |= PAGER_ERR_FULL;
         3021  +            if( !pPager->errCode ){
         3022  +              pager_error(pPager, SQLITE_FULL);
         3023  +            }
  3063   3024               return rc;
  3064   3025             }
  3065   3026             pPager->nRec++;
  3066   3027             assert( pPager->aInJournal!=0 );
  3067   3028             pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3068   3029             pPg->needSync = !pPager->noSync;
  3069   3030             if( pPager->stmtInUse ){
................................................................................
  3102   3063           CODEC(pPager, pData, pPg->pgno, 7);
  3103   3064           rc = sqlite3OsWrite(pPager->stfd,((char*)pData)-4,
  3104   3065                                  pPager->pageSize+4);
  3105   3066           TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  3106   3067           CODEC(pPager, pData, pPg->pgno, 0);
  3107   3068           if( rc!=SQLITE_OK ){
  3108   3069             sqlite3pager_rollback(pPager);
  3109         -          pPager->errMask |= PAGER_ERR_FULL;
         3070  +          if( !pPager->errCode ){
         3071  +            pager_error(pPager, SQLITE_FULL);
         3072  +          }
  3110   3073             return rc;
  3111   3074           }
  3112   3075           pPager->stmtNRec++;
  3113   3076           assert( pPager->aInStmt!=0 );
  3114   3077           pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3115   3078         }
  3116   3079         page_add_to_stmt_list(pPg);
................................................................................
  3262   3225   ** and an error code is returned.  If the commit worked, SQLITE_OK
  3263   3226   ** is returned.
  3264   3227   */
  3265   3228   int sqlite3pager_commit(Pager *pPager){
  3266   3229     int rc;
  3267   3230     PgHdr *pPg;
  3268   3231   
  3269         -  if( pPager->errMask==PAGER_ERR_FULL ){
         3232  +  if( pPager->errCode==SQLITE_FULL ){
  3270   3233       rc = sqlite3pager_rollback(pPager);
  3271   3234       if( rc==SQLITE_OK ){
  3272   3235         rc = SQLITE_FULL;
  3273   3236       }
  3274   3237       return rc;
  3275   3238     }
  3276         -  if( pPager->errMask!=0 ){
  3277         -    rc = pager_errcode(pPager);
         3239  +  if( pPager->errCode ){
         3240  +    rc = pPager->errCode;
  3278   3241       return rc;
  3279   3242     }
  3280   3243     if( pPager->state<PAGER_RESERVED ){
  3281   3244       return SQLITE_ERROR;
  3282   3245     }
  3283   3246     TRACE2("COMMIT %d\n", PAGERID(pPager));
  3284   3247     if( MEMDB ){
................................................................................
  3381   3344   
  3382   3345     if( !pPager->dirtyCache || !pPager->journalOpen ){
  3383   3346       rc = pager_unwritelock(pPager);
  3384   3347       pPager->dbSize = -1;
  3385   3348       return rc;
  3386   3349     }
  3387   3350   
  3388         -  if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
         3351  +  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
  3389   3352       if( pPager->state>=PAGER_EXCLUSIVE ){
  3390   3353         pager_playback(pPager);
  3391   3354       }
  3392         -    return pager_errcode(pPager);
         3355  +    return pPager->errCode;
  3393   3356     }
  3394   3357     if( pPager->state==PAGER_RESERVED ){
  3395   3358       int rc2;
  3396   3359       rc = pager_reload_cache(pPager);
  3397   3360       rc2 = pager_unwritelock(pPager);
  3398   3361       if( rc==SQLITE_OK ){
  3399   3362         rc = rc2;
  3400   3363       }
  3401   3364     }else{
  3402   3365       rc = pager_playback(pPager);
  3403   3366     }
  3404   3367     if( rc!=SQLITE_OK ){
  3405   3368       rc = SQLITE_CORRUPT_BKPT;
  3406         -    pPager->errMask |= PAGER_ERR_CORRUPT;
         3369  +    pager_error(pPager, SQLITE_CORRUPT);
  3407   3370     }
  3408   3371     pPager->dbSize = -1;
  3409   3372     return rc;
  3410   3373   }
  3411   3374   
  3412   3375   /*
  3413   3376   ** Return TRUE if the database file is opened read-only.  Return FALSE
................................................................................
  3423   3386   int *sqlite3pager_stats(Pager *pPager){
  3424   3387     static int a[11];
  3425   3388     a[0] = pPager->nRef;
  3426   3389     a[1] = pPager->nPage;
  3427   3390     a[2] = pPager->mxPage;
  3428   3391     a[3] = pPager->dbSize;
  3429   3392     a[4] = pPager->state;
  3430         -  a[5] = pPager->errMask;
         3393  +  a[5] = pPager->errCode;
  3431   3394   #ifdef SQLITE_TEST
  3432   3395     a[6] = pPager->nHit;
  3433   3396     a[7] = pPager->nMiss;
  3434   3397     a[8] = pPager->nOvfl;
  3435   3398     a[9] = pPager->nRead;
  3436   3399     a[10] = pPager->nWrite;
  3437   3400   #endif

Changes to SQLite.Interop/src/pager.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite page cache
    13     13   ** subsystem.  The page cache subsystem reads and writes a file a page
    14     14   ** at a time and provides a journal for rollback.
    15     15   **
    16         -** @(#) $Id: pager.h,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
           16  +** @(#) $Id: pager.h,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    17     17   */
    18     18   
    19     19   #ifndef _PAGER_H_
    20     20   #define _PAGER_H_
    21     21   
    22     22   /*
    23     23   ** The default size of a database page.

Changes to SQLite.Interop/src/pragma.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           14  +** $Id: pragma.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include "os.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /* Ignore this whole file if pragmas are disabled
    21     21   */

Changes to SQLite.Interop/src/prepare.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the implementation of the sqlite3_prepare()
    13     13   ** interface, and routines that contribute to loading the database schema
    14     14   ** from disk.
    15     15   **
    16         -** $Id: prepare.c,v 1.10 2006/01/12 20:54:07 rmsimpson Exp $
           16  +** $Id: prepare.c,v 1.11 2006/01/16 15:51:47 rmsimpson Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "os.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   /*
    23     23   ** Fill the InitData structure with an error message that indicates
................................................................................
   318    318   
   319    319   /*
   320    320   ** Initialize all database files - the main database file, the file
   321    321   ** used to store temporary tables, and any additional database files
   322    322   ** created using ATTACH statements.  Return a success code.  If an
   323    323   ** error occurs, write an error message into *pzErrMsg.
   324    324   **
   325         -** After the database is initialized, the SQLITE_Initialized
   326         -** bit is set in the flags field of the sqlite structure. 
          325  +** After a database is initialized, the DB_SchemaLoaded bit is set
          326  +** bit is set in the flags field of the Db structure. If the database
          327  +** file was of zero-length, then the DB_Empty flag is also set.
   327    328   */
   328    329   int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   329    330     int i, rc;
   330    331     int called_initone = 0;
   331    332     
   332    333     if( db->init.busy ) return SQLITE_OK;
   333    334     rc = SQLITE_OK;
................................................................................
   490    491     const char** pzTail       /* OUT: End of parsed string */
   491    492   ){
   492    493     Parse sParse;
   493    494     char *zErrMsg = 0;
   494    495     int rc = SQLITE_OK;
   495    496     int i;
   496    497   
          498  +  /* Assert that malloc() has not failed */
   497    499     assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
   498    500   
   499    501     assert( ppStmt );
   500    502     *ppStmt = 0;
   501    503     if( sqlite3SafetyOn(db) ){
   502    504       return SQLITE_MISUSE;
   503    505     }

Changes to SQLite.Interop/src/random.c.

    11     11   *************************************************************************
    12     12   ** This file contains code to implement a pseudo-random number
    13     13   ** generator (PRNG) for SQLite.
    14     14   **
    15     15   ** Random numbers are used by some of the database backends in order
    16     16   ** to generate random integer keys for tables or random filenames.
    17     17   **
    18         -** $Id: random.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           18  +** $Id: random.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   
    24     24   /*
    25     25   ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex

Changes to SQLite.Interop/src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.15 2006/01/13 03:36:39 rmsimpson Exp $
           15  +** $Id: select.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Allocate a new Select structure and return a pointer to that
    22     22   ** structure.
................................................................................
  1030   1030       memset(&sNC, 0, sizeof(sNC));
  1031   1031       sNC.pSrcList = pSelect->pSrc;
  1032   1032       zType = sqliteStrDup(columnType(&sNC, p));
  1033   1033       pCol->zType = zType;
  1034   1034       pCol->affinity = sqlite3ExprAffinity(p);
  1035   1035       pColl = sqlite3ExprCollSeq(pParse, p);
  1036   1036       if( pColl ){
  1037         -      pCol->zColl = sqlite3StrDup(pColl->zName);
         1037  +      pCol->zColl = sqliteStrDup(pColl->zName);
  1038   1038       }
  1039   1039     }
  1040   1040     pTab->iPKey = -1;
  1041   1041     return pTab;
  1042   1042   }
  1043   1043   
  1044   1044   /*

Changes to SQLite.Interop/src/server.c.

    14     14   ** or linked into the SQLite library unless you use a non-standard option:
    15     15   **
    16     16   **      -DSQLITE_SERVER=1
    17     17   **
    18     18   ** The configure script will never generate a Makefile with the option
    19     19   ** above.  You will need to manually modify the Makefile if you want to
    20     20   ** include any of the code from this file in your project.  Or, at your
    21         -** option, you may want to copy and paste the code from this file and
           21  +** option, you may copy and paste the code from this file and
    22     22   ** thereby avoiding a recompile of SQLite.
    23     23   **
    24     24   **
    25     25   ** This source file demonstrates how to use SQLite to create an SQL database 
    26     26   ** server thread in a multiple-threaded program.  One or more client threads
    27     27   ** send messages to the server thread and the server thread processes those
    28     28   ** messages in the order received and returns the results to the client.
................................................................................
    36     36   **         a thread different from the one where they were created.  With
    37     37   **         the client/server approach, all database connections are created
    38     38   **         and used within the server thread.  Client calls to the database
    39     39   **         can be made from multiple threads (though not at the same time!)
    40     40   **
    41     41   **    (2)  Beginning with SQLite version 3.3.0, when two or more 
    42     42   **         connections to the same database occur within the same thread,
    43         -**         they will share their database cache.  This reduces I/O
    44         -**         and memory requirements.
           43  +**         they can optionally share their database cache.  This reduces
           44  +**         I/O and memory requirements.  Cache shared is controlled using
           45  +**         the sqlite3_enable_shared_cache() API.
    45     46   **
    46     47   **    (3)  Database connections on a shared cache use table-level locking
    47     48   **         instead of file-level locking for improved concurrency.
    48     49   **
    49     50   **    (4)  Database connections on a shared cache can by optionally
    50     51   **         set to READ UNCOMMITTED isolation.  (The default isolation for
    51     52   **         SQLite is SERIALIZABLE.)  When this occurs, readers will
................................................................................
    92     93   **       sqlite3_client_reset
    93     94   **       sqlite3_client_finalize
    94     95   **       sqlite3_client_close
    95     96   **
    96     97   ** These interfaces work exactly like the standard core SQLite interfaces
    97     98   ** having the same names without the "_client_" infix.  Many other SQLite
    98     99   ** interfaces can be used directly without having to send messages to the
    99         -** server.  The following interfaces fall into this second category:
          100  +** server as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined.
          101  +** The following interfaces fall into this second category:
   100    102   **
   101    103   **       sqlite3_bind_*
   102    104   **       sqlite3_changes
   103    105   **       sqlite3_clear_bindings
   104    106   **       sqlite3_column_*
   105    107   **       sqlite3_complete
   106    108   **       sqlite3_create_collation
   107    109   **       sqlite3_create_function
   108    110   **       sqlite3_data_count
   109    111   **       sqlite3_db_handle
   110    112   **       sqlite3_errcode
   111    113   **       sqlite3_errmsg
   112    114   **       sqlite3_last_insert_rowid
   113         -**       sqlite3_libversion
   114         -**       sqlite3_mprintf
   115    115   **       sqlite3_total_changes
   116    116   **       sqlite3_transfer_bindings
   117         -**       sqlite3_vmprintf
   118    117   **
   119    118   ** A single SQLite connection (an sqlite3* object) or an SQLite statement
   120    119   ** (an sqlite3_stmt* object) should only be passed to a single interface
   121         -** function at a time.  The connections and statements can be freely used
   122         -** by any thread as long as only one thread is using them at a time.
          120  +** function at a time.  The connections and statements can be passed from
          121  +** any thread to any of the functions listed in the second group above as
          122  +** long as the same connection is not in use by two threads at once and
          123  +** as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined.  Additional
          124  +** information about the SQLITE_ENABLE_MEMORY_MANAGEMENT constraint is
          125  +** below.
   123    126   **
   124    127   ** The busy handler for all database connections should remain turned
   125    128   ** off.  That means that any lock contention will cause the associated
   126    129   ** sqlite3_client_step() call to return immediately with an SQLITE_BUSY
   127    130   ** error code.  If a busy handler is enabled and lock contention occurs,
   128    131   ** then the entire server thread will block.  This will cause not only
   129    132   ** the requesting client to block but every other database client as
   130    133   ** well.  It is possible to enhance the code below so that lock
   131    134   ** contention will cause the message to be placed back on the top of
   132    135   ** the queue to be tried again later.  But such enhanced processing is
   133    136   ** not included here, in order to keep the example simple.
   134    137   **
   135         -** This code assumes the use of pthreads.  Pthreads implementations
   136         -** are available for windows.  (See, for example
          138  +** This example code assumes the use of pthreads.  Pthreads
          139  +** implementations are available for windows.  (See, for example
   137    140   ** http://sourceware.org/pthreads-win32/announcement.html.)  Or, you
   138    141   ** can translate the locking and thread synchronization code to use
   139    142   ** windows primitives easily enough.  The details are left as an
   140    143   ** exercise to the reader.
          144  +**
          145  +**** Restrictions Associated With SQLITE_ENABLE_MEMORY_MANAGEMENT ****
          146  +**
          147  +** If you compile with SQLITE_ENABLE_MEMORY_MANAGEMENT defined, then
          148  +** SQLite includes code that tracks how much memory is being used by
          149  +** each thread.  These memory counts can become confused if memory
          150  +** is allocated by one thread and then freed by another.  For that
          151  +** reason, when SQLITE_ENABLE_MEMORY_MANAGEMENT is used, all operations
          152  +** that might allocate or free memory should be performanced in the same
          153  +** thread that originally created the database connection.  In that case,
          154  +** many of the operations that are listed above as safe to be performed
          155  +** in separate threads would need to be sent over to the server to be
          156  +** done there.  If SQLITE_ENABLE_MEMORY_MANAGEMENT is defined, then
          157  +** the following functions can be used safely from different threads
          158  +** without messing up the allocation counts:
          159  +**
          160  +**       sqlite3_bind_parameter_name
          161  +**       sqlite3_bind_parameter_index
          162  +**       sqlite3_changes
          163  +**       sqlite3_column_blob
          164  +**       sqlite3_column_count
          165  +**       sqlite3_complete
          166  +**       sqlite3_data_count
          167  +**       sqlite3_db_handle
          168  +**       sqlite3_errcode
          169  +**       sqlite3_errmsg
          170  +**       sqlite3_last_insert_rowid
          171  +**       sqlite3_total_changes
          172  +**
          173  +** The remaining functions are not thread-safe when memory management
          174  +** is enabled.  So one would have to define some new interface routines
          175  +** along the following lines:
          176  +**
          177  +**       sqlite3_client_bind_*
          178  +**       sqlite3_client_clear_bindings
          179  +**       sqlite3_client_column_*
          180  +**       sqlite3_client_create_collation
          181  +**       sqlite3_client_create_function
          182  +**       sqlite3_client_transfer_bindings
          183  +**
          184  +** The example code in this file is intended for use with memory
          185  +** management turned off.  So the implementation of these additional
          186  +** client interfaces is left as an exercise to the reader.
          187  +**
          188  +** It may seem surprising to the reader that the list of safe functions
          189  +** above does not include things like sqlite3_bind_int() or
          190  +** sqlite3_column_int().  But those routines might, in fact, allocate
          191  +** or deallocate memory.  In the case of sqlite3_bind_int(), if the
          192  +** parameter was previously bound to a string that string might need
          193  +** to be deallocated before the new integer value is inserted.  In
          194  +** the case of sqlite3_column_int(), the value of the column might be
          195  +** a UTF-16 string which will need to be converted to UTF-8 then into
          196  +** an integer.
   141    197   */
   142    198   
   143    199   /*
   144    200   ** Only compile the code in this file on UNIX with a THREADSAFE build
   145    201   ** and only if the SQLITE_SERVER macro is defined.
   146    202   */
   147    203   #ifdef SQLITE_SERVER

Changes to SQLite.Interop/src/shell.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code to implement the "sqlite" command line
    13     13   ** utility for accessing SQLite databases.
    14     14   **
    15         -** $Id: shell.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** $Id: shell.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include <stdlib.h>
    18     18   #include <string.h>
    19     19   #include <stdio.h>
    20     20   #include <assert.h>
    21     21   #include "sqlite3.h"
    22     22   #include <ctype.h>

Changes to SQLite.Interop/src/sqlite3.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the SQLite library
    13     13   ** presents to client programs.
    14     14   **
    15         -** @(#) $Id: sqlite3.h,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
           15  +** @(#) $Id: sqlite3.h,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #ifndef _SQLITE3_H_
    18     18   #define _SQLITE3_H_
    19     19   #include <stdarg.h>     /* Needed for the definition of va_list */
    20     20   
    21     21   /*
    22     22   ** Make sure we can call this stuff from C++.
................................................................................
    27     27   
    28     28   /*
    29     29   ** The version of the SQLite library.
    30     30   */
    31     31   #ifdef SQLITE_VERSION
    32     32   # undef SQLITE_VERSION
    33     33   #endif
    34         -#define SQLITE_VERSION         "3.3.0"
           34  +#define SQLITE_VERSION         "3.3.1"
    35     35   
    36     36   /*
    37     37   ** The format of the version string is "X.Y.Z<trailing string>", where
    38     38   ** X is the major version number, Y is the minor version number and Z
    39     39   ** is the release number. The trailing string is often "alpha" or "beta".
    40     40   ** For example "3.1.1beta".
    41     41   **
................................................................................
    44     44   ** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using 
    45     45   ** version 3.1.1 or greater at compile time, programs may use the test 
    46     46   ** (SQLITE_VERSION_NUMBER>=3001001).
    47     47   */
    48     48   #ifdef SQLITE_VERSION_NUMBER
    49     49   # undef SQLITE_VERSION_NUMBER
    50     50   #endif
    51         -#define SQLITE_VERSION_NUMBER 3003000
           51  +#define SQLITE_VERSION_NUMBER 3003001
    52     52   
    53     53   /*
    54     54   ** The version string is also compiled into the library so that a program
    55     55   ** can check to make sure that the lib*.a file and the *.h file are from
    56     56   ** the same version.  The sqlite3_libversion() function returns a pointer
    57     57   ** to the sqlite3_version variable - useful in DLLs which cannot access
    58     58   ** global variables.

Changes to SQLite.Interop/src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Extra interface definitions for those who need them
    21     21   */
................................................................................
   280    280   #define sqliteStrNDup(x,y)       sqlite3StrNDup(x,y)
   281    281   #define sqliteReallocOrFree(x,y) sqlite3ReallocOrFree(x,y)
   282    282   
   283    283   #endif
   284    284   
   285    285   #define sqliteFree(x)          sqlite3FreeX(x)
   286    286   #define sqliteAllocSize(x)     sqlite3AllocSize(x)
          287  +
   287    288   
   288    289   /*
   289    290   ** An instance of this structure might be allocated to store information
   290    291   ** specific to a single thread.
   291    292   **
   292    293   ** To avoid a memory leak on windows, the content of this structure is
   293    294   ** checked at the conclusion of each API call.  If it is all zero, it
................................................................................
  1749   1750   #ifndef SQLITE_OMIT_SHARED_CACHE
  1750   1751     void sqlite3TableLock(Parse *, int, int, u8, const char *);
  1751   1752   #else
  1752   1753     #define sqlite3TableLock(v,w,x,y,z)
  1753   1754   #endif
  1754   1755   
  1755   1756   void sqlite3MallocClearFailed();
  1756         -#ifdef NDEBUG
         1757  +#ifndef SQLITE_MEMDEBUG
  1757   1758     #define sqlite3MallocDisallow()
  1758   1759     #define sqlite3MallocAllow()
  1759   1760   #else
  1760   1761     void sqlite3MallocDisallow();
  1761   1762     void sqlite3MallocAllow();
  1762   1763   #endif
  1763   1764   
  1764   1765   #ifdef SQLITE_SSE
  1765   1766   #include "sseInt.h"
  1766   1767   #endif
  1767   1768   
  1768   1769   #endif

Changes to SQLite.Interop/src/tclsqlite.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** A TCL Interface to SQLite
    13     13   **
    14         -** $Id: tclsqlite.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
           14  +** $Id: tclsqlite.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    15     15   */
    16     16   #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "hash.h"
    20     20   #include "tcl.h"
    21     21   #include <stdlib.h>

Changes to SQLite.Interop/src/tokenize.c.

    11     11   *************************************************************************
    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that splits an SQL input string up into
    15     15   ** individual tokens and sends those tokens one-by-one over to the
    16     16   ** parser for analysis.
    17     17   **
    18         -** $Id: tokenize.c,v 1.18 2006/01/13 03:36:39 rmsimpson Exp $
           18  +** $Id: tokenize.c,v 1.19 2006/01/16 15:51:47 rmsimpson Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   #include <stdlib.h>
    24     24   
    25     25   /*

Changes to SQLite.Interop/src/update.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
           15  +** $Id: update.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** The most recently coded instruction was an OP_Column to retrieve the
    21     21   ** i-th column of table pTab. This routine sets the P3 parameter of the 
    22     22   ** OP_Column to the default value, if any.

Changes to SQLite.Interop/src/utf.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used to translate between UTF-8, 
    13     13   ** UTF-16, UTF-16BE, and UTF-16LE.
    14     14   **
    15         -** $Id: utf.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
           15  +** $Id: utf.c,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    16     16   **
    17     17   ** Notes on UTF-8:
    18     18   **
    19     19   **   Byte-0    Byte-1    Byte-2    Byte-3    Value
    20     20   **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    21     21   **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    22     22   **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
................................................................................
   460    460   char *sqlite3utf16to8(const void *z, int nByte){
   461    461     Mem m;
   462    462     memset(&m, 0, sizeof(m));
   463    463     sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   464    464     sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   465    465     assert( m.flags & MEM_Term );
   466    466     assert( m.flags & MEM_Str );
   467         -  return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3StrDup(m.z);
          467  +  return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z);
   468    468   }
   469    469   
   470    470   /*
   471    471   ** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
   472    472   ** return the number of bytes up to (but not including), the first pair
   473    473   ** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
   474    474   ** then return the number of bytes in the first nChar unicode characters

Changes to SQLite.Interop/src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.15 2006/01/12 20:54:08 rmsimpson Exp $
           17  +** $Id: util.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <stdarg.h>
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
................................................................................
   643    643   ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
   644    644   **
   645    645   ** The number of bytes allocated does not include any overhead inserted by 
   646    646   ** any malloc() wrapper functions that may be called. So the value returned
   647    647   ** is the number of bytes that were available to SQLite using pointer p, 
   648    648   ** regardless of how much memory was actually allocated.
   649    649   */
          650  +#if 0          /* This is never actually used */
   650    651   int sqlite3AllocSize(void *p){
   651    652     return OSSIZEOF(p);
   652    653   }
          654  +#endif
   653    655   
   654    656   /*
   655    657   ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
   656    658   ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
   657    659   ** is because when memory debugging is turned on, these two functions are 
   658    660   ** called via macros that record the current file and line number in the
   659    661   ** ThreadData structure.
................................................................................
  1349   1351   
  1350   1352   #ifndef NDEBUG
  1351   1353   /*
  1352   1354   ** This function sets a flag in the thread-specific-data structure that will
  1353   1355   ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
  1354   1356   */
  1355   1357   void sqlite3MallocDisallow(){
  1356         -  assert(!sqlite3ThreadData()->mallocDisallowed);
  1357         -  sqlite3ThreadData()->mallocDisallowed = 1;
         1358  +  assert( sqlite3ThreadData()->mallocDisallowed>=0 );
         1359  +  sqlite3ThreadData()->mallocDisallowed++;
  1358   1360   }
  1359   1361   
  1360   1362   /*
  1361   1363   ** This function clears the flag set in the thread-specific-data structure set
  1362   1364   ** by sqlite3MallocDisallow().
  1363   1365   */
  1364   1366   void sqlite3MallocAllow(){
  1365         -  assert(sqlite3ThreadData()->mallocDisallowed);
  1366         -  sqlite3ThreadData()->mallocDisallowed = 0;
         1367  +  assert( sqlite3ThreadData()->mallocDisallowed>0 );
         1368  +  sqlite3ThreadData()->mallocDisallowed--;
  1367   1369   }
  1368   1370   #endif

Changes to SQLite.Interop/src/vacuum.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the VACUUM command.
    13     13   **
    14     14   ** Most of the code in this file may be omitted by defining the
    15     15   ** SQLITE_OMIT_VACUUM macro.
    16     16   **
    17         -** $Id: vacuum.c,v 1.15 2006/01/13 03:36:39 rmsimpson Exp $
           17  +** $Id: vacuum.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "vdbeInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   #ifndef SQLITE_OMIT_VACUUM
    24     24   /*

Changes to SQLite.Interop/src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.15 2006/01/13 03:36:39 rmsimpson Exp $
           46  +** $Id: vdbe.c,v 1.16 2006/01/16 15:51:47 rmsimpson Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
   661    661     pTos->flags |= MEM_Real;
   662    662     sqlite3VdbeChangeEncoding(pTos, encoding);
   663    663     break;
   664    664   }
   665    665   
   666    666   /* Opcode: String8 * * P3
   667    667   **
   668         -** P3 points to a nul terminated UTF-8 string that is P1 character long
   669         -** (not counting the nul terminator). This opcode is transformed
          668  +** P3 points to a nul terminated UTF-8 string. This opcode is transformed 
   670    669   ** into an OP_String before it is executed for the first time.
   671    670   */
   672    671   case OP_String8: {         /* same as TK_STRING */
   673    672     assert( pOp->p3!=0 );
   674    673     pOp->opcode = OP_String;
   675    674     pOp->p1 = strlen(pOp->p3);
   676    675   
................................................................................
   683    682       pTos->flags &= ~(MEM_Dyn);
   684    683       pTos->flags |= MEM_Static;
   685    684       if( pOp->p3type==P3_DYNAMIC ){
   686    685         sqliteFree(pOp->p3);
   687    686       }
   688    687       pOp->p3type = P3_DYNAMIC;
   689    688       pOp->p3 = pTos->z;
   690         -    pOp->p1 *= 2;
          689  +    pOp->p1 = pTos->n;
   691    690       break;
   692    691     }
   693    692   #endif
   694    693     /* Otherwise fall through to the next case, OP_String */
   695    694   }
   696    695     
   697    696   /* Opcode: String P1 * P3
   698    697   **
   699         -** The string value P3 of length P1 is pushed onto the stack.
          698  +** The string value P3 of length P1 (bytes) is pushed onto the stack.
   700    699   */
   701    700   case OP_String: {
   702    701     pTos++;
   703    702     assert( pOp->p3!=0 );
   704    703     pTos->flags = MEM_Str|MEM_Static|MEM_Term;
   705    704     pTos->z = pOp->p3;
   706    705     pTos->n = pOp->p1;
................................................................................
  1879   1878     int p2 = pOp->p2;  /* column number to retrieve */
  1880   1879     Cursor *pC = 0;    /* The VDBE cursor */
  1881   1880     char *zRec;        /* Pointer to complete record-data */
  1882   1881     BtCursor *pCrsr;   /* The BTree cursor */
  1883   1882     u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
  1884   1883     u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
  1885   1884     u32 nField;        /* number of fields in the record */
  1886         -  u32 szHdr;         /* Number of bytes in the record header */
  1887   1885     int len;           /* The length of the serialized data for the column */
  1888         -  int offset = 0;    /* Offset into the data */
  1889         -  int idx;           /* Index into the header */
  1890   1886     int i;             /* Loop counter */
  1891   1887     char *zData;       /* Part of the record being decoded */
  1892   1888     Mem sMem;          /* For storing the record being decoded */
  1893   1889   
  1894   1890     sMem.flags = 0;
  1895   1891     assert( p1<p->nCursor );
  1896   1892     pTos++;
................................................................................
  1958   1954       payloadSize = 0;
  1959   1955       pCrsr = 0;
  1960   1956       nField = 0;
  1961   1957     }
  1962   1958   
  1963   1959     /* If payloadSize is 0, then just push a NULL onto the stack. */
  1964   1960     if( payloadSize==0 ){
  1965         -    pTos->flags = MEM_Null;
         1961  +    assert( pTos->flags==MEM_Null );
  1966   1962       break;
  1967   1963     }
  1968   1964   
  1969   1965     assert( p2<nField );
  1970   1966   
  1971   1967     /* Read and parse the table header.  Store the results of the parse
  1972   1968     ** into the record header cache fields of the cursor.
  1973   1969     */
  1974   1970     if( pC && pC->cacheStatus==p->cacheCtr ){
  1975   1971       aType = pC->aType;
  1976   1972       aOffset = pC->aOffset;
  1977   1973     }else{
  1978         -    int avail;    /* Number of bytes of available data */
         1974  +    u8 *zIdx;        /* Index into header */
         1975  +    u8 *zEndHdr;     /* Pointer to first byte after the header */
         1976  +    u32 offset;      /* Offset into the data */
         1977  +    int avail;       /* Number of bytes of available data */
  1979   1978       if( pC && pC->aType ){
  1980   1979         aType = pC->aType;
  1981   1980       }else{
  1982   1981         aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
  1983   1982       }
  1984   1983       aOffset = &aType[nField];
  1985   1984       if( aType==0 ){
................................................................................
  2003   2002         if( avail>=payloadSize ){
  2004   2003           zRec = zData;
  2005   2004           pC->aRow = (u8*)zData;
  2006   2005         }else{
  2007   2006           pC->aRow = 0;
  2008   2007         }
  2009   2008       }
  2010         -    idx = GetVarint((u8*)zData, szHdr);
  2011         -
         2009  +    zIdx = (u8 *)GetVarint((u8*)zData, offset);
  2012   2010   
  2013   2011       /* The KeyFetch() or DataFetch() above are fast and will get the entire
  2014   2012       ** record header in most cases.  But they will fail to get the complete
  2015   2013       ** record header if the record header does not fit on a single page
  2016   2014       ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
  2017   2015       ** acquire the complete header text.
  2018   2016       */
  2019         -    if( !zRec && avail<szHdr ){
  2020         -      rc = sqlite3VdbeMemFromBtree(pCrsr, 0, szHdr, pC->isIndex, &sMem);
         2017  +    if( !zRec && avail<offset ){
         2018  +      rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
  2021   2019         if( rc!=SQLITE_OK ){
  2022   2020           goto op_column_out;
  2023   2021         }
  2024   2022         zData = sMem.z;
  2025   2023       }
         2024  +    zEndHdr = (u8 *)zData + offset;
         2025  +    zIdx = zData + (int)zIdx;
  2026   2026   
  2027   2027       /* Scan the header and use it to fill in the aType[] and aOffset[]
  2028   2028       ** arrays.  aType[i] will contain the type integer for the i-th
  2029   2029       ** column and aOffset[i] will contain the offset from the beginning
  2030   2030       ** of the record to the start of the data for the i-th column
  2031   2031       */
  2032         -    offset = szHdr;
  2033         -    assert( offset>0 );
  2034         -    i = 0;
  2035         -    while( idx<szHdr && i<nField && offset<=payloadSize ){
  2036         -      aOffset[i] = offset;
  2037         -      idx += GetVarint((u8*)&zData[idx], aType[i]);
  2038         -      offset += sqlite3VdbeSerialTypeLen(aType[i]);
  2039         -      i++;
         2032  +    for(i=0; i<nField; i++){
         2033  +      if( zIdx<zEndHdr ){
         2034  +        aOffset[i] = offset;
         2035  +        zIdx += GetVarint(zIdx, aType[i]);
         2036  +        offset += sqlite3VdbeSerialTypeLen(aType[i]);
         2037  +      }else{
         2038  +        /* If i is less that nField, then there are less fields in this
         2039  +        ** record than SetNumColumns indicated there are columns in the
         2040  +        ** table. Set the offset for any extra columns not present in
         2041  +        ** the record to 0. This tells code below to push a NULL onto the
         2042  +        ** stack instead of deserializing a value from the record.
         2043  +        */
         2044  +        aOffset[i] = 0;
         2045  +      }
  2040   2046       }
  2041   2047       Release(&sMem);
  2042   2048       sMem.flags = MEM_Null;
  2043   2049   
  2044         -    /* If i is less that nField, then there are less fields in this
  2045         -    ** record than SetNumColumns indicated there are columns in the
  2046         -    ** table. Set the offset for any extra columns not present in
  2047         -    ** the record to 0. This tells code below to push a NULL onto the
  2048         -    ** stack instead of deserializing a value from the record.
         2050  +    /* If we have read more header data than was contained in the header,
         2051  +    ** or if the end of the last field appears to be past the end of the
         2052  +    ** record, then we must be dealing with a corrupt database.
  2049   2053       */
  2050         -    while( i<nField ){
  2051         -      aOffset[i++] = 0;
  2052         -    }
  2053         -
  2054         -    /* The header should end at the start of data and the data should
  2055         -    ** end at last byte of the record. If this is not the case then
  2056         -    ** we are dealing with a malformed record.
  2057         -    */
  2058         -    if( idx!=szHdr || offset!=payloadSize ){
         2054  +    if( zIdx>zEndHdr || offset>payloadSize ){
  2059   2055         rc = SQLITE_CORRUPT_BKPT;
  2060   2056         goto op_column_out;
  2061   2057       }
  2062   2058   
  2063   2059       /* Remember all aType and aColumn information if we have a cursor
  2064   2060       ** to remember it in. */
  2065   2061       if( pC ){
................................................................................
  4206   4202     int i = p->contextStackTop++;
  4207   4203     Context *pContext;
  4208   4204   
  4209   4205     assert( i>=0 );
  4210   4206     /* FIX ME: This should be allocated as part of the vdbe at compile-time */
  4211   4207     if( i>=p->contextStackDepth ){
  4212   4208       p->contextStackDepth = i+1;
  4213         -    sqlite3ReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1));
         4209  +    sqliteReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1));
  4214   4210       if( p->contextStack==0 ) goto no_mem;
  4215   4211     }
  4216   4212     pContext = &p->contextStack[i];
  4217   4213     pContext->lastRowid = db->lastRowid;
  4218   4214     pContext->nChange = p->nChange;
  4219   4215     pContext->sFifo = p->sFifo;
  4220   4216     sqlite3VdbeFifoInit(&p->sFifo);

Changes to SQLite.Interop/src/vdbe.h.

    11     11   *************************************************************************
    12     12   ** Header file for the Virtual DataBase Engine (VDBE)
    13     13   **
    14     14   ** This header defines the interface to the virtual database engine
    15     15   ** or VDBE.  The VDBE implements an abstract machine that runs a
    16     16   ** simple program to access and modify the underlying database.
    17     17   **
    18         -** $Id: vdbe.h,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
           18  +** $Id: vdbe.h,v 1.15 2006/01/16 15:51:47 rmsimpson Exp $
    19     19   */
    20     20   #ifndef _SQLITE_VDBE_H_
    21     21   #define _SQLITE_VDBE_H_
    22     22   #include <stdio.h>
    23     23   
    24     24   /*
    25     25   ** A single VDBE is an opaque structure named "Vdbe".  Only routines

Changes to SQLite.Interop/src/vdbeapi.c.

   152    152   ** statement is completely executed or an error occurs.
   153    153   */
   154    154   int sqlite3_step(sqlite3_stmt *pStmt){
   155    155     Vdbe *p = (Vdbe*)pStmt;
   156    156     sqlite3 *db;
   157    157     int rc;
   158    158   
   159         -  assert(!sqlite3ThreadDataReadOnly()->mallocFailed);
          159  +  /* Assert that malloc() has not failed */
          160  +  assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
   160    161   
   161    162     if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
   162    163       return SQLITE_MISUSE;
   163    164     }
   164    165     if( p->aborted ){
   165    166       return SQLITE_ABORT;
   166    167     }

Changes to SQLite.Interop/src/vdbeaux.c.

    98     98     int i;
    99     99     VdbeOp *pOp;
   100    100   
   101    101     i = p->nOp;
   102    102     p->nOp++;
   103    103     assert( p->magic==VDBE_MAGIC_INIT );
   104    104     resizeOpArray(p, i+1);
   105         -  if( p->aOp==0 || p->nOp<=i ){
          105  +  if( p->aOp==0 || p->nOpAlloc<=i ){
   106    106       return 0;
   107    107     }
   108    108     pOp = &p->aOp[i];
   109    109     pOp->opcode = op;
   110    110     pOp->p1 = p1;
   111    111     pOp->p2 = p2;
   112    112     pOp->p3 = 0;
................................................................................
   143    143   */
   144    144   int sqlite3VdbeMakeLabel(Vdbe *p){
   145    145     int i;
   146    146     i = p->nLabel++;
   147    147     assert( p->magic==VDBE_MAGIC_INIT );
   148    148     if( i>=p->nLabelAlloc ){
   149    149       p->nLabelAlloc = p->nLabelAlloc*2 + 10;
   150         -    sqlite3ReallocOrFree((void**)&p->aLabel,
          150  +    sqliteReallocOrFree((void**)&p->aLabel,
   151    151                             p->nLabelAlloc*sizeof(p->aLabel[0]));
   152    152     }
   153    153     if( p->aLabel ){
   154    154       p->aLabel[i] = -1;
   155    155     }
   156    156     return -1-i;
   157    157   }

Changes to SQLite.Interop/src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is reponsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.16 2006/01/12 20:54:08 rmsimpson Exp $
           19  +** $Id: where.c,v 1.17 2006/01/16 15:51:47 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
    25     25   */
    26     26   #define BMS  (sizeof(Bitmask)*8)
................................................................................
   639    639         pTerm = &pWC->a[idxTerm];
   640    640         pWC->a[idxNew].iParent = idxTerm;
   641    641       }
   642    642       pTerm->nChild = 2;
   643    643     }
   644    644   #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
   645    645   
   646         -#ifndef SQLITE_OMIT_OR_OPTIMIZATION
          646  +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
   647    647     /* Attempt to convert OR-connected terms into an IN operator so that
   648    648     ** they can make use of indices.  Example:
   649    649     **
   650    650     **      x = expr1  OR  expr2 = x  OR  x = expr3
   651    651     **
   652    652     ** is converted into
   653    653     **
   654    654     **      x IN (expr1,expr2,expr3)
          655  +  **
          656  +  ** This optimization must be omitted if OMIT_SUBQUERY is defined because
          657  +  ** the compiler for the the IN operator is part of sub-queries.
   655    658     */
   656    659     else if( pExpr->op==TK_OR ){
   657    660       int ok;
   658    661       int i, j;
   659    662       int iColumn, iCursor;
   660    663       WhereClause sOr;
   661    664       WhereTerm *pOrTerm;
................................................................................
  1160   1163   
  1161   1164   /*
  1162   1165   ** Generate code that builds a probe for an index.  Details:
  1163   1166   **
  1164   1167   **    *  Check the top nColumn entries on the stack.  If any
  1165   1168   **       of those entries are NULL, jump immediately to brk,
  1166   1169   **       which is the loop exit, since no index entry will match
  1167         -**       if any part of the key is NULL.
         1170  +**       if any part of the key is NULL. Pop (nColumn+nExtra) 
         1171  +**       elements from the stack.
  1168   1172   **
  1169   1173   **    *  Construct a probe entry from the top nColumn entries in
  1170         -**       the stack with affinities appropriate for index pIdx.
         1174  +**       the stack with affinities appropriate for index pIdx. 
         1175  +**       Only nColumn elements are popped from the stack in this case
         1176  +**       (by OP_MakeRecord).
         1177  +**
  1171   1178   */
  1172         -static void buildIndexProbe(Vdbe *v, int nColumn, int brk, Index *pIdx){
         1179  +static void buildIndexProbe(
         1180  +  Vdbe *v, 
         1181  +  int nColumn, 
         1182  +  int nExtra, 
         1183  +  int brk, 
         1184  +  Index *pIdx
         1185  +){
  1173   1186     sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
  1174         -  sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
         1187  +  sqlite3VdbeAddOp(v, OP_Pop, nColumn+nExtra, 0);
  1175   1188     sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
  1176   1189     sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
  1177   1190     sqlite3IndexAffinityStr(v, pIdx);
  1178   1191   }
  1179   1192   
  1180   1193   
  1181   1194   /*
................................................................................
  1206   1219       Vdbe *v = pParse->pVdbe;
  1207   1220   
  1208   1221       sqlite3CodeSubselect(pParse, pX);
  1209   1222       iTab = pX->iTable;
  1210   1223       sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk);
  1211   1224       VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
  1212   1225       pLevel->nIn++;
  1213         -    sqlite3ReallocOrFree((void**)&pLevel->aInLoop,
         1226  +    sqliteReallocOrFree((void**)&pLevel->aInLoop,
  1214   1227                                    sizeof(pLevel->aInLoop[0])*3*pLevel->nIn);
  1215   1228       aIn = pLevel->aInLoop;
  1216   1229       if( aIn ){
  1217   1230         aIn += pLevel->nIn*3 - 3;
  1218   1231         aIn[0] = OP_Next;
  1219   1232         aIn[1] = iTab;
  1220   1233         aIn[2] = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);
................................................................................
  1578   1591   #endif /* SQLITE_OMIT_EXPLAIN */
  1579   1592       pTabItem = &pTabList->a[pLevel->iFrom];
  1580   1593       pTab = pTabItem->pTab;
  1581   1594       iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1582   1595       if( pTab->isTransient || pTab->pSelect ) continue;
  1583   1596       if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
  1584   1597         sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, OP_OpenRead);
         1598  +      if( pTab->nCol<(sizeof(Bitmask)*8) ){
         1599  +        Bitmask b = pTabItem->colUsed;
         1600  +        int n = 0;
         1601  +        for(; b; b=b>>1, n++);
         1602  +        sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-1, n);
         1603  +        assert( n<=pTab->nCol );
         1604  +      }
  1585   1605       }else{
  1586   1606         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  1587   1607       }
  1588   1608       pLevel->iTabCur = pTabItem->iCursor;
  1589   1609       if( (pIx = pLevel->pIdx)!=0 ){
  1590   1610         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
  1591   1611         assert( pIx->pSchema==pTab->pSchema );
................................................................................
  1779   1799         }else{
  1780   1800           testOp = nEq>0 ? OP_IdxGE : OP_Noop;
  1781   1801           topEq = 1;
  1782   1802         }
  1783   1803         if( testOp!=OP_Noop ){
  1784   1804           int nCol = nEq + topLimit;
  1785   1805           pLevel->iMem = pParse->nMem++;
  1786         -        buildIndexProbe(v, nCol, brk, pIdx);
         1806  +        buildIndexProbe(v, nCol, nEq, brk, pIdx);
  1787   1807           if( bRev ){
  1788   1808             int op = topEq ? OP_MoveLe : OP_MoveLt;
  1789   1809             sqlite3VdbeAddOp(v, op, iIdxCur, brk);
  1790   1810           }else{
  1791   1811             sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
  1792   1812           }
  1793   1813         }else if( bRev ){
................................................................................
  1814   1834           btmEq = pTerm->operator & (WO_LE|WO_GE);
  1815   1835           disableTerm(pLevel, pTerm);
  1816   1836         }else{
  1817   1837           btmEq = 1;
  1818   1838         }
  1819   1839         if( nEq>0 || btmLimit ){
  1820   1840           int nCol = nEq + btmLimit;
  1821         -        buildIndexProbe(v, nCol, brk, pIdx);
         1841  +        buildIndexProbe(v, nCol, 0, brk, pIdx);
  1822   1842           if( bRev ){
  1823   1843             pLevel->iMem = pParse->nMem++;
  1824   1844             sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
  1825   1845             testOp = OP_IdxLT;
  1826   1846           }else{
  1827   1847             int op = btmEq ? OP_MoveGe : OP_MoveGt;
  1828   1848             sqlite3VdbeAddOp(v, op, iIdxCur, brk);
................................................................................
  1868   1888         ** and leave the values of those terms on the stack.
  1869   1889         */
  1870   1890         codeAllEqualityTerms(pParse, pLevel, &wc, notReady, brk);
  1871   1891   
  1872   1892         /* Generate a single key that will be used to both start and terminate
  1873   1893         ** the search
  1874   1894         */
  1875         -      buildIndexProbe(v, nEq, brk, pIdx);
         1895  +      buildIndexProbe(v, nEq, 0, brk, pIdx);
  1876   1896         sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
  1877   1897   
  1878   1898         /* Generate code (1) to move to the first matching element of the table.
  1879   1899         ** Then generate code (2) that jumps to "brk" after the cursor is past
  1880   1900         ** the last matching element of the table.  The code (1) is executed
  1881   1901         ** once to initialize the search, the code (2) is executed before each
  1882   1902         ** iteration of the scan to see if the scan has finished. */