⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 malloc.test

📁 sqlite嵌入式数据库源码
💻 TEST
📖 第 1 页 / 共 2 页
字号:
# when converting UTF-16 text to integers and real numbers is handled# correctly. ## This is done by retrieving a string from the database engine and# manipulating it using the sqlite3_column_*** APIs. This doesn't # actually return an error to the user when a malloc() fails.. That # could be viewed as a bug.## These tests only run if UTF-16 support is compiled in.#if {$::sqlite_options(utf16)} {  do_malloc_test 8 -tclprep {    set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"    set ::STMT [sqlite3_prepare $::DB $sql -1 X]    sqlite3_step $::STMT    if { $::tcl_platform(byteOrder)=="littleEndian" } {      set ::bomstr "\xFF\xFE"    } else {      set ::bomstr "\xFE\xFF"    }    append ::bomstr [encoding convertto unicode "123456789_123456789_12345678"]  } -tclbody {    sqlite3_column_text16 $::STMT 0    sqlite3_column_int $::STMT 0    sqlite3_column_text16 $::STMT 1    sqlite3_column_double $::STMT 1    sqlite3_reset $::STMT    sqlite3_bind_text16 $::STMT 1 $::bomstr 60    catch {sqlite3_finalize $::STMT}    if {[lindex [sqlite_malloc_stat] 2]<=0} {      error "out of memory"    }  } -cleanup {    sqlite3_finalize $::STMT  }}# This block tests that malloc() failures that occur whilst commiting# a multi-file transaction are handled correctly.#do_malloc_test 9 -sqlprep {  ATTACH 'test2.db' as test2;  CREATE TABLE abc1(a, b, c);  CREATE TABLE test2.abc2(a, b, c);} -sqlbody {  BEGIN;  INSERT INTO abc1 VALUES(1, 2, 3);  INSERT INTO abc2 VALUES(1, 2, 3);  COMMIT;}# This block tests malloc() failures that occur while opening a # connection to a database.do_malloc_test 10 -sqlprep {  CREATE TABLE abc(a, b, c);} -tclbody {  sqlite3 db2 test.db  db2 eval {SELECT * FROM sqlite_master}  db2 close} # This block tests malloc() failures that occur within calls to# sqlite3_create_function().do_malloc_test 11  -tclbody {  set rc [sqlite3_create_function $::DB]  if {[string match $rc SQLITE_NOMEM]} {    error "out of memory"  }}do_malloc_test 12 -tclbody {  set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]  append sql16 "\00\00"  set ::STMT [sqlite3_prepare16 $::DB $sql16 -1 DUMMY]  sqlite3_finalize $::STMT} # Test malloc errors when replaying two hot journals from a 2-file # transaction.ifcapable crashtest {  do_malloc_test 13 -tclprep {    set rc [crashsql 1 test2.db {      ATTACH 'test2.db' as aux;      PRAGMA cache_size = 10;      BEGIN;      CREATE TABLE aux.t2(a, b, c);      CREATE TABLE t1(a, b, c);      COMMIT;    }]    if {$rc!="1 {child process exited abnormally}"} {      error "Wrong error message: $rc"    }  } -tclbody {    db eval {ATTACH 'test2.db' as aux;}    set rc [catch {db eval {      SELECT * FROM t1;       SELECT * FROM t2;    }} err]    if {$rc && $err!="no such table: t1"} {      error $err    }  }}if {$tcl_platform(platform)!="windows"} {  do_malloc_test 14 -tclprep {    catch {db close}    sqlite3 db2 test2.db    db2 eval {      PRAGMA synchronous = 0;      CREATE TABLE t1(a, b);      INSERT INTO t1 VALUES(1, 2);      BEGIN;      INSERT INTO t1 VALUES(3, 4);    }    copy_file test2.db test.db    copy_file test2.db-journal test.db-journal    db2 close  } -tclbody {    sqlite3 db test.db    db eval {      SELECT * FROM t1;    }    }}proc string_compare {a b} {  return [string compare $a $b]}# Test for malloc() failures in sqlite3_create_collation() and # sqlite3_create_collation16().#do_malloc_test 15 -tclbody {  db collate string_compare string_compare  if {[catch {add_test_collate $::DB 1 1 1} msg]} {    if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}    error $msg  }  db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}  db complete {-- Useful comment}  execsql {    CREATE TABLE t1(a, b COLLATE string_compare);    INSERT INTO t1 VALUES(10, 'string');    INSERT INTO t1 VALUES(10, 'string2');  }}# Also test sqlite3_complete(). There are (currently) no malloc()# calls in this function, but test anyway against future changes.#do_malloc_test 16 -tclbody {  db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}  db complete {-- Useful comment}  db eval {    SELECT * FROM sqlite_master;  }}# Test handling of malloc() failures in sqlite3_open16().#do_malloc_test 17 -tclbody {  set DB2 0  set STMT 0  # open database using sqlite3_open16()  set DB2 [sqlite3_open16 test.db -unused]  if {0==$DB2} {    error "out of memory"  }  # Prepare statement  set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]  if {$rc} {    error [string range $msg 4 end]  }  set STMT $msg  # Finalize statement  set rc [sqlite3_finalize $STMT]  if {$rc!="SQLITE_OK"} {    error [sqlite3_errmsg $DB2]  }  set STMT 0  # Close database  set rc [sqlite3_close $DB2]  if {$rc!="SQLITE_OK"} {    error [sqlite3_errmsg $DB2]  }  set DB2 0} -cleanup {  if {$STMT!="0"} {    sqlite3_finalize $STMT  }  if {$DB2!="0"} {    set rc [sqlite3_close $DB2]  }}# Test handling of malloc() failures in sqlite3_errmsg16().#do_malloc_test 18 -tclbody {  catch {    db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"  } msg  if {$msg=="out of memory"} {error $msg}  set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]  binary scan $utf16 c* bytes  if {[llength $bytes]==0} {    error "out of memory"  }}# This test is aimed at coverage testing. Specificly, it is supposed to# cause a malloc() only used when converting between the two utf-16 # encodings to fail (i.e. little-endian->big-endian). It only actually # hits this malloc() on little-endian hosts.#set static_string "\x00h\x00e\x00l\x00l\x00o"for {set l 0} {$l<10} {incr l} {  append static_string $static_string}append static_string "\x00\x00"do_malloc_test 19 -tclprep {  execsql {    PRAGMA encoding = "UTF16be";    CREATE TABLE abc(a, b, c);  }} -tclbody {  unset -nocomplain ::STMT  set r [catch {    set ::STMT [sqlite3_prepare $::DB {SELECT ?} -1 DUMMY]    sqlite3_bind_text16 -static $::STMT 1 $static_string 112  } msg]  if {$r} {error [string range $msg 4 end]}  set msg} -cleanup {  if {[info exists ::STMT]} {    sqlite3_finalize $::STMT  }}unset static_string# Make sure SQLITE_NOMEM is reported out on an ATTACH failure even# when the malloc failure occurs within the nested parse.#do_malloc_test 20 -tclprep {  db close  file delete -force test2.db test2.db-journal  sqlite3 db test2.db  db eval {CREATE TABLE t1(x);}  db close} -tclbody {  if {[catch {sqlite3 db test.db}]} {    error "out of memory"  }} -sqlbody {  ATTACH DATABASE 'test2.db' AS t2;  SELECT * FROM t1;  DETACH DATABASE t2;} # Ensure that no file descriptors were leaked.do_test malloc-99.X {  catch {db close}  set sqlite_open_file_count} {0}puts open-file-count=$sqlite_open_file_countsqlite_malloc_fail 0finish_test

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -