/ Check-in [ed5b0968]
Login

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

Overview
Comment:Enhance the sqlite3_str_new() interface so that it always returns a valid and non-NULL pointer even in an OOM condition.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:ed5b09680fd6659ebbe5ace3c1c56f3962bbd75cfdf65c7565651900cf87917a
User & Date: drh 2018-05-16 15:35:03
Context
2018-05-17
13:29
Fix memory errors associated with argv in the CLI when it is compiled on Windows. check-in: 4474d69b user: drh tags: trunk
2018-05-16
20:58
Start of experimental implementation of SQL window functions. Does not yet work. check-in: 3781e520 user: dan tags: exp-window-functions
17:57
Initial implementation of the ability to have auxiliary columns in an rtree virtual table that store arbitrary content. It mostly works, but there are some minor issues in OOM corner cases. check-in: 0c87fec9 user: drh tags: aux-data-in-rtree
15:35
Enhance the sqlite3_str_new() interface so that it always returns a valid and non-NULL pointer even in an OOM condition. check-in: ed5b0968 user: drh tags: trunk
12:19
Correct output for the fullkey column of json_each() when the total JSON input is a simple value, not an array or object. check-in: b45b1885 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/printf.c.

   962    962       p->zText[p->nChar] = 0;
   963    963       if( p->mxAlloc>0 && !isMalloced(p) ){
   964    964         return strAccumFinishRealloc(p);
   965    965       }
   966    966     }
   967    967     return p->zText;
   968    968   }
          969  +
          970  +/*
          971  +** This singleton is an sqlite3_str object that is returned if
          972  +** sqlite3_malloc() fails to provide space for a real one.  This
          973  +** sqlite3_str object accepts no new text and always returns
          974  +** an SQLITE_NOMEM error.
          975  +*/
          976  +static sqlite3_str sqlite3OomStr = {
          977  +   0, 0, 0, 0, 0, SQLITE_NOMEM
          978  +};
   969    979   
   970    980   /* Finalize a string created using sqlite3_str_new().
   971    981   */
   972    982   char *sqlite3_str_finish(sqlite3_str *p){
   973    983     char *z;
   974         -  if( p ){
          984  +  if( p!=0 && p!=&sqlite3OomStr ){
   975    985       z = sqlite3StrAccumFinish(p);
   976    986       sqlite3_free(p);
   977    987     }else{
   978    988       z = 0;
   979    989     }
   980    990     return z;
   981    991   }
................................................................................
  1036   1046   
  1037   1047   /* Allocate and initialize a new dynamic string object */
  1038   1048   sqlite3_str *sqlite3_str_new(sqlite3 *db){
  1039   1049     sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
  1040   1050     if( p ){
  1041   1051       sqlite3StrAccumInit(p, 0, 0, 0,
  1042   1052               db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
         1053  +  }else{
         1054  +    p = &sqlite3OomStr;
  1043   1055     }
  1044   1056     return p;
  1045   1057   }
  1046   1058   
  1047   1059   /*
  1048   1060   ** Print into memory obtained from sqliteMalloc().  Use the internal
  1049   1061   ** %-conversion extensions.

Changes to src/sqlite.h.in.

  7151   7151   typedef struct sqlite3_str sqlite3_str;
  7152   7152   
  7153   7153   /*
  7154   7154   ** CAPI3REF: Create A New Dynamic String Object
  7155   7155   ** CONSTRUCTOR: sqlite3_str
  7156   7156   **
  7157   7157   ** ^The [sqlite3_str_new(D)] interface allocates and initializes
  7158         -** a new [sqlite3_str]
  7159         -** object.  ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory
  7160         -** condition.  To avoid memory leaks, the object returned by
         7158  +** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
  7161   7159   ** [sqlite3_str_new()] must be freed by a subsequent call to 
  7162   7160   ** [sqlite3_str_finish(X)].
         7161  +**
         7162  +** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
         7163  +** valid [sqlite3_str] object, though in the event of an out-of-memory
         7164  +** error the returned object might be a special singleton that will
         7165  +** silently reject new text, always return SQLITE_NOMEM from 
         7166  +** [sqlite3_str_errcode()], always return 0 for 
         7167  +** [sqlite3_str_length()], and always return NULL from
         7168  +** [sqlite3_str_finish(X)].  It is always safe to use the value
         7169  +** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
         7170  +** to any of the other [sqlite3_str] methods.
  7163   7171   **
  7164   7172   ** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
  7165   7173   ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
  7166   7174   ** length of the string contained in the [sqlite3_str] object will be
  7167   7175   ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
  7168   7176   ** of [SQLITE_MAX_LENGTH].
  7169   7177   */