| 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 |
sqlite3_fts5_may_be_corrupt 0 |
| 128 |
finish_test |