sl@0: # 2005 November 30 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # This file contains tests to ensure that the library handles malloc() failures sl@0: # correctly. The emphasis in this file is on sqlite3_column_XXX() APIs. sl@0: # sl@0: # $Id: malloc4.test,v 1.10 2008/02/18 22:24:58 drh Exp $ sl@0: sl@0: #--------------------------------------------------------------------------- sl@0: # NOTES ON EXPECTED BEHAVIOUR sl@0: # sl@0: # [193] When a memory allocation failure occurs during sqlite3_column_name(), sl@0: # sqlite3_column_name16(), sqlite3_column_decltype(), or sl@0: # sqlite3_column_decltype16() the function shall return NULL. sl@0: # sl@0: #--------------------------------------------------------------------------- sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: source $testdir/malloc_common.tcl sl@0: sl@0: # Only run these tests if memory debugging is turned on. sl@0: if {!$MEMDEBUG} { sl@0: puts "Skipping malloc4 tests: not compiled with -DSQLITE_MEMDEBUG..." sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: ifcapable !utf16 { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: proc do_stmt_test {id sql} { sl@0: set ::sql $sql sl@0: set go 1 sl@0: for {set n 0} {$go} {incr n} { sl@0: set testid "malloc4-$id.$n" sl@0: sl@0: # Prepare the statement sl@0: do_test ${testid}.1 { sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: expr [string length $::STMT] > 0 sl@0: } {1} sl@0: sl@0: # Set the Nth malloc() to fail. sl@0: sqlite3_memdebug_fail $n -repeat 0 sl@0: sl@0: # Test malloc failure in the _name(), _name16(), decltype() and sl@0: # decltype16() APIs. Calls that occur after the malloc() failure should sl@0: # return NULL. No error is raised though. sl@0: # sl@0: # ${testid}.2.1 - Call _name() sl@0: # ${testid}.2.2 - Call _name16() sl@0: # ${testid}.2.3 - Call _name() sl@0: # ${testid}.2.4 - Check that the return values of the above three calls are sl@0: # consistent with each other and with the simulated sl@0: # malloc() failures. sl@0: # sl@0: # Because the code that implements the _decltype() and _decltype16() APIs sl@0: # is the same as the _name() and _name16() implementations, we don't worry sl@0: # about explicitly testing them. sl@0: # sl@0: do_test ${testid}.2.1 { sl@0: set mf1 [expr [sqlite3_memdebug_pending] < 0] sl@0: set ::name8 [sqlite3_column_name $::STMT 0] sl@0: set mf2 [expr [sqlite3_memdebug_pending] < 0] sl@0: expr {$mf1 == $mf2 || $::name8 == ""} sl@0: } {1} sl@0: do_test ${testid}.2.2 { sl@0: set mf1 [expr [sqlite3_memdebug_pending] < 0] sl@0: set ::name16 [sqlite3_column_name16 $::STMT 0] sl@0: set ::name16 [encoding convertfrom unicode $::name16] sl@0: set ::name16 [string range $::name16 0 end-1] sl@0: set mf2 [expr [sqlite3_memdebug_pending] < 0] sl@0: expr {$mf1 == $mf2 || $::name16 == ""} sl@0: } {1} sl@0: do_test ${testid}.2.3 { sl@0: set mf1 [expr [sqlite3_memdebug_pending] < 0] sl@0: set ::name8_2 [sqlite3_column_name $::STMT 0] sl@0: set mf2 [expr [sqlite3_memdebug_pending] < 0] sl@0: expr {$mf1 == $mf2 || $::name8_2 == ""} sl@0: } {1} sl@0: set ::mallocFailed [expr [sqlite3_memdebug_pending] < 0] sl@0: do_test ${testid}.2.4 { sl@0: expr { sl@0: $::name8 == $::name8_2 && $::name16 == $::name8 && !$::mallocFailed || sl@0: $::name8 == $::name8_2 && $::name16 == "" && $::mallocFailed || sl@0: $::name8 == $::name16 && $::name8_2 == "" && $::mallocFailed || sl@0: $::name8_2 == $::name16 && $::name8 == "" && $::mallocFailed sl@0: } sl@0: } {1} sl@0: sl@0: # Step the statement so that we can call _text() and _text16(). Before sl@0: # running sqlite3_step(), make sure that malloc() is not about to fail. sl@0: # Memory allocation failures that occur within sqlite3_step() are tested sl@0: # elsewhere. sl@0: set mf [sqlite3_memdebug_pending] sl@0: sqlite3_memdebug_fail -1 sl@0: do_test ${testid}.3 { sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: sqlite3_memdebug_fail $mf sl@0: sl@0: # Test for malloc() failures within _text() and _text16(). sl@0: # sl@0: do_test ${testid}.4.1 { sl@0: set ::text8 [sqlite3_column_text $::STMT 0] sl@0: set mf [expr [sqlite3_memdebug_pending] < 0 && !$::mallocFailed] sl@0: expr {$mf==0 || $::text8 == ""} sl@0: } {1} sl@0: do_test ${testid}.4.2 { sl@0: set ::text16 [sqlite3_column_text16 $::STMT 0] sl@0: set ::text16 [encoding convertfrom unicode $::text16] sl@0: set ::text16 [string range $::text16 0 end-1] sl@0: set mf [expr [sqlite3_memdebug_pending] < 0 && !$::mallocFailed] sl@0: expr {$mf==0 || $::text16 == ""} sl@0: } {1} sl@0: do_test ${testid}.4.3 { sl@0: set ::text8_2 [sqlite3_column_text $::STMT 0] sl@0: set mf [expr [sqlite3_memdebug_pending] < 0 && !$::mallocFailed] sl@0: expr {$mf==0 || $::text8_2 == "" || ($::text16 == "" && $::text8 != "")} sl@0: } {1} sl@0: sl@0: # Test for malloc() failures within _int(), _int64() and _real(). The only sl@0: # way this can occur is if the string has to be translated from UTF-16 to sl@0: # UTF-8 before being converted to a numeric value. sl@0: do_test ${testid}.4.4.1 { sl@0: set mf [sqlite3_memdebug_pending] sl@0: sqlite3_memdebug_fail -1 sl@0: sqlite3_column_text16 $::STMT 0 sl@0: sqlite3_memdebug_fail $mf sl@0: sqlite3_column_int $::STMT 0 sl@0: } {0} sl@0: do_test ${testid}.4.5 { sl@0: set mf [sqlite3_memdebug_pending] sl@0: sqlite3_memdebug_fail -1 sl@0: sqlite3_column_text16 $::STMT 0 sl@0: sqlite3_memdebug_fail $mf sl@0: sqlite3_column_int64 $::STMT 0 sl@0: } {0} sl@0: sl@0: do_test ${testid}.4.6 { sl@0: set mf [sqlite3_memdebug_pending] sl@0: sqlite3_memdebug_fail -1 sl@0: sqlite3_column_text16 $::STMT 0 sl@0: sqlite3_memdebug_fail $mf sl@0: sqlite3_column_double $::STMT 0 sl@0: } {0.0} sl@0: sl@0: set mallocFailedAfterStep [expr \ sl@0: [sqlite3_memdebug_pending] < 0 && !$::mallocFailed sl@0: ] sl@0: sl@0: sqlite3_memdebug_fail -1 sl@0: # Test that if a malloc() failed the next call to sqlite3_step() returns sl@0: # SQLITE_ERROR. If malloc() did not fail, it should return SQLITE_DONE. sl@0: # sl@0: do_test ${testid}.5 { sl@0: sqlite3_step $::STMT sl@0: } [expr {$mallocFailedAfterStep ? "SQLITE_ERROR" : "SQLITE_DONE"}] sl@0: sl@0: do_test ${testid}.6 { sl@0: sqlite3_finalize $::STMT sl@0: } [expr {$mallocFailedAfterStep ? "SQLITE_NOMEM" : "SQLITE_OK"}] sl@0: sl@0: if {$::mallocFailed == 0 && $mallocFailedAfterStep == 0} { sl@0: sqlite3_memdebug_fail -1 sl@0: set go 0 sl@0: } sl@0: } sl@0: } sl@0: sl@0: execsql { sl@0: CREATE TABLE tbl( sl@0: the_first_reasonably_long_column_name that_also_has_quite_a_lengthy_type sl@0: ); sl@0: INSERT INTO tbl VALUES( sl@0: 'An extra long string. Far too long to be stored in NBFS bytes.' sl@0: ); sl@0: } sl@0: sl@0: do_stmt_test 1 "SELECT * FROM tbl" sl@0: sl@0: sqlite3_memdebug_fail -1 sl@0: finish_test