Skip to content

Commit

Permalink
Fix faulty asserts in the code generator.
Browse files Browse the repository at this point in the history
Ticket [c52b09c7f38903b1]
  • Loading branch information
D. Richard Hipp committed Jul 24, 2019
1 parent 1c1c585 commit d238a9c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -5869,8 +5869,15 @@ int sqlite3Select(
** technically harmless for it to be generated multiple times. The
** following assert() will detect if something changes to cause
** the same subquery to be coded multiple times, as a signal to the
** developers to try to optimize the situation. */
assert( pItem->addrFillSub==0 );
** developers to try to optimize the situation.
**
** Update 2019-07-24:
** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
** The dbsqlfuzz fuzzer found a case where the same subquery gets
** coded twice. So this assert() now becomes a testcase(). It should
** be very rare, though.
*/
testcase( pItem->addrFillSub!=0 );

/* Increment Parse.nHeight by the height of the largest expression
** tree referred to by this, the parent select. The child select
Expand Down Expand Up @@ -5944,7 +5951,7 @@ int sqlite3Select(
int retAddr;
struct SrcList_item *pPrior;

assert( pItem->addrFillSub==0 );
testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
pItem->regReturn = ++pParse->nMem;
topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
pItem->addrFillSub = topAddr+1;
Expand Down
65 changes: 65 additions & 0 deletions test/select1.test
Original file line number Diff line number Diff line change
Expand Up @@ -1100,4 +1100,69 @@ do_execsql_test select1-17.3 {
UNION ALL SELECT * FROM t2 WHERE y=3 ORDER BY y,z LIMIT 4);
} {1 2 3}

# 2019-07-24 Ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311
#
do_execsql_test select1-18.1 {
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(c);
CREATE TABLE t2(x PRIMARY KEY, y);
INSERT INTO t1(c) VALUES(123);
INSERT INTO t2(x) VALUES(123);
SELECT x FROM t2, t1 WHERE x BETWEEN c AND null OR x AND
x IN ((SELECT x FROM (SELECT x FROM t2, t1
WHERE x BETWEEN (SELECT x FROM (SELECT x COLLATE rtrim
FROM t2, t1 WHERE x BETWEEN c AND null
OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null
OR x AND x IN (c)) AND null
OR NOT EXISTS(SELECT -4.81 FROM t1, t2 WHERE x BETWEEN c AND null
OR x AND x IN ((SELECT x FROM (SELECT x FROM t2, t1
WHERE x BETWEEN (SELECT x FROM (SELECT x BETWEEN c AND null
OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null
OR x AND x IN (c)) AND null
OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null
OR x AND x IN (c)))) AND x IN (c)
), t1 WHERE x BETWEEN c AND null
OR x AND x IN (c)));
} {}
do_execsql_test select1-18.2 {
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(c);
CREATE TABLE t2(x PRIMARY KEY, y);
INSERT INTO t1(c) VALUES(123);
INSERT INTO t2(x) VALUES(123);
SELECT x FROM t2, t1 WHERE x BETWEEN c AND (c+1) OR x AND
x IN ((SELECT x FROM (SELECT x FROM t2, t1
WHERE x BETWEEN (SELECT x FROM (SELECT x COLLATE rtrim
FROM t2, t1 WHERE x BETWEEN c AND (c+1)
OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1)
OR x AND x IN (c)) AND (c+1)
OR NOT EXISTS(SELECT -4.81 FROM t1, t2 WHERE x BETWEEN c AND (c+1)
OR x AND x IN ((SELECT x FROM (SELECT x FROM t2, t1
WHERE x BETWEEN (SELECT x FROM (SELECT x BETWEEN c AND (c+1)
OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1)
OR x AND x IN (c)) AND (c+1)
OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1)
OR x AND x IN (c)))) AND x IN (c)
), t1 WHERE x BETWEEN c AND (c+1)
OR x AND x IN (c)));
} {123}
do_execsql_test select1-18.3 {
SELECT 1 FROM t1 WHERE (
SELECT 2 FROM t2 WHERE (
SELECT 3 FROM (
SELECT x FROM t2 WHERE x=c OR x=(SELECT x FROM (VALUES(0)))
) WHERE x>c OR x=c
)
);
} {1}
do_execsql_test select1-18.4 {
SELECT 1 FROM t1, t2 WHERE (
SELECT 3 FROM (
SELECT x FROM t2 WHERE x=c OR x=(SELECT x FROM (VALUES(0)))
) WHERE x>c OR x=c
);
} {1}

finish_test

0 comments on commit d238a9c

Please sign in to comment.