/ Check-in [823ad40c]
Login

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

Overview
Comment:Improvements to SQLITE_ENABLE_API_ARMOR.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:823ad40ccb5b51aaa0d5a48da63b465df9d0649a
User & Date: drh 2015-02-13 16:36:14
Context
2015-02-19
00:29
First small steps toward brining trunk and apple-osx closer together. check-in: 28284ccc user: drh tags: trunk
2015-02-13
16:42
Enhancements to SQLITE_ENABLE_API_ARMOR merged from trunk. check-in: 5147b94a user: drh tags: apple-osx
16:36
Improvements to SQLITE_ENABLE_API_ARMOR. check-in: 823ad40c user: drh tags: trunk
12:05
Make sure the prepared statement auto-resets on extended error codes of SQLITE_BUSY and SQLITE_LOCKED even when compiled using SQLITE_OMIT_AUTORESET. check-in: 3c6ca414 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/build.c.

   304    304   **
   305    305   ** See also sqlite3LocateTable().
   306    306   */
   307    307   Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   308    308     Table *p = 0;
   309    309     int i;
   310    310   
   311         -#ifdef SQLITE_ENABLE_API_ARMOR
   312         -  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0;
   313         -#endif
   314         -
   315    311     /* All mutexes are required for schema access.  Make sure we hold them. */
   316    312     assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   317    313   #if SQLITE_USER_AUTHENTICATION
   318    314     /* Only the admin user is allowed to know that the sqlite_user table
   319    315     ** exists */
   320    316     if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
   321    317       return 0;

Changes to src/main.c.

  1410   1410   */
  1411   1411   int sqlite3_busy_handler(
  1412   1412     sqlite3 *db,
  1413   1413     int (*xBusy)(void*,int),
  1414   1414     void *pArg
  1415   1415   ){
  1416   1416   #ifdef SQLITE_ENABLE_API_ARMOR
  1417         -  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE;
         1417  +  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
  1418   1418   #endif
  1419   1419     sqlite3_mutex_enter(db->mutex);
  1420   1420     db->busyHandler.xFunc = xBusy;
  1421   1421     db->busyHandler.pArg = pArg;
  1422   1422     db->busyHandler.nBusy = 0;
  1423   1423     db->busyTimeout = 0;
  1424   1424     sqlite3_mutex_leave(db->mutex);
................................................................................
  3138   3138     int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  3139   3139   ){
  3140   3140     int rc;
  3141   3141     char *zErrMsg = 0;
  3142   3142     Table *pTab = 0;
  3143   3143     Column *pCol = 0;
  3144   3144     int iCol = 0;
  3145         -
  3146   3145     char const *zDataType = 0;
  3147   3146     char const *zCollSeq = 0;
  3148   3147     int notnull = 0;
  3149   3148     int primarykey = 0;
  3150   3149     int autoinc = 0;
  3151   3150   
         3151  +
         3152  +#ifdef SQLITE_ENABLE_API_ARMOR
         3153  +  if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
         3154  +    return SQLITE_MISUSE_BKPT;
         3155  +  }
         3156  +#endif
         3157  +
  3152   3158     /* Ensure the database schema has been loaded */
  3153   3159     sqlite3_mutex_enter(db->mutex);
  3154   3160     sqlite3BtreeEnterAll(db);
  3155   3161     rc = sqlite3Init(db, &zErrMsg);
  3156   3162     if( SQLITE_OK!=rc ){
  3157   3163       goto error_out;
  3158   3164     }

Changes to src/mutex_noop.c.

   116    116         if( pNew ){
   117    117           pNew->id = id;
   118    118           pNew->cnt = 0;
   119    119         }
   120    120         break;
   121    121       }
   122    122       default: {
   123         -      assert( id-2 >= 0 );
   124         -      assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
          123  +#ifdef SQLITE_ENABLE_API_ARMOR
          124  +      if( id-2<0 || id-2>=ArraySize(aStatic) ){
          125  +        (void)SQLITE_MISUSE_BKPT;
          126  +        return 0;
          127  +      }
          128  +#endif
   125    129         pNew = &aStatic[id-2];
   126    130         pNew->id = id;
   127    131         break;
   128    132       }
   129    133     }
   130    134     return (sqlite3_mutex*)pNew;
   131    135   }
................................................................................
   132    136   
   133    137   /*
   134    138   ** This routine deallocates a previously allocated mutex.
   135    139   */
   136    140   static void debugMutexFree(sqlite3_mutex *pX){
   137    141     sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   138    142     assert( p->cnt==0 );
   139         -  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   140         -  sqlite3_free(p);
          143  +  if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
          144  +    sqlite3_free(p);
          145  +  }else{
          146  +#ifdef SQLITE_ENABLE_API_ARMOR
          147  +    (void)SQLITE_MISUSE_BKPT;
          148  +#endif
          149  +  }
   141    150   }
   142    151   
   143    152   /*
   144    153   ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   145    154   ** to enter a mutex.  If another thread is already within the mutex,
   146    155   ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   147    156   ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK

Changes to src/mutex_unix.c.

    36     36   #endif
    37     37   
    38     38   /*
    39     39   ** Each recursive mutex is an instance of the following structure.
    40     40   */
    41     41   struct sqlite3_mutex {
    42     42     pthread_mutex_t mutex;     /* Mutex controlling the lock */
    43         -#if SQLITE_MUTEX_NREF
           43  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
    44     44     int id;                    /* Mutex type */
           45  +#endif
           46  +#if SQLITE_MUTEX_NREF
    45     47     volatile int nRef;         /* Number of entrances */
    46     48     volatile pthread_t owner;  /* Thread that is within this mutex */
    47     49     int trace;                 /* True to trace changes */
    48     50   #endif
    49     51   };
    50     52   #if SQLITE_MUTEX_NREF
    51     53   #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
................................................................................
   154    156           /* Use a recursive mutex if it is available */
   155    157           pthread_mutexattr_t recursiveAttr;
   156    158           pthread_mutexattr_init(&recursiveAttr);
   157    159           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
   158    160           pthread_mutex_init(&p->mutex, &recursiveAttr);
   159    161           pthread_mutexattr_destroy(&recursiveAttr);
   160    162   #endif
   161         -#if SQLITE_MUTEX_NREF
   162         -        p->id = iType;
   163         -#endif
   164    163         }
   165    164         break;
   166    165       }
   167    166       case SQLITE_MUTEX_FAST: {
   168    167         p = sqlite3MallocZero( sizeof(*p) );
   169    168         if( p ){
   170         -#if SQLITE_MUTEX_NREF
   171         -        p->id = iType;
   172         -#endif
   173    169           pthread_mutex_init(&p->mutex, 0);
   174    170         }
   175    171         break;
   176    172       }
   177    173       default: {
   178    174   #ifdef SQLITE_ENABLE_API_ARMOR
   179    175         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
   180    176           (void)SQLITE_MISUSE_BKPT;
   181    177           return 0;
   182    178         }
   183    179   #endif
   184    180         p = &staticMutexes[iType-2];
   185         -#if SQLITE_MUTEX_NREF
   186         -      p->id = iType;
   187         -#endif
   188    181         break;
   189    182       }
   190    183     }
          184  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
          185  +  if( p ) p->id = iType;
          186  +#endif
   191    187     return p;
   192    188   }
   193    189   
   194    190   
   195    191   /*
   196    192   ** This routine deallocates a previously
   197    193   ** allocated mutex.  SQLite is careful to deallocate every
   198    194   ** mutex that it allocates.
   199    195   */
   200    196   static void pthreadMutexFree(sqlite3_mutex *p){
   201    197     assert( p->nRef==0 );
   202         -  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   203         -  pthread_mutex_destroy(&p->mutex);
   204         -  sqlite3_free(p);
          198  +#if SQLITE_ENABLE_API_ARMOR
          199  +  if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
          200  +#endif
          201  +  {
          202  +    pthread_mutex_destroy(&p->mutex);
          203  +    sqlite3_free(p);
          204  +  }
          205  +#ifdef SQLITE_ENABLE_API_ARMOR
          206  +  else{
          207  +    (void)SQLITE_MISUSE_BKPT;
          208  +  }
          209  +#endif
   205    210   }
   206    211   
   207    212   /*
   208    213   ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   209    214   ** to enter a mutex.  If another thread is already within the mutex,
   210    215   ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   211    216   ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK

Changes to src/mutex_w32.c.

   211    211       default: {
   212    212   #ifdef SQLITE_ENABLE_API_ARMOR
   213    213         if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
   214    214           (void)SQLITE_MISUSE_BKPT;
   215    215           return 0;
   216    216         }
   217    217   #endif
   218         -      assert( iType-2 >= 0 );
   219         -      assert( iType-2 < ArraySize(winMutex_staticMutexes) );
   220         -      assert( winMutex_isInit==1 );
   221    218         p = &winMutex_staticMutexes[iType-2];
   222    219   #ifdef SQLITE_DEBUG
   223    220         p->id = iType;
   224    221   #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
   225    222         p->trace = 1;
   226    223   #endif
   227    224   #endif
................................................................................
   235    232   /*
   236    233   ** This routine deallocates a previously
   237    234   ** allocated mutex.  SQLite is careful to deallocate every
   238    235   ** mutex that it allocates.
   239    236   */
   240    237   static void winMutexFree(sqlite3_mutex *p){
   241    238     assert( p );
   242         -#ifdef SQLITE_DEBUG
   243    239     assert( p->nRef==0 && p->owner==0 );
   244         -  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
          240  +  if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
          241  +    DeleteCriticalSection(&p->mutex);
          242  +    sqlite3_free(p);
          243  +  }else{
          244  +#ifdef SQLITE_ENABLE_API_ARMOR
          245  +    (void)SQLITE_MISUSE_BKPT;
   245    246   #endif
   246         -  assert( winMutex_isInit==1 );
   247         -  DeleteCriticalSection(&p->mutex);
   248         -  sqlite3_free(p);
          247  +  }
   249    248   }
   250    249   
   251    250   /*
   252    251   ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   253    252   ** to enter a mutex.  If another thread is already within the mutex,
   254    253   ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   255    254   ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK

Changes to src/printf.c.

   991    991   */
   992    992   char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
   993    993     StrAccum acc;
   994    994     if( n<=0 ) return zBuf;
   995    995   #ifdef SQLITE_ENABLE_API_ARMOR
   996    996     if( zBuf==0 || zFormat==0 ) {
   997    997       (void)SQLITE_MISUSE_BKPT;
   998         -    if( zBuf && n>0 ) zBuf[0] = 0;
          998  +    if( zBuf ) zBuf[0] = 0;
   999    999       return zBuf;
  1000   1000     }
  1001   1001   #endif
  1002   1002     sqlite3StrAccumInit(&acc, zBuf, n, 0);
  1003   1003     acc.useMalloc = 0;
  1004   1004     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1005   1005     return sqlite3StrAccumFinish(&acc);

Changes to src/tokenize.c.

   386    386     void *pEngine;                  /* The LEMON-generated LALR(1) parser */
   387    387     int tokenType;                  /* type of the next token */
   388    388     int lastTokenParsed = -1;       /* type of the previous token */
   389    389     u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
   390    390     sqlite3 *db = pParse->db;       /* The database connection */
   391    391     int mxSqlLen;                   /* Max length of an SQL string */
   392    392   
   393         -
   394         -#ifdef SQLITE_ENABLE_API_ARMOR
   395         -  if( zSql==0 || pzErrMsg==0 ) return SQLITE_MISUSE_BKPT;
   396         -#endif
          393  +  assert( zSql!=0 );
   397    394     mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   398    395     if( db->nVdbeActive==0 ){
   399    396       db->u1.isInterrupted = 0;
   400    397     }
   401    398     pParse->rc = SQLITE_OK;
   402    399     pParse->zTail = zSql;
   403    400     i = 0;

Changes to src/vtab.c.

   706    706     Parse *pParse;
   707    707   
   708    708     int rc = SQLITE_OK;
   709    709     Table *pTab;
   710    710     char *zErr = 0;
   711    711   
   712    712   #ifdef SQLITE_ENABLE_API_ARMOR
   713         -  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
          713  +  if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
          714  +    return SQLITE_MISUSE_BKPT;
          715  +  }
   714    716   #endif
   715    717     sqlite3_mutex_enter(db->mutex);
   716    718     if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
   717    719       sqlite3Error(db, SQLITE_MISUSE);
   718    720       sqlite3_mutex_leave(db->mutex);
   719    721       return SQLITE_MISUSE_BKPT;
   720    722     }