Make sqlite3SafetyOn() and sqlite3SafetyOff() macros which disappear when
compiling without -DSQLITE_DEBUG=1. (CVS 4744)

FossilOrigin-Name: 5375ad6b4b652f388469b0ce4e8e78b3f49169bd
diff --git a/src/analyze.c b/src/analyze.c
index b7e917f..7fd6162 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code associated with the ANALYZE command.
 **
-** @(#) $Id: analyze.c,v 1.39 2008/01/17 02:36:28 drh Exp $
+** @(#) $Id: analyze.c,v 1.40 2008/01/23 03:03:05 drh Exp $
 */
 #ifndef SQLITE_OMIT_ANALYZE
 #include "sqliteInt.h"
@@ -414,9 +414,9 @@
   /* Load new statistics out of the sqlite_stat1 table */
   zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
                         sInfo.zDatabase);
-  sqlite3SafetyOff(db);
+  (void)sqlite3SafetyOff(db);
   rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
-  sqlite3SafetyOn(db);
+  (void)sqlite3SafetyOn(db);
   sqlite3_free(zSql);
   return rc;
 }
diff --git a/src/attach.c b/src/attach.c
index af2312c..63ad3c7 100644
--- a/src/attach.c
+++ b/src/attach.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code used to implement the ATTACH and DETACH commands.
 **
-** $Id: attach.c,v 1.69 2008/01/17 16:22:15 drh Exp $
+** $Id: attach.c,v 1.70 2008/01/23 03:03:05 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -186,11 +186,11 @@
   ** we found it.
   */
   if( rc==SQLITE_OK ){
-    sqlite3SafetyOn(db);
+    (void)sqlite3SafetyOn(db);
     sqlite3BtreeEnterAll(db);
     rc = sqlite3Init(db, &zErrDyn);
     sqlite3BtreeLeaveAll(db);
-    sqlite3SafetyOff(db);
+    (void)sqlite3SafetyOff(db);
   }
   if( rc ){
     int iDb = db->nDb - 1;
diff --git a/src/main.c b/src/main.c
index 7fa01c2..2d9d2ca 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.412 2008/01/22 21:30:53 drh Exp $
+** $Id: main.c,v 1.413 2008/01/23 03:03:05 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -134,7 +134,7 @@
   if( !db ){
     return SQLITE_OK;
   }
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckSickOrOk(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
@@ -164,21 +164,7 @@
     sqlite3_mutex_leave(db->mutex);
     return SQLITE_BUSY;
   }
-  assert( !sqlite3SafetyCheck(db) );
-
-  /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
-  ** cannot be opened for some reason. So this routine needs to run in
-  ** that case. But maybe there should be an extra magic value for the
-  ** "failed to open" state.
-  **
-  ** TODO: Coverage tests do not test the case where this condition is
-  ** true. It's hard to see how to cause it without messing with threads.
-  */
-  if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
-    /* printf("DID NOT CLOSE\n"); fflush(stdout); */
-    sqlite3_mutex_leave(db->mutex);
-    return SQLITE_ERROR;
-  }
+  assert( sqlite3SafetyCheckSickOrOk(db) );
 
   for(j=0; j<db->nDb; j++){
     struct Db *pDb = &db->aDb[j];
@@ -240,6 +226,7 @@
   */
   sqlite3_free(db->aDb[1].pSchema);
   sqlite3_mutex_leave(db->mutex);
+  db->magic = SQLITE_MAGIC_CLOSED;
   sqlite3_mutex_free(db->mutex);
   sqlite3_free(db);
   return SQLITE_OK;
@@ -387,7 +374,7 @@
   int (*xBusy)(void*,int),
   void *pArg
 ){
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
@@ -410,7 +397,7 @@
   int (*xProgress)(void*), 
   void *pArg
 ){
-  if( !sqlite3SafetyCheck(db) ){
+  if( sqlite3SafetyCheckOk(db) ){
     sqlite3_mutex_enter(db->mutex);
     if( nOps>0 ){
       db->xProgress = xProgress;
@@ -432,7 +419,7 @@
 ** specified number of milliseconds before returning 0.
 */
 int sqlite3_busy_timeout(sqlite3 *db, int ms){
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   if( ms>0 ){
@@ -448,7 +435,7 @@
 ** Cause any pending operation to stop at its earliest opportunity.
 */
 void sqlite3_interrupt(sqlite3 *db){
-  if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){
+  if( sqlite3SafetyCheckOk(db) ){
     db->u1.isInterrupted = 1;
   }
 }
@@ -474,7 +461,7 @@
   int nName;
 
   assert( sqlite3_mutex_held(db->mutex) );
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   if( zFunctionName==0 ||
@@ -798,7 +785,7 @@
   if( !db ){
     return sqlite3ErrStr(SQLITE_NOMEM);
   }
-  if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
+  if( !sqlite3SafetyCheckSickOrOk(db) || db->errCode==SQLITE_MISUSE ){
     return sqlite3ErrStr(SQLITE_MISUSE);
   }
   sqlite3_mutex_enter(db->mutex);
@@ -840,7 +827,7 @@
   if( !db ){
     return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   }
-  if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
+  if( !sqlite3SafetyCheckSickOrOk(db) || db->errCode==SQLITE_MISUSE ){
     return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   }
   sqlite3_mutex_enter(db->mutex);
@@ -865,7 +852,7 @@
   if( !db || db->mallocFailed ){
     return SQLITE_NOMEM;
   }
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckSickOrOk(db) ){
     return SQLITE_MISUSE;
   }
   return db->errCode & db->errMask;
@@ -886,7 +873,7 @@
   CollSeq *pColl;
   int enc2;
   
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   assert( sqlite3_mutex_held(db->mutex) );
@@ -1000,7 +987,7 @@
   db->pVfs = sqlite3_vfs_find(zVfs);
   if( !db->pVfs ){
     rc = SQLITE_ERROR;
-    db->magic = SQLITE_MAGIC_CLOSED;
+    db->magic = SQLITE_MAGIC_SICK;
     sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)"));
     goto opendb_out;
   }
@@ -1016,7 +1003,7 @@
       (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 
   ){
     assert( db->mallocFailed );
-    db->magic = SQLITE_MAGIC_CLOSED;
+    db->magic = SQLITE_MAGIC_SICK;
     goto opendb_out;
   }
 
@@ -1037,7 +1024,7 @@
                            &db->aDb[0].pBt);
   if( rc!=SQLITE_OK ){
     sqlite3Error(db, rc, 0);
-    db->magic = SQLITE_MAGIC_CLOSED;
+    db->magic = SQLITE_MAGIC_SICK;
     goto opendb_out;
   }
   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
@@ -1253,7 +1240,7 @@
   void *pCollNeededArg, 
   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
 ){
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
@@ -1274,7 +1261,7 @@
   void *pCollNeededArg, 
   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
 ){
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
@@ -1358,7 +1345,7 @@
   int autoinc = 0;
 
   /* Ensure the database schema has been loaded */
-  if( sqlite3SafetyOn(db) ){
+  if( !sqlite3SafetyCheckOk(db) || sqlite3SafetyOn(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
diff --git a/src/os.c b/src/os.c
index 71284cd..57e2d0f 100644
--- a/src/os.c
+++ b/src/os.c
@@ -191,7 +191,9 @@
 ** first VFS on the list.
 */
 sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
+#ifndef SQLITE_MUTEX_NOOP
   sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+#endif
   sqlite3_vfs *pVfs = 0;
   static int isInit = 0;
   sqlite3_mutex_enter(mutex);
@@ -233,7 +235,9 @@
 ** true.
 */
 int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
+#ifndef SQLITE_MUTEX_NOOP
   sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+#endif
   sqlite3_vfs_find(0);  /* Make sure we are initialized */
   sqlite3_mutex_enter(mutex);
   vfsUnlink(pVfs);
@@ -253,7 +257,9 @@
 ** Unregister a VFS so that it is no longer accessible.
 */
 int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
+#ifndef SQLITE_MUTEX_NOOP
   sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+#endif
   sqlite3_mutex_enter(mutex);
   vfsUnlink(pVfs);
   sqlite3_mutex_leave(mutex);
diff --git a/src/prepare.c b/src/prepare.c
index 324b15a..a9bc844 100644
--- a/src/prepare.c
+++ b/src/prepare.c
@@ -13,7 +13,7 @@
 ** interface, and routines that contribute to loading the database schema
 ** from disk.
 **
-** $Id: prepare.c,v 1.74 2008/01/22 19:34:28 drh Exp $
+** $Id: prepare.c,v 1.75 2008/01/23 03:03:05 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -174,7 +174,6 @@
   zMasterName = SCHEMA_TABLE(iDb);
 
   /* Construct the schema tables.  */
-  sqlite3SafetyOff(db);
   azArg[0] = zMasterName;
   azArg[1] = "1";
   azArg[2] = zMasterSchema;
@@ -182,9 +181,10 @@
   initData.db = db;
   initData.iDb = iDb;
   initData.pzErrMsg = pzErrMsg;
+  (void)sqlite3SafetyOff(db);
   rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
+  (void)sqlite3SafetyOn(db);
   if( rc ){
-    sqlite3SafetyOn(db);
     rc = initData.rc;
     goto error_out;
   }
@@ -192,7 +192,6 @@
   if( pTab ){
     pTab->readOnly = 1;
   }
-  sqlite3SafetyOn(db);
 
   /* Create a cursor to hold the database open
   */
@@ -312,7 +311,7 @@
     zSql = sqlite3MPrintf(db, 
         "SELECT name, rootpage, sql FROM '%q'.%s",
         db->aDb[iDb].zName, zMasterName);
-    sqlite3SafetyOff(db);
+    (void)sqlite3SafetyOff(db);
 #ifndef SQLITE_OMIT_AUTHORIZATION
     {
       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
@@ -325,7 +324,7 @@
     }
 #endif
     if( rc==SQLITE_ABORT ) rc = initData.rc;
-    sqlite3SafetyOn(db);
+    (void)sqlite3SafetyOn(db);
     sqlite3_free(zSql);
 #ifndef SQLITE_OMIT_ANALYZE
     if( rc==SQLITE_OK ){
@@ -524,7 +523,7 @@
       if( rc ){
         const char *zDb = db->aDb[i].zName;
         sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
-        sqlite3SafetyOff(db);
+        (void)sqlite3SafetyOff(db);
         return SQLITE_LOCKED;
       }
     }
@@ -536,7 +535,7 @@
     char *zSqlCopy;
     if( SQLITE_MAX_SQL_LENGTH>0 && nBytes>SQLITE_MAX_SQL_LENGTH ){
       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
-      sqlite3SafetyOff(db);
+      (void)sqlite3SafetyOff(db);
       return SQLITE_TOOBIG;
     }
     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
@@ -622,7 +621,7 @@
   const char **pzTail       /* OUT: End of parsed string */
 ){
   int rc;
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
@@ -715,7 +714,7 @@
   const char *zTail8 = 0;
   int rc = SQLITE_OK;
 
-  if( sqlite3SafetyCheck(db) ){
+  if( !sqlite3SafetyCheckOk(db) ){
     return SQLITE_MISUSE;
   }
   sqlite3_mutex_enter(db->mutex);
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 1ba3b8d..1c9951d 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.653 2008/01/22 21:30:53 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.654 2008/01/23 03:03:05 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -590,6 +590,7 @@
 */
 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
+#define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
 
@@ -1816,9 +1817,15 @@
 FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
 void sqlite3RegisterBuiltinFunctions(sqlite3*);
 void sqlite3RegisterDateTimeFunctions(sqlite3*);
-int sqlite3SafetyOn(sqlite3*);
-int sqlite3SafetyOff(sqlite3*);
-int sqlite3SafetyCheck(sqlite3*);
+#ifdef SQLITE_DEBUG
+  int sqlite3SafetyOn(sqlite3*);
+  int sqlite3SafetyOff(sqlite3*);
+#else
+# define sqlite3SafetyOn(A) 0
+# define sqlite3SafetyOff(A) 0
+#endif
+int sqlite3SafetyCheckOk(sqlite3*);
+int sqlite3SafetyCheckSickOrOk(sqlite3*);
 void sqlite3ChangeCookie(Parse*, int);
 
 #ifndef SQLITE_OMIT_TRIGGER
@@ -2008,8 +2015,7 @@
 ** Available fault injectors.  Should be numbered beginning with 0.
 */
 #define SQLITE_FAULTINJECTOR_MALLOC     0
-#define SQLITE_FAULTINJECTOR_SAFETY     1
-#define SQLITE_FAULTINJECTOR_COUNT      2
+#define SQLITE_FAULTINJECTOR_COUNT      1
 
 /*
 ** The interface to the fault injector subsystem.  If the fault injector
diff --git a/src/test_devsym.c b/src/test_devsym.c
index 588f5d2..724b29d 100644
--- a/src/test_devsym.c
+++ b/src/test_devsym.c
@@ -344,4 +344,3 @@
 }
 
 #endif
-
diff --git a/src/util.c b/src/util.c
index e33bcae..f8f02a4 100644
--- a/src/util.c
+++ b/src/util.c
@@ -14,7 +14,7 @@
 ** This file contains functions for allocating memory, comparing
 ** strings, and stuff like that.
 **
-** $Id: util.c,v 1.215 2008/01/22 14:50:17 drh Exp $
+** $Id: util.c,v 1.216 2008/01/23 03:03:05 drh Exp $
 */
 #include "sqliteInt.h"
 #include <stdarg.h>
@@ -660,6 +660,7 @@
 ** call to sqlite3_close(db) and db has been deallocated.  And we do
 ** not want to write into deallocated memory.
 */
+#ifdef SQLITE_DEBUG
 int sqlite3SafetyOn(sqlite3 *db){
   if( db->magic==SQLITE_MAGIC_OPEN ){
     db->magic = SQLITE_MAGIC_BUSY;
@@ -670,38 +671,54 @@
   }
   return 1;
 }
+#endif
 
 /*
 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
 ** when this routine is called.
 */
+#ifdef SQLITE_DEBUG
 int sqlite3SafetyOff(sqlite3 *db){
   if( db->magic==SQLITE_MAGIC_BUSY ){
     db->magic = SQLITE_MAGIC_OPEN;
     return 0;
-  }else {
+  }else{
     db->magic = SQLITE_MAGIC_ERROR;
     db->u1.isInterrupted = 1;
     return 1;
   }
 }
+#endif
 
 /*
 ** Check to make sure we have a valid db pointer.  This test is not
 ** foolproof but it does provide some measure of protection against
 ** misuse of the interface such as passing in db pointers that are
 ** NULL or which have been previously closed.  If this routine returns
-** TRUE it means that the db pointer is invalid and should not be
+** 1 it means that the db pointer is valid and 0 if it should not be
 ** dereferenced for any reason.  The calling function should invoke
 ** SQLITE_MISUSE immediately.
+**
+** sqlite3SafetyCheckOk() requires that the db pointer be valid for
+** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
+** open properly and is not fit for general use but which can be
+** used as an argument to sqlite3_errmsg() or sqlite3_close().
 */
-int sqlite3SafetyCheck(sqlite3 *db){
+int sqlite3SafetyCheckOk(sqlite3 *db){
   int magic;
-  if( db==0 ) return 1;
+  if( db==0 ) return 0;
   magic = db->magic;
-  if( magic!=SQLITE_MAGIC_CLOSED &&
-         magic!=SQLITE_MAGIC_OPEN &&
-         magic!=SQLITE_MAGIC_BUSY ) return 1;
-  return 0;
+  if( magic!=SQLITE_MAGIC_OPEN &&
+      magic!=SQLITE_MAGIC_BUSY ) return 0;
+  return 1;
+}
+int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
+  int magic;
+  if( db==0 ) return 0;
+  magic = db->magic;
+  if( magic!=SQLITE_MAGIC_SICK &&
+      magic!=SQLITE_MAGIC_OPEN &&
+      magic!=SQLITE_MAGIC_BUSY ) return 0;
+  return 1;
 }
diff --git a/src/vdbe.c b/src/vdbe.c
index a827115..0f74f74 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -43,7 +43,7 @@
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.704 2008/01/22 21:30:53 drh Exp $
+** $Id: vdbe.c,v 1.705 2008/01/23 03:03:05 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -3905,7 +3905,7 @@
      "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
      db->aDb[iDb].zName, zMaster, pOp->p4.z);
   if( zSql==0 ) goto no_mem;
-  sqlite3SafetyOff(db);
+  (void)sqlite3SafetyOff(db);
   assert( db->init.busy==0 );
   db->init.busy = 1;
   assert( !db->mallocFailed );
@@ -3913,7 +3913,7 @@
   if( rc==SQLITE_ABORT ) rc = initData.rc;
   sqlite3_free(zSql);
   db->init.busy = 0;
-  sqlite3SafetyOn(db);
+  (void)sqlite3SafetyOn(db);
   if( rc==SQLITE_NOMEM ){
     goto no_mem;
   }
@@ -4798,11 +4798,7 @@
   */
 abort_due_to_interrupt:
   assert( db->u1.isInterrupted );
-  if( db->magic!=SQLITE_MAGIC_BUSY ){
-    rc = SQLITE_MISUSE;
-  }else{
-    rc = SQLITE_INTERRUPT;
-  }
+  rc = SQLITE_INTERRUPT;
   p->rc = rc;
   sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
   goto vdbe_error_halt;
diff --git a/src/vdbeapi.c b/src/vdbeapi.c
index 7d44fb1..df9c75f 100644
--- a/src/vdbeapi.c
+++ b/src/vdbeapi.c
@@ -44,7 +44,9 @@
     rc = SQLITE_OK;
   }else{
     Vdbe *v = (Vdbe*)pStmt;
+#ifndef SQLITE_MUTEX_NOOP
     sqlite3_mutex *mutex = v->db->mutex;
+#endif
     sqlite3_mutex_enter(mutex);
     rc = sqlite3VdbeFinalize(v);
     sqlite3_mutex_leave(mutex);
@@ -81,12 +83,14 @@
 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
   int i;
   int rc = SQLITE_OK;
-  Vdbe *v = (Vdbe*)pStmt;
-  sqlite3_mutex_enter(v->db->mutex);
+#ifndef SQLITE_MUTEX_NOOP
+  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
+#endif
+  sqlite3_mutex_enter(mutex);
   for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
     rc = sqlite3_bind_null(pStmt, i);
   }
-  sqlite3_mutex_leave(v->db->mutex);
+  sqlite3_mutex_leave(mutex);
   return rc;
 }
 
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
index dc4cc1a..c15207b 100644
--- a/src/vdbeaux.c
+++ b/src/vdbeaux.c
@@ -1021,9 +1021,9 @@
     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
     const sqlite3_module *pModule = pCx->pModule;
     p->inVtabMethod = 1;
-    sqlite3SafetyOff(p->db);
+    (void)sqlite3SafetyOff(p->db);
     pModule->xClose(pVtabCursor);
-    sqlite3SafetyOn(p->db);
+    (void)sqlite3SafetyOn(p->db);
     p->inVtabMethod = 0;
   }
 #endif
@@ -1167,9 +1167,9 @@
 
   /* If there are any write-transactions at all, invoke the commit hook */
   if( needXcommit && db->xCommitCallback ){
-    sqlite3SafetyOff(db);
+    (void)sqlite3SafetyOff(db);
     rc = db->xCommitCallback(db->pCommitArg);
-    sqlite3SafetyOn(db);
+    (void)sqlite3SafetyOn(db);
     if( rc ){
       return SQLITE_CONSTRAINT;
     }
@@ -1617,9 +1617,9 @@
   ** error, then it might not have been halted properly.  So halt
   ** it now.
   */
-  sqlite3SafetyOn(db);
+  (void)sqlite3SafetyOn(db);
   sqlite3VdbeHalt(p);
-  sqlite3SafetyOff(db);
+  (void)sqlite3SafetyOff(db);
 
   /* If the VDBE has be run even partially, then transfer the error code
   ** and error message from the VDBE into the main database structure.  But
diff --git a/src/vdbeblob.c b/src/vdbeblob.c
index 1fa71f1..6565294 100644
--- a/src/vdbeblob.c
+++ b/src/vdbeblob.c
@@ -12,7 +12,7 @@
 **
 ** This file contains code used to implement incremental BLOB I/O.
 **
-** $Id: vdbeblob.c,v 1.18 2008/01/10 23:50:11 drh Exp $
+** $Id: vdbeblob.c,v 1.19 2008/01/23 03:03:05 drh Exp $
 */
 
 #include "sqliteInt.h"
@@ -109,7 +109,7 @@
       }
       sqlite3_free(sParse.zErrMsg);
       rc = SQLITE_ERROR;
-      sqlite3SafetyOff(db);
+      (void)sqlite3SafetyOff(db);
       sqlite3BtreeLeaveAll(db);
       goto blob_open_out;
     }
@@ -123,7 +123,7 @@
     if( iCol==pTab->nCol ){
       sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn);
       rc = SQLITE_ERROR;
-      sqlite3SafetyOff(db);
+      (void)sqlite3SafetyOff(db);
       sqlite3BtreeLeaveAll(db);
       goto blob_open_out;
     }
@@ -141,7 +141,7 @@
             sqlite3_snprintf(sizeof(zErr), zErr,
                              "cannot open indexed column for writing");
             rc = SQLITE_ERROR;
-            sqlite3SafetyOff(db);
+            (void)sqlite3SafetyOff(db);
             sqlite3BtreeLeaveAll(db);
             goto blob_open_out;
           }
diff --git a/src/vtab.c b/src/vtab.c
index 5402695..776f36e 100644
--- a/src/vtab.c
+++ b/src/vtab.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code used to help implement virtual tables.
 **
-** $Id: vtab.c,v 1.62 2008/01/17 16:22:15 drh Exp $
+** $Id: vtab.c,v 1.63 2008/01/23 03:03:05 drh Exp $
 */
 #ifndef SQLITE_OMIT_VIRTUALTABLE
 #include "sqliteInt.h"
@@ -93,12 +93,12 @@
 void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
   pVtab->nRef--;
   assert(db);
-  assert(!sqlite3SafetyCheck(db));
+  assert( sqlite3SafetyCheckOk(db) );
   if( pVtab->nRef==0 ){
     if( db->magic==SQLITE_MAGIC_BUSY ){
-      sqlite3SafetyOff(db);
+      (void)sqlite3SafetyOff(db);
       pVtab->pModule->xDisconnect(pVtab);
-      sqlite3SafetyOn(db);
+      (void)sqlite3SafetyOn(db);
     } else {
       pVtab->pModule->xDisconnect(pVtab);
     }
@@ -594,7 +594,7 @@
     if( xDestroy ){
       rc = xDestroy(pTab->pVtab);
     }
-    sqlite3SafetyOn(db);
+    (void)sqlite3SafetyOn(db);
     if( rc==SQLITE_OK ){
       pTab->pVtab = 0;
     }
diff --git a/src/where.c b/src/where.c
index 1ad296e..ae8fb4b 100644
--- a/src/where.c
+++ b/src/where.c
@@ -16,7 +16,7 @@
 ** so is applicable.  Because this module is responsible for selecting
 ** indices, you might also think of this module as the "query optimizer".
 **
-** $Id: where.c,v 1.284 2008/01/19 20:11:26 drh Exp $
+** $Id: where.c,v 1.285 2008/01/23 03:03:05 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -1381,7 +1381,7 @@
     *(int*)&pIdxInfo->nOrderBy = 0;
   }
 
-  sqlite3SafetyOff(pParse->db);
+  (void)sqlite3SafetyOff(pParse->db);
   WHERETRACE(("xBestIndex for %s\n", pTab->zName));
   TRACE_IDX_INPUTS(pIdxInfo);
   rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo);
@@ -1392,10 +1392,8 @@
     }else {
       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
     }
-    sqlite3SafetyOn(pParse->db);
-  }else{
-    rc = sqlite3SafetyOn(pParse->db);
   }
+  (void)sqlite3SafetyOn(pParse->db);
   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
 
   return pIdxInfo->estimatedCost;