blob: a6b0f901eae14d0e4c933e24ceb60e5288b74ee5 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhed7c8552001-04-11 14:29:21 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drhed7c8552001-04-11 14:29:21 +00009#
10#***********************************************************************
11# This file attempts to check the library in an out-of-memory situation.
drhc89b91b2005-01-03 01:32:59 +000012# When compiled with -DSQLITE_DEBUG=1, the SQLite library accepts a special
drh6d4abfb2001-10-22 02:58:08 +000013# command (sqlite_malloc_fail N) which causes the N-th malloc to fail. This
drhed7c8552001-04-11 14:29:21 +000014# special feature is used to see what happens in the library if a malloc
15# were to really fail due to an out-of-memory situation.
16#
drh344a6272006-06-26 12:50:09 +000017# $Id: malloc.test,v 1.33 2006/06/26 12:50:09 drh Exp $
drhed7c8552001-04-11 14:29:21 +000018
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Only run these tests if memory debugging is turned on.
23#
drhb5f70c22004-02-14 01:39:50 +000024if {[info command sqlite_malloc_stat]==""} {
danielk1977261919c2005-12-06 12:52:59 +000025 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
drhed7c8552001-04-11 14:29:21 +000026 finish_test
27 return
28}
29
danielk19774397de52005-01-12 12:44:03 +000030# Usage: do_malloc_test <test number> <options...>
31#
32# The first argument, <test number>, is an integer used to name the
33# tests executed by this proc. Options are as follows:
34#
35# -tclprep TCL script to run to prepare test.
36# -sqlprep SQL script to run to prepare test.
danielk1977656152c2005-01-12 13:04:54 +000037# -tclbody TCL script to run with malloc failure simulation.
38# -sqlbody TCL script to run with malloc failure simulation.
danielk1977c08d4052005-01-13 13:35:57 +000039# -cleanup TCL script to run after the test.
danielk1977656152c2005-01-12 13:04:54 +000040#
41# This command runs a series of tests to verify SQLite's ability
42# to handle an out-of-memory condition gracefully. It is assumed
43# that if this condition occurs a malloc() call will return a
44# NULL pointer. Linux, for example, doesn't do that by default. See
45# the "BUGS" section of malloc(3).
46#
47# Each iteration of a loop, the TCL commands in any argument passed
48# to the -tclbody switch, followed by the SQL commands in any argument
49# passed to the -sqlbody switch are executed. Each iteration the
50# Nth call to sqliteMalloc() is made to fail, where N is increased
51# each time the loop runs starting from 1. When all commands execute
52# successfully, the loop ends.
danielk19774397de52005-01-12 12:44:03 +000053#
54proc do_malloc_test {tn args} {
danielk1977261919c2005-12-06 12:52:59 +000055 array unset ::mallocopts
danielk19774397de52005-01-12 12:44:03 +000056 array set ::mallocopts $args
57
58 set ::go 1
danielk1977261919c2005-12-06 12:52:59 +000059 for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
danielk19774397de52005-01-12 12:44:03 +000060 do_test malloc-$tn.$::n {
61
danielk1977261919c2005-12-06 12:52:59 +000062 # Remove all traces of database files test.db and test2.db from the files
63 # system. Then open (empty database) "test.db" with the handle [db].
64 #
danielk19774397de52005-01-12 12:44:03 +000065 sqlite_malloc_fail 0
danielk1977771151b2006-01-17 13:21:40 +000066 catch {db close}
danielk19774397de52005-01-12 12:44:03 +000067 catch {file delete -force test.db}
68 catch {file delete -force test.db-journal}
69 catch {file delete -force test2.db}
70 catch {file delete -force test2.db-journal}
danielk1977771151b2006-01-17 13:21:40 +000071 catch {sqlite3 db test.db}
72 set ::DB [sqlite3_connection_pointer db]
danielk19774397de52005-01-12 12:44:03 +000073
danielk1977261919c2005-12-06 12:52:59 +000074 # Execute any -tclprep and -sqlprep scripts.
75 #
danielk19774397de52005-01-12 12:44:03 +000076 if {[info exists ::mallocopts(-tclprep)]} {
77 eval $::mallocopts(-tclprep)
78 }
79 if {[info exists ::mallocopts(-sqlprep)]} {
80 execsql $::mallocopts(-sqlprep)
81 }
82
danielk1977261919c2005-12-06 12:52:59 +000083 # Now set the ${::n}th malloc() to fail and execute the -tclbody and
84 # -sqlbody scripts.
85 #
danielk19774397de52005-01-12 12:44:03 +000086 sqlite_malloc_fail $::n
87 set ::mallocbody {}
88 if {[info exists ::mallocopts(-tclbody)]} {
89 append ::mallocbody "$::mallocopts(-tclbody)\n"
90 }
91 if {[info exists ::mallocopts(-sqlbody)]} {
92 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
93 }
danielk19774397de52005-01-12 12:44:03 +000094 set v [catch $::mallocbody msg]
95
drha06ab2c2006-04-10 13:37:47 +000096 # If the test fails (if $v!=0) and the database connection actually
97 # exists, make sure the failure code is SQLITE_NOMEM.
98 if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)]
99 && [db errorcode]!=7} {
100 set v 999
101 }
102
danielk19774397de52005-01-12 12:44:03 +0000103 set leftover [lindex [sqlite_malloc_stat] 2]
104 if {$leftover>0} {
105 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
106 set ::go 0
danielk19779a30cf62006-01-18 04:26:07 +0000107 if {$v} {
108 puts "\nError message returned: $msg"
109 } else {
110 set v {1 1}
111 }
danielk19774397de52005-01-12 12:44:03 +0000112 } else {
113 set v2 [expr {$msg=="" || $msg=="out of memory"}]
114 if {!$v2} {puts "\nError message returned: $msg"}
115 lappend v $v2
116 }
117 } {1 1}
danielk1977c08d4052005-01-13 13:35:57 +0000118
119 if {[info exists ::mallocopts(-cleanup)]} {
danielk1977950f0542006-01-18 05:51:57 +0000120 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
danielk1977c08d4052005-01-13 13:35:57 +0000121 }
danielk19774397de52005-01-12 12:44:03 +0000122 }
danielk1977aca790a2005-01-13 11:07:52 +0000123 unset ::mallocopts
danielk19774397de52005-01-12 12:44:03 +0000124}
125
danielk1977c08d4052005-01-13 13:35:57 +0000126do_malloc_test 1 -tclprep {
127 db close
128} -tclbody {
129 if {[catch {sqlite3 db test.db}]} {
130 error "out of memory"
131 }
132} -sqlbody {
drh344a6272006-06-26 12:50:09 +0000133 DROP TABLE IF EXISTS t1;
danielk1977c08d4052005-01-13 13:35:57 +0000134 CREATE TABLE t1(
135 a int, b float, c double, d text, e varchar(20),
136 primary key(a,b,c)
137 );
138 CREATE INDEX i1 ON t1(a,b);
139 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
140 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
141 SELECT * FROM t1;
142 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
143 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
144 SELECT count(*) FROM t1;
145}
drhd4007282001-04-12 23:21:58 +0000146
danielk1977b5548a82004-06-26 13:51:33 +0000147# Ensure that no file descriptors were leaked.
148do_test malloc-1.X {
149 catch {db close}
150 set sqlite_open_file_count
151} {0}
152
danielk1977c08d4052005-01-13 13:35:57 +0000153do_malloc_test 2 -sqlbody {
drhfc233142005-08-19 01:07:15 +0000154 CREATE TABLE t1(a int, b int default 'abc', c int default 1);
danielk1977c08d4052005-01-13 13:35:57 +0000155 CREATE INDEX i1 ON t1(a,b);
156 INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
157 INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
158 INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
159 INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
160 INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
161 INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
162 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
163 UPDATE t1 SET b=b||b||b||b;
164 UPDATE t1 SET b=a WHERE a in (10,12,22);
165 INSERT INTO t1(c,b,a) VALUES(20,10,5);
166 INSERT INTO t1 SELECT * FROM t1
167 WHERE a IN (SELECT a FROM t1 WHERE a<10);
168 DELETE FROM t1 WHERE a>=10;
169 DROP INDEX i1;
170 DELETE FROM t1;
171}
drh6d4abfb2001-10-22 02:58:08 +0000172
danielk1977b5548a82004-06-26 13:51:33 +0000173# Ensure that no file descriptors were leaked.
174do_test malloc-2.X {
175 catch {db close}
176 set sqlite_open_file_count
177} {0}
178
danielk1977c08d4052005-01-13 13:35:57 +0000179do_malloc_test 3 -sqlbody {
180 BEGIN TRANSACTION;
181 CREATE TABLE t1(a int, b int, c int);
182 CREATE INDEX i1 ON t1(a,b);
183 INSERT INTO t1 VALUES(1,1,99);
184 INSERT INTO t1 VALUES(2,4,98);
185 INSERT INTO t1 VALUES(3,9,97);
186 INSERT INTO t1 VALUES(4,16,96);
187 INSERT INTO t1 VALUES(5,25,95);
188 INSERT INTO t1 VALUES(6,36,94);
189 INSERT INTO t1(c,b,a) VALUES(20,10,5);
190 DELETE FROM t1 WHERE a>=10;
191 DROP INDEX i1;
192 DELETE FROM t1;
193 ROLLBACK;
194}
195
danielk1977b5548a82004-06-26 13:51:33 +0000196
197# Ensure that no file descriptors were leaked.
198do_test malloc-3.X {
199 catch {db close}
200 set sqlite_open_file_count
201} {0}
202
danielk1977c08d4052005-01-13 13:35:57 +0000203do_malloc_test 4 -sqlbody {
204 BEGIN TRANSACTION;
205 CREATE TABLE t1(a int, b int, c int);
206 CREATE INDEX i1 ON t1(a,b);
207 INSERT INTO t1 VALUES(1,1,99);
208 INSERT INTO t1 VALUES(2,4,98);
209 INSERT INTO t1 VALUES(3,9,97);
210 INSERT INTO t1 VALUES(4,16,96);
211 INSERT INTO t1 VALUES(5,25,95);
212 INSERT INTO t1 VALUES(6,36,94);
213 UPDATE t1 SET b=a WHERE a in (10,12,22);
214 INSERT INTO t1 SELECT * FROM t1
215 WHERE a IN (SELECT a FROM t1 WHERE a<10);
216 DROP INDEX i1;
217 DELETE FROM t1;
218 COMMIT;
219}
danielk1977b5548a82004-06-26 13:51:33 +0000220
221# Ensure that no file descriptors were leaked.
222do_test malloc-4.X {
223 catch {db close}
224 set sqlite_open_file_count
225} {0}
226
danielk1977c08d4052005-01-13 13:35:57 +0000227do_malloc_test 5 -sqlbody {
228 BEGIN TRANSACTION;
229 CREATE TABLE t1(a,b);
230 CREATE TABLE t2(x,y);
231 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
232 INSERT INTO t2(x,y) VALUES(new.rowid,1);
233 END;
234 INSERT INTO t1(a,b) VALUES(2,3);
235 COMMIT;
236}
danielk1977b5548a82004-06-26 13:51:33 +0000237
238# Ensure that no file descriptors were leaked.
239do_test malloc-5.X {
240 catch {db close}
241 set sqlite_open_file_count
242} {0}
243
danielk1977c08d4052005-01-13 13:35:57 +0000244do_malloc_test 6 -sqlprep {
245 BEGIN TRANSACTION;
246 CREATE TABLE t1(a);
247 INSERT INTO t1 VALUES(1);
248 INSERT INTO t1 SELECT a*2 FROM t1;
249 INSERT INTO t1 SELECT a*2 FROM t1;
250 INSERT INTO t1 SELECT a*2 FROM t1;
251 INSERT INTO t1 SELECT a*2 FROM t1;
252 INSERT INTO t1 SELECT a*2 FROM t1;
253 INSERT INTO t1 SELECT a*2 FROM t1;
254 INSERT INTO t1 SELECT a*2 FROM t1;
255 INSERT INTO t1 SELECT a*2 FROM t1;
256 INSERT INTO t1 SELECT a*2 FROM t1;
257 INSERT INTO t1 SELECT a*2 FROM t1;
258 DELETE FROM t1 where rowid%5 = 0;
259 COMMIT;
260} -sqlbody {
261 VACUUM;
262}
danielk197796fb0dd2004-06-30 09:49:22 +0000263
danielk1977c08d4052005-01-13 13:35:57 +0000264do_malloc_test 7 -sqlprep {
265 CREATE TABLE t1(a, b);
266 INSERT INTO t1 VALUES(1, 2);
267 INSERT INTO t1 VALUES(3, 4);
268 INSERT INTO t1 VALUES(5, 6);
269 INSERT INTO t1 VALUES(7, randstr(1200,1200));
270} -sqlbody {
271 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
272 SELECT a FROM t1 WHERE a<6 ORDER BY a;
273 SELECT b FROM t1 WHERE a>6;
274}
danielk197701427a62005-01-11 13:02:33 +0000275
danielk1977b5402fb2005-01-12 07:15:04 +0000276# This block is designed to test that some malloc failures that may
277# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
278# when converting UTF-16 text to integers and real numbers is handled
279# correctly.
280#
danielk19778b60e0f2005-01-12 09:10:39 +0000281# This is done by retrieving a string from the database engine and
282# manipulating it using the sqlite3_column_*** APIs. This doesn't
283# actually return an error to the user when a malloc() fails.. That
284# could be viewed as a bug.
285#
286# These tests only run if UTF-16 support is compiled in.
danielk1977b5402fb2005-01-12 07:15:04 +0000287#
danielk1977c08d4052005-01-13 13:35:57 +0000288if {$::sqlite_options(utf16)} {
289 do_malloc_test 8 -tclprep {
290 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
291 set ::STMT [sqlite3_prepare $::DB $sql -1 X]
292 sqlite3_step $::STMT
293 if { $::tcl_platform(byteOrder)=="littleEndian" } {
294 set ::bomstr "\xFF\xFE"
295 } else {
296 set ::bomstr "\xFE\xFF"
297 }
298 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
299 } -tclbody {
300 sqlite3_column_text16 $::STMT 0
301 sqlite3_column_int $::STMT 0
302 sqlite3_column_text16 $::STMT 1
303 sqlite3_column_double $::STMT 1
304 sqlite3_reset $::STMT
305 sqlite3_bind_text16 $::STMT 1 $::bomstr 60
306 catch {sqlite3_finalize $::STMT}
307 if {[lindex [sqlite_malloc_stat] 2]<=0} {
308 error "out of memory"
309 }
310 } -cleanup {
311 sqlite3_finalize $::STMT
312 }
danielk1977b5402fb2005-01-12 07:15:04 +0000313}
314
danielk19778b60e0f2005-01-12 09:10:39 +0000315# This block tests that malloc() failures that occur whilst commiting
316# a multi-file transaction are handled correctly.
317#
danielk19774397de52005-01-12 12:44:03 +0000318do_malloc_test 9 -sqlprep {
319 ATTACH 'test2.db' as test2;
320 CREATE TABLE abc1(a, b, c);
321 CREATE TABLE test2.abc2(a, b, c);
322} -sqlbody {
323 BEGIN;
324 INSERT INTO abc1 VALUES(1, 2, 3);
325 INSERT INTO abc2 VALUES(1, 2, 3);
326 COMMIT;
danielk19778b60e0f2005-01-12 09:10:39 +0000327}
328
danielk19774397de52005-01-12 12:44:03 +0000329# This block tests malloc() failures that occur while opening a
330# connection to a database.
331do_malloc_test 10 -sqlprep {
332 CREATE TABLE abc(a, b, c);
333} -tclbody {
danielk1977771151b2006-01-17 13:21:40 +0000334 sqlite3 db2 test.db
danielk19774397de52005-01-12 12:44:03 +0000335 db2 eval {SELECT * FROM sqlite_master}
336 db2 close
337}
338
339# This block tests malloc() failures that occur within calls to
340# sqlite3_create_function().
341do_malloc_test 11 -tclbody {
danielk19772c336542005-01-13 02:14:23 +0000342 set rc [sqlite3_create_function $::DB]
343 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000344 error "out of memory"
345 }
346}
347
348do_malloc_test 12 -tclbody {
349 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
350 append sql16 "\00\00"
351 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]
352 sqlite3_finalize $::STMT
353}
danielk19778b60e0f2005-01-12 09:10:39 +0000354
danielk1977aca790a2005-01-13 11:07:52 +0000355# Test malloc errors when replaying two hot journals from a 2-file
drh66560ad2006-01-06 14:32:19 +0000356# transaction.
357ifcapable crashtest {
danielk1977aca790a2005-01-13 11:07:52 +0000358 do_malloc_test 13 -tclprep {
359 set rc [crashsql 1 test2.db {
360 ATTACH 'test2.db' as aux;
361 PRAGMA cache_size = 10;
362 BEGIN;
363 CREATE TABLE aux.t2(a, b, c);
364 CREATE TABLE t1(a, b, c);
365 COMMIT;
366 }]
367 if {$rc!="1 {child process exited abnormally}"} {
368 error "Wrong error message: $rc"
369 }
danielk1977950f0542006-01-18 05:51:57 +0000370 } -tclbody {
371 db eval {ATTACH 'test2.db' as aux;}
372 set rc [catch {db eval {
373 SELECT * FROM t1;
374 SELECT * FROM t2;
375 }} err]
376 if {$rc && $err!="no such table: t1"} {
377 error $err
378 }
danielk1977aca790a2005-01-13 11:07:52 +0000379 }
380}
381
danielk197776b047d2005-01-19 03:52:54 +0000382if {$tcl_platform(platform)!="windows"} {
danielk19779a30cf62006-01-18 04:26:07 +0000383 do_malloc_test 14 -tclprep {
384 catch {db close}
385 sqlite3 db2 test2.db
386 db2 eval {
387 PRAGMA synchronous = 0;
388 CREATE TABLE t1(a, b);
389 INSERT INTO t1 VALUES(1, 2);
390 BEGIN;
391 INSERT INTO t1 VALUES(3, 4);
392 }
393 copy_file test2.db test.db
394 copy_file test2.db-journal test.db-journal
395 db2 close
396 } -tclbody {
397 sqlite3 db test.db
398 db eval {
399 SELECT * FROM t1;
400 }
danielk1977aca790a2005-01-13 11:07:52 +0000401 }
danielk1977aca790a2005-01-13 11:07:52 +0000402}
danielk19779a30cf62006-01-18 04:26:07 +0000403
404proc string_compare {a b} {
405 return [string compare $a $b]
406}
407
408# Test for malloc() failures in sqlite3_create_collation() and
409# sqlite3_create_collation16().
danielk1977950f0542006-01-18 05:51:57 +0000410#
danielk19779a30cf62006-01-18 04:26:07 +0000411do_malloc_test 15 -tclbody {
412 db collate string_compare string_compare
413 if {[catch {add_test_collate $::DB 1 1 1} msg]} {
414 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
415 error $msg
416 }
danielk1977950f0542006-01-18 05:51:57 +0000417
418 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
419 db complete {-- Useful comment}
420
danielk19779a30cf62006-01-18 04:26:07 +0000421 execsql {
422 CREATE TABLE t1(a, b COLLATE string_compare);
423 INSERT INTO t1 VALUES(10, 'string');
424 INSERT INTO t1 VALUES(10, 'string2');
425 }
danielk197776b047d2005-01-19 03:52:54 +0000426}
danielk1977aca790a2005-01-13 11:07:52 +0000427
danielk1977950f0542006-01-18 05:51:57 +0000428# Also test sqlite3_complete(). There are (currently) no malloc()
429# calls in this function, but test anyway against future changes.
430#
431do_malloc_test 16 -tclbody {
432 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
433 db complete {-- Useful comment}
434 db eval {
435 SELECT * FROM sqlite_master;
436 }
437}
438
439# Test handling of malloc() failures in sqlite3_open16().
440#
441do_malloc_test 17 -tclbody {
442 set DB2 0
443 set STMT 0
444
445 # open database using sqlite3_open16()
446 set DB2 [sqlite3_open16 test.db -unused]
447 if {0==$DB2} {
448 error "out of memory"
449 }
450
451 # Prepare statement
452 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
453 if {$rc} {
454 error [string range $msg 4 end]
455 }
456 set STMT $msg
457
458 # Finalize statement
459 set rc [sqlite3_finalize $STMT]
460 if {$rc!="SQLITE_OK"} {
461 error [sqlite3_errmsg $DB2]
462 }
463 set STMT 0
464
465 # Close database
466 set rc [sqlite3_close $DB2]
467 if {$rc!="SQLITE_OK"} {
468 error [sqlite3_errmsg $DB2]
469 }
470 set DB2 0
471} -cleanup {
472 if {$STMT!="0"} {
473 sqlite3_finalize $STMT
474 }
475 if {$DB2!="0"} {
476 set rc [sqlite3_close $DB2]
477 }
478}
479
480# Test handling of malloc() failures in sqlite3_errmsg16().
481#
482do_malloc_test 18 -tclbody {
483 catch {
484 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
485 } msg
486 if {$msg=="out of memory"} {error $msg}
487 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
488 binary scan $utf16 c* bytes
489 if {[llength $bytes]==0} {
490 error "out of memory"
491 }
492}
493
danielk1977161fb792006-01-24 10:58:21 +0000494# This test is aimed at coverage testing. Specificly, it is supposed to
495# cause a malloc() only used when converting between the two utf-16
496# encodings to fail (i.e. little-endian->big-endian). It only actually
497# hits this malloc() on little-endian hosts.
498#
499set static_string "\x00h\x00e\x00l\x00l\x00o"
500for {set l 0} {$l<10} {incr l} {
501 append static_string $static_string
502}
503append static_string "\x00\x00"
504do_malloc_test 19 -tclprep {
505 execsql {
506 PRAGMA encoding = "UTF16be";
507 CREATE TABLE abc(a, b, c);
508 }
509} -tclbody {
510 unset -nocomplain ::STMT
511 set r [catch {
512 set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY]
513 sqlite3_bind_text16 -static $::STMT 1 $static_string 112
514 } msg]
515 if {$r} {error [string range $msg 4 end]}
516 set msg
517} -cleanup {
518 if {[info exists ::STMT]} {
519 sqlite3_finalize $::STMT
520 }
521}
522unset static_string
523
drh6103fe92006-04-05 11:57:37 +0000524# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
525# when the malloc failure occurs within the nested parse.
526#
527do_malloc_test 20 -tclprep {
528 db close
529 file delete -force test2.db test2.db-journal
530 sqlite3 db test2.db
531 db eval {CREATE TABLE t1(x);}
532 db close
533} -tclbody {
534 if {[catch {sqlite3 db test.db}]} {
535 error "out of memory"
536 }
537} -sqlbody {
538 ATTACH DATABASE 'test2.db' AS t2;
539 SELECT * FROM t1;
540 DETACH DATABASE t2;
541}
542
543
danielk197796fb0dd2004-06-30 09:49:22 +0000544# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000545do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000546 catch {db close}
547 set sqlite_open_file_count
548} {0}
549
drh344a6272006-06-26 12:50:09 +0000550puts open-file-count=$sqlite_open_file_count
drhed7c8552001-04-11 14:29:21 +0000551sqlite_malloc_fail 0
552finish_test