blob: 8e592c8764b9c0cfcb5a8398e999d3484c5625f7 [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#
danielk197759a33f92007-03-17 10:26:59 +000017# $Id: malloc.test,v 1.37 2007/03/17 10:26:59 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 {
danielk1977771151b2006-01-17 13:21:40 +0000337 sqlite3 db2 test.db
danielk19774397de52005-01-12 12:44:03 +0000338 db2 eval {SELECT * FROM sqlite_master}
339 db2 close
340}
341
342# This block tests malloc() failures that occur within calls to
343# sqlite3_create_function().
344do_malloc_test 11 -tclbody {
danielk19772c336542005-01-13 02:14:23 +0000345 set rc [sqlite3_create_function $::DB]
346 if {[string match $rc SQLITE_NOMEM]} {
danielk19774397de52005-01-12 12:44:03 +0000347 error "out of memory"
348 }
349}
350
351do_malloc_test 12 -tclbody {
352 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
353 append sql16 "\00\00"
354 set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]
355 sqlite3_finalize $::STMT
356}
danielk19778b60e0f2005-01-12 09:10:39 +0000357
danielk1977aca790a2005-01-13 11:07:52 +0000358# Test malloc errors when replaying two hot journals from a 2-file
drh66560ad2006-01-06 14:32:19 +0000359# transaction.
360ifcapable crashtest {
danielk1977aca790a2005-01-13 11:07:52 +0000361 do_malloc_test 13 -tclprep {
danielk197759a33f92007-03-17 10:26:59 +0000362 set rc [crashsql -delay 1 -file test2.db {
danielk1977aca790a2005-01-13 11:07:52 +0000363 ATTACH 'test2.db' as aux;
364 PRAGMA cache_size = 10;
365 BEGIN;
366 CREATE TABLE aux.t2(a, b, c);
367 CREATE TABLE t1(a, b, c);
368 COMMIT;
369 }]
370 if {$rc!="1 {child process exited abnormally}"} {
371 error "Wrong error message: $rc"
372 }
danielk1977950f0542006-01-18 05:51:57 +0000373 } -tclbody {
374 db eval {ATTACH 'test2.db' as aux;}
375 set rc [catch {db eval {
376 SELECT * FROM t1;
377 SELECT * FROM t2;
378 }} err]
379 if {$rc && $err!="no such table: t1"} {
380 error $err
381 }
danielk1977aca790a2005-01-13 11:07:52 +0000382 }
383}
384
danielk197776b047d2005-01-19 03:52:54 +0000385if {$tcl_platform(platform)!="windows"} {
danielk19779a30cf62006-01-18 04:26:07 +0000386 do_malloc_test 14 -tclprep {
387 catch {db close}
388 sqlite3 db2 test2.db
389 db2 eval {
390 PRAGMA synchronous = 0;
391 CREATE TABLE t1(a, b);
392 INSERT INTO t1 VALUES(1, 2);
393 BEGIN;
394 INSERT INTO t1 VALUES(3, 4);
395 }
396 copy_file test2.db test.db
397 copy_file test2.db-journal test.db-journal
398 db2 close
399 } -tclbody {
400 sqlite3 db test.db
401 db eval {
402 SELECT * FROM t1;
403 }
danielk1977aca790a2005-01-13 11:07:52 +0000404 }
danielk1977aca790a2005-01-13 11:07:52 +0000405}
danielk19779a30cf62006-01-18 04:26:07 +0000406
407proc string_compare {a b} {
408 return [string compare $a $b]
409}
410
411# Test for malloc() failures in sqlite3_create_collation() and
412# sqlite3_create_collation16().
danielk1977950f0542006-01-18 05:51:57 +0000413#
danielk19779a30cf62006-01-18 04:26:07 +0000414do_malloc_test 15 -tclbody {
415 db collate string_compare string_compare
416 if {[catch {add_test_collate $::DB 1 1 1} msg]} {
417 if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
418 error $msg
419 }
danielk1977950f0542006-01-18 05:51:57 +0000420
421 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
422 db complete {-- Useful comment}
423
danielk19779a30cf62006-01-18 04:26:07 +0000424 execsql {
425 CREATE TABLE t1(a, b COLLATE string_compare);
426 INSERT INTO t1 VALUES(10, 'string');
427 INSERT INTO t1 VALUES(10, 'string2');
428 }
danielk197776b047d2005-01-19 03:52:54 +0000429}
danielk1977aca790a2005-01-13 11:07:52 +0000430
danielk1977950f0542006-01-18 05:51:57 +0000431# Also test sqlite3_complete(). There are (currently) no malloc()
432# calls in this function, but test anyway against future changes.
433#
434do_malloc_test 16 -tclbody {
435 db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
436 db complete {-- Useful comment}
437 db eval {
438 SELECT * FROM sqlite_master;
439 }
440}
441
442# Test handling of malloc() failures in sqlite3_open16().
443#
444do_malloc_test 17 -tclbody {
445 set DB2 0
446 set STMT 0
447
448 # open database using sqlite3_open16()
drhd9910fe2006-10-04 11:55:49 +0000449 set filename [encoding convertto unicode test.db]
450 append filename "\x00\x00"
451 set DB2 [sqlite3_open16 $filename -unused]
danielk1977950f0542006-01-18 05:51:57 +0000452 if {0==$DB2} {
453 error "out of memory"
454 }
455
456 # Prepare statement
457 set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
458 if {$rc} {
459 error [string range $msg 4 end]
460 }
461 set STMT $msg
462
463 # Finalize statement
464 set rc [sqlite3_finalize $STMT]
465 if {$rc!="SQLITE_OK"} {
466 error [sqlite3_errmsg $DB2]
467 }
468 set STMT 0
469
470 # Close database
471 set rc [sqlite3_close $DB2]
472 if {$rc!="SQLITE_OK"} {
473 error [sqlite3_errmsg $DB2]
474 }
475 set DB2 0
476} -cleanup {
477 if {$STMT!="0"} {
478 sqlite3_finalize $STMT
479 }
480 if {$DB2!="0"} {
481 set rc [sqlite3_close $DB2]
482 }
483}
484
485# Test handling of malloc() failures in sqlite3_errmsg16().
486#
487do_malloc_test 18 -tclbody {
488 catch {
489 db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
490 } msg
491 if {$msg=="out of memory"} {error $msg}
492 set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
493 binary scan $utf16 c* bytes
494 if {[llength $bytes]==0} {
495 error "out of memory"
496 }
497}
498
danielk1977161fb792006-01-24 10:58:21 +0000499# This test is aimed at coverage testing. Specificly, it is supposed to
500# cause a malloc() only used when converting between the two utf-16
501# encodings to fail (i.e. little-endian->big-endian). It only actually
502# hits this malloc() on little-endian hosts.
503#
504set static_string "\x00h\x00e\x00l\x00l\x00o"
505for {set l 0} {$l<10} {incr l} {
506 append static_string $static_string
507}
508append static_string "\x00\x00"
509do_malloc_test 19 -tclprep {
510 execsql {
511 PRAGMA encoding = "UTF16be";
512 CREATE TABLE abc(a, b, c);
513 }
514} -tclbody {
515 unset -nocomplain ::STMT
516 set r [catch {
517 set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY]
518 sqlite3_bind_text16 -static $::STMT 1 $static_string 112
519 } msg]
520 if {$r} {error [string range $msg 4 end]}
521 set msg
522} -cleanup {
523 if {[info exists ::STMT]} {
524 sqlite3_finalize $::STMT
525 }
526}
527unset static_string
528
drh6103fe92006-04-05 11:57:37 +0000529# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
530# when the malloc failure occurs within the nested parse.
531#
532do_malloc_test 20 -tclprep {
533 db close
534 file delete -force test2.db test2.db-journal
535 sqlite3 db test2.db
536 db eval {CREATE TABLE t1(x);}
537 db close
538} -tclbody {
539 if {[catch {sqlite3 db test.db}]} {
540 error "out of memory"
541 }
542} -sqlbody {
543 ATTACH DATABASE 'test2.db' AS t2;
544 SELECT * FROM t1;
545 DETACH DATABASE t2;
546}
547
548
danielk197796fb0dd2004-06-30 09:49:22 +0000549# Ensure that no file descriptors were leaked.
danielk1977b5402fb2005-01-12 07:15:04 +0000550do_test malloc-99.X {
danielk197796fb0dd2004-06-30 09:49:22 +0000551 catch {db close}
552 set sqlite_open_file_count
553} {0}
554
drh344a6272006-06-26 12:50:09 +0000555puts open-file-count=$sqlite_open_file_count
drhed7c8552001-04-11 14:29:21 +0000556sqlite_malloc_fail 0
557finish_test