/ Check-in [94954850]
Login

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

Overview
Comment:Add new APIs that take 64-bit length parameters: sqlite3_malloc64(), sqlite3_realloc64(), sqlite3_bind_blob64(), sqlite3_bind_texte64(), sqlite3_result_blob64(), and sqlite3_result_texte64(). Internal memory allocation routines also now use 64-bit unsigned length parameters for safety. Also add the sqlite3_msize() interface. Fix the sqlite3_get_table() to use sqlite3_realloc64() to avoid a integer overflow problem.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 64-bit-lengths
Files: files | file ages | folders
SHA1: 94954850cf2e1ec0b7f590c7f46cdc54c72558ce
User & Date: drh 2014-09-09 17:27:35
Context
2014-09-09
18:41
Add new interfaces to the loadable extension mechanism. check-in: 18d80cbc user: drh tags: 64-bit-lengths
17:27
Add new APIs that take 64-bit length parameters: sqlite3_malloc64(), sqlite3_realloc64(), sqlite3_bind_blob64(), sqlite3_bind_texte64(), sqlite3_result_blob64(), and sqlite3_result_texte64(). Internal memory allocation routines also now use 64-bit unsigned length parameters for safety. Also add the sqlite3_msize() interface. Fix the sqlite3_get_table() to use sqlite3_realloc64() to avoid a integer overflow problem. check-in: 94954850 user: drh tags: 64-bit-lengths
2014-09-06
17:06
Fixes to os_unix.c to support database (and other) files larger than 2GiB on Android. check-in: ad7063aa user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

   602    602     /* If this is an intKey table, then the above call to BtreeKeySize()
   603    603     ** stores the integer key in pCur->nKey. In this case this value is
   604    604     ** all that is required. Otherwise, if pCur is not open on an intKey
   605    605     ** table, then malloc space for and store the pCur->nKey bytes of key 
   606    606     ** data.
   607    607     */
   608    608     if( 0==pCur->apPage[0]->intKey ){
   609         -    void *pKey = sqlite3Malloc( (int)pCur->nKey );
          609  +    void *pKey = sqlite3Malloc( pCur->nKey );
   610    610       if( pKey ){
   611    611         rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
   612    612         if( rc==SQLITE_OK ){
   613    613           pCur->pKey = pKey;
   614    614         }else{
   615    615           sqlite3_free(pKey);
   616    616         }

Changes to src/func.c.

   386    386     assert( nByte>0 );
   387    387     testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
   388    388     testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
   389    389     if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   390    390       sqlite3_result_error_toobig(context);
   391    391       z = 0;
   392    392     }else{
   393         -    z = sqlite3Malloc((int)nByte);
          393  +    z = sqlite3Malloc(nByte);
   394    394       if( !z ){
   395    395         sqlite3_result_error_nomem(context);
   396    396       }
   397    397     }
   398    398     return z;
   399    399   }
   400    400   

Changes to src/malloc.c.

   290    290     return nFull;
   291    291   }
   292    292   
   293    293   /*
   294    294   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   295    295   ** assumes the memory subsystem has already been initialized.
   296    296   */
   297         -void *sqlite3Malloc(int n){
          297  +void *sqlite3Malloc(u64 n){
   298    298     void *p;
   299         -  if( n<=0               /* IMP: R-65312-04917 */ 
   300         -   || n>=0x7fffff00
   301         -  ){
          299  +  if( n==0 || n>=0x7fffff00 ){
   302    300       /* A memory allocation of a number of bytes which is near the maximum
   303    301       ** signed integer value might cause an integer overflow inside of the
   304    302       ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   305    303       ** 255 bytes of overhead.  SQLite itself will never use anything near
   306    304       ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   307    305       p = 0;
   308    306     }else if( sqlite3GlobalConfig.bMemstat ){
   309    307       sqlite3_mutex_enter(mem0.mutex);
   310    308       mallocWithAlarm(n, &p);
   311    309       sqlite3_mutex_leave(mem0.mutex);
   312    310     }else{
   313         -    p = sqlite3GlobalConfig.m.xMalloc(n);
          311  +    p = sqlite3GlobalConfig.m.xMalloc((int)n);
   314    312     }
   315    313     assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
   316    314     return p;
   317    315   }
   318    316   
   319    317   /*
   320    318   ** This version of the memory allocation is for use by the application.
................................................................................
   321    319   ** First make sure the memory subsystem is initialized, then do the
   322    320   ** allocation.
   323    321   */
   324    322   void *sqlite3_malloc(int n){
   325    323   #ifndef SQLITE_OMIT_AUTOINIT
   326    324     if( sqlite3_initialize() ) return 0;
   327    325   #endif
          326  +  return n<=0 ? 0 : sqlite3Malloc(n);
          327  +}
          328  +void *sqlite3_malloc64(sqlite3_uint64 n){
          329  +#ifndef SQLITE_OMIT_AUTOINIT
          330  +  if( sqlite3_initialize() ) return 0;
          331  +#endif
   328    332     return sqlite3Malloc(n);
   329    333   }
   330    334   
   331    335   /*
   332    336   ** Each thread may only have a single outstanding allocation from
   333    337   ** xScratchMalloc().  We verify this constraint in the single-threaded
   334    338   ** case by setting scratchAllocOut to 1 when an allocation
................................................................................
   454    458     }else{
   455    459       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   456    460       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   457    461       assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   458    462       return sqlite3GlobalConfig.m.xSize(p);
   459    463     }
   460    464   }
          465  +sqlite3_uint64 sqlite3_msize(void *p){
          466  +  return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
          467  +}
   461    468   
   462    469   /*
   463    470   ** Free memory previously obtained from sqlite3Malloc().
   464    471   */
   465    472   void sqlite3_free(void *p){
   466    473     if( p==0 ) return;  /* IMP: R-49053-54554 */
   467    474     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
................................................................................
   515    522     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   516    523     sqlite3_free(p);
   517    524   }
   518    525   
   519    526   /*
   520    527   ** Change the size of an existing memory allocation
   521    528   */
   522         -void *sqlite3Realloc(void *pOld, int nBytes){
          529  +void *sqlite3Realloc(void *pOld, u64 nBytes){
   523    530     int nOld, nNew, nDiff;
   524    531     void *pNew;
   525    532     if( pOld==0 ){
   526    533       return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
   527    534     }
   528         -  if( nBytes<=0 ){
          535  +  if( nBytes==0 ){
   529    536       sqlite3_free(pOld); /* IMP: R-31593-10574 */
   530    537       return 0;
   531    538     }
   532    539     if( nBytes>=0x7fffff00 ){
   533    540       /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
   534    541       return 0;
   535    542     }
   536    543     nOld = sqlite3MallocSize(pOld);
   537    544     /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
   538    545     ** argument to xRealloc is always a value returned by a prior call to
   539    546     ** xRoundup. */
   540         -  nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
          547  +  nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
   541    548     if( nOld==nNew ){
   542    549       pNew = pOld;
   543    550     }else if( sqlite3GlobalConfig.bMemstat ){
   544    551       sqlite3_mutex_enter(mem0.mutex);
   545    552       sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
   546    553       nDiff = nNew - nOld;
   547    554       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
................................................................................
   571    578   ** The public interface to sqlite3Realloc.  Make sure that the memory
   572    579   ** subsystem is initialized prior to invoking sqliteRealloc.
   573    580   */
   574    581   void *sqlite3_realloc(void *pOld, int n){
   575    582   #ifndef SQLITE_OMIT_AUTOINIT
   576    583     if( sqlite3_initialize() ) return 0;
   577    584   #endif
          585  +  if( n<0 ) n = 0;
          586  +  return sqlite3Realloc(pOld, n);
          587  +}
          588  +void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
          589  +#ifndef SQLITE_OMIT_AUTOINIT
          590  +  if( sqlite3_initialize() ) return 0;
          591  +#endif
   578    592     return sqlite3Realloc(pOld, n);
   579    593   }
   580    594   
   581    595   
   582    596   /*
   583    597   ** Allocate and zero memory.
   584    598   */ 
   585         -void *sqlite3MallocZero(int n){
          599  +void *sqlite3MallocZero(u64 n){
   586    600     void *p = sqlite3Malloc(n);
   587    601     if( p ){
   588    602       memset(p, 0, n);
   589    603     }
   590    604     return p;
   591    605   }
   592    606   
   593    607   /*
   594    608   ** Allocate and zero memory.  If the allocation fails, make
   595    609   ** the mallocFailed flag in the connection pointer.
   596    610   */
   597         -void *sqlite3DbMallocZero(sqlite3 *db, int n){
          611  +void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
   598    612     void *p = sqlite3DbMallocRaw(db, n);
   599    613     if( p ){
   600    614       memset(p, 0, n);
   601    615     }
   602    616     return p;
   603    617   }
   604    618   
................................................................................
   616    630   **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
   617    631   **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
   618    632   **         if( b ) a[10] = 9;
   619    633   **
   620    634   ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
   621    635   ** that all prior mallocs (ex: "a") worked too.
   622    636   */
   623         -void *sqlite3DbMallocRaw(sqlite3 *db, int n){
          637  +void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
   624    638     void *p;
   625    639     assert( db==0 || sqlite3_mutex_held(db->mutex) );
   626    640     assert( db==0 || db->pnBytesFreed==0 );
   627    641   #ifndef SQLITE_OMIT_LOOKASIDE
   628    642     if( db ){
   629    643       LookasideSlot *pBuf;
   630    644       if( db->mallocFailed ){
................................................................................
   660    674     return p;
   661    675   }
   662    676   
   663    677   /*
   664    678   ** Resize the block of memory pointed to by p to n bytes. If the
   665    679   ** resize fails, set the mallocFailed flag in the connection object.
   666    680   */
   667         -void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
          681  +void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
   668    682     void *pNew = 0;
   669    683     assert( db!=0 );
   670    684     assert( sqlite3_mutex_held(db->mutex) );
   671    685     if( db->mallocFailed==0 ){
   672    686       if( p==0 ){
   673    687         return sqlite3DbMallocRaw(db, n);
   674    688       }
................................................................................
   697    711     return pNew;
   698    712   }
   699    713   
   700    714   /*
   701    715   ** Attempt to reallocate p.  If the reallocation fails, then free p
   702    716   ** and set the mallocFailed flag in the database connection.
   703    717   */
   704         -void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
          718  +void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
   705    719     void *pNew;
   706    720     pNew = sqlite3DbRealloc(db, p, n);
   707    721     if( !pNew ){
   708    722       sqlite3DbFree(db, p);
   709    723     }
   710    724     return pNew;
   711    725   }
................................................................................
   727    741     assert( (n&0x7fffffff)==n );
   728    742     zNew = sqlite3DbMallocRaw(db, (int)n);
   729    743     if( zNew ){
   730    744       memcpy(zNew, z, n);
   731    745     }
   732    746     return zNew;
   733    747   }
   734         -char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
          748  +char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
   735    749     char *zNew;
   736    750     if( z==0 ){
   737    751       return 0;
   738    752     }
   739    753     assert( (n&0x7fffffff)==n );
   740    754     zNew = sqlite3DbMallocRaw(db, n+1);
   741    755     if( zNew ){

Changes to src/pager.c.

  2424   2424     ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
  2425   2425     ** sufficient space (in zMasterPtr) to hold the names of master
  2426   2426     ** journal files extracted from regular rollback-journals.
  2427   2427     */
  2428   2428     rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
  2429   2429     if( rc!=SQLITE_OK ) goto delmaster_out;
  2430   2430     nMasterPtr = pVfs->mxPathname+1;
  2431         -  zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
         2431  +  zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
  2432   2432     if( !zMasterJournal ){
  2433   2433       rc = SQLITE_NOMEM;
  2434   2434       goto delmaster_out;
  2435   2435     }
  2436   2436     zMasterPtr = &zMasterJournal[nMasterJournal+1];
  2437   2437     rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
  2438   2438     if( rc!=SQLITE_OK ) goto delmaster_out;

Changes to src/sqlite.h.in.

  2293   2293   **
  2294   2294   ** ^The sqlite3_malloc() routine returns a pointer to a block
  2295   2295   ** of memory at least N bytes in length, where N is the parameter.
  2296   2296   ** ^If sqlite3_malloc() is unable to obtain sufficient free
  2297   2297   ** memory, it returns a NULL pointer.  ^If the parameter N to
  2298   2298   ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  2299   2299   ** a NULL pointer.
         2300  +**
         2301  +** ^The sqlite3_malloc64(N) routine works just like
         2302  +** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
         2303  +** of a signed 32-bit integer.
  2300   2304   **
  2301   2305   ** ^Calling sqlite3_free() with a pointer previously returned
  2302   2306   ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  2303   2307   ** that it might be reused.  ^The sqlite3_free() routine is
  2304   2308   ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
  2305   2309   ** to sqlite3_free() is harmless.  After being freed, memory
  2306   2310   ** should neither be read nor written.  Even reading previously freed
  2307   2311   ** memory might result in a segmentation fault or other severe error.
  2308   2312   ** Memory corruption, a segmentation fault, or other severe error
  2309   2313   ** might result if sqlite3_free() is called with a non-NULL pointer that
  2310   2314   ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  2311   2315   **
  2312         -** ^(The sqlite3_realloc() interface attempts to resize a
  2313         -** prior memory allocation to be at least N bytes, where N is the
  2314         -** second parameter.  The memory allocation to be resized is the first
  2315         -** parameter.)^ ^ If the first parameter to sqlite3_realloc()
         2316  +** ^The sqlite3_realloc(X,N) interface attempts to resize a
         2317  +** prior memory allocation X to be at least N bytes.
         2318  +** ^If the X parameter to sqlite3_realloc(X,N)
  2316   2319   ** is a NULL pointer then its behavior is identical to calling
  2317         -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  2318         -** ^If the second parameter to sqlite3_realloc() is zero or
         2320  +** sqlite3_malloc(N).
         2321  +** ^If the N parameter to sqlite3_realloc(X,N) is zero or
  2319   2322   ** negative then the behavior is exactly the same as calling
  2320         -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  2321         -** ^sqlite3_realloc() returns a pointer to a memory allocation
  2322         -** of at least N bytes in size or NULL if sufficient memory is unavailable.
         2323  +** sqlite3_free(X).
         2324  +** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
         2325  +** of at least N bytes in size or NULL if insufficient memory is available.
  2323   2326   ** ^If M is the size of the prior allocation, then min(N,M) bytes
  2324   2327   ** of the prior allocation are copied into the beginning of buffer returned
  2325         -** by sqlite3_realloc() and the prior allocation is freed.
  2326         -** ^If sqlite3_realloc() returns NULL, then the prior allocation
  2327         -** is not freed.
         2328  +** by sqlite3_realloc(X,N) and the prior allocation is freed.
         2329  +** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
         2330  +** prior allocation is not freed.
  2328   2331   **
  2329         -** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
         2332  +** ^The sqlite3_realloc64(X,N) interfaces works the same as
         2333  +** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
         2334  +** of a 32-bit signed integer.
         2335  +**
         2336  +** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
         2337  +** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
         2338  +** sqlite3_msize(X) returns the size of that memory allocation in bytes.
         2339  +** ^The value returned by sqlite3_msize(X) might be larger than the number
         2340  +** of bytes requested when X was allocated.  ^If X is a NULL pointer then
         2341  +** sqlite3_msize(X) returns zero.  If X points to something that is not
         2342  +** the beginning of memory allocation, or if it points to a formerly
         2343  +** valid memory allocation that has now been freed, then the behavior
         2344  +** of sqlite3_msize(X) is undefined and possibly harmful.
         2345  +**
         2346  +** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
         2347  +** sqlite3_malloc64(), and sqlite3_realloc64()
  2330   2348   ** is always aligned to at least an 8 byte boundary, or to a
  2331   2349   ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
  2332   2350   ** option is used.
  2333   2351   **
  2334   2352   ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  2335   2353   ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  2336   2354   ** implementation of these routines to be omitted.  That capability
................................................................................
  2350   2368   ** not yet been released.
  2351   2369   **
  2352   2370   ** The application must not read or write any part of
  2353   2371   ** a block of memory after it has been released using
  2354   2372   ** [sqlite3_free()] or [sqlite3_realloc()].
  2355   2373   */
  2356   2374   void *sqlite3_malloc(int);
         2375  +void *sqlite3_malloc64(sqlite3_uint64);
  2357   2376   void *sqlite3_realloc(void*, int);
         2377  +void *sqlite3_realloc64(void*, sqlite3_uint64);
  2358   2378   void sqlite3_free(void*);
         2379  +sqlite3_uint64 sqlite3_msize(void*);
  2359   2380   
  2360   2381   /*
  2361   2382   ** CAPI3REF: Memory Allocator Statistics
  2362   2383   **
  2363   2384   ** SQLite provides these two interfaces for reporting on the status
  2364   2385   ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  2365   2386   ** routines, which form the built-in memory allocation subsystem.
................................................................................
  3360   3381   ** number of <u>bytes</u> in the value, not the number of characters.)^
  3361   3382   ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
  3362   3383   ** is negative, then the length of the string is
  3363   3384   ** the number of bytes up to the first zero terminator.
  3364   3385   ** If the fourth parameter to sqlite3_bind_blob() is negative, then
  3365   3386   ** the behavior is undefined.
  3366   3387   ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  3367         -** or sqlite3_bind_text16() then that parameter must be the byte offset
         3388  +** or sqlite3_bind_text16() or sqlite3_bind_texte64() then
         3389  +** that parameter must be the byte offset
  3368   3390   ** where the NUL terminator would occur assuming the string were NUL
  3369   3391   ** terminated.  If any NUL characters occur at byte offsets less than 
  3370   3392   ** the value of the fourth parameter then the resulting string value will
  3371   3393   ** contain embedded NULs.  The result of expressions involving strings
  3372   3394   ** with embedded NULs is undefined.
  3373   3395   **
  3374   3396   ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
................................................................................
  3378   3400   ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  3379   3401   ** ^If the fifth argument is
  3380   3402   ** the special value [SQLITE_STATIC], then SQLite assumes that the
  3381   3403   ** information is in static, unmanaged space and does not need to be freed.
  3382   3404   ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  3383   3405   ** SQLite makes its own private copy of the data immediately, before
  3384   3406   ** the sqlite3_bind_*() routine returns.
         3407  +**
         3408  +** ^The sixth argument to sqlite3_bind_texte64() must be one of
         3409  +** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
         3410  +** to specify the encoding of the text in the third parameter.  If
         3411  +** the sixth argument to sqlite3_bind_texte64() is not how of the
         3412  +** allowed values shown above, or if the text encoding is different
         3413  +** from the encoding specified by the sixth parameter, then the behavior
         3414  +** is undefined.
  3385   3415   **
  3386   3416   ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  3387   3417   ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  3388   3418   ** (just an integer to hold its size) while it is being processed.
  3389   3419   ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  3390   3420   ** content is later written using
  3391   3421   ** [sqlite3_blob_open | incremental BLOB I/O] routines.
................................................................................
  3399   3429   ** result is undefined and probably harmful.
  3400   3430   **
  3401   3431   ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  3402   3432   ** ^Unbound parameters are interpreted as NULL.
  3403   3433   **
  3404   3434   ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  3405   3435   ** [error code] if anything goes wrong.
         3436  +** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
         3437  +** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
         3438  +** [SQLITE_MAX_LENGTH].
  3406   3439   ** ^[SQLITE_RANGE] is returned if the parameter
  3407   3440   ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
  3408   3441   **
  3409   3442   ** See also: [sqlite3_bind_parameter_count()],
  3410   3443   ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  3411   3444   */
  3412   3445   int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
         3446  +int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
         3447  +                        void(*)(void*));
  3413   3448   int sqlite3_bind_double(sqlite3_stmt*, int, double);
  3414   3449   int sqlite3_bind_int(sqlite3_stmt*, int, int);
  3415   3450   int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3416   3451   int sqlite3_bind_null(sqlite3_stmt*, int);
  3417         -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
         3452  +int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
  3418   3453   int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
         3454  +int sqlite3_bind_texte64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
         3455  +                         void(*)(void*), unsigned char encoding);
  3419   3456   int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3420   3457   int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3421   3458   
  3422   3459   /*
  3423   3460   ** CAPI3REF: Number Of SQL Parameters
  3424   3461   **
  3425   3462   ** ^This routine can be used to find the number of [SQL parameters]
................................................................................
  4403   4440   ** of the application-defined function to be the 64-bit signed integer
  4404   4441   ** value given in the 2nd argument.
  4405   4442   **
  4406   4443   ** ^The sqlite3_result_null() interface sets the return value
  4407   4444   ** of the application-defined function to be NULL.
  4408   4445   **
  4409   4446   ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  4410         -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
         4447  +** sqlite3_result_text16le(), and sqlite3_result_text16be()
  4411   4448   ** set the return value of the application-defined function to be
  4412   4449   ** a text string which is represented as UTF-8, UTF-16 native byte order,
  4413   4450   ** UTF-16 little endian, or UTF-16 big endian, respectively.
         4451  +** ^The sqlite3_result_texte64() interface sets the return value of an
         4452  +** application-defined function to be a text string in an encoding
         4453  +** specified by the fifth (and last) parameter, which must be one
         4454  +** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
  4414   4455   ** ^SQLite takes the text result from the application from
  4415   4456   ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4416   4457   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4417   4458   ** is negative, then SQLite takes result text from the 2nd parameter
  4418   4459   ** through the first zero character.
  4419   4460   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4420   4461   ** is non-negative, then as many bytes (not characters) of the text
................................................................................
  4450   4491   ** kind of [sqlite3_value] object can be used with this interface.
  4451   4492   **
  4452   4493   ** If these routines are called from within the different thread
  4453   4494   ** than the one containing the application-defined function that received
  4454   4495   ** the [sqlite3_context] pointer, the results are undefined.
  4455   4496   */
  4456   4497   void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
         4498  +void sqlite3_result_blob64(sqlite3_context*,const void*,sqlite3_uint64,void(*)(void*));
  4457   4499   void sqlite3_result_double(sqlite3_context*, double);
  4458   4500   void sqlite3_result_error(sqlite3_context*, const char*, int);
  4459   4501   void sqlite3_result_error16(sqlite3_context*, const void*, int);
  4460   4502   void sqlite3_result_error_toobig(sqlite3_context*);
  4461   4503   void sqlite3_result_error_nomem(sqlite3_context*);
  4462   4504   void sqlite3_result_error_code(sqlite3_context*, int);
  4463   4505   void sqlite3_result_int(sqlite3_context*, int);
  4464   4506   void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  4465   4507   void sqlite3_result_null(sqlite3_context*);
  4466   4508   void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
         4509  +void sqlite3_result_texte64(sqlite3_context*, const char*,sqlite3_uint64,
         4510  +                            void(*)(void*), unsigned char encoding);
  4467   4511   void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  4468   4512   void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4469   4513   void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4470   4514   void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4471   4515   void sqlite3_result_zeroblob(sqlite3_context*, int n);
  4472   4516   
  4473   4517   /*

Changes to src/sqliteInt.h.

  2927   2927   */
  2928   2928   #define sqlite3StrICmp sqlite3_stricmp
  2929   2929   int sqlite3Strlen30(const char*);
  2930   2930   #define sqlite3StrNICmp sqlite3_strnicmp
  2931   2931   
  2932   2932   int sqlite3MallocInit(void);
  2933   2933   void sqlite3MallocEnd(void);
  2934         -void *sqlite3Malloc(int);
  2935         -void *sqlite3MallocZero(int);
  2936         -void *sqlite3DbMallocZero(sqlite3*, int);
  2937         -void *sqlite3DbMallocRaw(sqlite3*, int);
         2934  +void *sqlite3Malloc(u64);
         2935  +void *sqlite3MallocZero(u64);
         2936  +void *sqlite3DbMallocZero(sqlite3*, u64);
         2937  +void *sqlite3DbMallocRaw(sqlite3*, u64);
  2938   2938   char *sqlite3DbStrDup(sqlite3*,const char*);
  2939         -char *sqlite3DbStrNDup(sqlite3*,const char*, int);
  2940         -void *sqlite3Realloc(void*, int);
  2941         -void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
  2942         -void *sqlite3DbRealloc(sqlite3 *, void *, int);
         2939  +char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
         2940  +void *sqlite3Realloc(void*, u64);
         2941  +void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
         2942  +void *sqlite3DbRealloc(sqlite3 *, void *, u64);
  2943   2943   void sqlite3DbFree(sqlite3*, void*);
  2944   2944   int sqlite3MallocSize(void*);
  2945   2945   int sqlite3DbMallocSize(sqlite3*, void*);
  2946   2946   void *sqlite3ScratchMalloc(int);
  2947   2947   void sqlite3ScratchFree(void*);
  2948   2948   void *sqlite3PageMalloc(int);
  2949   2949   void sqlite3PageFree(void*);

Changes to src/table.c.

    25     25   /*
    26     26   ** This structure is used to pass data from sqlite3_get_table() through
    27     27   ** to the callback function is uses to build the result.
    28     28   */
    29     29   typedef struct TabResult {
    30     30     char **azResult;   /* Accumulated output */
    31     31     char *zErrMsg;     /* Error message text, if an error occurs */
    32         -  int nAlloc;        /* Slots allocated for azResult[] */
    33         -  int nRow;          /* Number of rows in the result */
    34         -  int nColumn;       /* Number of columns in the result */
    35         -  int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
           32  +  u32 nAlloc;        /* Slots allocated for azResult[] */
           33  +  u32 nRow;          /* Number of rows in the result */
           34  +  u32 nColumn;       /* Number of columns in the result */
           35  +  u32 nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
    36     36     int rc;            /* Return code from sqlite3_exec() */
    37     37   } TabResult;
    38     38   
    39     39   /*
    40     40   ** This routine is called once for each row in the result table.  Its job
    41     41   ** is to fill in the TabResult structure appropriately, allocating new
    42     42   ** memory as necessary.
................................................................................
    54     54       need = nCol*2;
    55     55     }else{
    56     56       need = nCol;
    57     57     }
    58     58     if( p->nData + need > p->nAlloc ){
    59     59       char **azNew;
    60     60       p->nAlloc = p->nAlloc*2 + need;
    61         -    azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
           61  +    azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
    62     62       if( azNew==0 ) goto malloc_failed;
    63     63       p->azResult = azNew;
    64     64     }
    65     65   
    66     66     /* If this is the first row, then generate an extra row containing
    67     67     ** the names of all columns.
    68     68     */

Changes to src/vdbeapi.c.

   211    211   /**************************** sqlite3_result_  *******************************
   212    212   ** The following routines are used by user-defined functions to specify
   213    213   ** the function result.
   214    214   **
   215    215   ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
   216    216   ** result as a string or blob but if the string or blob is too large, it
   217    217   ** then sets the error code to SQLITE_TOOBIG
          218  +**
          219  +** The invokeValueDestructor(P,X) routine invokes destructor function X()
          220  +** on value P is not going to be used and need to be destroyed.
   218    221   */
   219    222   static void setResultStrOrError(
   220    223     sqlite3_context *pCtx,  /* Function context */
   221    224     const char *z,          /* String pointer */
   222    225     int n,                  /* Bytes in string, or negative */
   223    226     u8 enc,                 /* Encoding of z.  0 for BLOBs */
   224    227     void (*xDel)(void*)     /* Destructor function */
   225    228   ){
   226    229     if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
   227    230       sqlite3_result_error_toobig(pCtx);
   228    231     }
          232  +}
          233  +static int invokeValueDestructor(
          234  +  const void *p,             /* Value to destroy */
          235  +  void (*xDel)(void*),       /* The destructor */
          236  +  sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if no NULL */
          237  +){
          238  +  if( xDel==0 ){
          239  +    /* noop */
          240  +  }else if( xDel==SQLITE_TRANSIENT ){
          241  +    /* noop */
          242  +  }else if( xDel==SQLITE_DYNAMIC ){
          243  +    sqlite3_free((void*)p);
          244  +  }else{
          245  +    xDel((void*)p);
          246  +  }
          247  +  if( pCtx ) sqlite3_result_error_toobig(pCtx);
          248  +  return SQLITE_TOOBIG;
   229    249   }
   230    250   void sqlite3_result_blob(
   231    251     sqlite3_context *pCtx, 
   232    252     const void *z, 
   233    253     int n, 
   234    254     void (*xDel)(void *)
   235    255   ){
   236    256     assert( n>=0 );
   237    257     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   238    258     setResultStrOrError(pCtx, z, n, 0, xDel);
          259  +}
          260  +void sqlite3_result_blob64(
          261  +  sqlite3_context *pCtx, 
          262  +  const void *z, 
          263  +  sqlite3_uint64 n,
          264  +  void (*xDel)(void *)
          265  +){
          266  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          267  +  if( n>0x7fffffff ){
          268  +    (void)invokeValueDestructor(z, xDel, pCtx);
          269  +  }else{
          270  +    setResultStrOrError(pCtx, z, (int)n, 0, xDel);
          271  +  }
   239    272   }
   240    273   void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
   241    274     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   242    275     sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
   243    276   }
   244    277   void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   245    278     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
................................................................................
   271    304     sqlite3_context *pCtx, 
   272    305     const char *z, 
   273    306     int n,
   274    307     void (*xDel)(void *)
   275    308   ){
   276    309     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   277    310     setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
          311  +}
          312  +void sqlite3_result_texte64(
          313  +  sqlite3_context *pCtx, 
          314  +  const char *z, 
          315  +  sqlite3_uint64 n,
          316  +  void (*xDel)(void *),
          317  +  unsigned char enc
          318  +){
          319  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          320  +  if( n>0x7fffffff ){
          321  +    (void)invokeValueDestructor(z, xDel, pCtx);
          322  +  }else{
          323  +    setResultStrOrError(pCtx, z, (int)n, enc, xDel);
          324  +  }
   278    325   }
   279    326   #ifndef SQLITE_OMIT_UTF16
   280    327   void sqlite3_result_text16(
   281    328     sqlite3_context *pCtx, 
   282    329     const void *z, 
   283    330     int n, 
   284    331     void (*xDel)(void *)
................................................................................
  1120   1167     sqlite3_stmt *pStmt, 
  1121   1168     int i, 
  1122   1169     const void *zData, 
  1123   1170     int nData, 
  1124   1171     void (*xDel)(void*)
  1125   1172   ){
  1126   1173     return bindText(pStmt, i, zData, nData, xDel, 0);
         1174  +}
         1175  +int sqlite3_bind_blob64(
         1176  +  sqlite3_stmt *pStmt, 
         1177  +  int i, 
         1178  +  const void *zData, 
         1179  +  sqlite3_uint64 nData, 
         1180  +  void (*xDel)(void*)
         1181  +){
         1182  +  if( nData>0x7fffffff ){
         1183  +    return invokeValueDestructor(zData, xDel, 0);
         1184  +  }else{
         1185  +    return bindText(pStmt, i, zData, nData, xDel, 0);
         1186  +  }
  1127   1187   }
  1128   1188   int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
  1129   1189     int rc;
  1130   1190     Vdbe *p = (Vdbe *)pStmt;
  1131   1191     rc = vdbeUnbind(p, i);
  1132   1192     if( rc==SQLITE_OK ){
  1133   1193       sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
................................................................................
  1161   1221     sqlite3_stmt *pStmt, 
  1162   1222     int i, 
  1163   1223     const char *zData, 
  1164   1224     int nData, 
  1165   1225     void (*xDel)(void*)
  1166   1226   ){
  1167   1227     return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
         1228  +}
         1229  +int sqlite3_bind_texte64( 
         1230  +  sqlite3_stmt *pStmt, 
         1231  +  int i, 
         1232  +  const char *zData, 
         1233  +  sqlite3_uint64 nData, 
         1234  +  void (*xDel)(void*),
         1235  +  unsigned char enc
         1236  +){
         1237  +  if( nData>0x7fffffff ){
         1238  +    return invokeValueDestructor(zData, xDel, 0);
         1239  +  }else{
         1240  +    return bindText(pStmt, i, zData, nData, xDel, enc);
         1241  +  }
  1168   1242   }
  1169   1243   #ifndef SQLITE_OMIT_UTF16
  1170   1244   int sqlite3_bind_text16(
  1171   1245     sqlite3_stmt *pStmt, 
  1172   1246     int i, 
  1173   1247     const void *zData, 
  1174   1248     int nData,