corruptG.test 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # 2013-08-01
  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. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. set testprefix corruptG
  15. # Do not use a codec for tests in this file, as the database file is
  16. # manipulated directly using tcl scripts (using the [hexio_write] command).
  17. #
  18. do_not_use_codec
  19. # Create a simple database with a single entry. Then corrupt the
  20. # header-size varint on the index payload so that it maps into a
  21. # negative number. Try to use the database.
  22. #
  23. do_execsql_test 1.1 {
  24. PRAGMA page_size=512;
  25. CREATE TABLE t1(a,b,c);
  26. INSERT INTO t1(rowid,a,b,c) VALUES(52,'abc','xyz','123');
  27. CREATE INDEX t1abc ON t1(a,b,c);
  28. }
  29. set idxroot [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1abc'}]
  30. # Corrupt the file
  31. db close
  32. hexio_write test.db [expr {$idxroot*512 - 15}] 888080807f
  33. sqlite3 db test.db
  34. # Try to use the file.
  35. do_test 1.2 {
  36. catchsql {
  37. SELECT c FROM t1 WHERE a>'abc';
  38. }
  39. } {0 {}}
  40. do_test 1.3 {
  41. catchsql {
  42. PRAGMA integrity_check
  43. }
  44. } {0 ok}
  45. do_test 1.4 {
  46. catchsql {
  47. SELECT c FROM t1 ORDER BY a;
  48. }
  49. } {1 {database disk image is malformed}}
  50. # Corrupt the same file in a slightly different way. Make the record header
  51. # sane, but corrupt one of the serial_type value to indicate a huge payload
  52. # such that the payload begins in allocated space but overflows the buffer.
  53. #
  54. db close
  55. hexio_write test.db [expr {$idxroot*512-15}] 0513ff7f01
  56. sqlite3 db test.db
  57. do_test 2.1 {
  58. catchsql {
  59. SELECT rowid FROM t1 WHERE a='abc' and b='xyz123456789XYZ';
  60. }
  61. # The following test result is brittle. The point above is to try to
  62. # force a buffer overread by a corrupt database file. If we get an
  63. # incorrect answer from a corrupt database file, that is OK. If the
  64. # result below changes, that just means that "undefined behavior" has
  65. # changed.
  66. } {0 52}
  67. finish_test