Improved enforcement of the SQLITE_LIMIT_LENGTH limit. (CVS 5368)

FossilOrigin-Name: ee93150878436ce6e992ea8a1d348fb58b03b5e2
diff --git a/manifest b/manifest
index bc4979d..c0bc3ed 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Another\scorruption\srelated\stest\scase.\s(CVS\s5367)
-D 2008-07-08T14:31:15
+C Improved\senforcement\sof\sthe\sSQLITE_LIMIT_LENGTH\slimit.\s(CVS\s5368)
+D 2008-07-08T14:52:08
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 325dfac0a0dd1cb4d975f1ace6453157892e6042
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -98,15 +98,15 @@
 F src/btree.c e00268557794be741e26cbca1cd77aa37e53b1cc
 F src/btree.h b1bd7e0b8c2e33658aaf447cb0d1d94f74664b6b
 F src/btreeInt.h 02325f04758dba0fcd0c08ac55cd9b189dad61a5
-F src/build.c 8779e9276bef80143a9db5d51b19301457680fc1
-F src/callback.c 3ba98ae46f60aa7c2c40eac7d18fe5ba9b706b83
+F src/build.c 142ceacdc14e479f337aeb01db631f22683496f8
+F src/callback.c aa492a0ad8c2d454edff9fb8a57fae13743cf71d
 F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c
 F src/date.c e841168e5520bbbb2a1cbcdce7531d8b23017b4d
 F src/delete.c d3fc5987f2eb88f7b9549d58a5dfea079a83fe8b
 F src/expr.c 8f2caa888af148ebf9cdb2cf7ffaae2a09767c48
 F src/fault.c 3638519d1e0b82bccfafcb9f5ff491918b28f8e1
 F src/func.c 1e7d9569570134ac0771a00382d9d4b41c4aa052
-F src/global.c 2304cfa3288763bd2fed10caf8c6fbaa2b383f4e
+F src/global.c f12dc84d23f52d2ed42d1bdb5460fa3caf63e890
 F src/hash.c eb64e48f3781100e5934f759fbe72a63a8fe78cb
 F src/hash.h 031cd9f915aff27e12262cb9eb570ac1b8326b53
 F src/hwtime.h 745961687a65ef8918cd551c02e5ccb4b8e772de
@@ -114,7 +114,7 @@
 F src/journal.c cffd2cd214e58c0e99c3ff632b3bee6c7cbb260e
 F src/legacy.c 3626c71fb70912abec9a4312beba753a9ce800df
 F src/loadext.c ae0eed9fa96d74172d2a90ee63b5bc36d284295c
-F src/main.c 9d5efc560928db31c20475b5784191654e660538
+F src/main.c 7f69c952cf2a95f21439cd260224d5341b705596
 F src/malloc.c f52166df8abd7ff6990dbee1a0ce3534addc8617
 F src/md5.c 008216bbb5d34c6fbab5357aa68575ad8a31516a
 F src/mem1.c 8340fa5f969e9f9b9bdeb54106457a2003456d2b
@@ -144,7 +144,7 @@
 F src/shell.c 484e7297e066f22830f9c15d7abbcdd2acb097b0
 F src/sqlite.h.in 22c32cedf7e435d5b3bb8564ce0a640c40dbbfb1
 F src/sqlite3ext.h 1e3887c9bd3ae66cb599e922824b04cd0d0f2c3e
-F src/sqliteInt.h 35e420ced9e27af5ee387cd40f2e7a431665f858
+F src/sqliteInt.h 46e40a482ae3b93a3c26eaf6a82ea6502653d474
 F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8
 F src/status.c 6cb10377992505bd69f1ca1d75c1240a65f25a58
 F src/table.c 1fa8f8113ac9cbc09ae4801c6d2a7f0af82c5822
@@ -179,7 +179,7 @@
 F src/trigger.c 1e751f8d5ceeb328d26bf1ccfb2de50653670d49
 F src/update.c 2d7143b9014e955509cc4f323f9a9584fb898f34
 F src/utf.c 8d52f620a7153d90b058502124fe51d821fcdf57
-F src/util.c aec21ebb1e21f6220af1237078b1723968803990
+F src/util.c e202291456d34f8e2cf1c0a6f7e2538a77587445
 F src/vacuum.c 14eb21b480924d87e791cd8ab6fb35ac563243ef
 F src/vdbe.c fb6df1bd31a321647b4677f5d20716d8f7f71255
 F src/vdbe.h c46155c221418bea29ee3a749d5950fcf85a70e2
@@ -188,7 +188,7 @@
 F src/vdbeaux.c 3f41742bf0588e68e48b32e5cb74cb5f19a8fe45
 F src/vdbeblob.c 9345f6dcd675fdcfdb537d2d2f487542d9ea136a
 F src/vdbefifo.c c46dae1194e4277bf007144d7e5b0c0b1c24f136
-F src/vdbemem.c a39a822e6ae61c4cab4a512df4a315888b206911
+F src/vdbemem.c ee1a032b44ab59bd83a064544a0895003eecaec1
 F src/vtab.c 2096c03ec5540a43c8c73a8f43407dfd3549a982
 F src/where.c 6a22ceb86d774c4428ac53196f721bb14e6cbdfa
 F tclinstaller.tcl 4356d9d94d2b5ed5e68f9f0c80c4df3048dd7617
@@ -600,7 +600,7 @@
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P 94c95fad56965b68176e93f0690f0819ad40bcf7
-R 44e2f1c0e8d85d0179e55f7050a50307
-U danielk1977
-Z 66c4a0a833d5a6c4eb7405af47a10264
+P f781a68127c97e30763447a12314180f0728deb9
+R cfd14fea32bc656ba7f0a52d82871485
+U drh
+Z 00aff89bc5ac4605bda301a29f51994c
diff --git a/manifest.uuid b/manifest.uuid
index 0cb1ef1..d79de35 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-f781a68127c97e30763447a12314180f0728deb9
\ No newline at end of file
+ee93150878436ce6e992ea8a1d348fb58b03b5e2
\ No newline at end of file
diff --git a/src/build.c b/src/build.c
index 8fe2596..30037e3 100644
--- a/src/build.c
+++ b/src/build.c
@@ -22,7 +22,7 @@
 **     COMMIT
 **     ROLLBACK
 **
-** $Id: build.c,v 1.486 2008/07/08 00:06:50 drh Exp $
+** $Id: build.c,v 1.487 2008/07/08 14:52:08 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -276,11 +276,13 @@
 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   Table *p = 0;
   int i;
+  int nName;
   assert( zName!=0 );
+  nName = sqlite3Strlen(db, zName) + 1;
   for(i=OMIT_TEMPDB; i<db->nDb; i++){
     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
-    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, strlen(zName)+1);
+    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
     if( p ) break;
   }
   return p;
@@ -338,13 +340,14 @@
 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
   Index *p = 0;
   int i;
+  int nName = sqlite3Strlen(db, zName)+1;
   for(i=OMIT_TEMPDB; i<db->nDb; i++){
     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
     Schema *pSchema = db->aDb[j].pSchema;
     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
     assert( pSchema || (j==1 && !db->aDb[1].pBt) );
     if( pSchema ){
-      p = sqlite3HashFind(&pSchema->idxHash, zName, strlen(zName)+1);
+      p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
     }
     if( p ) break;
   }
@@ -371,7 +374,7 @@
   Index *pOld;
   const char *zName = p->zName;
 
-  pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, strlen( zName)+1, 0);
+  pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, strlen(zName)+1, 0);
   assert( pOld==0 || pOld==p );
   freeIndex(p);
 }
@@ -387,7 +390,7 @@
   int len;
   Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
 
-  len = strlen(zIdxName);
+  len = sqlite3Strlen(db, zIdxName);
   pIndex = sqlite3HashInsert(pHash, zIdxName, len+1, 0);
   if( pIndex ){
     if( pIndex->pTable->pIndex==pIndex ){
@@ -1265,7 +1268,7 @@
     pColl = sqlite3GetCollSeq(db, pColl, zName, nName);
     if( !pColl ){
       if( nName<0 ){
-        nName = strlen(zName);
+        nName = sqlite3Strlen(db, zName);
       }
       sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
       pColl = 0;
diff --git a/src/callback.c b/src/callback.c
index 1a8198b..09f6841 100644
--- a/src/callback.c
+++ b/src/callback.c
@@ -13,7 +13,7 @@
 ** This file contains functions used to access the internal hash tables
 ** of user defined functions and collation sequences.
 **
-** $Id: callback.c,v 1.24 2008/06/23 16:53:47 danielk1977 Exp $
+** $Id: callback.c,v 1.25 2008/07/08 14:52:10 drh Exp $
 */
 
 #include "sqliteInt.h"
@@ -25,7 +25,7 @@
 */
 static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
   assert( !db->xCollNeeded || !db->xCollNeeded16 );
-  if( nName<0 ) nName = strlen(zName);
+  if( nName<0 ) nName = sqlite3Strlen(db, zName);
   if( db->xCollNeeded ){
     char *zExternal = sqlite3DbStrNDup(db, zName, nName);
     if( !zExternal ) return;
@@ -158,7 +158,7 @@
   int create
 ){
   CollSeq *pColl;
-  if( nName<0 ) nName = strlen(zName);
+  if( nName<0 ) nName = sqlite3Strlen(db, zName);
   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 
   if( 0==pColl && create ){
diff --git a/src/global.c b/src/global.c
index 9d3a2eb..4211c58 100644
--- a/src/global.c
+++ b/src/global.c
@@ -12,7 +12,7 @@
 **
 ** This file contains definitions of global variables and contants.
 **
-** $Id: global.c,v 1.2 2008/06/14 16:56:22 drh Exp $
+** $Id: global.c,v 1.3 2008/07/08 14:52:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -66,4 +66,4 @@
 ** The following singleton contains the global configuration for
 ** the SQLite library.
 */
-struct Sqlite3Config sqlite3Config = { 1, 1, 1, };
+struct Sqlite3Config sqlite3Config = { 1, 1, 1, 0x7ffffffe };
diff --git a/src/main.c b/src/main.c
index 90487d0..74e8aa9 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.470 2008/07/08 12:02:35 danielk1977 Exp $
+** $Id: main.c,v 1.471 2008/07/08 14:52:10 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -680,7 +680,7 @@
       (!xFunc && (xFinal && !xStep)) ||
       (!xFunc && (!xFinal && xStep)) ||
       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
-      (255<(nName = strlen(zFunctionName))) ){
+      (255<(nName = sqlite3Strlen(db, zFunctionName))) ){
     sqlite3Error(db, SQLITE_ERROR, "bad parameters");
     return SQLITE_ERROR;
   }
@@ -806,7 +806,7 @@
   const char *zName,
   int nArg
 ){
-  int nName = strlen(zName);
+  int nName = sqlite3Strlen(db, zName);
   int rc;
   sqlite3_mutex_enter(db->mutex);
   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
@@ -1094,6 +1094,7 @@
 ){
   CollSeq *pColl;
   int enc2;
+  int nName;
   
   assert( sqlite3_mutex_held(db->mutex) );
 
@@ -1113,7 +1114,8 @@
   ** sequence. If so, and there are active VMs, return busy. If there
   ** are no active VMs, invalidate any pre-compiled statements.
   */
-  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0);
+  nName = sqlite3Strlen(db, zName);
+  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, nName, 0);
   if( pColl && pColl->xCmp ){
     if( db->activeVdbeCnt ){
       sqlite3Error(db, SQLITE_BUSY, 
@@ -1129,7 +1131,7 @@
     ** to be called.
     */ 
     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
-      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName));
+      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
       int j;
       for(j=0; j<3; j++){
         CollSeq *p = &aColl[j];
@@ -1143,7 +1145,7 @@
     }
   }
 
-  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1);
+  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, nName, 1);
   if( pColl ){
     pColl->xCmp = xCompare;
     pColl->pUser = pCtx;
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index b9ecdf6..7be6793 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.734 2008/06/27 14:05:25 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.735 2008/07/08 14:52:10 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1739,6 +1739,7 @@
   int bMemstat;                     /* True to enable memory status */
   int bCoreMutex;                   /* True to enable core mutexing */
   int bFullMutex;                   /* True to enable full mutexing */
+  int mxStrlen;                     /* Maximum string length */
   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
   void *pHeap;                      /* Heap storage space */
@@ -1786,6 +1787,7 @@
 int sqlite3StrICmp(const char *, const char *);
 int sqlite3StrNICmp(const char *, const char *, int);
 int sqlite3IsNumber(const char*, int*, u8);
+int sqlite3Strlen(sqlite3*, const char*);
 
 int sqlite3MallocInit(void);
 void sqlite3MallocEnd(void);
diff --git a/src/util.c b/src/util.c
index b4979ba..bcf55e0 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.233 2008/07/06 00:21:35 drh Exp $
+** $Id: util.c,v 1.234 2008/07/08 14:52:10 drh Exp $
 */
 #include "sqliteInt.h"
 #include <stdarg.h>
@@ -42,6 +42,20 @@
 }
 
 /*
+** Return the length of a string, except do not allow the string length
+** to exceed the SQLITE_LIMIT_LENGTH setting.
+*/
+int sqlite3Strlen(sqlite3 *db, const char *z){
+  const char *z2 = z;
+  while( *z2 ){ z2++; }
+  if( z2 > &z[db->aLimit[SQLITE_LIMIT_LENGTH]] ){
+    return db->aLimit[SQLITE_LIMIT_LENGTH];
+  }else{
+    return (int)(z2 - z);
+  }
+}
+
+/*
 ** Set the most recent error code and error string for the sqlite
 ** handle "db". The error code is set to "err_code".
 **
diff --git a/src/vdbemem.c b/src/vdbemem.c
index 6be8d99..7abf08d 100644
--- a/src/vdbemem.c
+++ b/src/vdbemem.c
@@ -15,7 +15,7 @@
 ** only within the VDBE.  Interface routines refer to a Mem using the
 ** name sqlite_value
 **
-** $Id: vdbemem.c,v 1.115 2008/05/16 04:51:55 danielk1977 Exp $
+** $Id: vdbemem.c,v 1.116 2008/07/08 14:52:10 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -588,6 +588,7 @@
   void (*xDel)(void*) /* Destructor function */
 ){
   int nByte = n;      /* New value for pMem->n */
+  int iLimit;         /* Maximum allowed string or blob size */
   int flags = 0;      /* New value for pMem->flags */
 
   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
@@ -598,16 +599,24 @@
     return SQLITE_OK;
   }
 
+  if( pMem->db ){
+    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
+  }else{
+    iLimit = SQLITE_MAX_LENGTH;
+  }
   flags = (enc==0?MEM_Blob:MEM_Str);
   if( nByte<0 ){
     assert( enc!=0 );
     if( enc==SQLITE_UTF8 ){
-      for(nByte=0; z[nByte]; nByte++){}
+      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
     }else{
-      for(nByte=0; z[nByte] | z[nByte+1]; nByte+=2){}
+      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
     }
     flags |= MEM_Term;
   }
+  if( nByte>iLimit ){
+    return SQLITE_TOOBIG;
+  }
 
   /* The following block sets the new values of Mem.z and Mem.xDel. It
   ** also sets a flag in local variable "flags" to indicate the memory