sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2020-12-16 |
| 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 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
| 16 | set testprefix corruptN |
| 17 | |
| 18 | # These tests deal with corrupt database files |
| 19 | # |
| 20 | database_may_be_corrupt |
| 21 | |
| 22 | reset_db |
| 23 | do_test 1.0 { |
| 24 | sqlite3 db {} |
| 25 | db deserialize [decode_hexdb { |
| 26 | .open --hexdb |
| 27 | | size 4096 pagesize 512 filename sql024239.txt.db |
| 28 | | page 1 offset 0 |
| 29 | | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. |
| 30 | | 16: 02 00 01 01 00 40 20 20 00 00 00 0c 00 00 00 07 .....@ ........ |
| 31 | | 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ |
| 32 | | 48: 00 00 00 00 89 00 00 04 00 10 00 01 0a 00 00 01 ................ |
| 33 | | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ |
| 34 | | 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ |
| 35 | | 112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... |
| 36 | | 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61 .............1ta |
| 37 | | 272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54 blet4t4.CREATE T |
| 38 | | 288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11 ABLE t4(x)*..... |
| 39 | | 304: 01 3f 69 6e 64 65 78 74 33 78 74 33 05 43 52 45 .?indext3xt3.CRE |
| 40 | | 320: 41 54 45 20 49 4e 44 45 58 20 74 33 78 20 4f 4e ATE INDEX t3x ON |
| 41 | | 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein |
| 42 | | 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE |
| 43 | | 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t |
| 44 | | 384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61 2(c,d)(......=ta |
| 45 | | 400: 62 6c 65 74 33 74 33 07 43 52 45 41 54 45 20 54 blet3t3.CREATE T |
| 46 | | 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) |
| 47 | | 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t |
| 48 | | 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t |
| 49 | | 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... |
| 50 | | 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA |
| 51 | | 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) |
| 52 | | page 2 offset 512 |
| 53 | | 0: 0d 00 00 00 04 01 41 00 01 fa 01 f3 01 de 01 cf ......A......... |
| 54 | | 160: 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 .. ............. |
| 55 | | 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ |
| 56 | | 464: 04 03 17 17 73 65 76 65 6e 65 69 67 68 74 13 03 ....seveneight.. |
| 57 | | 480: 03 07 07 40 14 00 00 00 00 00 00 40 18 00 00 00 ...@.......@.... |
| 58 | | 496: 00 00 00 05 02 03 01 01 03 04 04 01 03 09 01 02 ................ |
| 59 | | page 3 offset 1024 |
| 60 | | 0: 0d 00 00 00 08 01 54 00 01 f7 01 ec 01 c5 01 aa ......T......... |
| 61 | | 16: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ |
| 62 | | 112: 00 00 dd 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ |
| 63 | | 336: 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 74 ...........eight |
| 64 | | 352: 65 69 67 68 74 73 65 76 65 6e 73 65 76 65 6e 25 eightsevenseven% |
| 65 | | 368: 07 05 07 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ |
| 66 | | 432: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ |
| 67 | | 480: 00 00 0f 04 17 17 01 65 69 67 68 74 65 69 67 68 .......eighteigh |
| 68 | | 496: 74 08 15 04 07 07 01 40 18 00 00 00 00 00 00 40 t......@.......@ |
| 69 | | page 4 offset 1536 |
| 70 | | 0: 18 00 00 00 00 00 00 07 07 04 01 01 01 04 04 06 ................ |
| 71 | | 16: 07 04 01 01 01 02 02 05 0f 04 17 17 01 73 6d 76 .............smv |
| 72 | | 32: 65 6e 65 69 67 68 74 04 15 04 07 07 01 40 14 00 eneight......@.. |
| 73 | | page 5 offset 2048 |
| 74 | | 0: 0a 00 00 00 08 01 96 00 01 fa 01 c4 01 f2 01 bc ................ |
| 75 | | 16: 01 dc 01 e1 01 96 01 cc 00 00 00 00 00 00 00 00 ................ |
| 76 | | 160: 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00 00 ......2......... |
| 77 | | 368: 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 ................ |
| 78 | | 400: 00 00 00 00 00 00 0f 04 17 17 01 85 69 67 68 74 ............ight |
| 79 | | 416: 65 69 67 68 74 08 15 04 07 07 01 40 18 00 00 00 eight......@.... |
| 80 | | 432: 00 00 00 40 18 00 00 00 00 00 00 07 07 04 01 01 ...@............ |
| 81 | | 448: 01 04 04 06 07 04 01 01 01 02 02 05 0f 04 17 17 ................ |
| 82 | | 464: 01 73 6d 76 65 6e 65 69 67 68 74 04 15 04 07 07 .smveneight..... |
| 83 | | 480: 01 40 14 00 00 00 00 00 00 40 18 00 00 00 00 00 .@.......@...... |
| 84 | | 496: 00 03 07 04 01 01 01 03 04 02 05 04 03 01 09 02 ................ |
| 85 | | page 6 offset 2560 |
| 86 | | 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ |
| 87 | | 16: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 ................ |
| 88 | | 304: 00 00 00 26 00 00 00 00 00 00 00 00 00 00 00 00 ...&............ |
| 89 | | page 7 offset 3072 |
| 90 | | 0: 0d 00 00 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec ................ |
| 91 | | 16: 01 e0 01 d4 01 cb 01 c2 00 00 00 00 00 00 00 00 ................ |
| 92 | | 128: 00 00 00 00 00 00 00 00 00 00 00 00 00 20 00 04 ............. .. |
| 93 | | 384: 00 00 00 00 00 00 00 00 00 07 08 02 17 65 69 fc .............ei. |
| 94 | | 400: 68 74 07 07 02 17 65 69 67 68 74 0a fb fd f8 bf ht....eight..... |
| 95 | | 416: e7 ff ff ff 00 00 00 0a 05 02 07 40 18 00 00 00 ...........@.... |
| 96 | | 432: 00 00 00 03 04 02 01 04 03 03 02 01 04 03 02 01 ................ |
| 97 | | 448: ff ff ff ff ff ff 00 00 00 00 00 00 00 00 00 00 ................ |
| 98 | | end sql024239.txt.db |
| 99 | }]} {} |
| 100 | |
| 101 | do_catchsql_test 1.1 { |
| 102 | VACUUM; |
| 103 | } {1 {database disk image is malformed}} |
| 104 | |
| 105 | # 2021-04-05 dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 |
| 106 | do_test 2.0 { |
| 107 | sqlite3 db {} |
| 108 | db deserialize [decode_hexdb { |
| 109 | | size 16384 pagesize 4096 filename c-b92b.txt.db |
| 110 | | page 1 offset 0 |
| 111 | | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. |
| 112 | | 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 04 .....@ ........ |
| 113 | | 32: 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00 04 ................ |
| 114 | | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ |
| 115 | | 96: 00 00 00 00 0d 0f f8 00 04 0f 12 00 0f 91 0f d3 ................ |
| 116 | | 112: 0f 67 0f 12 00 00 00 00 00 00 00 00 00 00 00 00 .g.............. |
| 117 | | 3856: 00 00 53 04 07 1b 13 11 08 81 0d 74 72 69 67 67 ..S........trigg |
| 118 | | 3872: 65 72 74 72 30 74 31 43 52 45 41 54 45 20 54 52 ertr0t1CREATE TR |
| 119 | | 3888: 49 47 47 45 52 20 74 72 30 20 44 45 4c 45 54 45 IGGER tr0 DELETE |
| 120 | | 3904: 20 4f 4e 20 74 31 20 42 45 47 49 4e 0a 20 20 55 ON t1 BEGIN. U |
| 121 | | 3920: 50 44 41 54 45 20 74 31 20 53 45 54 20 62 20 3d PDATE t1 SET b = |
| 122 | | 3936: 20 61 3b 0a 45 4e 44 28 03 06 17 11 11 01 3d 69 a;.END(......=i |
| 123 | | 3952: 6e 64 65 78 69 30 74 31 04 43 52 45 41 54 45 20 ndexi0t1.CREATE |
| 124 | | 3968: 49 4e 44 45 58 20 69 30 20 4f 4e 20 74 31 28 62 INDEX i0 ON t1(b |
| 125 | | 3984: 29 40 01 06 17 11 11 01 6d 74 61 62 6c 65 74 31 )@......mtablet1 |
| 126 | | 4000: 74 31 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 t1.CREATE TABLE |
| 127 | | 4016: 74 31 28 61 20 55 4e 49 51 55 45 20 4f 4e 20 43 t1(a UNIQUE ON C |
| 128 | | 4032: 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45 2c ONFLICT REPLACE, |
| 129 | | 4048: 20 62 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b)#...7...index |
| 130 | | 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex |
| 131 | | 4080: 5f 74 31 5f 31 74 31 03 00 00 00 08 00 00 00 00 _t1_1t1......... |
| 132 | | page 2 offset 4096 |
| 133 | | 0: 0d 00 00 00 02 0f 00 00 00 00 00 00 00 00 00 00 ................ |
| 134 | | 4080: 00 00 05 02 03 01 01 09 0d 05 01 03 01 01 04 0c ................ |
| 135 | | page 3 offset 8192 |
| 136 | | 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ |
| 137 | | 4080: 00 00 00 00 00 05 03 01 01 09 02 04 03 01 09 04 ................ |
| 138 | | page 4 offset 12288 |
| 139 | | 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ |
| 140 | | 4080: 00 00 00 00 00 05 03 01 01 0d 02 04 03 00 00 00 ................ |
| 141 | | end c-b92b.txt.db |
| 142 | }]} {} |
| 143 | |
| 144 | |
| 145 | reset_db |
| 146 | if {![info exists ::G(perm:presql)]} { |
| 147 | do_execsql_test 3.0 { |
| 148 | CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y); |
| 149 | PRAGMA writable_schema = 1; |
| 150 | UPDATE sqlite_schema |
| 151 | SET sql = 'CREATE TABLE sqlite_sequence(name-seq)' |
| 152 | WHERE name = 'sqlite_sequence'; |
| 153 | } |
| 154 | db close |
| 155 | sqlite3 db test.db |
| 156 | do_catchsql_test 3.1 { |
| 157 | PRAGMA writable_schema = 1; |
| 158 | INSERT INTO t1(y) VALUES('abc'); |
| 159 | } {1 {database disk image is malformed}} |
| 160 | reset_db |
| 161 | |
| 162 | do_execsql_test 4.1 { |
| 163 | CREATE TABLE x1(a INTEGER PRIMARY KEY, b UNIQUE, c UNIQUE); |
| 164 | INSERT INTO x1 VALUES(1, 1, 2); |
| 165 | INSERT INTO x1 VALUES(2, 2, 3); |
| 166 | INSERT INTO x1 VALUES(3, 3, 4); |
| 167 | INSERT INTO x1 VALUES(4, 5, 6); |
| 168 | PRAGMA writable_schema = 1; |
| 169 | |
| 170 | UPDATE sqlite_schema SET rootpage = ( |
| 171 | SELECT rootpage FROM sqlite_schema WHERE name = 'sqlite_autoindex_x1_2' |
| 172 | ) WHERE name = 'sqlite_autoindex_x1_1'; |
| 173 | } |
| 174 | |
| 175 | db close |
| 176 | sqlite3 db test.db |
| 177 | breakpoint |
| 178 | do_catchsql_test 4.2 { |
| 179 | PRAGMA writable_schema = 1; |
| 180 | REPLACE INTO x1 VALUES(5, 2, 3); |
| 181 | } {0 {}} |
| 182 | |
| 183 | } |
| 184 | |
| 185 | #------------------------------------------------------------------------- |
| 186 | |
| 187 | reset_db |
| 188 | |
| 189 | ifcapable json1&&vtab { |
| 190 | db func strreplace strreplace |
| 191 | proc strreplace {orig a b} { |
| 192 | string map [list $a $b] $orig |
| 193 | } |
| 194 | |
| 195 | do_execsql_test 5.0 { |
| 196 | CREATE TABLE t1(a, b); |
| 197 | CREATE INDEX t1a ON t1(a); |
| 198 | CREATE INDEX t1b ON t1(b); |
| 199 | |
| 200 | PRAGMA writable_schema = 1; |
| 201 | UPDATE sqlite_schema |
| 202 | SET sql = strreplace(sql, 't1', 'json_each') |
| 203 | WHERE type='index'; |
| 204 | } |
| 205 | |
| 206 | # Do not run this tests if there is any presql (SQL run from within |
| 207 | # the [sqlite3] command) configured. In this case the schema is parsed |
| 208 | # before the "PRAGMA writable_schema" command is executed and the |
| 209 | # script throws and exception. |
| 210 | if {[info exists ::G(perm:presql)]==0 || $::G(perm:presql)==""} { |
| 211 | db close |
| 212 | sqlite3 db test.db |
| 213 | |
| 214 | do_execsql_test 5.1 { |
| 215 | PRAGMA writable_schema = 1; |
| 216 | SELECT * FROM t1 |
| 217 | } |
| 218 | } |
| 219 | }; # ifcapable json1&&vtab |
| 220 | |
| 221 | #------------------------------------------------------------------------- |
| 222 | reset_db |
| 223 | |
| 224 | do_execsql_test 6.0 { |
| 225 | PRAGMA auto_vacuum = 0; |
| 226 | PRAGMA page_size=1024; |
| 227 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
| 228 | INSERT INTO t1(b) VALUES(zeroblob(300)),(zeroblob(300)),(zeroblob(300)),(zeroblob(300)); |
| 229 | CREATE TABLE t2(a); |
| 230 | CREATE TRIGGER t1tr BEFORE UPDATE ON t1 BEGIN DELETE FROM t2; END; |
| 231 | PRAGMA writable_schema=ON; |
| 232 | UPDATE sqlite_schema SET rootpage=3 WHERE rowid=2; |
| 233 | PRAGMA writable_schema=RESET; |
| 234 | INSERT INTO t2 VALUES('active'),('boomer'),('atom'),('atomic'), |
| 235 | ('alpha channel backup abandon test aback boomer atom alpha active'); |
| 236 | } |
| 237 | do_catchsql_test 6.1 { |
| 238 | UPDATE t1 SET b=zeroblob(299); |
| 239 | } {1 {database disk image is malformed}} |
| 240 | |
| 241 | reset_db |
| 242 | do_execsql_test 6.2 { |
| 243 | -- Make "t1" a large table. Large enough that the children of the root |
| 244 | -- node are interior nodes. |
| 245 | PRAGMA page_size = 1024; |
| 246 | PRAGMA auto_vacuum = 0; |
| 247 | CREATE TABLE t1(x); |
| 248 | WITH s(i) AS ( |
| 249 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<500 |
| 250 | ) |
| 251 | INSERT INTO t1 SELECT zeroblob(300) FROM s; |
| 252 | |
| 253 | CREATE TABLE t2(y); |
| 254 | CREATE TRIGGER tr BEFORE UPDATE ON t1 BEGIN |
| 255 | DELETE FROM t2; |
| 256 | END; |
| 257 | |
| 258 | -- Set the root of table t2 to 137 - the leftmost child of the root of t1. |
| 259 | PRAGMA writable_schema = ON; |
| 260 | UPDATE sqlite_schema SET rootpage = 137 WHERE name='t2'; |
| 261 | PRAGMA writable_schema = RESET; |
| 262 | } |
| 263 | |
| 264 | do_catchsql_test 6.3 { |
| 265 | -- Run an UPDATE on t1 that will hit a child of page 136. Have the trigger |
| 266 | -- clear page 136 and its children. Assert fails. |
| 267 | UPDATE t1 SET x='hello world' WHERE rowid=1; |
| 268 | } {1 {database disk image is malformed}} |
| 269 | |
| 270 | #------------------------------------------------------------------------- |
| 271 | reset_db |
| 272 | do_execsql_test 7.0 { |
| 273 | BEGIN; |
| 274 | CREATE TABLE p1(x PRIMARY KEY); |
| 275 | CREATE TABLE c1(y); |
| 276 | |
| 277 | PRAGMA schema_version = 0; |
| 278 | PRAGMA writable_schema = RESET; |
| 279 | |
| 280 | INSERT INTO c1 VALUES(1000); |
| 281 | ROLLBACK; |
| 282 | } |
| 283 | |
| 284 | do_execsql_test 7.1 { |
| 285 | PRAGMA table_info = p1; |
| 286 | } {0 x {} 0 {} 1} |
| 287 | |
| 288 | do_catchsql_test 7.2 { |
| 289 | SELECT * FROM p1; |
| 290 | } {1 {database disk image is malformed}} |
| 291 | |
| 292 | do_catchsql_test 7.3 { |
| 293 | PRAGMA integrity_check |
| 294 | } {1 {database disk image is malformed}} |
| 295 | |
| 296 | |
| 297 | finish_test |