From af72ceaf22e73fd78e32ef439c1869292b94aaa1 Mon Sep 17 00:00:00 2001
From: Dan Kennedy <danielk1977@gmail.com>
Date: Thu, 20 Dec 2018 15:04:38 +0000
Subject: [PATCH 0631/1009] Fix a segfault caused by using the RAISE function
 incorrectly (library now returns an error instead of crashing).

https://github.com/mackyle/sqlite/commit/af72ceaf22e73fd78e32ef439c1869292b94aaa1

---
 src/expr.c         |  2 +-
 test/triggerC.test | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/expr.c b/src/expr.c
index 5d36502..b1a06bd 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -4745,7 +4745,7 @@ int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
     }
     return 2;
   }
-  if( pA->op!=pB->op ){
+  if( pA->op!=pB->op || pA->op==TK_RAISE ){
     if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
       return 1;
     }
diff --git a/test/triggerC.test b/test/triggerC.test
index 3e47521..49d4eca 100644
--- a/test/triggerC.test
+++ b/test/triggerC.test
@@ -1042,4 +1042,20 @@ do_execsql_test 15.2.1 {
 do_execsql_test 15.2.2 { SELECT * FROM x2;       } {1 2 3 4}
 do_execsql_test 15.2.3 { SELECT * FROM """x2"""; } {3 11 x y}
 
+#-------------------------------------------------------------------------
+# At one point queries such as the following were causing segfaults.
+#
+do_catchsql_test 16.1 {
+  SELECT raise(ABORT, 'msg') FROM sqlite_master 
+  UNION SELECT 1 
+  ORDER BY raise(IGNORE);
+} {1 {1st ORDER BY term does not match any column in the result set}}
+
+do_catchsql_test 16.2 {
+  SELECT count(*) FROM sqlite_master 
+  GROUP BY raise(IGNORE) 
+  HAVING raise(ABORT, 'msg');
+} {1 {RAISE() may only be used within a trigger-program}}
+
 finish_test
+
-- 
1.8.3.1

