/ Check-in [118dc0ba]
Login

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

Overview
Comment:Fix a memory leak that can follow a malloc failure in sqlite3_initialize. (CVS 5731)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:118dc0ba082dd9abba5602dafc86bd56e756db86
User & Date: danielk1977 2008-09-22 17:22:20
Context
2008-09-22
17:54
Get rid of all of the compiler magic associated with SQLITE_EXPERIMENTAL and SQLITE_DEPRECATED. It was causing more problems than it was solving. Ticket #3395 et al. (CVS 5732) check-in: bc040073 user: drh tags: trunk
17:22
Fix a memory leak that can follow a malloc failure in sqlite3_initialize. (CVS 5731) check-in: 118dc0ba user: danielk1977 tags: trunk
11:46
Add instrumentation to os_unix.c to test that a return value of SQLITE_FULL from an xSync() callback is handled correctly. (CVS 5730) check-in: 7bd2da93 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.500 2008/09/08 08:08:09 danielk1977 Exp $
           17  +** $Id: main.c,v 1.501 2008/09/22 17:22:20 danielk1977 Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   #ifdef SQLITE_ENABLE_FTS3
    23     23   # include "fts3.h"
    24     24   #endif
................................................................................
   131    131       sqlite3GlobalConfig.isMallocInit = 1;
   132    132       if( !sqlite3GlobalConfig.pInitMutex ){
   133    133         sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
   134    134         if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
   135    135           rc = SQLITE_NOMEM;
   136    136         }
   137    137       }
          138  +  }
          139  +  if( rc==SQLITE_OK ){
   138    140       sqlite3GlobalConfig.nRefInitMutex++;
   139    141     }
   140    142     sqlite3_mutex_leave(pMaster);
   141    143   
   142    144     /* If unable to initialize the malloc subsystem, then return early.
   143    145     ** There is little hope of getting SQLite to run if the malloc
   144    146     ** subsystem cannot be initialized.

Changes to test/malloc.test.

    12     12   # This file attempts to check the behavior of the SQLite library in 
    13     13   # an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, 
    14     14   # the SQLite library accepts a special command (sqlite3_memdebug_fail N C)
    15     15   # which causes the N-th malloc to fail.  This special feature is used
    16     16   # to see what happens in the library if a malloc were to really fail
    17     17   # due to an out-of-memory situation.
    18     18   #
    19         -# $Id: malloc.test,v 1.65 2008/09/05 05:02:47 danielk1977 Exp $
           19  +# $Id: malloc.test,v 1.66 2008/09/22 17:22:20 danielk1977 Exp $
    20     20   
    21     21   set testdir [file dirname $argv0]
    22     22   source $testdir/tester.tcl
    23     23   
    24     24   
    25     25   # Only run these tests if memory debugging is turned on.
    26     26   #
................................................................................
    34     34   # Do a couple of memory dumps just to exercise the memory dump logic
    35     35   # that that we can say that we have.
    36     36   #
    37     37   puts stderr "This is a test.  Ignore the error that follows:"
    38     38   sqlite3_memdebug_dump $testdir
    39     39   puts "Memory dump to file memdump.txt..."
    40     40   sqlite3_memdebug_dump memdump.txt
           41  +
    41     42   
    42     43   ifcapable bloblit&&subquery {
    43     44     do_malloc_test 1 -tclprep {
    44     45       db close
    45     46     } -tclbody {
    46     47       if {[catch {sqlite3 db test.db}]} {
    47     48         error "out of memory"
................................................................................
   631    632     INSERT INTO t1 VALUES(1, randomblob(210));
   632    633     COMMIT;
   633    634   } -tclbody {
   634    635     db close
   635    636     sqlite3 db test.db
   636    637     db eval { INSERT INTO t1 VALUES(1, randomblob(210)) }
   637    638   }
          639  +
          640  +# Test that no memory is leaked following a malloc() failure in
          641  +# sqlite3_initialize().
          642  +#
          643  +do_malloc_test 27 -tclprep {
          644  +  db close
          645  +  sqlite3_shutdown
          646  +} -tclbody {
          647  +  set rc [sqlite3_initialize]
          648  +  if {$rc == "SQLITE_NOMEM"} {
          649  +    error "out of memory"
          650  +  }
          651  +}
   638    652   
   639    653   # Ensure that no file descriptors were leaked.
   640    654   do_test malloc-99.X {
   641    655     catch {db close}
   642    656     set sqlite_open_file_count
   643    657   } {0}
   644    658   
   645    659   puts open-file-count=$sqlite_open_file_count
   646    660   finish_test