1# 2005 November 30 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# 12# This file contains tests to ensure that the library handles malloc() failures 13# correctly. The emphasis in this file is on sqlite3_column_XXX() APIs. 14# 15# $Id: malloc4.test,v 1.2 2005/12/09 14:25:12 danielk1977 Exp $ 16 17#--------------------------------------------------------------------------- 18# NOTES ON EXPECTED BEHAVIOUR 19# 20# [193] When a memory allocation failure occurs during sqlite3_column_name(), 21# sqlite3_column_name16(), sqlite3_column_decltype(), or 22# sqlite3_column_decltype16() the function shall return NULL. 23# 24#--------------------------------------------------------------------------- 25 26set testdir [file dirname $argv0] 27source $testdir/tester.tcl 28 29# Only run these tests if memory debugging is turned on. 30if {[info command sqlite_malloc_stat]==""} { 31 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." 32 finish_test 33 return 34} 35 36proc do_stmt_test {id sql} { 37 set ::sql $sql 38 set go 1 39 for {set n 1} {$go} {incr n} { 40 set testid "malloc4-$id.(iFail $n)" 41 42 # Prepare the statement 43 do_test ${testid}.1 { 44 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] 45 expr [string length $::STMT] > 0 46 } {1} 47 48 # Set the Nth malloc() to fail. 49 sqlite_malloc_fail $n 50 51 # Test malloc failure in the _name(), _name16(), decltype() and 52 # decltype16() APIs. Calls that occur after the malloc() failure should 53 # return NULL. No error is raised though. 54 # 55 # ${testid}.2.1 - Call _name() 56 # ${testid}.2.2 - Call _name16() 57 # ${testid}.2.3 - Call _name() 58 # ${testid}.2.4 - Check that the return values of the above three calls are 59 # consistent with each other and with the simulated 60 # malloc() failures. 61 # 62 # Because the code that implements the _decltype() and _decltype16() APIs 63 # is the same as the _name() and _name16() implementations, we don't worry 64 # about explicitly testing them. 65 # 66 do_test ${testid}.2.1 { 67 set mf1 [expr [lindex [sqlite_malloc_stat] 2] <= 0] 68 set ::name8 [sqlite3_column_name $::STMT 0] 69 set mf2 [expr [lindex [sqlite_malloc_stat] 2] <= 0] 70 expr {$mf1 == $mf2 || $::name8 == ""} 71 } {1} 72 do_test ${testid}.2.2 { 73 set mf1 [expr [lindex [sqlite_malloc_stat] 2] <= 0] 74 set ::name16 [sqlite3_column_name16 $::STMT 0] 75 set ::name16 [encoding convertfrom unicode $::name16] 76 set ::name16 [string range $::name16 0 end-1] 77 set mf2 [expr [lindex [sqlite_malloc_stat] 2] <= 0] 78 expr {$mf1 == $mf2 || $::name16 == ""} 79 } {1} 80 do_test ${testid}.2.3 { 81 set mf1 [expr [lindex [sqlite_malloc_stat] 2] <= 0] 82 set ::name8_2 [sqlite3_column_name $::STMT 0] 83 set mf2 [expr [lindex [sqlite_malloc_stat] 2] <= 0] 84 expr {$mf1 == $mf2 || $::name8_2 == ""} 85 } {1} 86 set ::mallocFailed [expr [lindex [sqlite_malloc_stat] 2] <= 0] 87 do_test ${testid}.2.4 { 88 expr { 89 $::name8 == $::name8_2 && $::name16 == $::name8 && !$::mallocFailed || 90 $::name8 == $::name8_2 && $::name16 == "" && $::mallocFailed || 91 $::name8 == $::name16 && $::name8_2 == "" && $::mallocFailed || 92 $::name8_2 == $::name16 && $::name8 == "" && $::mallocFailed 93 } 94 } {1} 95 96 # Step the statement so that we can call _text() and _text16(). Before 97 # running sqlite3_step(), make sure that malloc() is not about to fail. 98 # Memory allocation failures that occur within sqlite3_step() are tested 99 # elsewhere. 100 set mf [lindex [sqlite_malloc_stat] 2] 101 sqlite_malloc_fail 0 102 do_test ${testid}.3 { 103 sqlite3_step $::STMT 104 } {SQLITE_ROW} 105 sqlite_malloc_fail $mf 106 107 # Test for malloc() failures within _text() and _text16(). 108 # 109 do_test ${testid}.4.1 { 110 set ::text8 [sqlite3_column_text $::STMT 0] 111 set mf [expr [lindex [sqlite_malloc_stat] 2] <= 0 && !$::mallocFailed] 112 expr {$mf==0 || $::text8 == ""} 113 } {1} 114 do_test ${testid}.4.2 { 115 set ::text16 [sqlite3_column_text16 $::STMT 0] 116 set ::text16 [encoding convertfrom unicode $::text16] 117 set ::text16 [string range $::text16 0 end-1] 118 set mf [expr [lindex [sqlite_malloc_stat] 2] <= 0 && !$::mallocFailed] 119 expr {$mf==0 || $::text16 == ""} 120 } {1} 121 do_test ${testid}.4.3 { 122 set ::text8_2 [sqlite3_column_text $::STMT 0] 123 set mf [expr [lindex [sqlite_malloc_stat] 2] <= 0 && !$::mallocFailed] 124 expr {$mf==0 || $::text8_2 == "" || ($::text16 == "" && $::text8 != "")} 125 } {1} 126 127 # Test for malloc() failures within _int(), _int64() and _real(). The only 128 # way this can occur is if the string has to be translated from UTF-16 to 129 # UTF-8 before being converted to a numeric value. 130 do_test ${testid}.4.4.1 { 131 set mf [lindex [sqlite_malloc_stat] 2] 132 sqlite_malloc_fail 0 133 sqlite3_column_text16 $::STMT 0 134 sqlite_malloc_fail $mf 135 sqlite3_column_int $::STMT 0 136 } {0} 137 do_test ${testid}.4.5 { 138 set mf [lindex [sqlite_malloc_stat] 2] 139 sqlite_malloc_fail 0 140 sqlite3_column_text16 $::STMT 0 141 sqlite_malloc_fail $mf 142 sqlite3_column_int64 $::STMT 0 143 } {0} 144 145 do_test ${testid}.4.6 { 146 set mf [lindex [sqlite_malloc_stat] 2] 147 sqlite_malloc_fail 0 148 sqlite3_column_text16 $::STMT 0 149 sqlite_malloc_fail $mf 150 sqlite3_column_double $::STMT 0 151 } {0.0} 152 153 set mallocFailedAfterStep [expr \ 154 [lindex [sqlite_malloc_stat] 2] <= 0 && !$::mallocFailed 155 ] 156 157 sqlite_malloc_fail 0 158 # Test that if a malloc() failed the next call to sqlite3_step() returns 159 # SQLITE_ERROR. If malloc() did not fail, it should return SQLITE_DONE. 160 # 161 do_test ${testid}.5 { 162 sqlite3_step $::STMT 163 } [expr {$mallocFailedAfterStep ? "SQLITE_ERROR" : "SQLITE_DONE"}] 164 165 do_test ${testid}.6 { 166 sqlite3_finalize $::STMT 167 } [expr {$mallocFailedAfterStep ? "SQLITE_NOMEM" : "SQLITE_OK"}] 168 169 if {$::mallocFailed == 0 && $mallocFailedAfterStep == 0} { 170 sqlite_malloc_fail 0 171 set go 0 172 } 173 } 174} 175 176execsql { 177 CREATE TABLE tbl( 178 the_first_reasonably_long_column_name that_also_has_quite_a_lengthy_type 179 ); 180 INSERT INTO tbl VALUES( 181 'An extra long string. Far too long to be stored in NBFS bytes.' 182 ); 183} 184 185do_stmt_test 1 "SELECT * FROM tbl" 186 187sqlite_malloc_fail 0 188finish_test 189 190