drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6 | # 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 11 | # |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 12 | # This file attempts to check the behavior of the SQLite library in |
| 13 | # an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, |
| 14 | # the SQLite library accepts a special command (sqlite3_memdebug_fail N C) |
| 15 | # which causes the N-th malloc to fail. This special feature is used |
| 16 | # to see what happens in the library if a malloc were to really fail |
| 17 | # due to an out-of-memory situation. |
| 18 | # |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 19 | # $Id: malloc.test,v 1.45 2007/08/29 12:31:29 danielk1977 Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 20 | |
| 21 | set testdir [file dirname $argv0] |
| 22 | source $testdir/tester.tcl |
| 23 | |
| 24 | # Only run these tests if memory debugging is turned on. |
| 25 | # |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 26 | ifcapable !memdebug { |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 27 | puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 28 | finish_test |
| 29 | return |
| 30 | } |
| 31 | |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 32 | source $testdir/malloc_common.tcl |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 33 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 34 | do_malloc_test 1 -tclprep { |
| 35 | db close |
| 36 | } -tclbody { |
| 37 | if {[catch {sqlite3 db test.db}]} { |
| 38 | error "out of memory" |
| 39 | } |
| 40 | } -sqlbody { |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 41 | DROP TABLE IF EXISTS t1; |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 42 | CREATE TABLE t1( |
| 43 | a int, b float, c double, d text, e varchar(20), |
| 44 | primary key(a,b,c) |
| 45 | ); |
| 46 | CREATE INDEX i1 ON t1(a,b); |
drh | 76f8079 | 2006-07-11 12:40:25 +0000 | [diff] [blame] | 47 | INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500'); |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 48 | INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder'); |
| 49 | SELECT * FROM t1; |
| 50 | SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; |
| 51 | DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1); |
| 52 | SELECT count(*) FROM t1; |
| 53 | } |
drh | d400728 | 2001-04-12 23:21:58 +0000 | [diff] [blame] | 54 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 55 | # Ensure that no file descriptors were leaked. |
| 56 | do_test malloc-1.X { |
| 57 | catch {db close} |
| 58 | set sqlite_open_file_count |
| 59 | } {0} |
| 60 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 61 | do_malloc_test 2 -sqlbody { |
drh | fc23314 | 2005-08-19 01:07:15 +0000 | [diff] [blame] | 62 | CREATE TABLE t1(a int, b int default 'abc', c int default 1); |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 63 | CREATE INDEX i1 ON t1(a,b); |
| 64 | INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz'); |
| 65 | INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz'); |
| 66 | INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz'); |
| 67 | INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz'); |
| 68 | INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz'); |
| 69 | INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz'); |
| 70 | SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1; |
| 71 | UPDATE t1 SET b=b||b||b||b; |
| 72 | UPDATE t1 SET b=a WHERE a in (10,12,22); |
| 73 | INSERT INTO t1(c,b,a) VALUES(20,10,5); |
| 74 | INSERT INTO t1 SELECT * FROM t1 |
| 75 | WHERE a IN (SELECT a FROM t1 WHERE a<10); |
| 76 | DELETE FROM t1 WHERE a>=10; |
| 77 | DROP INDEX i1; |
| 78 | DELETE FROM t1; |
| 79 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 80 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 81 | # Ensure that no file descriptors were leaked. |
| 82 | do_test malloc-2.X { |
| 83 | catch {db close} |
| 84 | set sqlite_open_file_count |
| 85 | } {0} |
| 86 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 87 | do_malloc_test 3 -sqlbody { |
| 88 | BEGIN TRANSACTION; |
| 89 | CREATE TABLE t1(a int, b int, c int); |
| 90 | CREATE INDEX i1 ON t1(a,b); |
| 91 | INSERT INTO t1 VALUES(1,1,99); |
| 92 | INSERT INTO t1 VALUES(2,4,98); |
| 93 | INSERT INTO t1 VALUES(3,9,97); |
| 94 | INSERT INTO t1 VALUES(4,16,96); |
| 95 | INSERT INTO t1 VALUES(5,25,95); |
| 96 | INSERT INTO t1 VALUES(6,36,94); |
| 97 | INSERT INTO t1(c,b,a) VALUES(20,10,5); |
| 98 | DELETE FROM t1 WHERE a>=10; |
| 99 | DROP INDEX i1; |
| 100 | DELETE FROM t1; |
| 101 | ROLLBACK; |
| 102 | } |
| 103 | |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 104 | |
| 105 | # Ensure that no file descriptors were leaked. |
| 106 | do_test malloc-3.X { |
| 107 | catch {db close} |
| 108 | set sqlite_open_file_count |
| 109 | } {0} |
| 110 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 111 | do_malloc_test 4 -sqlbody { |
| 112 | BEGIN TRANSACTION; |
| 113 | CREATE TABLE t1(a int, b int, c int); |
| 114 | CREATE INDEX i1 ON t1(a,b); |
| 115 | INSERT INTO t1 VALUES(1,1,99); |
| 116 | INSERT INTO t1 VALUES(2,4,98); |
| 117 | INSERT INTO t1 VALUES(3,9,97); |
| 118 | INSERT INTO t1 VALUES(4,16,96); |
| 119 | INSERT INTO t1 VALUES(5,25,95); |
| 120 | INSERT INTO t1 VALUES(6,36,94); |
| 121 | UPDATE t1 SET b=a WHERE a in (10,12,22); |
| 122 | INSERT INTO t1 SELECT * FROM t1 |
| 123 | WHERE a IN (SELECT a FROM t1 WHERE a<10); |
| 124 | DROP INDEX i1; |
| 125 | DELETE FROM t1; |
| 126 | COMMIT; |
| 127 | } |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 128 | |
| 129 | # Ensure that no file descriptors were leaked. |
| 130 | do_test malloc-4.X { |
| 131 | catch {db close} |
| 132 | set sqlite_open_file_count |
| 133 | } {0} |
| 134 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 135 | do_malloc_test 5 -sqlbody { |
| 136 | BEGIN TRANSACTION; |
| 137 | CREATE TABLE t1(a,b); |
| 138 | CREATE TABLE t2(x,y); |
| 139 | CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 140 | INSERT INTO t2(x,y) VALUES(new.rowid,1); |
danielk1977 | 9fb3ecb | 2007-08-16 04:39:01 +0000 | [diff] [blame] | 141 | INSERT INTO t2(x,y) SELECT * FROM t2; |
| 142 | INSERT INTO t2 SELECT * FROM t2; |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 143 | UPDATE t2 SET y=y+1 WHERE x=new.rowid; |
| 144 | SELECT 123; |
| 145 | DELETE FROM t2 WHERE x=new.rowid; |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 146 | END; |
| 147 | INSERT INTO t1(a,b) VALUES(2,3); |
| 148 | COMMIT; |
| 149 | } |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 150 | |
| 151 | # Ensure that no file descriptors were leaked. |
| 152 | do_test malloc-5.X { |
| 153 | catch {db close} |
| 154 | set sqlite_open_file_count |
| 155 | } {0} |
| 156 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 157 | do_malloc_test 6 -sqlprep { |
| 158 | BEGIN TRANSACTION; |
| 159 | CREATE TABLE t1(a); |
| 160 | INSERT INTO t1 VALUES(1); |
| 161 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 162 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 163 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 164 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 165 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 166 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 167 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 168 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 169 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 170 | INSERT INTO t1 SELECT a*2 FROM t1; |
| 171 | DELETE FROM t1 where rowid%5 = 0; |
| 172 | COMMIT; |
| 173 | } -sqlbody { |
| 174 | VACUUM; |
| 175 | } |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 176 | |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 177 | do_malloc_test 7 -sqlprep { |
| 178 | CREATE TABLE t1(a, b); |
| 179 | INSERT INTO t1 VALUES(1, 2); |
| 180 | INSERT INTO t1 VALUES(3, 4); |
| 181 | INSERT INTO t1 VALUES(5, 6); |
| 182 | INSERT INTO t1 VALUES(7, randstr(1200,1200)); |
| 183 | } -sqlbody { |
| 184 | SELECT min(a) FROM t1 WHERE a<6 GROUP BY b; |
| 185 | SELECT a FROM t1 WHERE a<6 ORDER BY a; |
| 186 | SELECT b FROM t1 WHERE a>6; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 187 | } |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 188 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 189 | # This block is designed to test that some malloc failures that may |
| 190 | # occur in vdbeapi.c. Specifically, if a malloc failure that occurs |
| 191 | # when converting UTF-16 text to integers and real numbers is handled |
| 192 | # correctly. |
| 193 | # |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 194 | # This is done by retrieving a string from the database engine and |
| 195 | # manipulating it using the sqlite3_column_*** APIs. This doesn't |
| 196 | # actually return an error to the user when a malloc() fails.. That |
| 197 | # could be viewed as a bug. |
| 198 | # |
| 199 | # These tests only run if UTF-16 support is compiled in. |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 200 | # |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 201 | if {$::sqlite_options(utf16)} { |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 202 | set ::STMT {} |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 203 | do_malloc_test 8 -tclprep { |
| 204 | set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 205 | set ::STMT [sqlite3_prepare db $sql -1 X] |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 206 | sqlite3_step $::STMT |
| 207 | if { $::tcl_platform(byteOrder)=="littleEndian" } { |
| 208 | set ::bomstr "\xFF\xFE" |
| 209 | } else { |
| 210 | set ::bomstr "\xFE\xFF" |
| 211 | } |
| 212 | append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"] |
| 213 | } -tclbody { |
| 214 | sqlite3_column_text16 $::STMT 0 |
| 215 | sqlite3_column_int $::STMT 0 |
| 216 | sqlite3_column_text16 $::STMT 1 |
| 217 | sqlite3_column_double $::STMT 1 |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 218 | set rc [sqlite3_reset $::STMT] |
| 219 | if {$rc eq "SQLITE_NOMEM"} {error "out of memory"} |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 220 | sqlite3_bind_text16 $::STMT 1 $::bomstr 60 |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 221 | #catch {sqlite3_finalize $::STMT} |
| 222 | #if {[lindex [sqlite_malloc_stat] 2]<=0} { |
| 223 | # error "out of memory" |
| 224 | #} |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 225 | } -cleanup { |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 226 | if {$::STMT!=""} { |
| 227 | sqlite3_finalize $::STMT |
| 228 | set ::STMT {} |
| 229 | } |
danielk1977 | c08d405 | 2005-01-13 13:35:57 +0000 | [diff] [blame] | 230 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 231 | } |
| 232 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 233 | # This block tests that malloc() failures that occur whilst commiting |
| 234 | # a multi-file transaction are handled correctly. |
| 235 | # |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 236 | do_malloc_test 9 -sqlprep { |
| 237 | ATTACH 'test2.db' as test2; |
| 238 | CREATE TABLE abc1(a, b, c); |
| 239 | CREATE TABLE test2.abc2(a, b, c); |
| 240 | } -sqlbody { |
| 241 | BEGIN; |
| 242 | INSERT INTO abc1 VALUES(1, 2, 3); |
| 243 | INSERT INTO abc2 VALUES(1, 2, 3); |
| 244 | COMMIT; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 245 | } |
| 246 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 247 | # This block tests malloc() failures that occur while opening a |
| 248 | # connection to a database. |
| 249 | do_malloc_test 10 -sqlprep { |
| 250 | CREATE TABLE abc(a, b, c); |
| 251 | } -tclbody { |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 252 | db close |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 253 | sqlite3 db2 test.db |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 254 | db2 eval {SELECT * FROM sqlite_master} |
| 255 | db2 close |
| 256 | } |
| 257 | |
| 258 | # This block tests malloc() failures that occur within calls to |
| 259 | # sqlite3_create_function(). |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 260 | do_malloc_test 11 -tclbody { |
| 261 | set rc [sqlite3_create_function db] |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 262 | if {[string match $rc SQLITE_NOMEM]} { |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 263 | error "out of memory" |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | do_malloc_test 12 -tclbody { |
| 268 | set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"] |
| 269 | append sql16 "\00\00" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 270 | set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY] |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 271 | sqlite3_finalize $::STMT |
danielk1977 | c585971 | 2007-03-26 12:26:27 +0000 | [diff] [blame] | 272 | } |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 273 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 274 | # Test malloc errors when replaying two hot journals from a 2-file |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 275 | # transaction. |
| 276 | ifcapable crashtest { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 277 | do_malloc_test 13 -tclprep { |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 278 | set rc [crashsql -delay 1 -file test2.db { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 279 | ATTACH 'test2.db' as aux; |
| 280 | PRAGMA cache_size = 10; |
| 281 | BEGIN; |
| 282 | CREATE TABLE aux.t2(a, b, c); |
| 283 | CREATE TABLE t1(a, b, c); |
| 284 | COMMIT; |
| 285 | }] |
| 286 | if {$rc!="1 {child process exited abnormally}"} { |
| 287 | error "Wrong error message: $rc" |
| 288 | } |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 289 | } -tclbody { |
| 290 | db eval {ATTACH 'test2.db' as aux;} |
| 291 | set rc [catch {db eval { |
| 292 | SELECT * FROM t1; |
| 293 | SELECT * FROM t2; |
| 294 | }} err] |
| 295 | if {$rc && $err!="no such table: t1"} { |
| 296 | error $err |
| 297 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
danielk1977 | 76b047d | 2005-01-19 03:52:54 +0000 | [diff] [blame] | 301 | if {$tcl_platform(platform)!="windows"} { |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 302 | do_malloc_test 14 -tclprep { |
| 303 | catch {db close} |
| 304 | sqlite3 db2 test2.db |
| 305 | db2 eval { |
| 306 | PRAGMA synchronous = 0; |
| 307 | CREATE TABLE t1(a, b); |
| 308 | INSERT INTO t1 VALUES(1, 2); |
| 309 | BEGIN; |
| 310 | INSERT INTO t1 VALUES(3, 4); |
| 311 | } |
| 312 | copy_file test2.db test.db |
| 313 | copy_file test2.db-journal test.db-journal |
| 314 | db2 close |
| 315 | } -tclbody { |
| 316 | sqlite3 db test.db |
| 317 | db eval { |
| 318 | SELECT * FROM t1; |
| 319 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 320 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 321 | } |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 322 | |
| 323 | proc string_compare {a b} { |
| 324 | return [string compare $a $b] |
| 325 | } |
| 326 | |
| 327 | # Test for malloc() failures in sqlite3_create_collation() and |
| 328 | # sqlite3_create_collation16(). |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 329 | # |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 330 | do_malloc_test 15 -start 4 -tclbody { |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 331 | db collate string_compare string_compare |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 332 | if {[catch {add_test_collate db 1 1 1} msg]} { |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 333 | if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"} |
| 334 | error $msg |
| 335 | } |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 336 | |
| 337 | db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} |
| 338 | db complete {-- Useful comment} |
| 339 | |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 340 | execsql { |
| 341 | CREATE TABLE t1(a, b COLLATE string_compare); |
| 342 | INSERT INTO t1 VALUES(10, 'string'); |
| 343 | INSERT INTO t1 VALUES(10, 'string2'); |
| 344 | } |
danielk1977 | 76b047d | 2005-01-19 03:52:54 +0000 | [diff] [blame] | 345 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 346 | |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 347 | # Also test sqlite3_complete(). There are (currently) no malloc() |
| 348 | # calls in this function, but test anyway against future changes. |
| 349 | # |
| 350 | do_malloc_test 16 -tclbody { |
| 351 | db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;} |
| 352 | db complete {-- Useful comment} |
| 353 | db eval { |
| 354 | SELECT * FROM sqlite_master; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | # Test handling of malloc() failures in sqlite3_open16(). |
| 359 | # |
| 360 | do_malloc_test 17 -tclbody { |
| 361 | set DB2 0 |
| 362 | set STMT 0 |
| 363 | |
| 364 | # open database using sqlite3_open16() |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 365 | set filename [encoding convertto unicode test.db] |
| 366 | append filename "\x00\x00" |
| 367 | set DB2 [sqlite3_open16 $filename -unused] |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 368 | if {0==$DB2} { |
| 369 | error "out of memory" |
| 370 | } |
| 371 | |
| 372 | # Prepare statement |
| 373 | set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg] |
| 374 | if {$rc} { |
| 375 | error [string range $msg 4 end] |
| 376 | } |
| 377 | set STMT $msg |
| 378 | |
| 379 | # Finalize statement |
| 380 | set rc [sqlite3_finalize $STMT] |
| 381 | if {$rc!="SQLITE_OK"} { |
| 382 | error [sqlite3_errmsg $DB2] |
| 383 | } |
| 384 | set STMT 0 |
| 385 | |
| 386 | # Close database |
| 387 | set rc [sqlite3_close $DB2] |
| 388 | if {$rc!="SQLITE_OK"} { |
| 389 | error [sqlite3_errmsg $DB2] |
| 390 | } |
| 391 | set DB2 0 |
| 392 | } -cleanup { |
| 393 | if {$STMT!="0"} { |
| 394 | sqlite3_finalize $STMT |
| 395 | } |
| 396 | if {$DB2!="0"} { |
| 397 | set rc [sqlite3_close $DB2] |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | # Test handling of malloc() failures in sqlite3_errmsg16(). |
| 402 | # |
| 403 | do_malloc_test 18 -tclbody { |
| 404 | catch { |
| 405 | db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master" |
| 406 | } msg |
| 407 | if {$msg=="out of memory"} {error $msg} |
| 408 | set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]] |
| 409 | binary scan $utf16 c* bytes |
| 410 | if {[llength $bytes]==0} { |
| 411 | error "out of memory" |
| 412 | } |
| 413 | } |
| 414 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 415 | # This test is aimed at coverage testing. Specificly, it is supposed to |
| 416 | # cause a malloc() only used when converting between the two utf-16 |
| 417 | # encodings to fail (i.e. little-endian->big-endian). It only actually |
| 418 | # hits this malloc() on little-endian hosts. |
| 419 | # |
| 420 | set static_string "\x00h\x00e\x00l\x00l\x00o" |
| 421 | for {set l 0} {$l<10} {incr l} { |
| 422 | append static_string $static_string |
| 423 | } |
| 424 | append static_string "\x00\x00" |
| 425 | do_malloc_test 19 -tclprep { |
| 426 | execsql { |
| 427 | PRAGMA encoding = "UTF16be"; |
| 428 | CREATE TABLE abc(a, b, c); |
| 429 | } |
| 430 | } -tclbody { |
| 431 | unset -nocomplain ::STMT |
| 432 | set r [catch { |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 433 | set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY] |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 434 | sqlite3_bind_text16 -static $::STMT 1 $static_string 112 |
| 435 | } msg] |
| 436 | if {$r} {error [string range $msg 4 end]} |
| 437 | set msg |
| 438 | } -cleanup { |
| 439 | if {[info exists ::STMT]} { |
| 440 | sqlite3_finalize $::STMT |
| 441 | } |
| 442 | } |
| 443 | unset static_string |
| 444 | |
drh | 6103fe9 | 2006-04-05 11:57:37 +0000 | [diff] [blame] | 445 | # Make sure SQLITE_NOMEM is reported out on an ATTACH failure even |
| 446 | # when the malloc failure occurs within the nested parse. |
| 447 | # |
| 448 | do_malloc_test 20 -tclprep { |
| 449 | db close |
| 450 | file delete -force test2.db test2.db-journal |
| 451 | sqlite3 db test2.db |
| 452 | db eval {CREATE TABLE t1(x);} |
| 453 | db close |
| 454 | } -tclbody { |
| 455 | if {[catch {sqlite3 db test.db}]} { |
| 456 | error "out of memory" |
| 457 | } |
| 458 | } -sqlbody { |
| 459 | ATTACH DATABASE 'test2.db' AS t2; |
| 460 | SELECT * FROM t1; |
| 461 | DETACH DATABASE t2; |
| 462 | } |
| 463 | |
danielk1977 | b5584c0 | 2007-03-30 07:10:50 +0000 | [diff] [blame] | 464 | # Test malloc failure whilst installing a foreign key. |
danielk1977 | 69b637b | 2007-03-29 17:07:52 +0000 | [diff] [blame] | 465 | # |
| 466 | do_malloc_test 21 -sqlbody { |
| 467 | CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b)) |
| 468 | } |
| 469 | |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 470 | # Test malloc failure in an sqlite3_prepare_v2() call. |
| 471 | # |
| 472 | do_malloc_test 22 -tclbody { |
| 473 | set ::STMT "" |
| 474 | set r [catch { |
| 475 | set ::STMT [ |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 476 | sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 477 | ] |
| 478 | } msg] |
| 479 | if {$r} {error [string range $msg 4 end]} |
| 480 | } -cleanup { |
| 481 | if {$::STMT ne ""} { |
| 482 | sqlite3_finalize $::STMT |
| 483 | set ::STMT "" |
| 484 | } |
| 485 | } |
| 486 | |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 487 | # Ensure that no file descriptors were leaked. |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 488 | do_test malloc-99.X { |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 489 | catch {db close} |
| 490 | set sqlite_open_file_count |
| 491 | } {0} |
| 492 | |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 493 | puts open-file-count=$sqlite_open_file_count |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 494 | finish_test |