SQLite User Forum

32-bit overflow in DBSTAT virtual table
Login

32-bit overflow in DBSTAT virtual table

(1) By Patrick Rapin (prapin) on 2022-12-02 14:39:27 [source]

I have a big SQLite3 database, mostly containing two tables of more than 18 GB each. When I tried to have disk statistics with the DBSTAT feature I just discovered, the pgsize column clearly overflows:

SQLite version 3.40.0 2022-11-16 12:10:08
Enter ".help" for usage hints or "select help();" for functions reference.
sqlite> .header on
sqlite> select * from dbstat('main',1);
name|path|pageno|pagetype|ncell|payload|unused|mx_payload|pgoffset|pgsize
sqlite_schema||1||5|559|3408|131||4096
metadata||1||9|341|3711|49||4096
tiles_compr||4869094||3081160|1402847824|1301544727|784237||-1531027456
macro_tiles||4548767||56502|1408865160|24080855|1001258||1451880448
metadata_idx1||1||6|185|3885|33||4096
metadata_idx2||1||3|52|4027|18||4096
sqlite> 

The last column contains wrong values for both tiles_compr and macro_tiles. We can clearly verify that the problem is due to a 32-bit overflow, because:

  • 4869094 * 4096 = 19943809024 = 0x4'A4BE'6000, while -1531027456 = 0xA4BE'6000 in 32-bit
  • 4548767 * 4096 = 18631749632 = 0x4'5689'F000, while 1451880448 = 0x5689F000

SQLite should have performed the computation for pgsize as 64-bit integer, but from the result we see that the value somewhere got truncated to a 32-bit signed integer.

(2) By Larry Brasfield (larrybr) on 2022-12-02 15:43:34 in reply to 1 [link] [source]

Thanks for the report and diagnosis. Fixed here.