8d0611
From 959187c0035564df5b5a597a3b1ff6c59c49d8e1 Mon Sep 17 00:00:00 2001
8d0611
From: Ondrej Dubaj <odubaj@redhat.com>
8d0611
Date: Fri, 3 Jan 2020 13:13:18 +0100
8d0611
Subject: [PATCH] More improvements to shadow table corruption detection in
8d0611
 FTS3.
8d0611
8d0611
---
8d0611
 ext/fts3/fts3.c       |  4 ++++
8d0611
 ext/fts3/fts3Int.h    | 16 ++++++++++++++++
8d0611
 ext/fts3/fts3_write.c | 12 ++++++++++--
8d0611
 3 files changed, 30 insertions(+), 2 deletions(-)
8d0611
8d0611
diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c
8d0611
index c00a13f..2a61d10 100644
8d0611
--- a/ext/fts3/fts3.c
8d0611
+++ b/ext/fts3/fts3.c
8d0611
@@ -1355,6 +1355,10 @@ static int fts3InitVtab(
8d0611
   fts3DatabasePageSize(&rc, p);
8d0611
   p->nNodeSize = p->nPgsz-35;
8d0611
 
8d0611
+#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
8d0611
+  p->nMergeCount = FTS3_MERGE_COUNT;
8d0611
+#endif
8d0611
+
8d0611
   /* Declare the table schema to SQLite. */
8d0611
   fts3DeclareVtab(&rc, p);
8d0611
 
8d0611
diff --git a/ext/fts3/fts3Int.h b/ext/fts3/fts3Int.h
8d0611
index b19064c..bd0edfe 100644
8d0611
--- a/ext/fts3/fts3Int.h
8d0611
+++ b/ext/fts3/fts3Int.h
8d0611
@@ -254,8 +254,24 @@ struct Fts3Table {
8d0611
   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
8d0611
   int mxSavepoint;       /* Largest valid xSavepoint integer */
8d0611
 #endif
8d0611
+
8d0611
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
8d0611
+  /* True to disable the incremental doclist optimization. This is controled
8d0611
+  ** by special insert command 'test-no-incr-doclist'.  */
8d0611
+  int bNoIncrDoclist;
8d0611
+
8d0611
+  /* Number of segments in a level */
8d0611
+  int nMergeCount;
8d0611
+#endif
8d0611
 };
8d0611
 
8d0611
+/* Macro to find the number of segments to merge */
8d0611
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
8d0611
+# define MergeCount(P) ((P)->nMergeCount)
8d0611
+#else
8d0611
+# define MergeCount(P) FTS3_MERGE_COUNT
8d0611
+#endif
8d0611
+
8d0611
 /*
8d0611
 ** When the core wants to read from the virtual table, it creates a
8d0611
 ** virtual table cursor (an instance of the following structure) using
8d0611
diff --git a/ext/fts3/fts3_write.c b/ext/fts3/fts3_write.c
8d0611
index 269d1dd..a557aba 100644
8d0611
--- a/ext/fts3/fts3_write.c
8d0611
+++ b/ext/fts3/fts3_write.c
8d0611
@@ -1130,7 +1130,7 @@ static int fts3AllocateSegdirIdx(
8d0611
     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
8d0611
     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
8d0611
     */
8d0611
-    if( iNext>=FTS3_MERGE_COUNT ){
8d0611
+    if( iNext>=MergeCount(p) ){
8d0611
       fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
8d0611
       rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
8d0611
       *piIdx = 0;
8d0611
@@ -4002,6 +4002,10 @@ static int fts3IncrmergeLoad(
8d0611
       int i;
8d0611
       int nHeight = (int)aRoot[0];
8d0611
       NodeWriter *pNode;
8d0611
+      if( nHeight<1 || nHeight>FTS_MAX_APPENDABLE_HEIGHT ){
8d0611
+        sqlite3_reset(pSelect);
8d0611
+        return FTS_CORRUPT_VTAB;
8d0611
+      }
8d0611
 
8d0611
       pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
8d0611
       pWriter->iStart = iStart;
8d0611
@@ -4728,7 +4732,7 @@ static int fts3DoIncrmerge(
8d0611
   const char *zParam              /* Nul-terminated string containing "A,B" */
8d0611
 ){
8d0611
   int rc;
8d0611
-  int nMin = (FTS3_MERGE_COUNT / 2);
8d0611
+  int nMin = (MergeCount(p) / 2);
8d0611
   int nMerge = 0;
8d0611
   const char *z = zParam;
8d0611
 
8d0611
@@ -5049,6 +5053,10 @@ static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
8d0611
   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
8d0611
     p->nMaxPendingData = atoi(&zVal[11]);
8d0611
     rc = SQLITE_OK;
8d0611
+  }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
8d0611
+    int v = atoi(&zVal[11]);
8d0611
+    if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v;
8d0611
+    rc = SQLITE_OK;
8d0611
 #endif
8d0611
   }else{
8d0611
     rc = SQLITE_ERROR;
8d0611
-- 
8d0611
2.19.1
8d0611