/ 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 Unified Diffs Ignore Whitespace Patch

Changes to src/main.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
131
132
133
134
135
136
137


138
139
140
141
142
143
144
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.500 2008/09/08 08:08:09 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

#ifdef SQLITE_ENABLE_FTS3
# include "fts3.h"
#endif
................................................................................
    sqlite3GlobalConfig.isMallocInit = 1;
    if( !sqlite3GlobalConfig.pInitMutex ){
      sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
        rc = SQLITE_NOMEM;
      }
    }


    sqlite3GlobalConfig.nRefInitMutex++;
  }
  sqlite3_mutex_leave(pMaster);

  /* If unable to initialize the malloc subsystem, then return early.
  ** There is little hope of getting SQLite to run if the malloc
  ** subsystem cannot be initialized.







|







 







>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.501 2008/09/22 17:22:20 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

#ifdef SQLITE_ENABLE_FTS3
# include "fts3.h"
#endif
................................................................................
    sqlite3GlobalConfig.isMallocInit = 1;
    if( !sqlite3GlobalConfig.pInitMutex ){
      sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
        rc = SQLITE_NOMEM;
      }
    }
  }
  if( rc==SQLITE_OK ){
    sqlite3GlobalConfig.nRefInitMutex++;
  }
  sqlite3_mutex_leave(pMaster);

  /* If unable to initialize the malloc subsystem, then return early.
  ** There is little hope of getting SQLite to run if the malloc
  ** subsystem cannot be initialized.

Changes to test/malloc.test.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
..
34
35
36
37
38
39
40

41
42
43
44
45
46
47
...
631
632
633
634
635
636
637













638
639
640
641
642
643
644
645
646
# This file attempts to check the behavior of the SQLite library in 
# an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, 
# the SQLite library accepts a special command (sqlite3_memdebug_fail N C)
# which causes the N-th malloc to fail.  This special feature is used
# to see what happens in the library if a malloc were to really fail
# due to an out-of-memory situation.
#
# $Id: malloc.test,v 1.65 2008/09/05 05:02:47 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl


# Only run these tests if memory debugging is turned on.
#
................................................................................
# Do a couple of memory dumps just to exercise the memory dump logic
# that that we can say that we have.
#
puts stderr "This is a test.  Ignore the error that follows:"
sqlite3_memdebug_dump $testdir
puts "Memory dump to file memdump.txt..."
sqlite3_memdebug_dump memdump.txt


ifcapable bloblit&&subquery {
  do_malloc_test 1 -tclprep {
    db close
  } -tclbody {
    if {[catch {sqlite3 db test.db}]} {
      error "out of memory"
................................................................................
  INSERT INTO t1 VALUES(1, randomblob(210));
  COMMIT;
} -tclbody {
  db close
  sqlite3 db test.db
  db eval { INSERT INTO t1 VALUES(1, randomblob(210)) }
}














# Ensure that no file descriptors were leaked.
do_test malloc-99.X {
  catch {db close}
  set sqlite_open_file_count
} {0}

puts open-file-count=$sqlite_open_file_count
finish_test







|







 







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>









12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
..
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
...
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# This file attempts to check the behavior of the SQLite library in 
# an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, 
# the SQLite library accepts a special command (sqlite3_memdebug_fail N C)
# which causes the N-th malloc to fail.  This special feature is used
# to see what happens in the library if a malloc were to really fail
# due to an out-of-memory situation.
#
# $Id: malloc.test,v 1.66 2008/09/22 17:22:20 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl


# Only run these tests if memory debugging is turned on.
#
................................................................................
# Do a couple of memory dumps just to exercise the memory dump logic
# that that we can say that we have.
#
puts stderr "This is a test.  Ignore the error that follows:"
sqlite3_memdebug_dump $testdir
puts "Memory dump to file memdump.txt..."
sqlite3_memdebug_dump memdump.txt


ifcapable bloblit&&subquery {
  do_malloc_test 1 -tclprep {
    db close
  } -tclbody {
    if {[catch {sqlite3 db test.db}]} {
      error "out of memory"
................................................................................
  INSERT INTO t1 VALUES(1, randomblob(210));
  COMMIT;
} -tclbody {
  db close
  sqlite3 db test.db
  db eval { INSERT INTO t1 VALUES(1, randomblob(210)) }
}

# Test that no memory is leaked following a malloc() failure in
# sqlite3_initialize().
#
do_malloc_test 27 -tclprep {
  db close
  sqlite3_shutdown
} -tclbody {
  set rc [sqlite3_initialize]
  if {$rc == "SQLITE_NOMEM"} {
    error "out of memory"
  }
}

# Ensure that no file descriptors were leaked.
do_test malloc-99.X {
  catch {db close}
  set sqlite_open_file_count
} {0}

puts open-file-count=$sqlite_open_file_count
finish_test