malloc4.test 6.5 KB

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