blob: 34404b211b625d65580dacde99e60a3cecfb0e1b [file] [log] [blame]
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Darwin Huang <huangdarwin@chromium.org>
Date: Fri, 3 May 2019 14:46:06 -0700
Subject: [PATCH 3/8] Fix memory-leak/segfault
Backports https://www.sqlite.org/src/info/a9b90aa12eecdd9f
Bug: 958351
---
third_party/sqlite/patched/src/vdbe.c | 7 +++--
third_party/sqlite/patched/test/with3.test | 36 ++++++++++++++++++++++
2 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/third_party/sqlite/patched/src/vdbe.c b/third_party/sqlite/patched/src/vdbe.c
index 2acc5873c531..b563627af83a 100644
--- a/third_party/sqlite/patched/src/vdbe.c
+++ b/third_party/sqlite/patched/src/vdbe.c
@@ -264,7 +264,7 @@ static VdbeCursor *allocateCursor(
** is clear. Otherwise, if this is an ephemeral cursor created by
** OP_OpenDup, the cursor will not be closed and will still be part
** of a BtShared.pCursor list. */
- p->apCsr[iCur]->isEphemeral = 0;
+ if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
p->apCsr[iCur] = 0;
}
@@ -3686,7 +3686,10 @@ case OP_OpenEphemeral: {
if( pCx ){
/* If the ephermeral table is already open, erase all existing content
** so that the table is empty again, rather than creating a new table. */
- rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
+ assert( pCx->isEphemeral );
+ if( pCx->pBtx ){
+ rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
+ }
}else{
pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
if( pCx==0 ) goto no_mem;
diff --git a/third_party/sqlite/patched/test/with3.test b/third_party/sqlite/patched/test/with3.test
index 0fd21aa92e48..f651207d2901 100644
--- a/third_party/sqlite/patched/test/with3.test
+++ b/third_party/sqlite/patched/test/with3.test
@@ -130,4 +130,40 @@ do_eqp_test 3.2.2 {
`--SEARCH TABLE w1 USING INTEGER PRIMARY KEY (rowid=?)
}
+do_execsql_test 4.0 {
+ WITH t5(t5col1) AS (
+ SELECT (
+ WITH t3(t3col1) AS (
+ WITH t2 AS (
+ WITH t1 AS (SELECT 1 AS c1 GROUP BY 1)
+ SELECT a.c1 FROM t1 AS a, t1 AS b
+ WHERE anoncol1 = 1
+ )
+ SELECT (SELECT 1 FROM t2) FROM t2
+ )
+ SELECT t3col1 FROM t3 WHERE t3col1
+ ) FROM (SELECT 1 AS anoncol1)
+ )
+ SELECT t5col1, t5col1 FROM t5
+} {1 1}
+do_execsql_test 4.1 {
+ SELECT EXISTS (
+ WITH RECURSIVE Table0 AS (
+ WITH RECURSIVE Table0(Col0) AS (SELECT ALL 1 )
+ SELECT ALL (
+ WITH RECURSIVE Table0 AS (
+ WITH RECURSIVE Table0 AS (
+ WITH RECURSIVE Table0 AS (SELECT DISTINCT 1 GROUP BY 1 )
+ SELECT DISTINCT * FROM Table0 NATURAL INNER JOIN Table0
+ WHERE Col0 = 1
+ )
+ SELECT ALL (SELECT DISTINCT * FROM Table0) FROM Table0 WHERE Col0 = 1
+ )
+ SELECT ALL * FROM Table0 NATURAL INNER JOIN Table0
+ ) FROM Table0 )
+ SELECT DISTINCT * FROM Table0 NATURAL INNER JOIN Table0
+ );
+} {1}
+
+
finish_test
--
2.21.0