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