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