/ Check-in [7acc8cd3]
Login

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

Overview
Comment:Documentation changes to warn that sqlite3_set_auxdata() might call the destructor even before it returns. Also fix the regexp extension to deal with that case. Ticket [406d3b2ef91c].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:7acc8cd32d593a473c9e9adaf323220a7a46480a
User & Date: drh 2013-07-18 14:16:48
Context
2013-07-18
14:50
Fix a 8-byte alignment problem in the query planner that might cause problems on sparc when compiled with -m32. check-in: 5dcffa67 user: drh tags: trunk
14:16
Documentation changes to warn that sqlite3_set_auxdata() might call the destructor even before it returns. Also fix the regexp extension to deal with that case. Ticket [406d3b2ef91c]. check-in: 7acc8cd3 user: drh tags: trunk
2013-07-17
21:08
Fix copy/paste errors in comments in the transitive_closure virtual table. No changes to code. check-in: b1b0de29 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/regexp.c.

   709    709     int argc, 
   710    710     sqlite3_value **argv
   711    711   ){
   712    712     ReCompiled *pRe;          /* Compiled regular expression */
   713    713     const char *zPattern;     /* The regular expression */
   714    714     const unsigned char *zStr;/* String being searched */
   715    715     const char *zErr;         /* Compile error message */
          716  +  int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
   716    717   
   717    718     pRe = sqlite3_get_auxdata(context, 0);
   718    719     if( pRe==0 ){
   719    720       zPattern = (const char*)sqlite3_value_text(argv[0]);
   720    721       if( zPattern==0 ) return;
   721    722       zErr = re_compile(&pRe, zPattern, 0);
   722    723       if( zErr ){
................................................................................
   724    725         sqlite3_result_error(context, zErr, -1);
   725    726         return;
   726    727       }
   727    728       if( pRe==0 ){
   728    729         sqlite3_result_error_nomem(context);
   729    730         return;
   730    731       }
   731         -    sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
          732  +    setAux = 1;
   732    733     }
   733    734     zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
   734    735     if( zStr!=0 ){
   735    736       sqlite3_result_int(context, re_match(pRe, zStr, -1));
   736    737     }
          738  +  if( setAux ){
          739  +    sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
          740  +  }
   737    741   }
   738    742   
   739    743   /*
   740    744   ** Invoke this routine to register the regexp() function with the
   741    745   ** SQLite database connection.
   742    746   */
   743    747   #ifdef _WIN32

Changes to src/sqlite.h.in.

  4175   4175   
  4176   4176   /*
  4177   4177   ** CAPI3REF: Function Auxiliary Data
  4178   4178   **
  4179   4179   ** The following two functions may be used by scalar SQL functions to
  4180   4180   ** associate metadata with argument values. If the same value is passed to
  4181   4181   ** multiple invocations of the same SQL function during query execution, under
  4182         -** some circumstances the associated metadata may be preserved. This may
  4183         -** be used, for example, to add a regular-expression matching scalar
         4182  +** some circumstances the associated metadata may be preserved. This might
         4183  +** be used, for example, in a regular-expression matching
  4184   4184   ** function. The compiled version of the regular expression is stored as
  4185   4185   ** metadata associated with the SQL value passed as the regular expression
  4186   4186   ** pattern.  The compiled regular expression can be reused on multiple
  4187   4187   ** invocations of the same function so that the original pattern string
  4188   4188   ** does not need to be recompiled on each invocation.
  4189   4189   **
  4190   4190   ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  4191   4191   ** associated by the sqlite3_set_auxdata() function with the Nth argument
  4192   4192   ** value to the application-defined function. ^If no metadata has been ever
  4193   4193   ** been set for the Nth argument of the function, or if the corresponding
  4194   4194   ** function parameter has changed since the meta-data was set,
  4195   4195   ** then sqlite3_get_auxdata() returns a NULL pointer.
  4196   4196   **
  4197         -** ^The sqlite3_set_auxdata() interface saves the metadata
  4198         -** pointed to by its 3rd parameter as the metadata for the N-th
  4199         -** argument of the application-defined function.  Subsequent
  4200         -** calls to sqlite3_get_auxdata() might return this data, if it has
  4201         -** not been destroyed.
  4202         -** ^If it is not NULL, SQLite will invoke the destructor
  4203         -** function given by the 4th parameter to sqlite3_set_auxdata() on
  4204         -** the metadata when the corresponding function parameter changes
  4205         -** or when the SQL statement completes, whichever comes first.
         4197  +** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
         4198  +** argument of the application-defined function.  ^Subsequent
         4199  +** calls to sqlite3_get_auxdata(C,N) return P from the most recent
         4200  +** sqlite3_set_auxdata(C,N,P,X) call if the data has not been dropped, or
         4201  +** NULL if the data has been dropped.
         4202  +** ^(If it is not NULL, SQLite will invoke the destructor
         4203  +** function X passed to sqlite3_set_auxdata(C,N,P,X) when <ul>
         4204  +** <li> the corresponding function parameter changes,
         4205  +** <li> [sqlite3_reset()] or [sqlite3_finalize()] is called for the
         4206  +**      SQL statement,
         4207  +** <li> sqlite3_set_auxdata() is invoked again on the same parameter, or
         4208  +** <li> a memory allocation error occurs. </ul>)^
  4206   4209   **
  4207   4210   ** SQLite is free to call the destructor and drop metadata on any
  4208   4211   ** parameter of any function at any time.  ^The only guarantee is that
  4209         -** the destructor will be called before the metadata is dropped.
         4212  +** the destructor will be called when the [prepared statement] is destroyed.
         4213  +** Note in particular that the destructor X in the call to
         4214  +** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before
         4215  +** the sqlite3_set_auxdata() call even returns.  Hence sqlite3_set_auxdata()
         4216  +** should be called near the end of the function implementation and the
         4217  +** implementation should not make any use of P after sqlite3_set_auxdata()
         4218  +** has been called.
  4210   4219   **
  4211   4220   ** ^(In practice, metadata is preserved between function calls for
  4212         -** expressions that are constant at compile time. This includes literal
  4213         -** values and [parameters].)^
         4221  +** function parameters that are compile-time constants, including literal
         4222  +** values and [parameters] and expressions composed from the same.)^
  4214   4223   **
  4215   4224   ** These routines must be called from the same thread in which
  4216   4225   ** the SQL function is running.
  4217   4226   */
  4218   4227   void *sqlite3_get_auxdata(sqlite3_context*, int N);
  4219   4228   void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  4220   4229