094f44
From 6b06304c2a46e17a6dc4402eadc75ccac24da893 Mon Sep 17 00:00:00 2001
094f44
From: Ondrej Dubaj <odubaj@redhat.com>
094f44
Date: Fri, 17 Jan 2020 13:03:54 +0100
094f44
Subject: [PATCH] When an error occurs while rewriting the parser tree for
094f44
 window functions in the sqlite3WindowRewrite() routine, make sure that
094f44
 pParse->nErr is set, and make sure that this shuts down any subsequent code
094f44
 generation that might depend on the transformations that were implemented. 
094f44
 This fixes a problem discovered by the Yongheng and Rui fuzzer.
094f44
094f44
---
094f44
 src/expr.c    | 1 +
094f44
 src/vdbeaux.c | 3 ++-
094f44
 src/window.c  | 5 +++++
094f44
 3 files changed, 8 insertions(+), 1 deletion(-)
094f44
094f44
diff --git a/src/expr.c b/src/expr.c
094f44
index d4eb9de..b081ca2 100644
094f44
--- a/src/expr.c
094f44
+++ b/src/expr.c
094f44
@@ -344,6 +344,7 @@ static int codeCompare(
094f44
   int addr;
094f44
   CollSeq *p4;
094f44
 
094f44
+  if( pParse->nErr ) return 0;
094f44
   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
094f44
   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
094f44
   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
094f44
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
094f44
index f1496a3..b74141b 100644
094f44
--- a/src/vdbeaux.c
094f44
+++ b/src/vdbeaux.c
094f44
@@ -1160,7 +1160,8 @@ void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
094f44
 */
094f44
 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
094f44
   assert( p->nOp>0 || p->aOp==0 );
094f44
-  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
094f44
+  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed
094f44
+          || p->pParse->nErr>0 );
094f44
   if( p->nOp ){
094f44
     assert( p->aOp );
094f44
     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
094f44
diff --git a/src/window.c b/src/window.c
094f44
index f5deae9..56c0145 100644
094f44
--- a/src/window.c
094f44
+++ b/src/window.c
094f44
@@ -843,6 +843,11 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){
094f44
     if( db->mallocFailed ) rc = SQLITE_NOMEM;
094f44
   }
094f44
 
094f44
+  if( rc && pParse->nErr==0 ){
094f44
+    assert( pParse->db->mallocFailed );
094f44
+    return SQLITE_NOMEM;
094f44
+  }
094f44
+
094f44
   return rc;
094f44
 }
094f44
 
094f44
-- 
094f44
2.19.1
094f44