blob: eac5548e2091a2200587ca8ab43a85a8ffc3a928 [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#
danielk19777e29e952007-04-19 11:09:01 +000017# $Id: malloc.test,v 1.41 2007/04/19 11:09:02 danielk1977 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);
drh76f80792006-07-11 12:40:25 +0000139 INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500');
danielk1977c08d4052005-01-13 13:35:57 +0000140 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
drh28f45912006-10-18 23:26:38 +0000232 INSERT INTO t2(x,y) VALUES(new.rowid,1);
233 UPDATE t2 SET y=y+1 WHERE x=new.rowid;
234 SELECT 123;
235 DELETE FROM t2 WHERE x=new.rowid;
danielk1977c08d4052005-01-13 13:35:57 +0000236 END;
237 INSERT INTO t1(a,b) VALUES(2,3);
238 COMMIT;
239}
danielk1977b5548a82004-06-26 13:51:33 +0000240
241# Ensure that no file descriptors were leaked.
242do_test malloc-5.X {
243 catch {db close}
244 set sqlite_open_file_count
245} {0}
246
danielk1977c08d4052005-01-13 13:35:57 +0000247do_malloc_test 6 -sqlprep {
248 BEGIN TRANSACTION;
249 CREATE TABLE t1(a);
250 INSERT INTO t1 VALUES(1);
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 INSERT INTO t1 SELECT a*2 FROM t1;
259 INSERT INTO t1 SELECT a*2 FROM t1;
260 INSERT INTO t1 SELECT a*2 FROM t1;
261 DELETE FROM t1 where rowid%5 = 0;
262 COMMIT;
263} -sqlbody {
264 VACUUM;
265}
danielk197796fb0dd2004-06-30 09:49:22 +0000266
danielk1977c08d4052005-01-13 13:35:57 +0000267do_malloc_test 7 -sqlprep {
268 CREATE TABLE t1(a, b);
269 INSERT INTO t1 VALUES(1, 2);
270 INSERT INTO t1 VALUES(3, 4);
271 INSERT INTO t1 VALUES(5, 6);
272 INSERT INTO t1 VALUES(7, randstr(1200,1200));
273} -sqlbody {
274 SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
275 SELECT a FROM t1 WHERE a<6 ORDER BY a;
276 SELECT b FROM t1 WHERE a>6;
277}
danielk197701427a62005-01-11 13:02:33 +0000278
danielk1977b5402fb2005-01-12 07:15:04 +0000279# This block is designed to test that some malloc failures that may
280# occur in vdbeapi.c. Specifically, if a malloc failure that occurs
281# when converting UTF-16 text to integers and real numbers is handled
282# correctly.
283#
danielk19778b60e0f2005-01-12 09:10:39 +0000284# This is done by retrieving a string from the database engine and
285# manipulating it using the sqlite3_column_*** APIs. This doesn't
286# actually return an error to the user when a malloc() fails.. That
287# could be viewed as a bug.
288#
289# These tests only run if UTF-16 support is compiled in.
danielk1977b5402fb2005-01-12 07:15:04 +0000290#
danielk1977c08d4052005-01-13 13:35:57 +0000291if {$::sqlite_options(utf16)} {
292 do_malloc_test 8 -tclprep {
293 set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
294 set ::STMT [sqlite3_prepare $::DB $sql -1 X]
295 sqlite3_step $::STMT
296 if { $::tcl_platform(byteOrder)=="littleEndian" } {
297 set ::bomstr "\xFF\xFE"
298 } else {
299 set ::bomstr "\xFE\xFF"
300 }
301 append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]
302 } -tclbody {
303 sqlite3_column_text16 $::STMT 0
304 sqlite3_column_int $::STMT 0
305 sqlite3_column_text16 $::STMT 1
306 sqlite3_column_double $::STMT 1
307 sqlite3_reset $::STMT
308 sqlite3_bind_text16 $::STMT 1 $::bomstr 60
309 catch {sqlite3_finalize $::STMT}
310 if {[lindex [sqlite_malloc_stat] 2]<=0} {
311 error "out of memory"
312 }
313 } -cleanup {
314 sqlite3_finalize $::STMT
315 }
danielk1977b5402fb2005-01-12 07:15:04 +0000316}
317
danielk19778b60e0f2005-01-12 09:10:39 +0000318# This block tests that malloc() failures that occur whilst commiting
319# a multi-file transaction are handled correctly.
320#
danielk19774397de52005-01-12 12:44:03 +0000321do_malloc_test 9 -sqlprep {
322 ATTACH 'test2.db' as test2;
323 CREATE TABLE abc1(a, b, c);
324 CREATE TABLE test2.abc2(a, b, c);
325} -sqlbody {
326 BEGIN;
327 INSERT INTO abc1 VALUES(1, 2, 3);
328 INSERT INTO abc2 VALUES(1, 2, 3);
329 COMMIT;
danielk19778b60e0f2005-01-12 09:10:39 +0000330}
331
danielk19774397de52005-01-12 12:44:03 +0000332# This block tests malloc() failures that occur while opening a
333# connection to a database.
334do_malloc_test 10 -sqlprep {
335 CREATE TABLE abc(a, b, c);
336} -tclbody {
danielk1977c5859712007-03-26 12:26:27 +0000337 db close
danielk1977771151b2006-01-17 13:21:40 +0000338 sqlite3 db2 test.db
danielk19774397de52005-01-12 12:44:03 +0000339 db2 eval {SELECT * FROM sqlite_master}
340 db2 close
341}
342
343# This block tests malloc() failures that occur within calls to
344# sqlite3_create_function().
345do_malloc_test 11 -tclbody {
danielk19772c336542005-01-13 02:14:23 +0000346 set rc [sqlite3_create_function $::DB]
347 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000348 error "out of memory"
349 }
350}
351
352do_malloc_test 12 -tclbody {
353 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
354 append sql16 "\00\00"
355 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]
356 sqlite3_finalize $::STMT
danielk1977c5859712007-03-26 12:26:27 +0000357}
danielk19778b60e0f2005-01-12 09:10:39 +0000358
danielk1977aca790a2005-01-13 11:07:52 +0000359# Test malloc errors when replaying two hot journals from a 2-file
drh66560ad2006-01-06 14:32:19 +0000360# transaction.
361ifcapable crashtest {
danielk1977aca790a2005-01-13 11:07:52 +0000362 do_malloc_test 13 -tclprep {
danielk197759a33f92007-03-17 10:26:59 +0000363 set rc [crashsql -delay 1 -file test2.db {
danielk1977aca790a2005-01-13 11:07:52 +0000364 ATTACH 'test2.db' as aux;
365 PRAGMA cache_size = 10;
366 BEGIN;
367 CREATE TABLE aux.t2(a, b, c);
368 CREATE TABLE t1(a, b, c);
369 COMMIT;
370 }]
371 if {$rc!="1 {child process exited abnormally}"} {
372 error "Wrong error message: $rc"
373 }
danielk1977950f0542006-01-18 05:51:57 +0000374 } -tclbody {
375 db eval {ATTACH 'test2.db' as aux;}
376 set rc [catch {db eval {
377 SELECT * FROM t1;
378 SELECT * FROM t2;
379 }} err]
380 if {$rc && $err!="no such table: t1"} {
381 error $err
382 }
danielk1977aca790a2005-01-13 11:07:52 +0000383 }
384}
385
danielk197776b047d2005-01-19 03:52:54 +0000386if {$tcl_platform(platform)!="windows"} {
danielk19779a30cf62006-01-18 04:26:07 +0000387 do_malloc_test 14 -tclprep {
388 catch {db close}
389 sqlite3 db2 test2.db
390 db2 eval {
391 PRAGMA synchronous = 0;
392 CREATE TABLE t1(a, b);
393 INSERT INTO t1 VALUES(1, 2);
394 BEGIN;
395 INSERT INTO t1 VALUES(3, 4);
396 }
397 copy_file test2.db test.db
398 copy_file test2.db-journal test.db-journal
399 db2 close
400 } -tclbody {
401 sqlite3 db test.db
402 db eval {
403 SELECT * FROM t1;
404 }
danielk1977aca790a2005-01-13 11:07:52 +0000405 }
danielk1977aca790a2005-01-13 11:07:52 +0000406}
danielk19779a30cf62006-01-18 04:26:07 +0000407
408proc string_compare {a b} {
409 return [string compare $a $b]
410}
411
412# Test for malloc() failures in sqlite3_create_collation() and
413# sqlite3_create_collation16().
danielk1977950f0542006-01-18 05:51:57 +0000414#
danielk19779a30cf62006-01-18 04:26:07 +0000415do_malloc_test 15 -tclbody {
416 db collate string_compare string_compare
417 if {[catch {add_test_collate $::DB 1 1 1} msg]} {
418 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
419 error $msg
420 }
danielk1977950f0542006-01-18 05:51:57 +0000421
422 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
423 db complete {-- Useful comment}
424
danielk19779a30cf62006-01-18 04:26:07 +0000425 execsql {
426 CREATE TABLE t1(a, b COLLATE string_compare);
427 INSERT INTO t1 VALUES(10, 'string');
428 INSERT INTO t1 VALUES(10, 'string2');
429 }
danielk197776b047d2005-01-19 03:52:54 +0000430}
danielk1977aca790a2005-01-13 11:07:52 +0000431
danielk1977950f0542006-01-18 05:51:57 +0000432# Also test sqlite3_complete(). There are (currently) no malloc()
433# calls in this function, but test anyway against future changes.
434#
435do_malloc_test 16 -tclbody {
436 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
437 db complete {-- Useful comment}
438 db eval {
439 SELECT * FROM sqlite_master;
440 }
441}
442
443# Test handling of malloc() failures in sqlite3_open16().
444#
445do_malloc_test 17 -tclbody {
446 set DB2 0
447 set STMT 0
448
449 # open database using sqlite3_open16()
drhd9910fe2006-10-04 11:55:49 +0000450 set filename [encoding convertto unicode test.db]
451 append filename "\x00\x00"
452 set DB2 [sqlite3_open16 $filename -unused]
danielk1977950f0542006-01-18 05:51:57 +0000453 if {0==$DB2} {
454 error "out of memory"
455 }
456
457 # Prepare statement
458 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
459 if {$rc} {
460 error [string range $msg 4 end]
461 }
462 set STMT $msg
463
464 # Finalize statement
465 set rc [sqlite3_finalize $STMT]
466 if {$rc!="SQLITE_OK"} {
467 error [sqlite3_errmsg $DB2]
468 }
469 set STMT 0
470
471 # Close database
472 set rc [sqlite3_close $DB2]
473 if {$rc!="SQLITE_OK"} {
474 error [sqlite3_errmsg $DB2]
475 }
476 set DB2 0
477} -cleanup {
478 if {$STMT!="0"} {
479 sqlite3_finalize $STMT
480 }
481 if {$DB2!="0"} {
482 set rc [sqlite3_close $DB2]
483 }
484}
485
486# Test handling of malloc() failures in sqlite3_errmsg16().
487#
488do_malloc_test 18 -tclbody {
489 catch {
490 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
491 } msg
492 if {$msg=="out of memory"} {error $msg}
493 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
494 binary scan $utf16 c* bytes
495 if {[llength $bytes]==0} {
496 error "out of memory"
497 }
498}
499
danielk1977161fb792006-01-24 10:58:21 +0000500# This test is aimed at coverage testing. Specificly, it is supposed to
501# cause a malloc() only used when converting between the two utf-16
502# encodings to fail (i.e. little-endian->big-endian). It only actually
503# hits this malloc() on little-endian hosts.
504#
505set static_string "\x00h\x00e\x00l\x00l\x00o"
506for {set l 0} {$l<10} {incr l} {
507 append static_string $static_string
508}
509append static_string "\x00\x00"
510do_malloc_test 19 -tclprep {
511 execsql {
512 PRAGMA encoding = "UTF16be";
513 CREATE TABLE abc(a, b, c);
514 }
515} -tclbody {
516 unset -nocomplain ::STMT
517 set r [catch {
518 set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY]
519 sqlite3_bind_text16 -static $::STMT 1 $static_string 112
520 } msg]
521 if {$r} {error [string range $msg 4 end]}
522 set msg
523} -cleanup {
524 if {[info exists ::STMT]} {
525 sqlite3_finalize $::STMT
526 }
527}
528unset static_string
529
drh6103fe92006-04-05 11:57:37 +0000530# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
531# when the malloc failure occurs within the nested parse.
532#
533do_malloc_test 20 -tclprep {
534 db close
535 file delete -force test2.db test2.db-journal
536 sqlite3 db test2.db
537 db eval {CREATE TABLE t1(x);}
538 db close
539} -tclbody {
540 if {[catch {sqlite3 db test.db}]} {
541 error "out of memory"
542 }
543} -sqlbody {
544 ATTACH DATABASE 'test2.db' AS t2;
545 SELECT * FROM t1;
546 DETACH DATABASE t2;
547}
548
danielk1977b5584c02007-03-30 07:10:50 +0000549# Test malloc failure whilst installing a foreign key.
danielk197769b637b2007-03-29 17:07:52 +0000550#
551do_malloc_test 21 -sqlbody {
552 CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b))
553}
554
danielk19777e29e952007-04-19 11:09:01 +0000555# Test malloc failure in an sqlite3_prepare_v2() call.
556#
557do_malloc_test 22 -tclbody {
558 set ::STMT ""
559 set r [catch {
560 set ::STMT [
561 sqlite3_prepare_v2 $::DB "SELECT * FROM sqlite_master" -1 DUMMY
562 ]
563 } msg]
564 if {$r} {error [string range $msg 4 end]}
565} -cleanup {
566 if {$::STMT ne ""} {
567 sqlite3_finalize $::STMT
568 set ::STMT ""
569 }
570}
571
danielk197796fb0dd2004-06-30 09:49:22 +0000572# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000573do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000574 catch {db close}
575 set sqlite_open_file_count
576} {0}
577
drh344a6272006-06-26 12:50:09 +0000578puts open-file-count=$sqlite_open_file_count
drhed7c8552001-04-11 14:29:21 +0000579sqlite_malloc_fail 0
580finish_test