/ Check-in [6f368b54]
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:Modify the sqlite3_log() interface and implementation so that it never uses dynamic memory allocation - to avoid deadlocking when called while holding the memory allocator mutex. Cherry-pick merge of [28d1bc98d6].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | branch-3.6.22
Files: files | file ages | folders
SHA1: 6f368b5448363ae497bd4ff49b4ceeafb27f8cb3
User & Date: drh 2010-03-03 22:40:10
Context
2010-03-22
23:55
Sync the database file after a rollback. This is a cherrypick merge of check-in [b21b911f23]. Leaf check-in: 82dd61fc user: drh tags: branch-3.6.22
2010-03-03
22:40
Modify the sqlite3_log() interface and implementation so that it never uses dynamic memory allocation - to avoid deadlocking when called while holding the memory allocator mutex. Cherry-pick merge of [28d1bc98d6]. check-in: 6f368b54 user: drh tags: branch-3.6.22
00:02
When TEMP files are in memory, also put the massive TEMP file used by the VACUUM command in memory. This is a cherry-pick merge of [9daf4e7d07] check-in: e5342234 user: drh tags: branch-3.6.22
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/printf.c.

   932    932     acc.useMalloc = 0;
   933    933     va_start(ap,zFormat);
   934    934     sqlite3VXPrintf(&acc, 0, zFormat, ap);
   935    935     va_end(ap);
   936    936     z = sqlite3StrAccumFinish(&acc);
   937    937     return z;
   938    938   }
          939  +
          940  +/*
          941  +** This is the routine that actually formats the sqlite3_log() message.
          942  +** We house it in a separate routine from sqlite3_log() to avoid using
          943  +** stack space on small-stack systems when logging is disabled.
          944  +**
          945  +** sqlite3_log() must render into a static buffer.  It cannot dynamically
          946  +** allocate memory because it might be called while the memory allocator
          947  +** mutex is held.
          948  +*/
          949  +static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
          950  +  StrAccum acc;                           /* String accumulator */
          951  +#ifdef SQLITE_SMALL_STACK
          952  +  char zMsg[150];                         /* Complete log message */
          953  +#else
          954  +  char zMsg[400];                         /* Complete log message */
          955  +#endif
          956  +
          957  +  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
          958  +  acc.useMalloc = 0;
          959  +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
          960  +  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
          961  +                           sqlite3StrAccumFinish(&acc));
          962  +}
   939    963   
   940    964   /*
   941    965   ** Format and write a message to the log if logging is enabled.
   942    966   */
   943    967   void sqlite3_log(int iErrCode, const char *zFormat, ...){
   944         -  void (*xLog)(void*, int, const char*);  /* The global logger function */
   945         -  void *pLogArg;                          /* First argument to the logger */
   946    968     va_list ap;                             /* Vararg list */
   947         -  char *zMsg;                             /* Complete log message */
   948         -  
   949         -  xLog = sqlite3GlobalConfig.xLog;
   950         -  if( xLog && zFormat ){
          969  +  if( sqlite3GlobalConfig.xLog ){
   951    970       va_start(ap, zFormat);
   952         -    sqlite3BeginBenignMalloc();
   953         -    zMsg = sqlite3_vmprintf(zFormat, ap);
   954         -    sqlite3EndBenignMalloc();
          971  +    renderLogMsg(iErrCode, zFormat, ap);
   955    972       va_end(ap);
   956         -    pLogArg = sqlite3GlobalConfig.pLogArg;
   957         -    xLog(pLogArg, iErrCode, zMsg ? zMsg : zFormat);
   958         -    sqlite3_free(zMsg);
   959    973     }
   960    974   }
   961    975   
   962    976   #if defined(SQLITE_DEBUG)
   963    977   /*
   964    978   ** A version of printf() that understands %lld.  Used for debugging.
   965    979   ** The printf() built into some versions of windows does not understand %lld

Changes to src/sqlite.h.in.

  5649   5649   ** ^The [sqlite3_log()] interface writes a message into the error log
  5650   5650   ** established by the [SQLITE_CONFIG_ERRORLOG] option to [sqlite3_config()].
  5651   5651   **
  5652   5652   ** The sqlite3_log() interface is intended for use by extensions such as
  5653   5653   ** virtual tables, collating functions, and SQL functions.  While there is
  5654   5654   ** nothing to prevent an application from calling sqlite3_log(), doing so
  5655   5655   ** is considered bad form.
         5656  +**
         5657  +** To avoid deadlocks and other threading problems, the sqlite3_log() routine
         5658  +** will not use dynamically allocated memory.  The log message is stored in
         5659  +** a fixed-length buffer on the stack.  If the log message is longer than
         5660  +** a few hundred characters, it will be truncated to the length of the
         5661  +** buffer.
  5656   5662   */
  5657   5663   void sqlite3_log(int iErrCode, const char *zFormat, ...);
  5658   5664   
  5659   5665   /*
  5660   5666   ** Undo the hack that converts floating point types to integer for
  5661   5667   ** builds on processors without floating point support.
  5662   5668   */