Improved enforcement of the SQLITE_LIMIT_LENGTH limit. (CVS 5368)

FossilOrigin-Name: ee93150878436ce6e992ea8a1d348fb58b03b5e2
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