sl@0: # 2001 September 15 sl@0: # sl@0: # Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved. sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # This file attempts to check the behavior of the SQLite library in sl@0: # an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, sl@0: # the SQLite library accepts a special command (sqlite3_memdebug_fail N C) sl@0: # which causes the N-th malloc to fail. This special feature is used sl@0: # to see what happens in the library if a malloc were to really fail sl@0: # due to an out-of-memory situation. sl@0: # sl@0: # $Id: malloc.test,v 1.67 2008/09/23 16:41:30 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: sl@0: # Only run these tests if memory debugging is turned on. sl@0: # sl@0: source $testdir/malloc_common.tcl sl@0: if {!$MEMDEBUG} { sl@0: puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: if {$tcl_platform(platform)!="symbian"} { sl@0: # Do a couple of memory dumps just to exercise the memory dump logic sl@0: # that that we can say that we have. sl@0: # sl@0: puts stderr "This is a test. Ignore the error that follows:" sl@0: sqlite3_memdebug_dump $testdir sl@0: puts "Memory dump to file memdump.txt..." sl@0: sqlite3_memdebug_dump memdump.txt sl@0: } sl@0: sl@0: ifcapable bloblit&&subquery { sl@0: do_malloc_test 1 -tclprep { sl@0: db close sl@0: } -tclbody { sl@0: if {[catch {sqlite3 db test.db}]} { sl@0: error "out of memory" sl@0: } sl@0: sqlite3_extended_result_codes db 1 sl@0: } -sqlbody { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE TABLE t1( sl@0: a int, b float, c double, d text, e varchar(20), sl@0: primary key(a,b,c) sl@0: ); sl@0: CREATE INDEX i1 ON t1(a,b); sl@0: INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500'); sl@0: INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder'); sl@0: SELECT * FROM t1; sl@0: SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; sl@0: DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1); sl@0: SELECT count(*), group_concat(e) FROM t1; sl@0: SELECT b FROM t1 ORDER BY 1 COLLATE nocase; sl@0: } sl@0: } sl@0: sl@0: # Ensure that no file descriptors were leaked. sl@0: do_test malloc-1.X { sl@0: catch {db close} sl@0: set sqlite_open_file_count sl@0: } {0} sl@0: sl@0: ifcapable subquery { sl@0: do_malloc_test 2 -sqlbody { sl@0: CREATE TABLE t1(a int, b int default 'abc', c int default 1); sl@0: CREATE INDEX i1 ON t1(a,b); sl@0: INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz'); sl@0: INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz'); sl@0: INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz'); sl@0: INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz'); sl@0: INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz'); sl@0: INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz'); sl@0: SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1; sl@0: UPDATE t1 SET b=b||b||b||b; sl@0: UPDATE t1 SET b=a WHERE a in (10,12,22); sl@0: INSERT INTO t1(c,b,a) VALUES(20,10,5); sl@0: INSERT INTO t1 SELECT * FROM t1 sl@0: WHERE a IN (SELECT a FROM t1 WHERE a<10); sl@0: DELETE FROM t1 WHERE a>=10; sl@0: DROP INDEX i1; sl@0: DELETE FROM t1; sl@0: } sl@0: } sl@0: sl@0: # Ensure that no file descriptors were leaked. sl@0: do_test malloc-2.X { sl@0: catch {db close} sl@0: set sqlite_open_file_count sl@0: } {0} sl@0: sl@0: do_malloc_test 3 -sqlbody { sl@0: BEGIN TRANSACTION; sl@0: CREATE TABLE t1(a int, b int, c int); sl@0: CREATE INDEX i1 ON t1(a,b); sl@0: INSERT INTO t1 VALUES(1,1,99); sl@0: INSERT INTO t1 VALUES(2,4,98); sl@0: INSERT INTO t1 VALUES(3,9,97); sl@0: INSERT INTO t1 VALUES(4,16,96); sl@0: INSERT INTO t1 VALUES(5,25,95); sl@0: INSERT INTO t1 VALUES(6,36,94); sl@0: INSERT INTO t1(c,b,a) VALUES(20,10,5); sl@0: DELETE FROM t1 WHERE a>=10; sl@0: DROP INDEX i1; sl@0: DELETE FROM t1; sl@0: ROLLBACK; sl@0: } sl@0: sl@0: sl@0: # Ensure that no file descriptors were leaked. sl@0: do_test malloc-3.X { sl@0: catch {db close} sl@0: set sqlite_open_file_count sl@0: } {0} sl@0: sl@0: ifcapable subquery { sl@0: do_malloc_test 4 -sqlbody { sl@0: BEGIN TRANSACTION; sl@0: CREATE TABLE t1(a int, b int, c int); sl@0: CREATE INDEX i1 ON t1(a,b); sl@0: INSERT INTO t1 VALUES(1,1,99); sl@0: INSERT INTO t1 VALUES(2,4,98); sl@0: INSERT INTO t1 VALUES(3,9,97); sl@0: INSERT INTO t1 VALUES(4,16,96); sl@0: INSERT INTO t1 VALUES(5,25,95); sl@0: INSERT INTO t1 VALUES(6,36,94); sl@0: UPDATE t1 SET b=a WHERE a in (10,12,22); sl@0: INSERT INTO t1 SELECT * FROM t1 sl@0: WHERE a IN (SELECT a FROM t1 WHERE a<10); sl@0: DROP INDEX i1; sl@0: DELETE FROM t1; sl@0: COMMIT; sl@0: } sl@0: } sl@0: sl@0: # Ensure that no file descriptors were leaked. sl@0: do_test malloc-4.X { sl@0: catch {db close} sl@0: set sqlite_open_file_count sl@0: } {0} sl@0: sl@0: ifcapable trigger { sl@0: do_malloc_test 5 -sqlbody { sl@0: BEGIN TRANSACTION; sl@0: CREATE TABLE t1(a,b); sl@0: CREATE TABLE t2(x,y); sl@0: CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a = 2 BEGIN sl@0: INSERT INTO t2(x,y) VALUES(new.rowid,1); sl@0: INSERT INTO t2(x,y) SELECT * FROM t2; sl@0: INSERT INTO t2 SELECT * FROM t2; sl@0: UPDATE t2 SET y=y+1 WHERE x=new.rowid; sl@0: SELECT 123; sl@0: DELETE FROM t2 WHERE x=new.rowid; sl@0: END; sl@0: INSERT INTO t1(a,b) VALUES(2,3); sl@0: COMMIT; sl@0: } sl@0: } sl@0: sl@0: # Ensure that no file descriptors were leaked. sl@0: do_test malloc-5.X { sl@0: catch {db close} sl@0: set sqlite_open_file_count sl@0: } {0} sl@0: sl@0: ifcapable vacuum { sl@0: do_malloc_test 6 -sqlprep { sl@0: BEGIN TRANSACTION; sl@0: CREATE TABLE t1(a); sl@0: INSERT INTO t1 VALUES(1); sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2 FROM t1; sl@0: DELETE FROM t1 where rowid%5 = 0; sl@0: COMMIT; sl@0: } -sqlbody { sl@0: VACUUM; sl@0: } sl@0: } sl@0: sl@0: do_malloc_test 7 -sqlprep { sl@0: CREATE TABLE t1(a, b); sl@0: INSERT INTO t1 VALUES(1, 2); sl@0: INSERT INTO t1 VALUES(3, 4); sl@0: INSERT INTO t1 VALUES(5, 6); sl@0: INSERT INTO t1 VALUES(7, randstr(1200,1200)); sl@0: } -sqlbody { sl@0: SELECT min(a) FROM t1 WHERE a<6 GROUP BY b; sl@0: SELECT a FROM t1 WHERE a<6 ORDER BY a; sl@0: SELECT b FROM t1 WHERE a>6; sl@0: } sl@0: sl@0: # This block is designed to test that some malloc failures that may sl@0: # occur in vdbeapi.c. Specifically, if a malloc failure that occurs sl@0: # when converting UTF-16 text to integers and real numbers is handled sl@0: # correctly. sl@0: # sl@0: # This is done by retrieving a string from the database engine and sl@0: # manipulating it using the sqlite3_column_*** APIs. This doesn't sl@0: # actually return an error to the user when a malloc() fails.. That sl@0: # could be viewed as a bug. sl@0: # sl@0: # These tests only run if UTF-16 support is compiled in. sl@0: # sl@0: ifcapable utf16 { sl@0: set ::STMT {} sl@0: do_malloc_test 8 -tclprep { sl@0: set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?" sl@0: set ::STMT [sqlite3_prepare db $sql -1 X] sl@0: sqlite3_step $::STMT sl@0: if { $::tcl_platform(byteOrder)=="littleEndian" } { sl@0: set ::bomstr "\xFF\xFE" sl@0: } else { sl@0: set ::bomstr "\xFE\xFF" sl@0: } sl@0: append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"] sl@0: } -tclbody { sl@0: sqlite3_column_text16 $::STMT 0 sl@0: sqlite3_column_int $::STMT 0 sl@0: sqlite3_column_text16 $::STMT 1 sl@0: sqlite3_column_double $::STMT 1 sl@0: set rc [sqlite3_reset $::STMT] sl@0: if {$rc eq "SQLITE_NOMEM"} {error "out of memory"} sl@0: sqlite3_bind_text16 $::STMT 1 $::bomstr 60 sl@0: #catch {sqlite3_finalize $::STMT} sl@0: #if {[lindex [sqlite_malloc_stat] 2]<=0} { sl@0: # error "out of memory" sl@0: #} sl@0: } -cleanup { sl@0: if {$::STMT!=""} { sl@0: sqlite3_finalize $::STMT sl@0: set ::STMT {} sl@0: } sl@0: } sl@0: } sl@0: sl@0: # This block tests that malloc() failures that occur whilst commiting sl@0: # a multi-file transaction are handled correctly. sl@0: # sl@0: do_malloc_test 9 -sqlprep { sl@0: ATTACH 'test2.db' as test2; sl@0: CREATE TABLE abc1(a, b, c); sl@0: CREATE TABLE test2.abc2(a, b, c); sl@0: } -sqlbody { sl@0: BEGIN; sl@0: INSERT INTO abc1 VALUES(1, 2, 3); sl@0: INSERT INTO abc2 VALUES(1, 2, 3); sl@0: COMMIT; sl@0: } sl@0: sl@0: # This block tests malloc() failures that occur while opening a sl@0: # connection to a database. sl@0: do_malloc_test 10 -tclprep { sl@0: catch {db2 close} sl@0: db close sl@0: file delete -force test.db test.db-journal sl@0: sqlite3 db test.db sl@0: sqlite3_extended_result_codes db 1 sl@0: db eval {CREATE TABLE abc(a, b, c)} sl@0: } -tclbody { sl@0: db close sl@0: sqlite3 db2 test.db sl@0: sqlite3_extended_result_codes db2 1 sl@0: db2 eval {SELECT * FROM sqlite_master} sl@0: db2 close sl@0: } sl@0: sl@0: # This block tests malloc() failures that occur within calls to sl@0: # sqlite3_create_function(). sl@0: do_malloc_test 11 -tclbody { sl@0: set rc [sqlite3_create_function db] sl@0: if {[string match $rc SQLITE_OK]} { sl@0: set rc [sqlite3_create_aggregate db] sl@0: } sl@0: if {[string match $rc SQLITE_NOMEM]} { sl@0: error "out of memory" sl@0: } sl@0: } sl@0: sl@0: do_malloc_test 12 -tclbody { sl@0: set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] sl@0: append sql16 "\00\00" sl@0: set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY] sl@0: sqlite3_finalize $::STMT sl@0: } sl@0: sl@0: # Test malloc errors when replaying two hot journals from a 2-file sl@0: # transaction. sl@0: ifcapable crashtest&&attach { sl@0: do_malloc_test 13 -tclprep { sl@0: set rc [crashsql -delay 1 -file test2.db { sl@0: ATTACH 'test2.db' as aux; sl@0: PRAGMA cache_size = 10; sl@0: BEGIN; sl@0: CREATE TABLE aux.t2(a, b, c); sl@0: CREATE TABLE t1(a, b, c); sl@0: COMMIT; sl@0: }] sl@0: if {$rc!="1 {child process exited abnormally}"} { sl@0: error "Wrong error message: $rc" sl@0: } sl@0: } -tclbody { sl@0: db eval {ATTACH 'test2.db' as aux;} sl@0: set rc [catch {db eval { sl@0: SELECT * FROM t1; sl@0: SELECT * FROM t2; sl@0: }} err] sl@0: if {$rc && $err!="no such table: t1"} { sl@0: error $err sl@0: } sl@0: } sl@0: } sl@0: sl@0: if {$tcl_platform(platform)!="windows" && $tcl_platform(platform)!="symbian"} { sl@0: do_malloc_test 14 -tclprep { sl@0: catch {db close} sl@0: sqlite3 db2 test2.db sl@0: sqlite3_extended_result_codes db2 1 sl@0: db2 eval { sl@0: PRAGMA synchronous = 0; sl@0: CREATE TABLE t1(a, b); sl@0: INSERT INTO t1 VALUES(1, 2); sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(3, 4); sl@0: } sl@0: copy_file test2.db test.db sl@0: copy_file test2.db-journal test.db-journal sl@0: db2 close sl@0: } -tclbody { sl@0: sqlite3 db test.db sl@0: sqlite3_extended_result_codes db 1 sl@0: sl@0: # If an out-of-memory occurs within a call to a VFS layer function during sl@0: # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit sl@0: # [5668] for details. sl@0: set rc [catch {db eval { SELECT * FROM t1 }} msg] sl@0: if {$msg eq "database disk image is malformed"} { set msg "out of memory" } sl@0: if {$rc} { error $msg } sl@0: set msg sl@0: } sl@0: } sl@0: sl@0: proc string_compare {a b} { sl@0: return [string compare $a $b] sl@0: } sl@0: sl@0: # Test for malloc() failures in sqlite3_create_collation() and sl@0: # sqlite3_create_collation16(). sl@0: # sl@0: ifcapable utf16 { sl@0: do_malloc_test 15 -start 4 -tclbody { sl@0: db collate string_compare string_compare sl@0: if {[catch {add_test_collate db 1 1 1} msg]} { sl@0: if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"} sl@0: error $msg sl@0: } sl@0: sl@0: db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} sl@0: db complete {-- Useful comment} sl@0: sl@0: execsql { sl@0: CREATE TABLE t1(a, b COLLATE string_compare); sl@0: INSERT INTO t1 VALUES(10, 'string'); sl@0: INSERT INTO t1 VALUES(10, 'string2'); sl@0: } sl@0: } sl@0: } sl@0: sl@0: # Also test sqlite3_complete(). There are (currently) no malloc() sl@0: # calls in this function, but test anyway against future changes. sl@0: # sl@0: do_malloc_test 16 -tclbody { sl@0: db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} sl@0: db complete {-- Useful comment} sl@0: db eval { sl@0: SELECT * FROM sqlite_master; sl@0: } sl@0: } sl@0: sl@0: # Test handling of malloc() failures in sqlite3_open16(). sl@0: # sl@0: ifcapable utf16 { sl@0: do_malloc_test 17 -tclbody { sl@0: set DB2 0 sl@0: set STMT 0 sl@0: sl@0: # open database using sqlite3_open16() sl@0: set filename [encoding convertto unicode test.db] sl@0: append filename "\x00\x00" sl@0: set DB2 [sqlite3_open16 $filename -unused] sl@0: if {0==$DB2} { sl@0: error "out of memory" sl@0: } sl@0: sqlite3_extended_result_codes $DB2 1 sl@0: sl@0: # Prepare statement sl@0: set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg] sl@0: if {[sqlite3_errcode $DB2] eq "SQLITE_IOERR+12"} { sl@0: error "out of memory" sl@0: } sl@0: if {[regexp ".*automatic extension loading.*" [sqlite3_errmsg $DB2]]} { sl@0: error "out of memory" sl@0: } sl@0: if {$rc} { sl@0: error [string range $msg 4 end] sl@0: } sl@0: set STMT $msg sl@0: sl@0: # Finalize statement sl@0: set rc [sqlite3_finalize $STMT] sl@0: if {$rc!="SQLITE_OK"} { sl@0: error [sqlite3_errmsg $DB2] sl@0: } sl@0: set STMT 0 sl@0: sl@0: # Close database sl@0: set rc [sqlite3_close $DB2] sl@0: if {$rc!="SQLITE_OK"} { sl@0: error [sqlite3_errmsg $DB2] sl@0: } sl@0: set DB2 0 sl@0: } -cleanup { sl@0: if {$STMT!="0"} { sl@0: sqlite3_finalize $STMT sl@0: } sl@0: if {$DB2!="0"} { sl@0: set rc [sqlite3_close $DB2] sl@0: } sl@0: } sl@0: } sl@0: sl@0: # Test handling of malloc() failures in sqlite3_errmsg16(). sl@0: # sl@0: ifcapable utf16 { sl@0: do_malloc_test 18 -tclprep { sl@0: catch { sl@0: db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master" sl@0: } sl@0: } -tclbody { sl@0: set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]] sl@0: binary scan $utf16 c* bytes sl@0: if {[llength $bytes]==0} { sl@0: error "out of memory" sl@0: } sl@0: } sl@0: } sl@0: sl@0: # This test is aimed at coverage testing. Specificly, it is supposed to sl@0: # cause a malloc() only used when converting between the two utf-16 sl@0: # encodings to fail (i.e. little-endian->big-endian). It only actually sl@0: # hits this malloc() on little-endian hosts. sl@0: # sl@0: set static_string "\x00h\x00e\x00l\x00l\x00o" sl@0: for {set l 0} {$l<10} {incr l} { sl@0: append static_string $static_string sl@0: } sl@0: append static_string "\x00\x00" sl@0: do_malloc_test 19 -tclprep { sl@0: execsql { sl@0: PRAGMA encoding = "UTF16be"; sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: } -tclbody { sl@0: unset -nocomplain ::STMT sl@0: set r [catch { sl@0: set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY] sl@0: sqlite3_bind_text16 -static $::STMT 1 $static_string 112 sl@0: } msg] sl@0: if {$r} {error [string range $msg 4 end]} sl@0: set msg sl@0: } -cleanup { sl@0: if {[info exists ::STMT]} { sl@0: sqlite3_finalize $::STMT sl@0: } sl@0: } sl@0: unset static_string sl@0: sl@0: # Make sure SQLITE_NOMEM is reported out on an ATTACH failure even sl@0: # when the malloc failure occurs within the nested parse. sl@0: # sl@0: ifcapable attach { sl@0: do_malloc_test 20 -tclprep { sl@0: db close sl@0: file delete -force test2.db test2.db-journal sl@0: sqlite3 db test2.db sl@0: sqlite3_extended_result_codes db 1 sl@0: db eval {CREATE TABLE t1(x);} sl@0: db close sl@0: } -tclbody { sl@0: if {[catch {sqlite3 db test.db}]} { sl@0: error "out of memory" sl@0: } sl@0: sqlite3_extended_result_codes db 1 sl@0: } -sqlbody { sl@0: ATTACH DATABASE 'test2.db' AS t2; sl@0: SELECT * FROM t1; sl@0: DETACH DATABASE t2; sl@0: } sl@0: } sl@0: sl@0: # Test malloc failure whilst installing a foreign key. sl@0: # sl@0: ifcapable foreignkey { sl@0: do_malloc_test 21 -sqlbody { sl@0: CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b)) sl@0: } sl@0: } sl@0: sl@0: # Test malloc failure in an sqlite3_prepare_v2() call. sl@0: # sl@0: do_malloc_test 22 -tclbody { sl@0: set ::STMT "" sl@0: set r [catch { sl@0: set ::STMT [ sl@0: sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY sl@0: ] sl@0: } msg] sl@0: if {$r} {error [string range $msg 4 end]} sl@0: } -cleanup { sl@0: if {$::STMT ne ""} { sl@0: sqlite3_finalize $::STMT sl@0: set ::STMT "" sl@0: } sl@0: } sl@0: sl@0: ifcapable {pager_pragmas} { sl@0: # This tests a special case - that an error that occurs while the pager sl@0: # is trying to recover from error-state in exclusive-access mode works. sl@0: # sl@0: do_malloc_test 23 -tclprep { sl@0: db eval { sl@0: PRAGMA cache_size = 10; sl@0: PRAGMA locking_mode = exclusive; sl@0: BEGIN; sl@0: CREATE TABLE abc(a, b, c); sl@0: CREATE INDEX abc_i ON abc(a, b, c); sl@0: INSERT INTO abc sl@0: VALUES(randstr(100,100), randstr(100,100), randstr(100,100)); sl@0: INSERT INTO abc sl@0: SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; sl@0: INSERT INTO abc sl@0: SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; sl@0: INSERT INTO abc sl@0: SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; sl@0: INSERT INTO abc sl@0: SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; sl@0: INSERT INTO abc sl@0: SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; sl@0: COMMIT; sl@0: } sl@0: sl@0: # This puts the pager into error state. sl@0: # sl@0: db eval BEGIN sl@0: db eval {UPDATE abc SET a = 0 WHERE oid%2} sl@0: set ::sqlite_io_error_pending 10 sl@0: catch {db eval {ROLLBACK}} msg sl@0: sl@0: } -tclbody { sl@0: # If an out-of-memory occurs within a call to a VFS layer function during sl@0: # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit sl@0: # [5668] for details. sl@0: set rc [catch {db eval { SELECT * FROM abc LIMIT 10 }} msg] sl@0: if {$msg eq "database disk image is malformed"} { set msg "out of memory" } sl@0: if {$rc} { error $msg } sl@0: set msg sl@0: } -cleanup { sl@0: set e [db eval {PRAGMA integrity_check}] sl@0: if {$e ne "ok"} {error $e} sl@0: } sl@0: } sl@0: sl@0: ifcapable compound { sl@0: do_malloc_test 24 -sqlprep { sl@0: CREATE TABLE t1(a, b, c) sl@0: } -sqlbody { sl@0: SELECT 1 FROM t1 UNION SELECT 2 FROM t1 ORDER BY 1 sl@0: } sl@0: } sl@0: sl@0: ifcapable view&&trigger { sl@0: do_malloc_test 25 -sqlprep { sl@0: CREATE TABLE t1(a, b, c); sl@0: CREATE VIEW v1 AS SELECT * FROM t1; sl@0: CREATE TRIGGER v1t1 INSTEAD OF DELETE ON v1 BEGIN SELECT 1; END; sl@0: CREATE TRIGGER v1t2 INSTEAD OF INSERT ON v1 BEGIN SELECT 1; END; sl@0: CREATE TRIGGER v1t3 INSTEAD OF UPDATE ON v1 BEGIN SELECT 1; END; sl@0: } -sqlbody { sl@0: DELETE FROM v1 WHERE a = 1; sl@0: INSERT INTO v1 VALUES(1, 2, 3); sl@0: UPDATE v1 SET a = 1 WHERE b = 2; sl@0: } sl@0: } sl@0: sl@0: do_malloc_test 25 -sqlprep { sl@0: CREATE TABLE abc(a, b, c); sl@0: CREATE INDEX i1 ON abc(a, b); sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: INSERT INTO abc VALUES(4, 5, 6); sl@0: } -tclbody { sl@0: # For each UPDATE executed, the cursor used for the SELECT statement sl@0: # must be "saved". Because the cursor is open on an index, this requires sl@0: # a malloc() to allocate space to save the index key. This test case is sl@0: # aimed at testing the response of the library to a failure in that sl@0: # particular malloc() call. sl@0: db eval {SELECT a FROM abc ORDER BY a} { sl@0: db eval {UPDATE abc SET b = b - 1 WHERE a = $a} sl@0: } sl@0: } sl@0: sl@0: # This test is designed to test a specific juncture in the sqlite code. sl@0: # The database set up by -sqlprep script contains a single table B-Tree sl@0: # of height 2. In the -tclbody script, the existing database connection sl@0: # is closed and a new one opened and used to insert a new row into the sl@0: # table B-Tree. By using a new connection, the outcome of a malloc() sl@0: # failure while seeking to the right-hand side of the B-Tree to insert sl@0: # a new record can be tested. sl@0: # sl@0: do_malloc_test 26 -sqlprep { sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b); sl@0: INSERT INTO t1 VALUES(1, randomblob(210)); sl@0: INSERT INTO t1 VALUES(1, randomblob(210)); sl@0: INSERT INTO t1 VALUES(1, randomblob(210)); sl@0: INSERT INTO t1 VALUES(1, randomblob(210)); sl@0: INSERT INTO t1 VALUES(1, randomblob(210)); sl@0: COMMIT; sl@0: } -tclbody { sl@0: db close sl@0: sqlite3 db test.db sl@0: db eval { INSERT INTO t1 VALUES(1, randomblob(210)) } sl@0: } sl@0: sl@0: # Test that no memory is leaked following a malloc() failure in sl@0: # sqlite3_initialize(). sl@0: # sl@0: do_malloc_test 27 -tclprep { sl@0: db close sl@0: sqlite3_shutdown sl@0: } -tclbody { sl@0: set rc [sqlite3_initialize] sl@0: if {$rc == "SQLITE_NOMEM"} { sl@0: error "out of memory" sl@0: } sl@0: } sl@0: sl@0: # Ensure that no file descriptors were leaked. sl@0: do_test malloc-99.X { sl@0: catch {db close} sl@0: set sqlite_open_file_count sl@0: } {0} sl@0: sl@0: puts open-file-count=$sqlite_open_file_count sl@0: finish_test