sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / fts5 / test / fts5corruptA.test
| 1 | # 2026 May 11 |
| 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 | |
| 13 | source [file join [file dirname [info script]] fts5_common.tcl] |
| 14 | set testprefix fts5corruptA |
| 15 | |
| 16 | # If SQLITE_ENABLE_FTS5 is not defined, omit this file. |
| 17 | ifcapable !fts5 { |
| 18 | finish_test |
| 19 | return |
| 20 | } |
| 21 | sqlite3_fts5_may_be_corrupt 1 |
| 22 | |
| 23 | do_execsql_test 1.0 { |
| 24 | CREATE VIRTUAL TABLE t USING fts5(x, detail='full'); |
| 25 | INSERT INTO t(t, rank) VALUES('pgsz', 32); |
| 26 | } |
| 27 | |
| 28 | set big [string repeat "a " 200] |
| 29 | do_execsql_test 1.1 { |
| 30 | INSERT INTO t(rowid, x) VALUES(1, $big) |
| 31 | } |
| 32 | |
| 33 | do_test 1.2 { |
| 34 | db eval { |
| 35 | SELECT min(rowid) AS base_rowid, count(*) AS page_count FROM t_data |
| 36 | WHERE rowid>1000 |
| 37 | } {} |
| 38 | } {} |
| 39 | |
| 40 | do_test 1.3 { |
| 41 | for {set ii 0} {$ii < 5} {incr ii} { |
| 42 | db eval { |
| 43 | INSERT INTO t_data(rowid, block) |
| 44 | VALUES( $base_rowid + $page_count + $ii, zeroblob(4) ); |
| 45 | } |
| 46 | } |
| 47 | db eval { |
| 48 | INSERT INTO t_data(rowid, block) |
| 49 | VALUES( $base_rowid + $page_count + 5, |
| 50 | unhex('00000080' || 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC') ); |
| 51 | } |
| 52 | set {} {} |
| 53 | } {} |
| 54 | |
| 55 | db close |
| 56 | |
| 57 | do_test 1.4 { |
| 58 | set hex [hexio_read test.db 0 [file size test.db]] |
| 59 | |
| 60 | set off [string first "023061018310" $hex] |
| 61 | set hex [string replace $hex $off [expr $off+11] 023061018370] |
| 62 | hexio_write test.db 0 $hex |
| 63 | } {6144} |
| 64 | |
| 65 | sqlite3 db test.db |
| 66 | |
| 67 | do_catchsql_test 1.5 { |
| 68 | SELECT rowid FROM t WHERE t MATCH 'a' |
| 69 | } {1 {fts5: corruption found reading blob 137438953481 from table "t"}} |
| 70 | |
| 71 | #------------------------------------------------------------------------- |
| 72 | reset_db |
| 73 | |
| 74 | do_execsql_test 2.0 { |
| 75 | CREATE VIRTUAL TABLE t1 USING fts5(a); |
| 76 | BEGIN; |
| 77 | INSERT INTO t1(rowid, a) VALUES(1, 'aaaa'); |
| 78 | INSERT INTO t1(rowid, a) VALUES(2, 'bbbb'); |
| 79 | INSERT INTO t1(rowid, a) VALUES(3, 'cccc'); |
| 80 | COMMIT; |
| 81 | } |
| 82 | |
| 83 | db close |
| 84 | |
| 85 | do_test 2.1 { |
| 86 | set hex [hexio_read test.db 0 [file size test.db]] |
| 87 | set off [string first "010463636363" $hex] |
| 88 | set hex [string replace $hex $off [expr $off+11] 017F63636363] |
| 89 | hexio_write test.db 0 $hex |
| 90 | set {} {} |
| 91 | } {} |
| 92 | |
| 93 | sqlite3 db test.db |
| 94 | |
| 95 | do_catchsql_test 2.2 { |
| 96 | SELECT rowid FROM t1('cccccccccccccccccccccccccccccccccccccccccccccccc'); |
| 97 | } {1 {fts5: corruption on page 1, segment 1, table "t1"}} |
| 98 | |
| 99 | #------------------------------------------------------------------------- |
| 100 | reset_db |
| 101 | do_execsql_test 3.0 { |
| 102 | CREATE VIRTUAL TABLE t1 USING fts5(a, b, c); |
| 103 | INSERT INTO t1 VALUES( |
| 104 | 'hello', |
| 105 | 'alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whiskey xray yankee zulu', |
| 106 | 'ant bear cat dog elephant fox gorilla hippo iguana jaguar kangaroo lion monkey newt octopus panda quail rabbit snake turtle unicorn vulture walrus xerus yak zebra' |
| 107 | ); |
| 108 | |
| 109 | UPDATE t1_data SET block = unhex( |
| 110 | -- Preserve bytes 0-168: header through hello's nSz field |
| 111 | hex(substr(block, 1, 169)) |
| 112 | -- Inject 448 bytes of 0xFF: corrupted poslist + subsequent term data |
| 113 | || replace(hex(zeroblob(448)), '00', 'FF') |
| 114 | -- Preserve 16 bytes of page index needed for hello lookup |
| 115 | || hex(substr(block, 618, 16)) |
| 116 | -- Corrupt remaining 37 footer bytes (not needed for hello lookup) |
| 117 | || replace(hex(zeroblob(37)), '00', 'FF') |
| 118 | ) |
| 119 | WHERE id = 137438953473; |
| 120 | } |
| 121 | |
| 122 | do_test 3.1 { |
| 123 | execsql { SELECT * FROM t1 WHERE t1 MATCH '{a b}: hello' } |
| 124 | set {} {} |
| 125 | } {} |
| 126 | |
| 127 | #------------------------------------------------------------------------- |
| 128 | reset_db |
| 129 | do_execsql_test 4.0 { |
| 130 | CREATE VIRTUAL TABLE t1 USING fts5(content); |
| 131 | INSERT INTO t1(t1, rank) VALUES('pgsz', 32); |
| 132 | BEGIN; |
| 133 | WITH s(i) AS ( |
| 134 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<5000 |
| 135 | ) |
| 136 | INSERT INTO t1(content) SELECT 'xyzterm' FROM s; |
| 137 | COMMIT; |
| 138 | INSERT INTO t1(t1) VALUES('optimize'); |
| 139 | } |
| 140 | |
| 141 | do_test 4.1 { |
| 142 | db eval { |
| 143 | SELECT rowid AS a, hex(block) AS block FROM t1_data |
| 144 | WHERE ((rowid>>36) & 0x01)==1 |
| 145 | AND ((rowid>>31) & 0x1F)==0 |
| 146 | } { |
| 147 | set n [string length $block] |
| 148 | set block [string range $block 0 11] |
| 149 | append block [string repeat "00" [expr ($n/2)-7-2]] |
| 150 | append block "8080" |
| 151 | db eval { |
| 152 | UPDATE t1_data SET block = unhex($block) WHERE rowid = $a |
| 153 | } |
| 154 | } |
| 155 | } {} |
| 156 | |
| 157 | do_catchsql_test 4.2 { |
| 158 | SELECT count(*) FROM ( |
| 159 | SELECT rowid FROM t1 WHERE t1 MATCH 'xyzterm' ORDER BY rowid ASC |
| 160 | ) |
| 161 | } {0 5000} |
| 162 | |
| 163 | do_catchsql_test 4.3 { |
| 164 | SELECT count(*) FROM ( |
| 165 | SELECT rowid FROM t1 WHERE t1 MATCH 'xyzterm' ORDER BY rowid DESC |
| 166 | ) |
| 167 | } {0 4987} |
| 168 | |
| 169 | do_test 4.4 { |
| 170 | db eval { |
| 171 | SELECT rowid AS a, hex(block) AS block FROM t1_data |
| 172 | WHERE ((rowid>>36) & 0x01)==1 |
| 173 | AND ((rowid>>31) & 0x1F)==0 |
| 174 | } { |
| 175 | set n [string length $block] |
| 176 | set block [string range $block 0 $n-5] |
| 177 | append block [string repeat "00" 2] |
| 178 | db eval { |
| 179 | UPDATE t1_data SET block = unhex($block) WHERE rowid = $a |
| 180 | } |
| 181 | } |
| 182 | } {} |
| 183 | |
| 184 | do_catchsql_test 4.5 { |
| 185 | SELECT count(*) FROM ( |
| 186 | SELECT rowid FROM t1 WHERE t1 MATCH 'xyzterm' ORDER BY rowid ASC |
| 187 | ) |
| 188 | } {0 5000} |
| 189 | |
| 190 | do_catchsql_test 4.6 { |
| 191 | SELECT count(*) FROM ( |
| 192 | SELECT rowid FROM t1 WHERE t1 MATCH 'xyzterm' ORDER BY rowid DESC |
| 193 | ) |
| 194 | } {0 4879} |
| 195 | |
| 196 | #------------------------------------------------------------------------- |
| 197 | reset_db |
| 198 | |
| 199 | do_execsql_test 5.0 { |
| 200 | CREATE VIRTUAL TABLE t USING fts5(x); |
| 201 | INSERT INTO t(t,rank) VALUES('pgsz', 64); |
| 202 | WITH s(i) AS ( |
| 203 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<80 |
| 204 | ) |
| 205 | INSERT INTO t SELECT 'shared word' || (i%10) || ' shared shared' FROM s; |
| 206 | INSERT INTO t(t) VALUES('optimize'); |
| 207 | } |
| 208 | |
| 209 | set lLeaf [db eval { SELECT id FROM t_data }] |
| 210 | db_save_and_close |
| 211 | |
| 212 | foreach leaf $lLeaf { |
| 213 | db_restore_and_reopen |
| 214 | set leaf [expr $leaf] |
| 215 | set hex [db one { |
| 216 | SELECT hex(block) FROM t_data WHERE id=$leaf |
| 217 | }] |
| 218 | |
| 219 | # Replace the first 4 bytes of each leaf page with the size of the leaf in |
| 220 | # bytes plus 50 as a 2 byte integer, followed by 0x7FFF. |
| 221 | # |
| 222 | set nn [expr [string length $hex]/2] |
| 223 | set first "[format %.4x [expr $nn+50]]7FFF" |
| 224 | set hex [string replace $hex 0 7 $first] |
| 225 | |
| 226 | db eval { UPDATE t_data SET block=unhex($hex) WHERE id=$leaf } |
| 227 | do_test 5.1.$leaf { |
| 228 | catchsql { |
| 229 | SELECT rowid FROM t WHERE t MATCH 'shared' ORDER BY rowid DESC; |
| 230 | } |
| 231 | set {} {} |
| 232 | } {} |
| 233 | |
| 234 | do_test 5.1.$leaf.2 { |
| 235 | catchsql { |
| 236 | PRAGMA integrity_check; |
| 237 | } |
| 238 | set {} {} |
| 239 | } {} |
| 240 | } |
| 241 | |
| 242 | #------------------------------------------------------------------------- |
| 243 | reset_db |
| 244 | |
| 245 | do_execsql_test 6.0 { |
| 246 | CREATE VIRTUAL TABLE t USING fts5(x, content='', contentless_delete=1); |
| 247 | INSERT INTO t(rowid,x) |
| 248 | VALUES(1,'a b'), (2,'c d'), (3,'e f'), (4,'g h'), (5,'i j'); |
| 249 | INSERT INTO t(t) VALUES('optimize'); |
| 250 | DELETE FROM t WHERE rowid=2; |
| 251 | } |
| 252 | |
| 253 | do_test 6.1 { |
| 254 | db eval { |
| 255 | SELECT rowid AS rid, hex(block) AS blk |
| 256 | FROM t_data WHERE rowid>1_000_000_000_000 |
| 257 | } {} |
| 258 | |
| 259 | set blk [string replace $blk 8 15 20000000] |
| 260 | execsql { |
| 261 | UPDATE t_data SET block = unhex($blk) WHERE rowid=$rid |
| 262 | } |
| 263 | } {} |
| 264 | |
| 265 | do_execsql_test 6.2 { |
| 266 | DELETE FROM t WHERE rowid=3; |
| 267 | } |
| 268 | |
| 269 | #------------------------------------------------------------------------- |
| 270 | reset_db |
| 271 | do_execsql_test 7.0 { |
| 272 | CREATE VIRTUAL TABLE ft USING fts5(x); |
| 273 | INSERT INTO ft(ft,rank) VALUES('pgsz',64); |
| 274 | WITH s(i) AS ( |
| 275 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10 |
| 276 | ) |
| 277 | INSERT INTO ft SELECT 'a b b a c c d' FROM s; |
| 278 | } |
| 279 | |
| 280 | set B "00000040FFFF61010402050104020501040205010402050104020501040205010402050104020501040205010402050101620104030301040303010403030104042b" |
| 281 | set N "306162[string repeat 00 5000]" |
| 282 | |
| 283 | do_execsql_test 7.1 { |
| 284 | UPDATE ft_data SET block = unhex($B) WHERE id = 137438953473; |
| 285 | UPDATE ft_idx SET term=unhex($N) WHERE segid=1 AND pgno=2; |
| 286 | } |
| 287 | |
| 288 | do_execsql_test 7.2 { |
| 289 | PRAGMA integrity_check |
| 290 | } { |
| 291 | {fts5: corruption found reading blob 137438953473 from table "ft"} |
| 292 | } |
| 293 | |
| 294 | sqlite3_fts5_may_be_corrupt 0 |
| 295 | finish_test |
| 296 | |
| 297 |