1bb246c4dSdan# 2012 January 12 2bb246c4dSdan# 3bb246c4dSdan# The author disclaims copyright to this source code. In place of 4bb246c4dSdan# a legal notice, here is a blessing: 5bb246c4dSdan# 6bb246c4dSdan# May you do good and not evil. 7bb246c4dSdan# May you find forgiveness for yourself and forgive others. 8bb246c4dSdan# May you share freely, never taking more than you give. 9bb246c4dSdan# 10bb246c4dSdan#*********************************************************************** 11bb246c4dSdan# 12bb246c4dSdan 13bb246c4dSdanset testdir [file dirname $argv0] 14bb246c4dSdansource $testdir/tester.tcl 15bb246c4dSdanset testprefix corruptF 16bb246c4dSdan 17bb246c4dSdan# Do not use a codec for tests in this file, as the database file is 18bb246c4dSdan# manipulated directly using tcl scripts (using the [hexio_write] command). 19bb246c4dSdan# 20bb246c4dSdando_not_use_codec 21bb246c4dSdan 22*09fe6143Sdrh# These tests deal with corrupt database files 23*09fe6143Sdrh# 24*09fe6143Sdrhdatabase_may_be_corrupt 25*09fe6143Sdrh 26bb246c4dSdanproc str {i} { format %08d $i } 27bb246c4dSdan 28bb246c4dSdan# Create a 6 page database containing a single table - t1. Table t1 29bb246c4dSdan# consists of page 2 (the root page) and pages 5 and 6 (leaf pages). 30bb246c4dSdan# Database pages 3 and 4 are on the free list. 31bb246c4dSdan# 32bb246c4dSdanproc create_test_db {} { 33bb246c4dSdan catch { db close } 34bb246c4dSdan forcedelete test.db 35bb246c4dSdan sqlite3 db test.db 36bb246c4dSdan db func str str 37bb246c4dSdan execsql { 38bb246c4dSdan PRAGMA auto_vacuum = 0; 39bb246c4dSdan PRAGMA page_size = 1024; 40bb246c4dSdan CREATE TABLE t1(x); /* root page = 2 */ 41bb246c4dSdan CREATE TABLE t2(x); /* root page = 3 */ 42bb246c4dSdan CREATE TABLE t3(x); /* root page = 4 */ 43bb246c4dSdan 44bb246c4dSdan INSERT INTO t1 VALUES(str(1)); 45bb246c4dSdan INSERT INTO t1 SELECT str(rowid+1) FROM t1; 46bb246c4dSdan INSERT INTO t1 SELECT str(rowid+2) FROM t1; 47bb246c4dSdan INSERT INTO t1 SELECT str(rowid+4) FROM t1; 48bb246c4dSdan INSERT INTO t1 SELECT str(rowid+8) FROM t1; 49bb246c4dSdan INSERT INTO t1 SELECT str(rowid+16) FROM t1; 50bb246c4dSdan INSERT INTO t1 SELECT str(rowid+32) FROM t1; 51bb246c4dSdan INSERT INTO t1 SELECT str(rowid+64) FROM t1; 52bb246c4dSdan DROP TABLE t2; 53bb246c4dSdan DROP TABLE t3; 54bb246c4dSdan } 55bb246c4dSdan db close 56bb246c4dSdan} 57bb246c4dSdan 58bb246c4dSdando_test 1.1 { create_test_db } {} 59bb246c4dSdan 60bb246c4dSdan# Check the db is as we expect. 6 pages in total, with 3 and 4 on the free 61bb246c4dSdan# list. Page 3 is the free list trunk and page 4 is a leaf. 62bb246c4dSdan# 63bb246c4dSdando_test 1.2 { file size test.db } [expr 6*1024] 64bb246c4dSdando_test 1.3 { hexio_read test.db 32 4 } 00000003 65bb246c4dSdando_test 1.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004 66bb246c4dSdan 67bb246c4dSdan# Change the free-list entry to page 6 and reopen the db file. 68bb246c4dSdando_test 1.5 { 69bb246c4dSdan hexio_write test.db [expr 2*1024 + 8] 00000006 70bb246c4dSdan sqlite3 db test.db 71bb246c4dSdan} {} 72bb246c4dSdan 73bb246c4dSdan# Now create a new table in the database file. The root of the new table 74bb246c4dSdan# is page 6, which is also the right-most leaf page in table t1. 75bb246c4dSdan# 76bb246c4dSdando_execsql_test 1.6 { 77bb246c4dSdan CREATE TABLE t4(x); 78bb246c4dSdan SELECT * FROM sqlite_master; 79bb246c4dSdan} { 80bb246c4dSdan table t1 t1 2 {CREATE TABLE t1(x)} 81bb246c4dSdan table t4 t4 6 {CREATE TABLE t4(x)} 82bb246c4dSdan} 83bb246c4dSdan 84bb246c4dSdan# At one point this was causing an assert to fail. 85bb246c4dSdan# 86bb246c4dSdan# This statement opens a cursor on table t1 and does a full table scan. As 87bb246c4dSdan# each row is visited, it is copied into table t4. There is no temporary 88bb246c4dSdan# table. 89bb246c4dSdan# 90bb246c4dSdan# When the t1 cursor reaches page 6 (which is both the right-most leaf of 91bb246c4dSdan# t1 and the root of t4), it continues to iterate through the keys within 92bb246c4dSdan# it (which at this point are keys that have been inserted into t4). And 93bb246c4dSdan# for each row visited, another row is inserted into page 6 - it being the 94bb246c4dSdan# root page of t4. Eventually, page 6 becomes full and the height of the 95bb246c4dSdan# b-tree for table t4 increased. From the point of view of the t1 cursor, 96bb246c4dSdan# this unexpectedly reduces the number of keys on page 6 in the middle of 97bb246c4dSdan# its iteration, which causes an assert() to fail. 98bb246c4dSdan# 99bb246c4dSdandb_save_and_close 100bb246c4dSdanif 1 { 101bb246c4dSdanfor {set i 0} {$i < 128} {incr i} { 102bb246c4dSdan db_restore_and_reopen 103bb246c4dSdan do_test 1.7.$i { 104bb246c4dSdan set res [ 105bb246c4dSdan catchsql { INSERT INTO t4 SELECT x FROM t1 WHERE rowid>$i } 106bb246c4dSdan ] 107bb246c4dSdan if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} { 108bb246c4dSdan set res "" 109bb246c4dSdan } 110bb246c4dSdan set res 111bb246c4dSdan } {} 112bb246c4dSdan} 113bb246c4dSdan} 114bb246c4dSdan 115bb246c4dSdando_test 2.1 { create_test_db } {} 116bb246c4dSdando_test 2.2 { file size test.db } [expr 6*1024] 117bb246c4dSdando_test 2.3 { hexio_read test.db 32 4 } 00000003 118bb246c4dSdando_test 2.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004 119bb246c4dSdan 120bb246c4dSdan# Change the free-list entry to page 5 and reopen the db file. 121bb246c4dSdando_test 2.5 { 122bb246c4dSdan hexio_write test.db [expr 2*1024 + 8] 00000005 123bb246c4dSdan sqlite3 db test.db 124bb246c4dSdan} {} 125bb246c4dSdan 126bb246c4dSdan# Now create a new table in the database file. The root of the new table 127bb246c4dSdan# is page 5, which is also the right-most leaf page in table t1. 128bb246c4dSdan# 129bb246c4dSdando_execsql_test 2.6 { 130bb246c4dSdan CREATE TABLE t4(x); 131bb246c4dSdan SELECT * FROM sqlite_master; 132bb246c4dSdan} { 133bb246c4dSdan table t1 t1 2 {CREATE TABLE t1(x)} 134bb246c4dSdan table t4 t4 5 {CREATE TABLE t4(x)} 135bb246c4dSdan} 136bb246c4dSdan 137bb246c4dSdandb_save_and_close 138bb246c4dSdanfor {set i 127} {$i >= 0} {incr i -1} { 139bb246c4dSdan db_restore_and_reopen 140bb246c4dSdan do_test 2.7.$i { 141bb246c4dSdan set res [ 142bb246c4dSdan catchsql { 143bb246c4dSdan INSERT INTO t4 SELECT x FROM t1 WHERE rowid<$i ORDER BY rowid DESC 144bb246c4dSdan } 145bb246c4dSdan ] 146bb246c4dSdan if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} { 147bb246c4dSdan set res "" 148bb246c4dSdan } 149bb246c4dSdan set res 150bb246c4dSdan } {} 151bb246c4dSdan} 152bb246c4dSdan 153bb246c4dSdanfinish_test 154