1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-01 06:27:03 +03:00

Fix a file descriptor leak following malloc failure on DROP TABLE IF EXISTS. (CVS 3296)

FossilOrigin-Name: 6a63f76c8de977b628c4cab258be5a11d7d7def9
This commit is contained in:
drh
2006-06-26 12:50:09 +00:00
parent a2e48b24c7
commit 344a627661
5 changed files with 170 additions and 11 deletions

View File

@ -1,5 +1,5 @@
C Fix\strivial\scompiler\swarnings.\s(CVS\s3295) C Fix\sa\sfile\sdescriptor\sleak\sfollowing\smalloc\sfailure\son\sDROP\sTABLE\sIF\sEXISTS.\s(CVS\s3296)
D 2006-06-26T11:17:51 D 2006-06-26T12:50:09
F Makefile.in f839b470345d3cb4b0644068474623fe2464b5d3 F Makefile.in f839b470345d3cb4b0644068474623fe2464b5d3
F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935 F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -66,7 +66,7 @@ F src/pager.c 7ef4c5098b378d81d3687919d9de99ea99bbb6bb
F src/pager.h 43f32f3847421f7502cfbb66f4eb2302b8033818 F src/pager.h 43f32f3847421f7502cfbb66f4eb2302b8033818
F src/parse.y e0831a269fbbd21414bb367fd0b806569c934683 F src/parse.y e0831a269fbbd21414bb367fd0b806569c934683
F src/pragma.c 27d5e395c5d950931c7ac4fe610e7c2993e2fa55 F src/pragma.c 27d5e395c5d950931c7ac4fe610e7c2993e2fa55
F src/prepare.c 6dc945dab34cf97364c661d2b7a12be65d338267 F src/prepare.c e477df44112e3ce167f048226432fca9d9cba6a0
F src/printf.c 7029e5f7344a478394a02c52837ff296ee1ab240 F src/printf.c 7029e5f7344a478394a02c52837ff296ee1ab240
F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261 F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261
F src/select.c 380fa06c99ae01050c0054c4b1db91e9f1d8322d F src/select.c 380fa06c99ae01050c0054c4b1db91e9f1d8322d
@ -201,11 +201,12 @@ F test/lock.test 9b7afcb24f53d24da502abb33daaad2cd6d44107
F test/lock2.test d83ba79d3c4fffdb5b926c7d8ca7a36c34288a55 F test/lock2.test d83ba79d3c4fffdb5b926c7d8ca7a36c34288a55
F test/lock3.test 615111293cf32aa2ed16d01c6611737651c96fb9 F test/lock3.test 615111293cf32aa2ed16d01c6611737651c96fb9
F test/main.test e7212ce1023957c7209778cc87fa932bd79ba89a F test/main.test e7212ce1023957c7209778cc87fa932bd79ba89a
F test/malloc.test 684510c0e60dfb1660fa441e811ae79e97e8ea82 F test/malloc.test 98a189ae3d49ab6259578dbf776decc172c6b7b9
F test/malloc2.test e6e321db96d6c94cb18bf82ad7215070c41e624e F test/malloc2.test e6e321db96d6c94cb18bf82ad7215070c41e624e
F test/malloc3.test fd4186bee73c2a2638f4e2a05a684c06836f725b F test/malloc3.test fd4186bee73c2a2638f4e2a05a684c06836f725b
F test/malloc4.test 59cd02f71b363302a04c4e77b97c0a1572eaa210 F test/malloc4.test 59cd02f71b363302a04c4e77b97c0a1572eaa210
F test/malloc5.test 7425272e263325fda7d32cb55706e52b5c09e7e0 F test/malloc5.test 7425272e263325fda7d32cb55706e52b5c09e7e0
F test/malloc6.test 025ae0b78542e0ddd000d23f79d93e9be9ba0f15
F test/manydb.test 8de36b8d33aab5ef295b11d9e95310aeded31af8 F test/manydb.test 8de36b8d33aab5ef295b11d9e95310aeded31af8
F test/memdb.test a67bda4ff90a38f2b19f6c7f95aa7289e051d893 F test/memdb.test a67bda4ff90a38f2b19f6c7f95aa7289e051d893
F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac
@ -373,7 +374,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P df601a7b374cb6ec21a2e91abf17af4fed404393 P 3538beace8ece6339fe8aaf40852ce5e5e7da283
R d69c8936d3b462628cdb2178258952aa R 7f31a48109f4f6df3e35eac6efc09953
U danielk1977 U drh
Z 61efe5f570c81872f1fcd01cde3929c6 Z b3481a9c45f0d830fe849860e961f559

View File

@ -1 +1 @@
3538beace8ece6339fe8aaf40852ce5e5e7da283 6a63f76c8de977b628c4cab258be5a11d7d7def9

View File

@ -13,7 +13,7 @@
** interface, and routines that contribute to loading the database schema ** interface, and routines that contribute to loading the database schema
** from disk. ** from disk.
** **
** $Id: prepare.c,v 1.35 2006/06/12 06:09:18 danielk1977 Exp $ ** $Id: prepare.c,v 1.36 2006/06/26 12:50:09 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "os.h" #include "os.h"
@ -499,6 +499,9 @@ int sqlite3_prepare(
if( sParse.rc==SQLITE_SCHEMA ){ if( sParse.rc==SQLITE_SCHEMA ){
sqlite3ResetInternalSchema(db, 0); sqlite3ResetInternalSchema(db, 0);
} }
if( sqlite3MallocFailed() ){
sParse.rc = SQLITE_NOMEM;
}
if( pzTail ) *pzTail = sParse.zTail; if( pzTail ) *pzTail = sParse.zTail;
rc = sParse.rc; rc = sParse.rc;

View File

@ -14,7 +14,7 @@
# special feature is used to see what happens in the library if a malloc # 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. # were to really fail due to an out-of-memory situation.
# #
# $Id: malloc.test,v 1.32 2006/04/10 13:37:47 drh Exp $ # $Id: malloc.test,v 1.33 2006/06/26 12:50:09 drh Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@ -130,6 +130,7 @@ do_malloc_test 1 -tclprep {
error "out of memory" error "out of memory"
} }
} -sqlbody { } -sqlbody {
DROP TABLE IF EXISTS t1;
CREATE TABLE t1( CREATE TABLE t1(
a int, b float, c double, d text, e varchar(20), a int, b float, c double, d text, e varchar(20),
primary key(a,b,c) primary key(a,b,c)
@ -546,5 +547,6 @@ do_test malloc-99.X {
set sqlite_open_file_count set sqlite_open_file_count
} {0} } {0}
puts open-file-count=$sqlite_open_file_count
sqlite_malloc_fail 0 sqlite_malloc_fail 0
finish_test finish_test

153
test/malloc6.test Normal file
View File

@ -0,0 +1,153 @@
# 2006 June 25
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file attempts to check the library in an out-of-memory situation.
# When compiled with -DSQLITE_DEBUG=1, the SQLite library accepts a special
# command (sqlite_malloc_fail N) 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: malloc6.test,v 1.1 2006/06/26 12:50:09 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Only run these tests if memory debugging is turned on.
#
if {[info command sqlite_malloc_stat]==""} {
puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
finish_test
return
}
# Usage: do_malloc_test <test number> <options...>
#
# The first argument, <test number>, is an integer used to name the
# tests executed by this proc. Options are as follows:
#
# -tclprep TCL script to run to prepare test.
# -sqlprep SQL script to run to prepare test.
# -tclbody TCL script to run with malloc failure simulation.
# -sqlbody TCL script to run with malloc failure simulation.
# -cleanup TCL script to run after the test.
#
# This command runs a series of tests to verify SQLite's ability
# to handle an out-of-memory condition gracefully. It is assumed
# that if this condition occurs a malloc() call will return a
# NULL pointer. Linux, for example, doesn't do that by default. See
# the "BUGS" section of malloc(3).
#
# Each iteration of a loop, the TCL commands in any argument passed
# to the -tclbody switch, followed by the SQL commands in any argument
# passed to the -sqlbody switch are executed. Each iteration the
# Nth call to sqliteMalloc() is made to fail, where N is increased
# each time the loop runs starting from 1. When all commands execute
# successfully, the loop ends.
#
proc do_malloc_test {tn args} {
array unset ::mallocopts
array set ::mallocopts $args
set ::go 1
for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
do_test malloc6-$tn.$::n {
# Remove all traces of database files test.db and test2.db from the files
# system. Then open (empty database) "test.db" with the handle [db].
#
sqlite_malloc_fail 0
catch {db close}
catch {file delete -force test.db}
catch {file delete -force test.db-journal}
catch {file delete -force test2.db}
catch {file delete -force test2.db-journal}
catch {sqlite3 db test.db}
set ::DB [sqlite3_connection_pointer db]
# Execute any -tclprep and -sqlprep scripts.
#
if {[info exists ::mallocopts(-tclprep)]} {
eval $::mallocopts(-tclprep)
}
if {[info exists ::mallocopts(-sqlprep)]} {
execsql $::mallocopts(-sqlprep)
}
# Now set the ${::n}th malloc() to fail and execute the -tclbody and
# -sqlbody scripts.
#
sqlite_malloc_fail $::n
set ::mallocbody {}
if {[info exists ::mallocopts(-tclbody)]} {
append ::mallocbody "$::mallocopts(-tclbody)\n"
}
if {[info exists ::mallocopts(-sqlbody)]} {
append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
}
set v [catch $::mallocbody msg]
# If the test fails (if $v!=0) and the database connection actually
# exists, make sure the failure code is SQLITE_NOMEM.
if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)]
&& [db errorcode]!=7} {
set v 999
}
set leftover [lindex [sqlite_malloc_stat] 2]
if {$leftover>0} {
if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
set ::go 0
if {$v} {
puts "\nError message returned: $msg"
} else {
set v {1 1}
}
} else {
set v2 [expr {$msg=="" || $msg=="out of memory"}]
if {!$v2} {puts "\nError message returned: $msg"}
lappend v $v2
}
} {1 1}
if {[info exists ::mallocopts(-cleanup)]} {
catch [list uplevel #0 $::mallocopts(-cleanup)] msg
}
}
unset ::mallocopts
}
set sqlite_os_trace 0
do_malloc_test 1 -tclprep {
db close
} -tclbody {
if {[catch {sqlite3 db test.db}]} {
error "out of memory"
}
} -sqlbody {
DROP TABLE IF EXISTS t1;
CREATE TABLE IF NOT EXISTS t1(
a int, b float, c double, d text, e varchar(20),
primary key(a,b,c)
);
CREATE TABLE IF NOT EXISTS t1(
a int, b float, c double, d text, e varchar(20),
primary key(a,b,c)
);
DROP TABLE IF EXISTS t1;
}
# Ensure that no file descriptors were leaked.
do_test malloc6-1.X {
catch {db close}
set sqlite_open_file_count
} {0}
sqlite_malloc_fail 0
finish_test