SQLite User Forum

Assertion failure in Cast Zeroblob
Login

Assertion failure in Cast Zeroblob

(1) By Song Liu (songliu) on 2023-03-25 19:00:43 [source]

I found an assertion failure while SQLite (latest, a13c01d076) executes the following queries.

CREATE TABLE v0(c0 CHAR(0) COLLATE binary);
CREATE INDEX i ON v0(c0, '' REGEXP 0);
INSERT INTO v0 (c0, c0) VALUES(0,0);
ANALYZE;
SELECT 1 FROM v0 WHERE v0.c0 < CAST(zeroblob(0) AS TEXT)

Here are the outputs:

sqlite3: sqlite3.c:175330: binCollFunc: Assertion `pKey1 && pKey2' failed.
[1]    3495920 abort      ./sqlite3 < poc

Here is the result of bisecting:

 13 BAD     2022-01-01 17:21:55 c76a4c0b3cb62501 CURRENT                       
 12 GOOD    2022-01-01 12:26:01 0dd6b5fccd554ebe        

The assertion failure may be caused by the optimizations. SQLite crashes by default with all optimizations enabled. If I disable the SQLITE_Stat4 optimization, SQLite works well.

CREATE TABLE v0(c0 CHAR(0) COLLATE binary);
CREATE INDEX i ON v0(c0, '' REGEXP 0);
INSERT INTO v0 (c0, c0) VALUES(0,0);
ANALYZE;

.testctrl optimizations 0x00000800;
.print '-- disable optimization of SQLITE_Stat4'
SELECT 1 FROM v0 WHERE v0.c0 < CAST(zeroblob(0) AS TEXT)

My compilation flags:

    export CFLAGS="-g -O0 -DSQLITE_DEBUG 
                -DSQLITE_ENABLE_TREETRACE 
                -DSQLITE_ENABLE_WHERETRACE
                -DSQLITE_ENABLE_CURSOR_HINTS 
                -DSQLITE_COUNTOFVIEW_OPTIMIZATION 
                -DSQLITE_ENABLE_STAT4" 

(2) By Song Liu (songliu) on 2023-03-25 23:24:00 in reply to 1 [link] [source]

It seems the bug is fixed at commit 5c8dd8dfcaab9c36. Thanks for your work!