/ Check-in [17c4235c]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Introduce the (experimental) sqlite3_result_error_toobig() API that function implementations can use to signal SQLite that the function result is too big to represent. (CVS 3949)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 17c4235c492f746867c1d2b8621043b93f8aa10e
User & Date: drh 2007-05-08 15:15:02
Context
2007-05-08
15:34
Limit the length of the patterns on LIKE and GLOB to avoid problems with deep recursion and N^2 behavior. (CVS 3950) check-in: 42e6c826 user: drh tags: trunk
15:15
Introduce the (experimental) sqlite3_result_error_toobig() API that function implementations can use to signal SQLite that the function result is too big to represent. (CVS 3949) check-in: 17c4235c user: drh tags: trunk
14:51
Add the max_page_count pragma used to limit the maximum size of a database file. Untested. (CVS 3948) check-in: b1b74f06 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.64 2007/05/04 13:15:56 drh Exp $
           19  +** $Id: date.c,v 1.65 2007/05/08 15:15:02 drh Exp $
    20     20   **
    21     21   ** NOTES:
    22     22   **
    23     23   ** SQLite processes all times and dates as Julian Day numbers.  The
    24     24   ** dates and times are stored as the number of days since noon
    25     25   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    26     26   ** calendar system. 
................................................................................
   770    770   */
   771    771   static void strftimeFunc(
   772    772     sqlite3_context *context,
   773    773     int argc,
   774    774     sqlite3_value **argv
   775    775   ){
   776    776     DateTime x;
   777         -  int n, i, j;
          777  +  u64 n;
          778  +  int i, j;
   778    779     char *z;
   779    780     const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
   780    781     char zBuf[100];
   781    782     if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return;
   782    783     for(i=0, n=1; zFmt[i]; i++, n++){
   783    784       if( zFmt[i]=='%' ){
   784    785         switch( zFmt[i+1] ){
................................................................................
   810    811             return;  /* ERROR.  return a NULL */
   811    812         }
   812    813         i++;
   813    814       }
   814    815     }
   815    816     if( n<sizeof(zBuf) ){
   816    817       z = zBuf;
          818  +  }else if( n>SQLITE_MAX_LENGTH ){
          819  +    sqlite3_result_error_toobig(context);
          820  +    return;
   817    821     }else{
   818    822       z = sqliteMalloc( n );
   819    823       if( z==0 ) return;
   820    824     }
   821    825     computeJD(&x);
   822    826     computeYMD_HMS(&x);
   823    827     for(i=j=0; zFmt[i]; i++){

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.148 2007/05/08 14:39:04 danielk1977 Exp $
           19  +** $Id: func.c,v 1.149 2007/05/08 15:15:02 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <ctype.h>
    23     23   /* #include <math.h> */
    24     24   #include <stdlib.h>
    25     25   #include <assert.h>
    26     26   #include "vdbeInt.h"
................................................................................
   297    297     unsigned char *p;
   298    298     assert( argc==1 );
   299    299     n = sqlite3_value_int(argv[0]);
   300    300     if( n<1 ){
   301    301       n = 1;
   302    302     }
   303    303     if( n>SQLITE_MAX_LENGTH ){
   304         -    sqlite3_result_error(context, "randomblob() too large", -1);
          304  +    sqlite3_result_error_toobig(context);
   305    305       return;
   306    306     }
   307    307     p = sqliteMalloc(n);
   308    308     if( p ){
   309    309       sqlite3Randomness(n, p);
   310    310       sqlite3_result_blob(context, (char*)p, n, sqlite3FreeX);
   311    311     }
................................................................................
   620    620       }
   621    621       case SQLITE_BLOB: {
   622    622         char *zText = 0;
   623    623         int nBlob = sqlite3_value_bytes(argv[0]);
   624    624         char const *zBlob = sqlite3_value_blob(argv[0]);
   625    625   
   626    626         if( 2*nBlob+4>SQLITE_MAX_LENGTH ){
   627         -        sqlite3_result_error(context, "BLOB too big to quote", -1);
          627  +        sqlite3_result_error_toobig(context);
   628    628           return;
   629    629         }
   630    630         zText = (char *)sqliteMalloc((2*nBlob)+4); 
   631    631         if( !zText ){
   632    632           sqlite3_result_error(context, "out of memory", -1);
   633    633         }else{
   634    634           int i;
................................................................................
   650    650         u64 n;
   651    651         const unsigned char *zArg = sqlite3_value_text(argv[0]);
   652    652         char *z;
   653    653   
   654    654         if( zArg==0 ) return;
   655    655         for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
   656    656         if( i+n+3>SQLITE_MAX_LENGTH ){
   657         -        sqlite3_result_error(context, "string too big to quote", -1);
          657  +        sqlite3_result_error_toobig(context);
   658    658           return;
   659    659         }
   660    660         z = sqliteMalloc( i+n+3 );
   661    661         if( z==0 ) return;
   662    662         z[0] = '\'';
   663    663         for(i=0, j=1; zArg[i]; i++){
   664    664           z[j++] = zArg[i];
................................................................................
   685    685   ){
   686    686     int i, n;
   687    687     const unsigned char *pBlob;
   688    688     char *zHex, *z;
   689    689     assert( argc==1 );
   690    690     n = sqlite3_value_bytes(argv[0]);
   691    691     if( n*2+1>SQLITE_MAX_LENGTH ){
   692         -    sqlite3_result_error(context, "BLOB too big to convert to hex", -1);
          692  +    sqlite3_result_error_toobig(context);
   693    693       return;
   694    694     }
   695    695     pBlob = sqlite3_value_blob(argv[0]);
   696    696     z = zHex = sqlite3_malloc(n*2 + 1);
   697    697     if( zHex==0 ) return;
   698    698     for(i=0; i<n; i++, pBlob++){
   699    699       unsigned char c = *pBlob;
................................................................................
   760    760     if( zOut==0 ) return;
   761    761     loopLimit = nStr - nPattern;  
   762    762     for(i=j=0; i<=loopLimit; i++){
   763    763       if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
   764    764         zOut[j++] = zStr[i];
   765    765       }else{
   766    766         if( (j+nRep+loopLimit-i)>SQLITE_MAX_LENGTH ){
   767         -        sqlite3_result_error(context, "replace() is too large", -1);
          767  +        sqlite3_result_error_toobig(context);
   768    768           sqlite3_free(zOut);
   769    769           return;
   770    770         }
   771    771         memcpy(&zOut[j], zRep, nRep);
   772    772         j += nRep;
   773    773         i += nPattern-1;
   774    774       }

Changes to src/limits.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** 
    13     13   ** This file defines various limits of what SQLite can process.
    14     14   **
    15         -** @(#) $Id: limits.h,v 1.3 2007/05/08 14:51:37 drh Exp $
           15  +** @(#) $Id: limits.h,v 1.4 2007/05/08 15:15:02 drh Exp $
    16     16   */
    17     17   
    18     18   /*
    19     19   ** The maximum length of a TEXT or BLOB in bytes.   This also
    20     20   ** limits the size of a row in a table or index.
    21     21   **
    22     22   ** The hard limit is the ability of a 32-bit signed integer

Changes to src/sqlite.h.in.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the SQLite library
    13     13   ** presents to client programs.
    14     14   **
    15         -** @(#) $Id: sqlite.h.in,v 1.207 2007/05/08 01:08:49 drh Exp $
           15  +** @(#) $Id: sqlite.h.in,v 1.208 2007/05/08 15:15:02 drh Exp $
    16     16   */
    17     17   #ifndef _SQLITE3_H_
    18     18   #define _SQLITE3_H_
    19     19   #include <stdarg.h>     /* Needed for the definition of va_list */
    20     20   
    21     21   /*
    22     22   ** Make sure we can call this stuff from C++.
................................................................................
  1220   1220   void sqlite3_result_null(sqlite3_context*);
  1221   1221   void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  1222   1222   void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  1223   1223   void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  1224   1224   void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  1225   1225   void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  1226   1226   void sqlite3_result_zeroblob(sqlite3_context*, int n);
         1227  +void sqlite3_result_error_toobig(sqlite3_context*);
  1227   1228   
  1228   1229   /*
  1229   1230   ** These are the allowed values for the eTextRep argument to
  1230   1231   ** sqlite3_create_collation and sqlite3_create_function.
  1231   1232   */
  1232   1233   #define SQLITE_UTF8           1
  1233   1234   #define SQLITE_UTF16LE        2

Changes to src/vdbeapi.c.

   151    151   #endif /* SQLITE_OMIT_UTF16 */
   152    152   void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
   153    153     sqlite3VdbeMemCopy(&pCtx->s, pValue);
   154    154   }
   155    155   void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
   156    156     sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
   157    157   }
          158  +
          159  +/* Force an SQLITE_TOOBIG error. */
          160  +void sqlite3_result_error_toobig(sqlite3_context *pCtx){
          161  +  sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
          162  +}
   158    163   
   159    164   
   160    165   /*
   161    166   ** Execute the statement pStmt, either until a row of data is ready, the
   162    167   ** statement is completely executed or an error occurs.
   163    168   **
   164    169   ** This routine implements the bulk of the logic behind the sqlite_step()