blob: 2067b5b021aa98e53a8abf6adc6f79a217ae2bf0 [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#
drh6103fe92006-04-05 11:57:37 +000017# $Id: malloc.test,v 1.31 2006/04/05 11:57:37 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
96 set leftover [lindex [sqlite_malloc_stat] 2]
97 if {$leftover>0} {
98 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
99 set ::go 0
danielk19779a30cf62006-01-18 04:26:07 +0000100 if {$v} {
101 puts "\nError message returned: $msg"
102 } else {
103 set v {1 1}
104 }
danielk19774397de52005-01-12 12:44:03 +0000105 } else {
106 set v2 [expr {$msg=="" || $msg=="out of memory"}]
107 if {!$v2} {puts "\nError message returned: $msg"}
108 lappend v $v2
109 }
110 } {1 1}
danielk1977c08d4052005-01-13 13:35:57 +0000111
112 if {[info exists ::mallocopts(-cleanup)]} {
danielk1977950f0542006-01-18 05:51:57 +0000113 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
danielk1977c08d4052005-01-13 13:35:57 +0000114 }
danielk19774397de52005-01-12 12:44:03 +0000115 }
danielk1977aca790a2005-01-13 11:07:52 +0000116 unset ::mallocopts
danielk19774397de52005-01-12 12:44:03 +0000117}
118
danielk1977c08d4052005-01-13 13:35:57 +0000119do_malloc_test 1 -tclprep {
120 db close
121} -tclbody {
122 if {[catch {sqlite3 db test.db}]} {
123 error "out of memory"
124 }
125} -sqlbody {
126 CREATE TABLE t1(
127 a int, b float, c double, d text, e varchar(20),
128 primary key(a,b,c)
129 );
130 CREATE INDEX i1 ON t1(a,b);
131 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
132 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
133 SELECT * FROM t1;
134 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
135 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
136 SELECT count(*) FROM t1;
137}
drhd4007282001-04-12 23:21:58 +0000138
danielk1977b5548a82004-06-26 13:51:33 +0000139# Ensure that no file descriptors were leaked.
140do_test malloc-1.X {
141 catch {db close}
142 set sqlite_open_file_count
143} {0}
144
danielk1977c08d4052005-01-13 13:35:57 +0000145do_malloc_test 2 -sqlbody {
drhfc233142005-08-19 01:07:15 +0000146 CREATE TABLE t1(a int, b int default 'abc', c int default 1);
danielk1977c08d4052005-01-13 13:35:57 +0000147 CREATE INDEX i1 ON t1(a,b);
148 INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
149 INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
150 INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
151 INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
152 INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
153 INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
154 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
155 UPDATE t1 SET b=b||b||b||b;
156 UPDATE t1 SET b=a WHERE a in (10,12,22);
157 INSERT INTO t1(c,b,a) VALUES(20,10,5);
158 INSERT INTO t1 SELECT * FROM t1
159 WHERE a IN (SELECT a FROM t1 WHERE a<10);
160 DELETE FROM t1 WHERE a>=10;
161 DROP INDEX i1;
162 DELETE FROM t1;
163}
drh6d4abfb2001-10-22 02:58:08 +0000164
danielk1977b5548a82004-06-26 13:51:33 +0000165# Ensure that no file descriptors were leaked.
166do_test malloc-2.X {
167 catch {db close}
168 set sqlite_open_file_count
169} {0}
170
danielk1977c08d4052005-01-13 13:35:57 +0000171do_malloc_test 3 -sqlbody {
172 BEGIN TRANSACTION;
173 CREATE TABLE t1(a int, b int, c int);
174 CREATE INDEX i1 ON t1(a,b);
175 INSERT INTO t1 VALUES(1,1,99);
176 INSERT INTO t1 VALUES(2,4,98);
177 INSERT INTO t1 VALUES(3,9,97);
178 INSERT INTO t1 VALUES(4,16,96);
179 INSERT INTO t1 VALUES(5,25,95);
180 INSERT INTO t1 VALUES(6,36,94);
181 INSERT INTO t1(c,b,a) VALUES(20,10,5);
182 DELETE FROM t1 WHERE a>=10;
183 DROP INDEX i1;
184 DELETE FROM t1;
185 ROLLBACK;
186}
187
danielk1977b5548a82004-06-26 13:51:33 +0000188
189# Ensure that no file descriptors were leaked.
190do_test malloc-3.X {
191 catch {db close}
192 set sqlite_open_file_count
193} {0}
194
danielk1977c08d4052005-01-13 13:35:57 +0000195do_malloc_test 4 -sqlbody {
196 BEGIN TRANSACTION;
197 CREATE TABLE t1(a int, b int, c int);
198 CREATE INDEX i1 ON t1(a,b);
199 INSERT INTO t1 VALUES(1,1,99);
200 INSERT INTO t1 VALUES(2,4,98);
201 INSERT INTO t1 VALUES(3,9,97);
202 INSERT INTO t1 VALUES(4,16,96);
203 INSERT INTO t1 VALUES(5,25,95);
204 INSERT INTO t1 VALUES(6,36,94);
205 UPDATE t1 SET b=a WHERE a in (10,12,22);
206 INSERT INTO t1 SELECT * FROM t1
207 WHERE a IN (SELECT a FROM t1 WHERE a<10);
208 DROP INDEX i1;
209 DELETE FROM t1;
210 COMMIT;
211}
danielk1977b5548a82004-06-26 13:51:33 +0000212
213# Ensure that no file descriptors were leaked.
214do_test malloc-4.X {
215 catch {db close}
216 set sqlite_open_file_count
217} {0}
218
danielk1977c08d4052005-01-13 13:35:57 +0000219do_malloc_test 5 -sqlbody {
220 BEGIN TRANSACTION;
221 CREATE TABLE t1(a,b);
222 CREATE TABLE t2(x,y);
223 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
224 INSERT INTO t2(x,y) VALUES(new.rowid,1);
225 END;
226 INSERT INTO t1(a,b) VALUES(2,3);
227 COMMIT;
228}
danielk1977b5548a82004-06-26 13:51:33 +0000229
230# Ensure that no file descriptors were leaked.
231do_test malloc-5.X {
232 catch {db close}
233 set sqlite_open_file_count
234} {0}
235
danielk1977c08d4052005-01-13 13:35:57 +0000236do_malloc_test 6 -sqlprep {
237 BEGIN TRANSACTION;
238 CREATE TABLE t1(a);
239 INSERT INTO t1 VALUES(1);
240 INSERT INTO t1 SELECT a*2 FROM t1;
241 INSERT INTO t1 SELECT a*2 FROM t1;
242 INSERT INTO t1 SELECT a*2 FROM t1;
243 INSERT INTO t1 SELECT a*2 FROM t1;
244 INSERT INTO t1 SELECT a*2 FROM t1;
245 INSERT INTO t1 SELECT a*2 FROM t1;
246 INSERT INTO t1 SELECT a*2 FROM t1;
247 INSERT INTO t1 SELECT a*2 FROM t1;
248 INSERT INTO t1 SELECT a*2 FROM t1;
249 INSERT INTO t1 SELECT a*2 FROM t1;
250 DELETE FROM t1 where rowid%5 = 0;
251 COMMIT;
252} -sqlbody {
253 VACUUM;
254}
danielk197796fb0dd2004-06-30 09:49:22 +0000255
danielk1977c08d4052005-01-13 13:35:57 +0000256do_malloc_test 7 -sqlprep {
257 CREATE TABLE t1(a, b);
258 INSERT INTO t1 VALUES(1, 2);
259 INSERT INTO t1 VALUES(3, 4);
260 INSERT INTO t1 VALUES(5, 6);
261 INSERT INTO t1 VALUES(7, randstr(1200,1200));
262} -sqlbody {
263 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
264 SELECT a FROM t1 WHERE a<6 ORDER BY a;
265 SELECT b FROM t1 WHERE a>6;
266}
danielk197701427a62005-01-11 13:02:33 +0000267
danielk1977b5402fb2005-01-12 07:15:04 +0000268# This block is designed to test that some malloc failures that may
269# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
270# when converting UTF-16 text to integers and real numbers is handled
271# correctly.
272#
danielk19778b60e0f2005-01-12 09:10:39 +0000273# This is done by retrieving a string from the database engine and
274# manipulating it using the sqlite3_column_*** APIs. This doesn't
275# actually return an error to the user when a malloc() fails.. That
276# could be viewed as a bug.
277#
278# These tests only run if UTF-16 support is compiled in.
danielk1977b5402fb2005-01-12 07:15:04 +0000279#
danielk1977c08d4052005-01-13 13:35:57 +0000280if {$::sqlite_options(utf16)} {
281 do_malloc_test 8 -tclprep {
282 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
283 set ::STMT [sqlite3_prepare $::DB $sql -1 X]
284 sqlite3_step $::STMT
285 if { $::tcl_platform(byteOrder)=="littleEndian" } {
286 set ::bomstr "\xFF\xFE"
287 } else {
288 set ::bomstr "\xFE\xFF"
289 }
290 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
291 } -tclbody {
292 sqlite3_column_text16 $::STMT 0
293 sqlite3_column_int $::STMT 0
294 sqlite3_column_text16 $::STMT 1
295 sqlite3_column_double $::STMT 1
296 sqlite3_reset $::STMT
297 sqlite3_bind_text16 $::STMT 1 $::bomstr 60
298 catch {sqlite3_finalize $::STMT}
299 if {[lindex [sqlite_malloc_stat] 2]<=0} {
300 error "out of memory"
301 }
302 } -cleanup {
303 sqlite3_finalize $::STMT
304 }
danielk1977b5402fb2005-01-12 07:15:04 +0000305}
306
danielk19778b60e0f2005-01-12 09:10:39 +0000307# This block tests that malloc() failures that occur whilst commiting
308# a multi-file transaction are handled correctly.
309#
danielk19774397de52005-01-12 12:44:03 +0000310do_malloc_test 9 -sqlprep {
311 ATTACH 'test2.db' as test2;
312 CREATE TABLE abc1(a, b, c);
313 CREATE TABLE test2.abc2(a, b, c);
314} -sqlbody {
315 BEGIN;
316 INSERT INTO abc1 VALUES(1, 2, 3);
317 INSERT INTO abc2 VALUES(1, 2, 3);
318 COMMIT;
danielk19778b60e0f2005-01-12 09:10:39 +0000319}
320
danielk19774397de52005-01-12 12:44:03 +0000321# This block tests malloc() failures that occur while opening a
322# connection to a database.
323do_malloc_test 10 -sqlprep {
324 CREATE TABLE abc(a, b, c);
325} -tclbody {
danielk1977771151b2006-01-17 13:21:40 +0000326 sqlite3 db2 test.db
danielk19774397de52005-01-12 12:44:03 +0000327 db2 eval {SELECT * FROM sqlite_master}
328 db2 close
329}
330
331# This block tests malloc() failures that occur within calls to
332# sqlite3_create_function().
333do_malloc_test 11 -tclbody {
danielk19772c336542005-01-13 02:14:23 +0000334 set rc [sqlite3_create_function $::DB]
335 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000336 error "out of memory"
337 }
338}
339
340do_malloc_test 12 -tclbody {
341 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
342 append sql16 "\00\00"
343 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]
344 sqlite3_finalize $::STMT
345}
danielk19778b60e0f2005-01-12 09:10:39 +0000346
danielk1977aca790a2005-01-13 11:07:52 +0000347# Test malloc errors when replaying two hot journals from a 2-file
drh66560ad2006-01-06 14:32:19 +0000348# transaction.
349ifcapable crashtest {
danielk1977aca790a2005-01-13 11:07:52 +0000350 do_malloc_test 13 -tclprep {
351 set rc [crashsql 1 test2.db {
352 ATTACH 'test2.db' as aux;
353 PRAGMA cache_size = 10;
354 BEGIN;
355 CREATE TABLE aux.t2(a, b, c);
356 CREATE TABLE t1(a, b, c);
357 COMMIT;
358 }]
359 if {$rc!="1 {child process exited abnormally}"} {
360 error "Wrong error message: $rc"
361 }
danielk1977950f0542006-01-18 05:51:57 +0000362 } -tclbody {
363 db eval {ATTACH 'test2.db' as aux;}
364 set rc [catch {db eval {
365 SELECT * FROM t1;
366 SELECT * FROM t2;
367 }} err]
368 if {$rc && $err!="no such table: t1"} {
369 error $err
370 }
danielk1977aca790a2005-01-13 11:07:52 +0000371 }
372}
373
danielk197776b047d2005-01-19 03:52:54 +0000374if {$tcl_platform(platform)!="windows"} {
danielk19779a30cf62006-01-18 04:26:07 +0000375 do_malloc_test 14 -tclprep {
376 catch {db close}
377 sqlite3 db2 test2.db
378 db2 eval {
379 PRAGMA synchronous = 0;
380 CREATE TABLE t1(a, b);
381 INSERT INTO t1 VALUES(1, 2);
382 BEGIN;
383 INSERT INTO t1 VALUES(3, 4);
384 }
385 copy_file test2.db test.db
386 copy_file test2.db-journal test.db-journal
387 db2 close
388 } -tclbody {
389 sqlite3 db test.db
390 db eval {
391 SELECT * FROM t1;
392 }
danielk1977aca790a2005-01-13 11:07:52 +0000393 }
danielk1977aca790a2005-01-13 11:07:52 +0000394}
danielk19779a30cf62006-01-18 04:26:07 +0000395
396proc string_compare {a b} {
397 return [string compare $a $b]
398}
399
400# Test for malloc() failures in sqlite3_create_collation() and
401# sqlite3_create_collation16().
danielk1977950f0542006-01-18 05:51:57 +0000402#
danielk19779a30cf62006-01-18 04:26:07 +0000403do_malloc_test 15 -tclbody {
404 db collate string_compare string_compare
405 if {[catch {add_test_collate $::DB 1 1 1} msg]} {
406 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
407 error $msg
408 }
danielk1977950f0542006-01-18 05:51:57 +0000409
410 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
411 db complete {-- Useful comment}
412
danielk19779a30cf62006-01-18 04:26:07 +0000413 execsql {
414 CREATE TABLE t1(a, b COLLATE string_compare);
415 INSERT INTO t1 VALUES(10, 'string');
416 INSERT INTO t1 VALUES(10, 'string2');
417 }
danielk197776b047d2005-01-19 03:52:54 +0000418}
danielk1977aca790a2005-01-13 11:07:52 +0000419
danielk1977950f0542006-01-18 05:51:57 +0000420# Also test sqlite3_complete(). There are (currently) no malloc()
421# calls in this function, but test anyway against future changes.
422#
423do_malloc_test 16 -tclbody {
424 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
425 db complete {-- Useful comment}
426 db eval {
427 SELECT * FROM sqlite_master;
428 }
429}
430
431# Test handling of malloc() failures in sqlite3_open16().
432#
433do_malloc_test 17 -tclbody {
434 set DB2 0
435 set STMT 0
436
437 # open database using sqlite3_open16()
438 set DB2 [sqlite3_open16 test.db -unused]
439 if {0==$DB2} {
440 error "out of memory"
441 }
442
443 # Prepare statement
444 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
445 if {$rc} {
446 error [string range $msg 4 end]
447 }
448 set STMT $msg
449
450 # Finalize statement
451 set rc [sqlite3_finalize $STMT]
452 if {$rc!="SQLITE_OK"} {
453 error [sqlite3_errmsg $DB2]
454 }
455 set STMT 0
456
457 # Close database
458 set rc [sqlite3_close $DB2]
459 if {$rc!="SQLITE_OK"} {
460 error [sqlite3_errmsg $DB2]
461 }
462 set DB2 0
463} -cleanup {
464 if {$STMT!="0"} {
465 sqlite3_finalize $STMT
466 }
467 if {$DB2!="0"} {
468 set rc [sqlite3_close $DB2]
469 }
470}
471
472# Test handling of malloc() failures in sqlite3_errmsg16().
473#
474do_malloc_test 18 -tclbody {
475 catch {
476 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
477 } msg
478 if {$msg=="out of memory"} {error $msg}
479 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
480 binary scan $utf16 c* bytes
481 if {[llength $bytes]==0} {
482 error "out of memory"
483 }
484}
485
danielk1977161fb792006-01-24 10:58:21 +0000486# This test is aimed at coverage testing. Specificly, it is supposed to
487# cause a malloc() only used when converting between the two utf-16
488# encodings to fail (i.e. little-endian->big-endian). It only actually
489# hits this malloc() on little-endian hosts.
490#
491set static_string "\x00h\x00e\x00l\x00l\x00o"
492for {set l 0} {$l<10} {incr l} {
493 append static_string $static_string
494}
495append static_string "\x00\x00"
496do_malloc_test 19 -tclprep {
497 execsql {
498 PRAGMA encoding = "UTF16be";
499 CREATE TABLE abc(a, b, c);
500 }
501} -tclbody {
502 unset -nocomplain ::STMT
503 set r [catch {
504 set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY]
505 sqlite3_bind_text16 -static $::STMT 1 $static_string 112
506 } msg]
507 if {$r} {error [string range $msg 4 end]}
508 set msg
509} -cleanup {
510 if {[info exists ::STMT]} {
511 sqlite3_finalize $::STMT
512 }
513}
514unset static_string
515
drh6103fe92006-04-05 11:57:37 +0000516# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
517# when the malloc failure occurs within the nested parse.
518#
519do_malloc_test 20 -tclprep {
520 db close
521 file delete -force test2.db test2.db-journal
522 sqlite3 db test2.db
523 db eval {CREATE TABLE t1(x);}
524 db close
525} -tclbody {
526 if {[catch {sqlite3 db test.db}]} {
527 error "out of memory"
528 }
529} -sqlbody {
530 ATTACH DATABASE 'test2.db' AS t2;
531 SELECT * FROM t1;
532 DETACH DATABASE t2;
533}
534
535
danielk197796fb0dd2004-06-30 09:49:22 +0000536# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000537do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000538 catch {db close}
539 set sqlite_open_file_count
540} {0}
541
drhed7c8552001-04-11 14:29:21 +0000542sqlite_malloc_fail 0
543finish_test