Completely rework the sqlite3SetString() primitive so that it honors the
SQLITE_LIMIT_LENGTH and avoids the use of strlen(). (CVS 5374)

FossilOrigin-Name: 8ed04b1e26a55306e4baf3e93fb084514134d603
diff --git a/src/btree.c b/src/btree.c
index fa18163..23df64f 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -9,7 +9,7 @@
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** $Id: btree.c,v 1.472 2008/07/08 17:13:59 danielk1977 Exp $
+** $Id: btree.c,v 1.473 2008/07/08 19:34:07 drh Exp $
 **
 ** This file implements a external (disk-based) database using BTrees.
 ** See the header comment on "btreeInt.h" for additional information.
@@ -6401,23 +6401,18 @@
   ...
 ){
   va_list ap;
-  char *zMsg2;
   if( !pCheck->mxErr ) return;
   pCheck->mxErr--;
   pCheck->nErr++;
   va_start(ap, zFormat);
-  zMsg2 = sqlite3VMPrintf(0, zFormat, ap);
-  va_end(ap);
-  if( zMsg1==0 ) zMsg1 = "";
-  if( pCheck->zErrMsg ){
-    char *zOld = pCheck->zErrMsg;
-    pCheck->zErrMsg = 0;
-    sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
-    sqlite3_free(zOld);
-  }else{
-    sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
+  if( pCheck->errMsg.nChar ){
+    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
   }
-  sqlite3_free(zMsg2);
+  if( zMsg1 ){
+    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
+  }
+  sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
+  va_end(ap);
 }
 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 
@@ -6733,6 +6728,7 @@
   int nRef;
   IntegrityCk sCheck;
   BtShared *pBt = p->pBt;
+  char zErr[100];
 
   sqlite3BtreeEnter(p);
   pBt->db = p->db;
@@ -6770,7 +6766,7 @@
   if( i<=sCheck.nPage ){
     sCheck.anRef[i] = 1;
   }
-  sCheck.zErrMsg = 0;
+  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
 
   /* Check the integrity of the freelist
   */
@@ -6826,7 +6822,8 @@
   sqlite3BtreeLeave(p);
   sqlite3_free(sCheck.anRef);
   *pnErr = sCheck.nErr;
-  return sCheck.zErrMsg;
+  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
+  return sqlite3StrAccumFinish(&sCheck.errMsg);
 }
 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 
diff --git a/src/btreeInt.h b/src/btreeInt.h
index f5783fa..1bb5c6b 100644
--- a/src/btreeInt.h
+++ b/src/btreeInt.h
@@ -9,7 +9,7 @@
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** $Id: btreeInt.h,v 1.22 2008/06/15 02:51:47 drh Exp $
+** $Id: btreeInt.h,v 1.23 2008/07/08 19:34:07 drh Exp $
 **
 ** This file implements a external (disk-based) database using BTrees.
 ** For a detailed discussion of BTrees, refer to
@@ -612,8 +612,8 @@
   int nPage;        /* Number of pages in the database */
   int *anRef;       /* Number of times each page is referenced */
   int mxErr;        /* Stop accumulating errors when this reaches zero */
-  char *zErrMsg;    /* An error message.  NULL if no errors seen. */
   int nErr;         /* Number of messages written to zErrMsg so far */
+  StrAccum errMsg;  /* Accumulate the error message text here */
 };
 
 /*
diff --git a/src/build.c b/src/build.c
index 30037e3..1cff08b 100644
--- a/src/build.c
+++ b/src/build.c
@@ -22,7 +22,7 @@
 **     COMMIT
 **     ROLLBACK
 **
-** $Id: build.c,v 1.487 2008/07/08 14:52:08 drh Exp $
+** $Id: build.c,v 1.488 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -2458,15 +2458,11 @@
       goto exit_create_index;
     }
   }else{
-    char zBuf[30];
     int n;
     Index *pLoop;
     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
-    sqlite3_snprintf(sizeof(zBuf),zBuf,"_%d",n);
-    zName = 0;
-    sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
+    zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
     if( zName==0 ){
-      db->mallocFailed = 1;
       goto exit_create_index;
     }
   }
diff --git a/src/insert.c b/src/insert.c
index afb3914..bada466 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle INSERT statements in SQLite.
 **
-** $Id: insert.c,v 1.244 2008/07/04 10:56:08 danielk1977 Exp $
+** $Id: insert.c,v 1.245 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -1130,10 +1130,10 @@
       case OE_Rollback:
       case OE_Abort:
       case OE_Fail: {
-        char *zMsg = 0;
+        char *zMsg;
         sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError);
-        sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
-                        " may not be NULL", (char*)0);
+        zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
+                              pTab->zName, pTab->aCol[i].zName);
         sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
         break;
       }
diff --git a/src/main.c b/src/main.c
index 74e8aa9..2f31aa5 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.471 2008/07/08 14:52:10 drh Exp $
+** $Id: main.c,v 1.472 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -1737,8 +1737,9 @@
   if( pAutoinc ) *pAutoinc = autoinc;
 
   if( SQLITE_OK==rc && !pTab ){
-    sqlite3SetString(&zErrMsg, "no such table column: ", zTableName, ".", 
-        zColumnName, 0);
+    sqlite3_free(zErrMsg);
+    zErrMsg = sqlite3MPrintf("no such table column: %s.%s", zTableName,
+        zColumnName);
     rc = SQLITE_ERROR;
   }
   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
diff --git a/src/malloc.c b/src/malloc.c
index 36f0fae..351a38c 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -12,7 +12,7 @@
 **
 ** Memory allocation functions used throughout sqlite.
 **
-** $Id: malloc.c,v 1.25 2008/06/23 14:03:45 danielk1977 Exp $
+** $Id: malloc.c,v 1.26 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <stdarg.h>
@@ -631,39 +631,19 @@
 }
 
 /*
-** Create a string from the 2nd and subsequent arguments (up to the
-** first NULL argument), store the string in memory obtained from
-** sqliteMalloc() and make the pointer indicated by the 1st argument
-** point to that string.  The 1st argument must either be NULL or 
-** point to memory obtained from sqliteMalloc().
+** Create a string from the zFromat argument and the va_list that follows.
+** Store the string in memory obtained from sqliteMalloc() and make *pz
+** point to that string.
 */
-void sqlite3SetString(char **pz, ...){
+void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
   va_list ap;
-  int nByte;
-  const char *z;
-  char *zResult;
+  char *z;
 
-  assert( pz!=0 );
-  nByte = 1;
-  va_start(ap, pz);
-  while( (z = va_arg(ap, const char*))!=0 ){
-    nByte += strlen(z);
-  }
+  va_start(ap, zFormat);
+  z = sqlite3VMPrintf(db, zFormat, ap);
   va_end(ap);
   sqlite3_free(*pz);
-  *pz = zResult = sqlite3Malloc(nByte);
-  if( zResult==0 ){
-    return;
-  }
-  *zResult = 0;
-  va_start(ap, pz);
-  while( (z = va_arg(ap, const char*))!=0 ){
-    int n = strlen(z);
-    memcpy(zResult, z, n);
-    zResult += n;
-  }
-  zResult[0] = 0;
-  va_end(ap);
+  *pz = z;
 }
 
 
diff --git a/src/prepare.c b/src/prepare.c
index ec2e1c0..6d984f1 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.88 2008/06/23 16:53:47 danielk1977 Exp $
+** $Id: prepare.c,v 1.89 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -29,8 +29,12 @@
 ){
   if( !pData->db->mallocFailed ){
     if( zObj==0 ) zObj = "?";
-    sqlite3SetString(pData->pzErrMsg, "malformed database schema (",  zObj, ")",
-       zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
+    sqlite3SetString(pData->pzErrMsg, pData->db,
+       "malformed database schema (%s)", zObj);
+    if( zExtra && zExtra[0] ){
+      *pData->pzErrMsg = sqlite3MPrintf(pData->db, "%z - %s",
+                                  *pData->pzErrMsg, zExtra);
+    }
   }
   pData->rc = SQLITE_CORRUPT;
 }
@@ -215,7 +219,7 @@
   sqlite3BtreeEnter(pDb->pBt);
   rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, curMain);
   if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
-    sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
+    sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
     goto leave_error_out;
   }
 
@@ -242,7 +246,7 @@
       rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
     }
     if( rc ){
-      sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
+      sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
       goto leave_error_out;
     }
   }else{
@@ -263,8 +267,8 @@
     }else{
       /* If opening an attached database, the encoding much match ENC(db) */
       if( meta[4]!=ENC(db) ){
-        sqlite3SetString(pzErrMsg, "attached databases must use the same"
-            " text encoding as main database", (char*)0);
+        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
+            " text encoding as main database");
         rc = SQLITE_ERROR;
         goto leave_error_out;
       }
@@ -293,7 +297,7 @@
     pDb->pSchema->file_format = 1;
   }
   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
-    sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
+    sqlite3SetString(pzErrMsg, db, "unsupported file format");
     rc = SQLITE_ERROR;
     goto leave_error_out;
   }
@@ -340,7 +344,6 @@
 #endif
   }
   if( db->mallocFailed ){
-    /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
     rc = SQLITE_NOMEM;
     sqlite3ResetInternalSchema(db, 0);
   }
diff --git a/src/printf.c b/src/printf.c
index dc8d711..5a75fca 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -5,7 +5,7 @@
 ** an historical reference.  Most of the "enhancements" have been backed
 ** out so that the functionality is now the same as standard printf().
 **
-** $Id: printf.c,v 1.87 2008/06/16 20:51:16 drh Exp $
+** $Id: printf.c,v 1.88 2008/07/08 19:34:07 drh Exp $
 **
 **************************************************************************
 **
@@ -221,7 +221,7 @@
 ** seems to make a big difference in determining how fast this beast
 ** will run.
 */
-static void vxprintf(
+void sqlite3VXPrintf(
   StrAccum *pAccum,                  /* Accumulate results here */
   int useExtended,                   /* Allow extended %-conversions */
   const char *fmt,                   /* Format string */
@@ -794,14 +794,14 @@
 void sqlite3StrAccumReset(StrAccum *p){
   if( p->zText!=p->zBase ){
     sqlite3_free(p->zText);
-    p->zText = 0;
   }
+  p->zText = 0;
 }
 
 /*
 ** Initialize a string accumulator
 */
-static void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
+void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
   p->zText = p->zBase = zBase;
   p->nChar = 0;
   p->nAlloc = n;
@@ -821,7 +821,7 @@
   StrAccum acc;
   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
                       db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
-  vxprintf(&acc, 1, zFormat, ap);
+  sqlite3VXPrintf(&acc, 1, zFormat, ap);
   z = sqlite3StrAccumFinish(&acc);
   if( acc.mallocFailed && db ){
     db->mallocFailed = 1;
@@ -852,7 +852,7 @@
   StrAccum acc;
   sqlite3_initialize();
   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
-  vxprintf(&acc, 0, zFormat, ap);
+  sqlite3VXPrintf(&acc, 0, zFormat, ap);
   z = sqlite3StrAccumFinish(&acc);
   return z;
 }
@@ -888,7 +888,7 @@
   sqlite3StrAccumInit(&acc, zBuf, n, 0);
   acc.useMalloc = 0;
   va_start(ap,zFormat);
-  vxprintf(&acc, 0, zFormat, ap);
+  sqlite3VXPrintf(&acc, 0, zFormat, ap);
   va_end(ap);
   z = sqlite3StrAccumFinish(&acc);
   return z;
@@ -907,7 +907,7 @@
   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
   acc.useMalloc = 0;
   va_start(ap,zFormat);
-  vxprintf(&acc, 0, zFormat, ap);
+  sqlite3VXPrintf(&acc, 0, zFormat, ap);
   va_end(ap);
   sqlite3StrAccumFinish(&acc);
   fprintf(stdout,"%s", zBuf);
diff --git a/src/select.c b/src/select.c
index 6effc49..f7c5780 100644
--- a/src/select.c
+++ b/src/select.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.449 2008/07/08 18:05:26 drh Exp $
+** $Id: select.c,v 1.450 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -1111,7 +1111,7 @@
  
         zTab = pTabList->a[j].zAlias;
         if( fullNames || zTab==0 ) zTab = pTab->zName;
-        sqlite3SetString(&zName, zTab, ".", zCol, (char*)0);
+        zName = sqlite3MPrintf(db, "%s.%s", zTab, zCol);
         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P4_DYNAMIC);
       }else{
         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol));
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 7be6793..e56f6f5 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.735 2008/07/08 14:52:10 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.736 2008/07/08 19:34:07 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1826,6 +1826,7 @@
 
 int sqlite3IsNaN(double);
 
+void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
 char *sqlite3MPrintf(sqlite3*,const char*, ...);
 char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
@@ -1834,7 +1835,7 @@
 #if defined(SQLITE_TEST)
   void *sqlite3TextToPtr(const char*);
 #endif
-void sqlite3SetString(char **, ...);
+void sqlite3SetString(char **, sqlite3*, const char*, ...);
 void sqlite3ErrorMsg(Parse*, const char*, ...);
 void sqlite3ErrorClear(Parse*);
 void sqlite3Dequote(char*);
@@ -2146,6 +2147,7 @@
 int sqlite3ApiExit(sqlite3 *db, int);
 int sqlite3OpenTempDatabase(Parse *);
 
+void sqlite3StrAccumInit(StrAccum*, char*, int, int);
 void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 char *sqlite3StrAccumFinish(StrAccum*);
 void sqlite3StrAccumReset(StrAccum*);
diff --git a/src/tokenize.c b/src/tokenize.c
index 05622da..c7769f9 100644
--- a/src/tokenize.c
+++ b/src/tokenize.c
@@ -15,7 +15,7 @@
 ** individual tokens and sends those tokens one-by-one over to the
 ** parser for analysis.
 **
-** $Id: tokenize.c,v 1.145 2008/07/08 00:06:50 drh Exp $
+** $Id: tokenize.c,v 1.146 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -421,7 +421,7 @@
       case TK_COMMENT: {
         if( db->u1.isInterrupted ){
           pParse->rc = SQLITE_INTERRUPT;
-          sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
+          sqlite3SetString(pzErrMsg, db, "interrupt");
           goto abort_parse;
         }
         break;
@@ -460,7 +460,7 @@
     pParse->rc = SQLITE_NOMEM;
   }
   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
-    sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
+    sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
   }
   if( pParse->zErrMsg ){
     if( *pzErrMsg==0 ){
diff --git a/src/vacuum.c b/src/vacuum.c
index 89e9a28..45bc61e 100644
--- a/src/vacuum.c
+++ b/src/vacuum.c
@@ -14,7 +14,7 @@
 ** Most of the code in this file may be omitted by defining the
 ** SQLITE_OMIT_VACUUM macro.
 **
-** $Id: vacuum.c,v 1.80 2008/06/17 01:03:26 drh Exp $
+** $Id: vacuum.c,v 1.81 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include "vdbeInt.h"
@@ -96,8 +96,7 @@
   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
 
   if( !db->autoCommit ){
-    sqlite3SetString(pzErrMsg, "cannot VACUUM from within a transaction", 
-       (char*)0);
+    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
     rc = SQLITE_ERROR;
     goto end_of_vacuum;
   }
diff --git a/src/vdbe.c b/src/vdbe.c
index 255ce71..69bf418 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.758 2008/07/07 17:13:09 danielk1977 Exp $
+** $Id: vdbe.c,v 1.759 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -830,7 +830,7 @@
   p->pc = pc;
   p->errorAction = pOp->p2;
   if( pOp->p4.z ){
-    sqlite3SetString(&p->zErrMsg, pOp->p4.z, (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
   }
   rc = sqlite3VdbeHalt(p);
   assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
@@ -1361,7 +1361,7 @@
 
   /* If the function returned an error, throw an exception */
   if( ctx.isError ){
-    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
     rc = ctx.isError;
   }
 
@@ -2447,8 +2447,9 @@
     ** still running, and a transaction is active, return an error indicating
     ** that the other VMs must complete first. 
     */
-    sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit", 
-        " transaction - SQL statements in progress", (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "cannot %s transaction - "
+        "SQL statements in progress",
+        rollback ? "rollback" : "commit");
     rc = SQLITE_ERROR;
   }else if( i!=db->autoCommit ){
     if( pOp->p2 ){
@@ -2471,10 +2472,10 @@
     }
     goto vdbe_return;
   }else{
-    sqlite3SetString(&p->zErrMsg,
+    sqlite3SetString(&p->zErrMsg, db,
         (!i)?"cannot start a transaction within a transaction":(
         (rollback)?"cannot rollback - no transaction is active":
-                   "cannot commit - no transaction is active"), (char*)0);
+                   "cannot commit - no transaction is active"));
          
     rc = SQLITE_ERROR;
   }
@@ -4473,7 +4474,7 @@
   }
   (ctx.pFunc->xStep)(&ctx, n, apVal);
   if( ctx.isError ){
-    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
     rc = ctx.isError;
   }
   sqlite3VdbeMemRelease(&ctx.s);
@@ -4499,7 +4500,7 @@
   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
   rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
   if( rc==SQLITE_ERROR ){
-    sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
   }
   sqlite3VdbeChangeEncoding(pMem, encoding);
   UPDATE_MAX_BLOBSIZE(pMem);
@@ -4589,7 +4590,7 @@
   rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
   if( rc==SQLITE_LOCKED ){
     const char *z = pOp->p4.z;
-    sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
   }
   break;
 }
@@ -4912,7 +4913,7 @@
   int nArg = pOp->p2;
   assert( pOp->p4type==P4_VTAB );
   if( pModule->xUpdate==0 ){
-    sqlite3SetString(&p->zErrMsg, "read-only table", 0);
+    sqlite3SetString(&p->zErrMsg, db, "read-only table");
     rc = SQLITE_ERROR;
   }else{
     int i;
@@ -5058,7 +5059,7 @@
   ** is encountered.
   */
 too_big:
-  sqlite3SetString(&p->zErrMsg, "string or blob too big", (char*)0);
+  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
   rc = SQLITE_TOOBIG;
   goto vdbe_error_halt;
 
@@ -5066,7 +5067,7 @@
   */
 no_mem:
   db->mallocFailed = 1;
-  sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
+  sqlite3SetString(&p->zErrMsg, db, "out of memory");
   rc = SQLITE_NOMEM;
   goto vdbe_error_halt;
 
@@ -5083,7 +5084,7 @@
   assert( p->zErrMsg==0 );
   if( db->mallocFailed ) rc = SQLITE_NOMEM;
   if( rc!=SQLITE_IOERR_NOMEM ){
-    sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
   }
   goto vdbe_error_halt;
 
@@ -5094,6 +5095,6 @@
   assert( db->u1.isInterrupted );
   rc = SQLITE_INTERRUPT;
   p->rc = rc;
-  sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
+  sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
   goto vdbe_error_halt;
 }
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
index e2b235b..0a7dc6e 100644
--- a/src/vdbeaux.c
+++ b/src/vdbeaux.c
@@ -14,7 +14,7 @@
 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
 ** But that file was getting too big so this subroutines were split out.
 **
-** $Id: vdbeaux.c,v 1.393 2008/06/25 00:12:42 drh Exp $
+** $Id: vdbeaux.c,v 1.394 2008/07/08 19:34:07 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -832,7 +832,7 @@
   }else if( db->u1.isInterrupted ){
     p->rc = SQLITE_INTERRUPT;
     rc = SQLITE_ERROR;
-    sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
+    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
   }else{
     char *z;
     Op *pOp = &p->aOp[i];
@@ -1632,7 +1632,8 @@
         rc = xFunc(pBt);
         if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
           p->rc = rc;
-          sqlite3SetString(&p->zErrMsg, 0);
+          sqlite3_free(p->zErrMsg);
+          p->zErrMsg = 0;
         }
       }
     }