Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: third_party/sqlite/patches/0024-fts2-Fix-numerous-out-of-bounds-bugs-reading-corrupt.patch

Issue 885473002: [sql] Rewrite sqlite patching "system". (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 diff -ru ext-orig/fts2/fts2.c ext/fts2/fts2.c 1 From b6c1652dc114f32af6843cc0336a6976233a6a0e Mon Sep 17 00:00:00 2001
2 --- ext-orig/fts2/fts2.c» 2009-09-04 13:37:41.000000000 -0700 2 From: Scott Hess <shess@chromium.org>
3 +++ ext/fts2/fts2.c» 2009-09-30 14:48:14.000000000 -0700 3 Date: Mon, 22 Dec 2014 14:26:55 -0800
4 @@ -37,6 +37,20 @@ 4 Subject: [PATCH 24/24] [fts2] Fix numerous out-of-bounds bugs reading corrupt
5 ** This is an SQLite module implementing full-text search. 5 database.
6 */ 6
7 7 Fix numerous bugs in fts2 where a corrupt fts2 database could cause
8 +/* TODO(shess): To make it easier to spot changes without groveling 8 out-of-bounds reads and writes.
9 +** through changelogs, I've defined GEARS_FTS2_CHANGES to call them 9
10 +** out, and I will document them here. On imports, these changes 10 Original review URL: http://codereview.chromium.org/216026
11 +** should be reviewed to make sure they are still present, or are 11 ---
12 +** dropped as appropriate. 12 third_party/sqlite/src/ext/fts2/fts2.c | 751 ++++++++++++++++++++++-----------
13 +** 13 1 file changed, 514 insertions(+), 237 deletions(-)
14 +** SQLite core adds the custom function fts2_tokenizer() to be used 14
15 +** for defining new tokenizers. The second parameter is a vtable 15 diff --git a/third_party/sqlite/src/ext/fts2/fts2.c b/third_party/sqlite/src/ext /fts2/fts2.c
16 +** pointer encoded as a blob. Obviously this cannot be exposed to 16 index d5587b3..36d14ff 100644
17 +** Gears callers for security reasons. It could be suppressed in the 17 --- a/third_party/sqlite/src/ext/fts2/fts2.c
18 +** authorizer, but for now I have simply commented the definition out. 18 +++ b/third_party/sqlite/src/ext/fts2/fts2.c
19 +*/ 19 @@ -447,30 +447,41 @@ static int putVarint(char *p, sqlite_int64 v){
20 +#define GEARS_FTS2_CHANGES 1
21 +
22 /*
23 ** The code in this file is only compiled if:
24 **
25 @@ -326,8 +326,10 @@
26 #include "fts2_hash.h"
27 #include "fts2_tokenizer.h"
28 #include "sqlite3.h"
29 -#include "sqlite3ext.h"
30 -SQLITE_EXTENSION_INIT1
31 +#ifndef SQLITE_CORE
32 +# include "sqlite3ext.h"
33 + SQLITE_EXTENSION_INIT1
34 +#endif
35
36
37 /* TODO(shess) MAN, this thing needs some refactoring. At minimum, it
38 @@ -335,6 +349,16 @@
39 # define TRACE(A)
40 #endif
41
42 +#if 0
43 +/* Useful to set breakpoints. See main.c sqlite3Corrupt(). */
44 +static int fts2Corrupt(void){
45 + return SQLITE_CORRUPT;
46 +}
47 +# define SQLITE_CORRUPT_BKPT fts2Corrupt()
48 +#else
49 +# define SQLITE_CORRUPT_BKPT SQLITE_CORRUPT
50 +#endif
51 +
52 /* It is not safe to call isspace(), tolower(), or isalnum() on
53 ** hi-bit-set characters. This is the same solution used in the
54 ** tokenizer.
55 @@ -423,30 +447,41 @@
56 /* Read a 64-bit variable-length integer from memory starting at p[0]. 20 /* Read a 64-bit variable-length integer from memory starting at p[0].
57 * Return the number of bytes read, or 0 on error. 21 * Return the number of bytes read, or 0 on error.
58 * The value is stored in *v. */ 22 * The value is stored in *v. */
59 -static int getVarint(const char *p, sqlite_int64 *v){ 23 -static int getVarint(const char *p, sqlite_int64 *v){
60 +static int getVarintSafe(const char *p, sqlite_int64 *v, int max){ 24 +static int getVarintSafe(const char *p, sqlite_int64 *v, int max){
61 const unsigned char *q = (const unsigned char *) p; 25 const unsigned char *q = (const unsigned char *) p;
62 sqlite_uint64 x = 0, y = 1; 26 sqlite_uint64 x = 0, y = 1;
63 - while( (*q & 0x80) == 0x80 ){ 27 - while( (*q & 0x80) == 0x80 ){
64 + if( max>VARINT_MAX ) max = VARINT_MAX; 28 + if( max>VARINT_MAX ) max = VARINT_MAX;
65 + while( max && (*q & 0x80) == 0x80 ){ 29 + while( max && (*q & 0x80) == 0x80 ){
(...skipping 29 matching lines...) Expand all
95 return ret; 59 return ret;
96 } 60 }
97 61
98 +static int getVarint32(const char* p, int *pi){ 62 +static int getVarint32(const char* p, int *pi){
99 + return getVarint32Safe(p, pi, VARINT_MAX); 63 + return getVarint32Safe(p, pi, VARINT_MAX);
100 +} 64 +}
101 + 65 +
102 /*******************************************************************/ 66 /*******************************************************************/
103 /* DataBuffer is used to collect data into a buffer in piecemeal 67 /* DataBuffer is used to collect data into a buffer in piecemeal
104 ** fashion. It implements the usual distinction between amount of 68 ** fashion. It implements the usual distinction between amount of
105 @@ -615,7 +650,7 @@ 69 @@ -639,7 +650,7 @@ typedef struct DLReader {
106 70
107 static int dlrAtEnd(DLReader *pReader){ 71 static int dlrAtEnd(DLReader *pReader){
108 assert( pReader->nData>=0 ); 72 assert( pReader->nData>=0 );
109 - return pReader->nData==0; 73 - return pReader->nData==0;
110 + return pReader->nData<=0; 74 + return pReader->nData<=0;
111 } 75 }
112 static sqlite_int64 dlrDocid(DLReader *pReader){ 76 static sqlite_int64 dlrDocid(DLReader *pReader){
113 assert( !dlrAtEnd(pReader) ); 77 assert( !dlrAtEnd(pReader) );
114 @@ -639,7 +674,8 @@ 78 @@ -663,7 +674,8 @@ static int dlrAllDataBytes(DLReader *pReader){
115 */ 79 */
116 static const char *dlrPosData(DLReader *pReader){ 80 static const char *dlrPosData(DLReader *pReader){
117 sqlite_int64 iDummy; 81 sqlite_int64 iDummy;
118 - int n = getVarint(pReader->pData, &iDummy); 82 - int n = getVarint(pReader->pData, &iDummy);
119 + int n = getVarintSafe(pReader->pData, &iDummy, pReader->nElement); 83 + int n = getVarintSafe(pReader->pData, &iDummy, pReader->nElement);
120 + if( !n ) return NULL; 84 + if( !n ) return NULL;
121 assert( !dlrAtEnd(pReader) ); 85 assert( !dlrAtEnd(pReader) );
122 return pReader->pData+n; 86 return pReader->pData+n;
123 } 87 }
124 @@ -649,7 +685,7 @@ 88 @@ -673,7 +685,7 @@ static int dlrPosDataLen(DLReader *pReader){
125 assert( !dlrAtEnd(pReader) ); 89 assert( !dlrAtEnd(pReader) );
126 return pReader->nElement-n; 90 return pReader->nElement-n;
127 } 91 }
128 -static void dlrStep(DLReader *pReader){ 92 -static void dlrStep(DLReader *pReader){
129 +static int dlrStep(DLReader *pReader){ 93 +static int dlrStep(DLReader *pReader){
130 assert( !dlrAtEnd(pReader) ); 94 assert( !dlrAtEnd(pReader) );
131 95
132 /* Skip past current doclist element. */ 96 /* Skip past current doclist element. */
133 @@ -658,32 +694,48 @@ 97 @@ -682,32 +694,48 @@ static void dlrStep(DLReader *pReader){
134 pReader->nData -= pReader->nElement; 98 pReader->nData -= pReader->nElement;
135 99
136 /* If there is more data, read the next doclist element. */ 100 /* If there is more data, read the next doclist element. */
137 - if( pReader->nData!=0 ){ 101 - if( pReader->nData!=0 ){
138 + if( pReader->nData>0 ){ 102 + if( pReader->nData>0 ){
139 sqlite_int64 iDocidDelta; 103 sqlite_int64 iDocidDelta;
140 - int iDummy, n = getVarint(pReader->pData, &iDocidDelta); 104 - int iDummy, n = getVarint(pReader->pData, &iDocidDelta);
141 + int nTotal = 0; 105 + int nTotal = 0;
142 + int iDummy, n = getVarintSafe(pReader->pData, &iDocidDelta, pReader->nData) ; 106 + int iDummy, n = getVarintSafe(pReader->pData, &iDocidDelta, pReader->nData) ;
143 + if( !n ) return SQLITE_CORRUPT_BKPT; 107 + if( !n ) return SQLITE_CORRUPT_BKPT;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 - const char *pData, int nData){ 149 - const char *pData, int nData){
186 +static void dlrDestroy(DLReader *pReader){ 150 +static void dlrDestroy(DLReader *pReader){
187 + SCRAMBLE(pReader); 151 + SCRAMBLE(pReader);
188 +} 152 +}
189 +static int dlrInit(DLReader *pReader, DocListType iType, 153 +static int dlrInit(DLReader *pReader, DocListType iType,
190 + const char *pData, int nData){ 154 + const char *pData, int nData){
191 + int rc; 155 + int rc;
192 assert( pData!=NULL && nData!=0 ); 156 assert( pData!=NULL && nData!=0 );
193 pReader->iType = iType; 157 pReader->iType = iType;
194 pReader->pData = pData; 158 pReader->pData = pData;
195 @@ -692,10 +744,9 @@ 159 @@ -716,10 +744,9 @@ static void dlrInit(DLReader *pReader, DocListType iType,
196 pReader->iDocid = 0; 160 pReader->iDocid = 0;
197 161
198 /* Load the first element's data. There must be a first element. */ 162 /* Load the first element's data. There must be a first element. */
199 - dlrStep(pReader); 163 - dlrStep(pReader);
200 -} 164 -}
201 -static void dlrDestroy(DLReader *pReader){ 165 -static void dlrDestroy(DLReader *pReader){
202 - SCRAMBLE(pReader); 166 - SCRAMBLE(pReader);
203 + rc = dlrStep(pReader); 167 + rc = dlrStep(pReader);
204 + if( rc!=SQLITE_OK ) dlrDestroy(pReader); 168 + if( rc!=SQLITE_OK ) dlrDestroy(pReader);
205 + return rc; 169 + return rc;
206 } 170 }
207 171
208 #ifndef NDEBUG 172 #ifndef NDEBUG
209 @@ -782,9 +833,9 @@ 173 @@ -806,9 +833,9 @@ static void dlwDestroy(DLWriter *pWriter){
210 /* TODO(shess) This has become just a helper for docListMerge. 174 /* TODO(shess) This has become just a helper for docListMerge.
211 ** Consider a refactor to make this cleaner. 175 ** Consider a refactor to make this cleaner.
212 */ 176 */
213 -static void dlwAppend(DLWriter *pWriter, 177 -static void dlwAppend(DLWriter *pWriter,
214 - const char *pData, int nData, 178 - const char *pData, int nData,
215 - sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ 179 - sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){
216 +static int dlwAppend(DLWriter *pWriter, 180 +static int dlwAppend(DLWriter *pWriter,
217 + const char *pData, int nData, 181 + const char *pData, int nData,
218 + sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ 182 + sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){
219 sqlite_int64 iDocid = 0; 183 sqlite_int64 iDocid = 0;
220 char c[VARINT_MAX]; 184 char c[VARINT_MAX];
221 int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */ 185 int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */
222 @@ -793,7 +844,8 @@ 186 @@ -817,7 +844,8 @@ static void dlwAppend(DLWriter *pWriter,
223 #endif 187 #endif
224 188
225 /* Recode the initial docid as delta from iPrevDocid. */ 189 /* Recode the initial docid as delta from iPrevDocid. */
226 - nFirstOld = getVarint(pData, &iDocid); 190 - nFirstOld = getVarint(pData, &iDocid);
227 + nFirstOld = getVarintSafe(pData, &iDocid, nData); 191 + nFirstOld = getVarintSafe(pData, &iDocid, nData);
228 + if( !nFirstOld ) return SQLITE_CORRUPT_BKPT; 192 + if( !nFirstOld ) return SQLITE_CORRUPT_BKPT;
229 assert( nFirstOld<nData || (nFirstOld==nData && pWriter->iType==DL_DOCIDS) ); 193 assert( nFirstOld<nData || (nFirstOld==nData && pWriter->iType==DL_DOCIDS) );
230 nFirstNew = putVarint(c, iFirstDocid-pWriter->iPrevDocid); 194 nFirstNew = putVarint(c, iFirstDocid-pWriter->iPrevDocid);
231 195
232 @@ -814,10 +866,11 @@ 196 @@ -838,10 +866,11 @@ static void dlwAppend(DLWriter *pWriter,
233 dataBufferAppend(pWriter->b, c, nFirstNew); 197 dataBufferAppend(pWriter->b, c, nFirstNew);
234 } 198 }
235 pWriter->iPrevDocid = iLastDocid; 199 pWriter->iPrevDocid = iLastDocid;
236 + return SQLITE_OK; 200 + return SQLITE_OK;
237 } 201 }
238 -static void dlwCopy(DLWriter *pWriter, DLReader *pReader){ 202 -static void dlwCopy(DLWriter *pWriter, DLReader *pReader){
239 - dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), 203 - dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader),
240 - dlrDocid(pReader), dlrDocid(pReader)); 204 - dlrDocid(pReader), dlrDocid(pReader));
241 +static int dlwCopy(DLWriter *pWriter, DLReader *pReader){ 205 +static int dlwCopy(DLWriter *pWriter, DLReader *pReader){
242 + return dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), 206 + return dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader),
243 + dlrDocid(pReader), dlrDocid(pReader)); 207 + dlrDocid(pReader), dlrDocid(pReader));
244 } 208 }
245 static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){ 209 static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){
246 char c[VARINT_MAX]; 210 char c[VARINT_MAX];
247 @@ -878,45 +931,63 @@ 211 @@ -902,45 +931,63 @@ static int plrEndOffset(PLReader *pReader){
248 assert( !plrAtEnd(pReader) ); 212 assert( !plrAtEnd(pReader) );
249 return pReader->iEndOffset; 213 return pReader->iEndOffset;
250 } 214 }
251 -static void plrStep(PLReader *pReader){ 215 -static void plrStep(PLReader *pReader){
252 - int i, n; 216 - int i, n;
253 +static int plrStep(PLReader *pReader){ 217 +static int plrStep(PLReader *pReader){
254 + int i, n, nTotal = 0; 218 + int i, n, nTotal = 0;
255 219
256 assert( !plrAtEnd(pReader) ); 220 assert( !plrAtEnd(pReader) );
257 221
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 -static void plrInit(PLReader *pReader, DLReader *pDLReader){ 279 -static void plrInit(PLReader *pReader, DLReader *pDLReader){
316 +static void plrDestroy(PLReader *pReader){ 280 +static void plrDestroy(PLReader *pReader){
317 + SCRAMBLE(pReader); 281 + SCRAMBLE(pReader);
318 +} 282 +}
319 + 283 +
320 +static int plrInit(PLReader *pReader, DLReader *pDLReader){ 284 +static int plrInit(PLReader *pReader, DLReader *pDLReader){
321 + int rc; 285 + int rc;
322 pReader->pData = dlrPosData(pDLReader); 286 pReader->pData = dlrPosData(pDLReader);
323 pReader->nData = dlrPosDataLen(pDLReader); 287 pReader->nData = dlrPosDataLen(pDLReader);
324 pReader->iType = pDLReader->iType; 288 pReader->iType = pDLReader->iType;
325 @@ -924,10 +995,9 @@ 289 @@ -948,10 +995,9 @@ static void plrInit(PLReader *pReader, DLReader *pDLReader) {
326 pReader->iPosition = 0; 290 pReader->iPosition = 0;
327 pReader->iStartOffset = 0; 291 pReader->iStartOffset = 0;
328 pReader->iEndOffset = 0; 292 pReader->iEndOffset = 0;
329 - plrStep(pReader); 293 - plrStep(pReader);
330 -} 294 -}
331 -static void plrDestroy(PLReader *pReader){ 295 -static void plrDestroy(PLReader *pReader){
332 - SCRAMBLE(pReader); 296 - SCRAMBLE(pReader);
333 + rc = plrStep(pReader); 297 + rc = plrStep(pReader);
334 + if( rc!=SQLITE_OK ) plrDestroy(pReader); 298 + if( rc!=SQLITE_OK ) plrDestroy(pReader);
335 + return rc; 299 + return rc;
336 } 300 }
337 301
338 /*******************************************************************/ 302 /*******************************************************************/
339 @@ -1113,14 +1183,16 @@ 303 @@ -1137,14 +1183,16 @@ static void dlcDelete(DLCollector *pCollector){
340 ** deletion will be trimmed, and will thus not effect a deletion 304 ** deletion will be trimmed, and will thus not effect a deletion
341 ** during the merge. 305 ** during the merge.
342 */ 306 */
343 -static void docListTrim(DocListType iType, const char *pData, int nData, 307 -static void docListTrim(DocListType iType, const char *pData, int nData,
344 - int iColumn, DocListType iOutType, DataBuffer *out){ 308 - int iColumn, DocListType iOutType, DataBuffer *out){
345 +static int docListTrim(DocListType iType, const char *pData, int nData, 309 +static int docListTrim(DocListType iType, const char *pData, int nData,
346 + int iColumn, DocListType iOutType, DataBuffer *out){ 310 + int iColumn, DocListType iOutType, DataBuffer *out){
347 DLReader dlReader; 311 DLReader dlReader;
348 DLWriter dlWriter; 312 DLWriter dlWriter;
349 + int rc; 313 + int rc;
350 314
351 assert( iOutType<=iType ); 315 assert( iOutType<=iType );
352 316
353 - dlrInit(&dlReader, iType, pData, nData); 317 - dlrInit(&dlReader, iType, pData, nData);
354 + rc = dlrInit(&dlReader, iType, pData, nData); 318 + rc = dlrInit(&dlReader, iType, pData, nData);
355 + if( rc!=SQLITE_OK ) return rc; 319 + if( rc!=SQLITE_OK ) return rc;
356 dlwInit(&dlWriter, iOutType, out); 320 dlwInit(&dlWriter, iOutType, out);
357 321
358 while( !dlrAtEnd(&dlReader) ){ 322 while( !dlrAtEnd(&dlReader) ){
359 @@ -1128,7 +1200,8 @@ 323 @@ -1152,7 +1200,8 @@ static void docListTrim(DocListType iType, const char *pDa ta, int nData,
360 PLWriter plWriter; 324 PLWriter plWriter;
361 int match = 0; 325 int match = 0;
362 326
363 - plrInit(&plReader, &dlReader); 327 - plrInit(&plReader, &dlReader);
364 + rc = plrInit(&plReader, &dlReader); 328 + rc = plrInit(&plReader, &dlReader);
365 + if( rc!=SQLITE_OK ) break; 329 + if( rc!=SQLITE_OK ) break;
366 330
367 while( !plrAtEnd(&plReader) ){ 331 while( !plrAtEnd(&plReader) ){
368 if( iColumn==-1 || plrColumn(&plReader)==iColumn ){ 332 if( iColumn==-1 || plrColumn(&plReader)==iColumn ){
369 @@ -1139,7 +1212,11 @@ 333 @@ -1163,7 +1212,11 @@ static void docListTrim(DocListType iType, const char *pD ata, int nData,
370 plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader), 334 plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader),
371 plrStartOffset(&plReader), plrEndOffset(&plReader)); 335 plrStartOffset(&plReader), plrEndOffset(&plReader));
372 } 336 }
373 - plrStep(&plReader); 337 - plrStep(&plReader);
374 + rc = plrStep(&plReader); 338 + rc = plrStep(&plReader);
375 + if( rc!=SQLITE_OK ){ 339 + if( rc!=SQLITE_OK ){
376 + plrDestroy(&plReader); 340 + plrDestroy(&plReader);
377 + goto err; 341 + goto err;
378 + } 342 + }
379 } 343 }
380 if( match ){ 344 if( match ){
381 plwTerminate(&plWriter); 345 plwTerminate(&plWriter);
382 @@ -1147,10 +1224,13 @@ 346 @@ -1171,10 +1224,13 @@ static void docListTrim(DocListType iType, const char *p Data, int nData,
383 } 347 }
384 348
385 plrDestroy(&plReader); 349 plrDestroy(&plReader);
386 - dlrStep(&dlReader); 350 - dlrStep(&dlReader);
387 + rc = dlrStep(&dlReader); 351 + rc = dlrStep(&dlReader);
388 + if( rc!=SQLITE_OK ) break; 352 + if( rc!=SQLITE_OK ) break;
389 } 353 }
390 +err: 354 +err:
391 dlwDestroy(&dlWriter); 355 dlwDestroy(&dlWriter);
392 dlrDestroy(&dlReader); 356 dlrDestroy(&dlReader);
393 + return rc; 357 + return rc;
394 } 358 }
395 359
396 /* Used by docListMerge() to keep doclists in the ascending order by 360 /* Used by docListMerge() to keep doclists in the ascending order by
397 @@ -1207,19 +1287,20 @@ 361 @@ -1231,19 +1287,20 @@ static void orderedDLReaderReorder(OrderedDLReader *p, i nt n){
398 /* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably 362 /* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably
399 ** be fixed. 363 ** be fixed.
400 */ 364 */
401 -static void docListMerge(DataBuffer *out, 365 -static void docListMerge(DataBuffer *out,
402 - DLReader *pReaders, int nReaders){ 366 - DLReader *pReaders, int nReaders){
403 +static int docListMerge(DataBuffer *out, 367 +static int docListMerge(DataBuffer *out,
404 + DLReader *pReaders, int nReaders){ 368 + DLReader *pReaders, int nReaders){
405 OrderedDLReader readers[MERGE_COUNT]; 369 OrderedDLReader readers[MERGE_COUNT];
406 DLWriter writer; 370 DLWriter writer;
407 int i, n; 371 int i, n;
408 const char *pStart = 0; 372 const char *pStart = 0;
409 int nStart = 0; 373 int nStart = 0;
410 sqlite_int64 iFirstDocid = 0, iLastDocid = 0; 374 sqlite_int64 iFirstDocid = 0, iLastDocid = 0;
411 + int rc = SQLITE_OK; 375 + int rc = SQLITE_OK;
412 376
413 assert( nReaders>0 ); 377 assert( nReaders>0 );
414 if( nReaders==1 ){ 378 if( nReaders==1 ){
415 dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders)); 379 dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders));
416 - return; 380 - return;
417 + return SQLITE_OK; 381 + return SQLITE_OK;
418 } 382 }
419 383
420 assert( nReaders<=MERGE_COUNT ); 384 assert( nReaders<=MERGE_COUNT );
421 @@ -1252,20 +1333,23 @@ 385 @@ -1276,20 +1333,23 @@ static void docListMerge(DataBuffer *out,
422 nStart += dlrDocDataBytes(readers[0].pReader); 386 nStart += dlrDocDataBytes(readers[0].pReader);
423 }else{ 387 }else{
424 if( pStart!=0 ){ 388 if( pStart!=0 ){
425 - dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); 389 - dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid);
426 + rc = dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); 390 + rc = dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid);
427 + if( rc!=SQLITE_OK ) goto err; 391 + if( rc!=SQLITE_OK ) goto err;
428 } 392 }
429 pStart = dlrDocData(readers[0].pReader); 393 pStart = dlrDocData(readers[0].pReader);
430 nStart = dlrDocDataBytes(readers[0].pReader); 394 nStart = dlrDocDataBytes(readers[0].pReader);
431 iFirstDocid = iDocid; 395 iFirstDocid = iDocid;
432 } 396 }
433 iLastDocid = iDocid; 397 iLastDocid = iDocid;
434 - dlrStep(readers[0].pReader); 398 - dlrStep(readers[0].pReader);
435 + rc = dlrStep(readers[0].pReader); 399 + rc = dlrStep(readers[0].pReader);
436 + if( rc!=SQLITE_OK ) goto err; 400 + if( rc!=SQLITE_OK ) goto err;
437 401
438 /* Drop all of the older elements with the same docid. */ 402 /* Drop all of the older elements with the same docid. */
439 for(i=1; i<nReaders && 403 for(i=1; i<nReaders &&
440 !dlrAtEnd(readers[i].pReader) && 404 !dlrAtEnd(readers[i].pReader) &&
441 dlrDocid(readers[i].pReader)==iDocid; i++){ 405 dlrDocid(readers[i].pReader)==iDocid; i++){
442 - dlrStep(readers[i].pReader); 406 - dlrStep(readers[i].pReader);
443 + rc = dlrStep(readers[i].pReader); 407 + rc = dlrStep(readers[i].pReader);
444 + if( rc!=SQLITE_OK ) goto err; 408 + if( rc!=SQLITE_OK ) goto err;
445 } 409 }
446 410
447 /* Get the readers back into order. */ 411 /* Get the readers back into order. */
448 @@ -1275,8 +1359,11 @@ 412 @@ -1299,8 +1359,11 @@ static void docListMerge(DataBuffer *out,
449 } 413 }
450 414
451 /* Copy over any remaining elements. */ 415 /* Copy over any remaining elements. */
452 - if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); 416 - if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid);
453 + if( nStart>0 ) 417 + if( nStart>0 )
454 + rc = dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); 418 + rc = dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid);
455 +err: 419 +err:
456 dlwDestroy(&writer); 420 dlwDestroy(&writer);
457 + return rc; 421 + return rc;
458 } 422 }
459 423
460 /* Helper function for posListUnion(). Compares the current position 424 /* Helper function for posListUnion(). Compares the current position
461 @@ -1312,30 +1399,40 @@ 425 @@ -1336,30 +1399,40 @@ static int posListCmp(PLReader *pLeft, PLReader *pRight) {
462 ** work with any doclist type, though both inputs and the output 426 ** work with any doclist type, though both inputs and the output
463 ** should be the same type. 427 ** should be the same type.
464 */ 428 */
465 -static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ 429 -static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){
466 +static int posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ 430 +static int posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){
467 PLReader left, right; 431 PLReader left, right;
468 PLWriter writer; 432 PLWriter writer;
469 + int rc; 433 + int rc;
470 434
471 assert( dlrDocid(pLeft)==dlrDocid(pRight) ); 435 assert( dlrDocid(pLeft)==dlrDocid(pRight) );
(...skipping 27 matching lines...) Expand all
499 plwCopy(&writer, &left); 463 plwCopy(&writer, &left);
500 - plrStep(&left); 464 - plrStep(&left);
501 - plrStep(&right); 465 - plrStep(&right);
502 + rc = plrStep(&left); 466 + rc = plrStep(&left);
503 + if( rc != SQLITE_OK ) break; 467 + if( rc != SQLITE_OK ) break;
504 + rc = plrStep(&right); 468 + rc = plrStep(&right);
505 + if( rc != SQLITE_OK ) break; 469 + if( rc != SQLITE_OK ) break;
506 } 470 }
507 } 471 }
508 472
509 @@ -1343,56 +1440,75 @@ 473 @@ -1367,56 +1440,75 @@ static void posListUnion(DLReader *pLeft, DLReader *pRig ht, DLWriter *pOut){
510 plwDestroy(&writer); 474 plwDestroy(&writer);
511 plrDestroy(&left); 475 plrDestroy(&left);
512 plrDestroy(&right); 476 plrDestroy(&right);
513 + return rc; 477 + return rc;
514 } 478 }
515 479
516 /* Write the union of doclists in pLeft and pRight to pOut. For 480 /* Write the union of doclists in pLeft and pRight to pOut. For
517 ** docids in common between the inputs, the union of the position 481 ** docids in common between the inputs, the union of the position
518 ** lists is written. Inputs and outputs are always type DL_DEFAULT. 482 ** lists is written. Inputs and outputs are always type DL_DEFAULT.
519 */ 483 */
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 } 555 }
592 } 556 }
593 557
594 dlrDestroy(&left); 558 dlrDestroy(&left);
595 dlrDestroy(&right); 559 dlrDestroy(&right);
596 dlwDestroy(&writer); 560 dlwDestroy(&writer);
597 + return rc; 561 + return rc;
598 } 562 }
599 563
600 /* pLeft and pRight are DLReaders positioned to the same docid. 564 /* pLeft and pRight are DLReaders positioned to the same docid.
601 @@ -1407,35 +1523,47 @@ 565 @@ -1431,35 +1523,47 @@ static void docListUnion(
602 ** include the positions from pRight that are one more than a 566 ** include the positions from pRight that are one more than a
603 ** position in pLeft. In other words: pRight.iPos==pLeft.iPos+1. 567 ** position in pLeft. In other words: pRight.iPos==pLeft.iPos+1.
604 */ 568 */
605 -static void posListPhraseMerge(DLReader *pLeft, DLReader *pRight, 569 -static void posListPhraseMerge(DLReader *pLeft, DLReader *pRight,
606 - DLWriter *pOut){ 570 - DLWriter *pOut){
607 +static int posListPhraseMerge(DLReader *pLeft, DLReader *pRight, 571 +static int posListPhraseMerge(DLReader *pLeft, DLReader *pRight,
608 + DLWriter *pOut){ 572 + DLWriter *pOut){
609 PLReader left, right; 573 PLReader left, right;
610 PLWriter writer; 574 PLWriter writer;
611 int match = 0; 575 int match = 0;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0); 613 plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0);
650 - plrStep(&left); 614 - plrStep(&left);
651 - plrStep(&right); 615 - plrStep(&right);
652 + rc = plrStep(&left); 616 + rc = plrStep(&left);
653 + if( rc!=SQLITE_OK ) break; 617 + if( rc!=SQLITE_OK ) break;
654 + rc = plrStep(&right); 618 + rc = plrStep(&right);
655 + if( rc!=SQLITE_OK ) break; 619 + if( rc!=SQLITE_OK ) break;
656 } 620 }
657 } 621 }
658 622
659 @@ -1446,6 +1574,7 @@ 623 @@ -1470,6 +1574,7 @@ static void posListPhraseMerge(DLReader *pLeft, DLReader * pRight,
660 624
661 plrDestroy(&left); 625 plrDestroy(&left);
662 plrDestroy(&right); 626 plrDestroy(&right);
663 + return rc; 627 + return rc;
664 } 628 }
665 629
666 /* We have two doclists with positions: pLeft and pRight. 630 /* We have two doclists with positions: pLeft and pRight.
667 @@ -1457,7 +1586,7 @@ 631 @@ -1481,7 +1586,7 @@ static void posListPhraseMerge(DLReader *pLeft, DLReader * pRight,
668 ** iType controls the type of data written to pOut. If iType is 632 ** iType controls the type of data written to pOut. If iType is
669 ** DL_POSITIONS, the positions are those from pRight. 633 ** DL_POSITIONS, the positions are those from pRight.
670 */ 634 */
671 -static void docListPhraseMerge( 635 -static void docListPhraseMerge(
672 +static int docListPhraseMerge( 636 +static int docListPhraseMerge(
673 const char *pLeft, int nLeft, 637 const char *pLeft, int nLeft,
674 const char *pRight, int nRight, 638 const char *pRight, int nRight,
675 DocListType iType, 639 DocListType iType,
676 @@ -1465,152 +1594,198 @@ 640 @@ -1489,152 +1594,198 @@ static void docListPhraseMerge(
677 ){ 641 ){
678 DLReader left, right; 642 DLReader left, right;
679 DLWriter writer; 643 DLWriter writer;
680 + int rc; 644 + int rc;
681 645
682 - if( nLeft==0 || nRight==0 ) return; 646 - if( nLeft==0 || nRight==0 ) return;
683 + if( nLeft==0 || nRight==0 ) return SQLITE_OK; 647 + if( nLeft==0 || nRight==0 ) return SQLITE_OK;
684 648
685 assert( iType!=DL_POSITIONS_OFFSETS ); 649 assert( iType!=DL_POSITIONS_OFFSETS );
686 650
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 } 863 }
900 864
901 +err: 865 +err:
902 dlrDestroy(&left); 866 dlrDestroy(&left);
903 dlrDestroy(&right); 867 dlrDestroy(&right);
904 dlwDestroy(&writer); 868 dlwDestroy(&writer);
905 + return rc; 869 + return rc;
906 } 870 }
907 871
908 static char *string_dup_n(const char *s, int n){ 872 static char *string_dup_n(const char *s, int n){
909 @@ -1814,7 +1989,7 @@ 873 @@ -3437,7 +3588,8 @@ static int fulltextNext(sqlite3_vtab_cursor *pCursor){
910 /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?",
911 /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)",
912 /* SEGDIR_SELECT_LEVEL */
913 - "select start_block, leaves_end_block, root from %_segdir "
914 + "select start_block, leaves_end_block, root, idx from %_segdir "
915 " where level = ? order by idx",
916 /* SEGDIR_SPAN */
917 "select min(start_block), max(end_block) from %_segdir "
918 @@ -3413,7 +3588,8 @@
919 return SQLITE_OK; 874 return SQLITE_OK;
920 } 875 }
921 rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader)); 876 rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader));
922 - dlrStep(&c->reader); 877 - dlrStep(&c->reader);
923 + if( rc!=SQLITE_OK ) return rc; 878 + if( rc!=SQLITE_OK ) return rc;
924 + rc = dlrStep(&c->reader); 879 + rc = dlrStep(&c->reader);
925 if( rc!=SQLITE_OK ) return rc; 880 if( rc!=SQLITE_OK ) return rc;
926 /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ 881 /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
927 rc = sqlite3_step(c->pStmt); 882 rc = sqlite3_step(c->pStmt);
928 @@ -3421,8 +3597,11 @@ 883 @@ -3497,14 +3649,18 @@ static int docListOfTerm(
929 c->eof = 0;
930 return SQLITE_OK;
931 }
932 - /* an error occurred; abort */
933 - return rc==SQLITE_DONE ? SQLITE_ERROR : rc;
934 +
935 + /* Corrupt if the index refers to missing document. */
936 + if( rc==SQLITE_DONE ) return SQLITE_CORRUPT_BKPT;
937 +
938 + return rc;
939 }
940 }
941
942 @@ -3470,14 +3649,18 @@
943 return rc; 884 return rc;
944 } 885 }
945 dataBufferInit(&new, 0); 886 dataBufferInit(&new, 0);
946 - docListPhraseMerge(left.pData, left.nData, right.pData, right.nData, 887 - docListPhraseMerge(left.pData, left.nData, right.pData, right.nData,
947 - i<pQTerm->nPhrase ? DL_POSITIONS : DL_DOCIDS, &new); 888 - i<pQTerm->nPhrase ? DL_POSITIONS : DL_DOCIDS, &new);
948 + rc = docListPhraseMerge(left.pData, left.nData, right.pData, right.nData, 889 + rc = docListPhraseMerge(left.pData, left.nData, right.pData, right.nData,
949 + i<pQTerm->nPhrase ? DL_POSITIONS : DL_DOCIDS, &new) ; 890 + i<pQTerm->nPhrase ? DL_POSITIONS : DL_DOCIDS, &new) ;
950 dataBufferDestroy(&left); 891 dataBufferDestroy(&left);
951 dataBufferDestroy(&right); 892 dataBufferDestroy(&right);
952 + if( rc!=SQLITE_OK ){ 893 + if( rc!=SQLITE_OK ){
953 + dataBufferDestroy(&new); 894 + dataBufferDestroy(&new);
954 + return rc; 895 + return rc;
955 + } 896 + }
956 left = new; 897 left = new;
957 } 898 }
958 *pResult = left; 899 *pResult = left;
959 - return SQLITE_OK; 900 - return SQLITE_OK;
960 + return rc; 901 + return rc;
961 } 902 }
962 903
963 /* Add a new term pTerm[0..nTerm-1] to the query *q. 904 /* Add a new term pTerm[0..nTerm-1] to the query *q.
964 @@ -3544,6 +3727,7 @@ 905 @@ -3749,18 +3905,30 @@ static int fulltextQuery(
965 int firstIndex = pQuery->nTerms;
966 int iCol;
967 int nTerm = 1;
968 + int iEndLast = -1;
969
970 int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor);
971 if( rc!=SQLITE_OK ) return rc;
972 @@ -3568,6 +3752,20 @@
973 pQuery->nextIsOr = 1;
974 continue;
975 }
976 +
977 + /*
978 + * The ICU tokenizer considers '*' a break character, so the code below
979 + * sets isPrefix correctly, but since that code doesn't eat the '*', the
980 + * ICU tokenizer returns it as the next token. So eat it here until a
981 + * better solution presents itself.
982 + */
983 + if( pQuery->nTerms>0 && nToken==1 && pSegment[iBegin]=='*' &&
984 + iEndLast==iBegin){
985 + pQuery->pTerms[pQuery->nTerms-1].isPrefix = 1;
986 + continue;
987 + }
988 + iEndLast = iEnd;
989 +
990 queryAdd(pQuery, pToken, nToken);
991 if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){
992 pQuery->pTerms[pQuery->nTerms-1].isNot = 1;
993 @@ -3707,18 +3905,30 @@
994 return rc; 906 return rc;
995 } 907 }
996 dataBufferInit(&new, 0); 908 dataBufferInit(&new, 0);
997 - docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new); 909 - docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new);
998 + rc = docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new); 910 + rc = docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new);
999 dataBufferDestroy(&right); 911 dataBufferDestroy(&right);
1000 dataBufferDestroy(&or); 912 dataBufferDestroy(&or);
1001 + if( rc!=SQLITE_OK ){ 913 + if( rc!=SQLITE_OK ){
1002 + if( i!=nNot ) dataBufferDestroy(&left); 914 + if( i!=nNot ) dataBufferDestroy(&left);
1003 + queryClear(pQuery); 915 + queryClear(pQuery);
(...skipping 12 matching lines...) Expand all
1016 dataBufferDestroy(&right); 928 dataBufferDestroy(&right);
1017 dataBufferDestroy(&left); 929 dataBufferDestroy(&left);
1018 + if( rc!=SQLITE_OK ){ 930 + if( rc!=SQLITE_OK ){
1019 + queryClear(pQuery); 931 + queryClear(pQuery);
1020 + dataBufferDestroy(&new); 932 + dataBufferDestroy(&new);
1021 + return rc; 933 + return rc;
1022 + } 934 + }
1023 left = new; 935 left = new;
1024 } 936 }
1025 } 937 }
1026 @@ -3738,9 +3948,15 @@ 938 @@ -3780,9 +3948,15 @@ static int fulltextQuery(
1027 return rc; 939 return rc;
1028 } 940 }
1029 dataBufferInit(&new, 0); 941 dataBufferInit(&new, 0);
1030 - docListExceptMerge(left.pData, left.nData, right.pData, right.nData, &new); 942 - docListExceptMerge(left.pData, left.nData, right.pData, right.nData, &new);
1031 + rc = docListExceptMerge(left.pData, left.nData, 943 + rc = docListExceptMerge(left.pData, left.nData,
1032 + right.pData, right.nData, &new); 944 + right.pData, right.nData, &new);
1033 dataBufferDestroy(&right); 945 dataBufferDestroy(&right);
1034 dataBufferDestroy(&left); 946 dataBufferDestroy(&left);
1035 + if( rc!=SQLITE_OK ){ 947 + if( rc!=SQLITE_OK ){
1036 + queryClear(pQuery); 948 + queryClear(pQuery);
1037 + dataBufferDestroy(&new); 949 + dataBufferDestroy(&new);
1038 + return rc; 950 + return rc;
1039 + } 951 + }
1040 left = new; 952 left = new;
1041 } 953 }
1042 954
1043 @@ -3834,7 +4050,8 @@ 955 @@ -3876,7 +4050,8 @@ static int fulltextFilter(
1044 rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &c->result, &c-> q); 956 rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &c->result, &c-> q);
1045 if( rc!=SQLITE_OK ) return rc; 957 if( rc!=SQLITE_OK ) return rc;
1046 if( c->result.nData!=0 ){ 958 if( c->result.nData!=0 ){
1047 - dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData); 959 - dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData);
1048 + rc = dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData); 960 + rc = dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData);
1049 + if( rc!=SQLITE_OK ) return rc; 961 + if( rc!=SQLITE_OK ) return rc;
1050 } 962 }
1051 break; 963 break;
1052 } 964 }
1053 @@ -4335,22 +4552,19 @@ 965 @@ -4377,22 +4552,19 @@ static void interiorReaderDestroy(InteriorReader *pReade r){
1054 SCRAMBLE(pReader); 966 SCRAMBLE(pReader);
1055 } 967 }
1056 968
1057 -/* TODO(shess) The assertions are great, but what if we're in NDEBUG 969 -/* TODO(shess) The assertions are great, but what if we're in NDEBUG
1058 -** and the blob is empty or otherwise contains suspect data? 970 -** and the blob is empty or otherwise contains suspect data?
1059 -*/ 971 -*/
1060 -static void interiorReaderInit(const char *pData, int nData, 972 -static void interiorReaderInit(const char *pData, int nData,
1061 - InteriorReader *pReader){ 973 - InteriorReader *pReader){
1062 +static int interiorReaderInit(const char *pData, int nData, 974 +static int interiorReaderInit(const char *pData, int nData,
1063 + InteriorReader *pReader){ 975 + InteriorReader *pReader){
1064 int n, nTerm; 976 int n, nTerm;
1065 977
1066 - /* Require at least the leading flag byte */ 978 - /* Require at least the leading flag byte */
1067 + /* These conditions are checked and met by the callers. */ 979 + /* These conditions are checked and met by the callers. */
1068 assert( nData>0 ); 980 assert( nData>0 );
1069 assert( pData[0]!='\0' ); 981 assert( pData[0]!='\0' );
1070 982
1071 CLEAR(pReader); 983 CLEAR(pReader);
1072 984
1073 /* Decode the base blockid, and set the cursor to the first term. */ 985 /* Decode the base blockid, and set the cursor to the first term. */
1074 - n = getVarint(pData+1, &pReader->iBlockid); 986 - n = getVarint(pData+1, &pReader->iBlockid);
1075 - assert( 1+n<=nData ); 987 - assert( 1+n<=nData );
1076 + n = getVarintSafe(pData+1, &pReader->iBlockid, nData-1); 988 + n = getVarintSafe(pData+1, &pReader->iBlockid, nData-1);
1077 + if( !n ) return SQLITE_CORRUPT_BKPT; 989 + if( !n ) return SQLITE_CORRUPT_BKPT;
1078 pReader->pData = pData+1+n; 990 pReader->pData = pData+1+n;
1079 pReader->nData = nData-(1+n); 991 pReader->nData = nData-(1+n);
1080 992
1081 @@ -4361,17 +4575,18 @@ 993 @@ -4403,17 +4575,18 @@ static void interiorReaderInit(const char *pData, int nD ata,
1082 if( pReader->nData==0 ){ 994 if( pReader->nData==0 ){
1083 dataBufferInit(&pReader->term, 0); 995 dataBufferInit(&pReader->term, 0);
1084 }else{ 996 }else{
1085 - n = getVarint32(pReader->pData, &nTerm); 997 - n = getVarint32(pReader->pData, &nTerm);
1086 + n = getVarint32Safe(pReader->pData, &nTerm, pReader->nData); 998 + n = getVarint32Safe(pReader->pData, &nTerm, pReader->nData);
1087 + if( !n || nTerm<0 || nTerm>pReader->nData-n) return SQLITE_CORRUPT_BKPT; 999 + if( !n || nTerm<0 || nTerm>pReader->nData-n) return SQLITE_CORRUPT_BKPT;
1088 dataBufferInit(&pReader->term, nTerm); 1000 dataBufferInit(&pReader->term, nTerm);
1089 dataBufferReplace(&pReader->term, pReader->pData+n, nTerm); 1001 dataBufferReplace(&pReader->term, pReader->pData+n, nTerm);
1090 - assert( n+nTerm<=pReader->nData ); 1002 - assert( n+nTerm<=pReader->nData );
1091 pReader->pData += n+nTerm; 1003 pReader->pData += n+nTerm;
1092 pReader->nData -= n+nTerm; 1004 pReader->nData -= n+nTerm;
1093 } 1005 }
1094 + return SQLITE_OK; 1006 + return SQLITE_OK;
1095 } 1007 }
1096 1008
1097 static int interiorReaderAtEnd(InteriorReader *pReader){ 1009 static int interiorReaderAtEnd(InteriorReader *pReader){
1098 - return pReader->term.nData==0; 1010 - return pReader->term.nData==0;
1099 + return pReader->term.nData<=0; 1011 + return pReader->term.nData<=0;
1100 } 1012 }
1101 1013
1102 static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){ 1014 static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){
1103 @@ -4388,7 +4603,7 @@ 1015 @@ -4430,7 +4603,7 @@ static const char *interiorReaderTerm(InteriorReader *pRea der){
1104 } 1016 }
1105 1017
1106 /* Step forward to the next term in the node. */ 1018 /* Step forward to the next term in the node. */
1107 -static void interiorReaderStep(InteriorReader *pReader){ 1019 -static void interiorReaderStep(InteriorReader *pReader){
1108 +static int interiorReaderStep(InteriorReader *pReader){ 1020 +static int interiorReaderStep(InteriorReader *pReader){
1109 assert( !interiorReaderAtEnd(pReader) ); 1021 assert( !interiorReaderAtEnd(pReader) );
1110 1022
1111 /* If the last term has been read, signal eof, else construct the 1023 /* If the last term has been read, signal eof, else construct the
1112 @@ -4399,18 +4614,26 @@ 1024 @@ -4441,18 +4614,26 @@ static void interiorReaderStep(InteriorReader *pReader){
1113 }else{ 1025 }else{
1114 int n, nPrefix, nSuffix; 1026 int n, nPrefix, nSuffix;
1115 1027
1116 - n = getVarint32(pReader->pData, &nPrefix); 1028 - n = getVarint32(pReader->pData, &nPrefix);
1117 - n += getVarint32(pReader->pData+n, &nSuffix); 1029 - n += getVarint32(pReader->pData+n, &nSuffix);
1118 + n = getVarint32Safe(pReader->pData, &nPrefix, pReader->nData); 1030 + n = getVarint32Safe(pReader->pData, &nPrefix, pReader->nData);
1119 + if( !n ) return SQLITE_CORRUPT_BKPT; 1031 + if( !n ) return SQLITE_CORRUPT_BKPT;
1120 + pReader->nData -= n; 1032 + pReader->nData -= n;
1121 + pReader->pData += n; 1033 + pReader->pData += n;
1122 + n = getVarint32Safe(pReader->pData, &nSuffix, pReader->nData); 1034 + n = getVarint32Safe(pReader->pData, &nSuffix, pReader->nData);
(...skipping 12 matching lines...) Expand all
1135 - pReader->pData += n+nSuffix; 1047 - pReader->pData += n+nSuffix;
1136 - pReader->nData -= n+nSuffix; 1048 - pReader->nData -= n+nSuffix;
1137 + pReader->pData += nSuffix; 1049 + pReader->pData += nSuffix;
1138 + pReader->nData -= nSuffix; 1050 + pReader->nData -= nSuffix;
1139 } 1051 }
1140 pReader->iBlockid++; 1052 pReader->iBlockid++;
1141 + return SQLITE_OK; 1053 + return SQLITE_OK;
1142 } 1054 }
1143 1055
1144 /* Compare the current term to pTerm[nTerm], returning strcmp-style 1056 /* Compare the current term to pTerm[nTerm], returning strcmp-style
1145 @@ -4782,7 +5005,8 @@ 1057 @@ -4824,7 +5005,8 @@ static int leafWriterStepMerge(fulltext_vtab *v, LeafWrite r *pWriter,
1146 n = putVarint(c, nData); 1058 n = putVarint(c, nData);
1147 dataBufferAppend(&pWriter->data, c, n); 1059 dataBufferAppend(&pWriter->data, c, n);
1148 1060
1149 - docListMerge(&pWriter->data, pReaders, nReaders); 1061 - docListMerge(&pWriter->data, pReaders, nReaders);
1150 + rc = docListMerge(&pWriter->data, pReaders, nReaders); 1062 + rc = docListMerge(&pWriter->data, pReaders, nReaders);
1151 + if( rc!= SQLITE_OK ) return rc; 1063 + if( rc!= SQLITE_OK ) return rc;
1152 ASSERT_VALID_DOCLIST(DL_DEFAULT, 1064 ASSERT_VALID_DOCLIST(DL_DEFAULT,
1153 pWriter->data.pData+iDoclistData+n, 1065 pWriter->data.pData+iDoclistData+n,
1154 pWriter->data.nData-iDoclistData-n, NULL); 1066 pWriter->data.nData-iDoclistData-n, NULL);
1155 @@ -4892,7 +5116,8 @@ 1067 @@ -4934,7 +5116,8 @@ static int leafWriterStep(fulltext_vtab *v, LeafWriter *pW riter,
1156 int rc; 1068 int rc;
1157 DLReader reader; 1069 DLReader reader;
1158 1070
1159 - dlrInit(&reader, DL_DEFAULT, pData, nData); 1071 - dlrInit(&reader, DL_DEFAULT, pData, nData);
1160 + rc = dlrInit(&reader, DL_DEFAULT, pData, nData); 1072 + rc = dlrInit(&reader, DL_DEFAULT, pData, nData);
1161 + if( rc!=SQLITE_OK ) return rc; 1073 + if( rc!=SQLITE_OK ) return rc;
1162 rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1); 1074 rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1);
1163 dlrDestroy(&reader); 1075 dlrDestroy(&reader);
1164 1076
1165 @@ -4937,38 +5162,40 @@ 1077 @@ -4979,38 +5162,40 @@ static int leafReaderDataBytes(LeafReader *pReader){
1166 static const char *leafReaderData(LeafReader *pReader){ 1078 static const char *leafReaderData(LeafReader *pReader){
1167 int n, nData; 1079 int n, nData;
1168 assert( pReader->term.nData>0 ); 1080 assert( pReader->term.nData>0 );
1169 - n = getVarint32(pReader->pData, &nData); 1081 - n = getVarint32(pReader->pData, &nData);
1170 + n = getVarint32Safe(pReader->pData, &nData, pReader->nData); 1082 + n = getVarint32Safe(pReader->pData, &nData, pReader->nData);
1171 + if( !n || nData>pReader->nData-n ) return NULL; 1083 + if( !n || nData>pReader->nData-n ) return NULL;
1172 return pReader->pData+n; 1084 return pReader->pData+n;
1173 } 1085 }
1174 1086
1175 -static void leafReaderInit(const char *pData, int nData, 1087 -static void leafReaderInit(const char *pData, int nData,
(...skipping 28 matching lines...) Expand all
1204 assert( !leafReaderAtEnd(pReader) ); 1116 assert( !leafReaderAtEnd(pReader) );
1205 1117
1206 /* Skip previous entry's data block. */ 1118 /* Skip previous entry's data block. */
1207 - n = getVarint32(pReader->pData, &nData); 1119 - n = getVarint32(pReader->pData, &nData);
1208 - assert( n+nData<=pReader->nData ); 1120 - assert( n+nData<=pReader->nData );
1209 + n = getVarint32Safe(pReader->pData, &nData, pReader->nData); 1121 + n = getVarint32Safe(pReader->pData, &nData, pReader->nData);
1210 + if( !n || nData<0 || nData>pReader->nData-n ) return SQLITE_CORRUPT_BKPT; 1122 + if( !n || nData<0 || nData>pReader->nData-n ) return SQLITE_CORRUPT_BKPT;
1211 pReader->pData += n+nData; 1123 pReader->pData += n+nData;
1212 pReader->nData -= n+nData; 1124 pReader->nData -= n+nData;
1213 1125
1214 @@ -4976,15 +5203,23 @@ 1126 @@ -5018,15 +5203,23 @@ static void leafReaderStep(LeafReader *pReader){
1215 /* Construct the new term using a prefix from the old term plus a 1127 /* Construct the new term using a prefix from the old term plus a
1216 ** suffix from the leaf data. 1128 ** suffix from the leaf data.
1217 */ 1129 */
1218 - n = getVarint32(pReader->pData, &nPrefix); 1130 - n = getVarint32(pReader->pData, &nPrefix);
1219 - n += getVarint32(pReader->pData+n, &nSuffix); 1131 - n += getVarint32(pReader->pData+n, &nSuffix);
1220 - assert( n+nSuffix<pReader->nData ); 1132 - assert( n+nSuffix<pReader->nData );
1221 + n = getVarint32Safe(pReader->pData, &nPrefix, pReader->nData); 1133 + n = getVarint32Safe(pReader->pData, &nPrefix, pReader->nData);
1222 + if( !n ) return SQLITE_CORRUPT_BKPT; 1134 + if( !n ) return SQLITE_CORRUPT_BKPT;
1223 + pReader->nData -= n; 1135 + pReader->nData -= n;
1224 + pReader->pData += n; 1136 + pReader->pData += n;
1225 + n = getVarint32Safe(pReader->pData, &nSuffix, pReader->nData); 1137 + n = getVarint32Safe(pReader->pData, &nSuffix, pReader->nData);
1226 + if( !n ) return SQLITE_CORRUPT_BKPT; 1138 + if( !n ) return SQLITE_CORRUPT_BKPT;
1227 + pReader->nData -= n; 1139 + pReader->nData -= n;
1228 + pReader->pData += n; 1140 + pReader->pData += n;
1229 + if( nSuffix<0 || nSuffix>pReader->nData ) return SQLITE_CORRUPT_BKPT; 1141 + if( nSuffix<0 || nSuffix>pReader->nData ) return SQLITE_CORRUPT_BKPT;
1230 + if( nPrefix<0 || nPrefix>pReader->term.nData ) return SQLITE_CORRUPT_BKPT; 1142 + if( nPrefix<0 || nPrefix>pReader->term.nData ) return SQLITE_CORRUPT_BKPT;
1231 pReader->term.nData = nPrefix; 1143 pReader->term.nData = nPrefix;
1232 - dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); 1144 - dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix);
1233 + dataBufferAppend(&pReader->term, pReader->pData, nSuffix); 1145 + dataBufferAppend(&pReader->term, pReader->pData, nSuffix);
1234 1146
1235 - pReader->pData += n+nSuffix; 1147 - pReader->pData += n+nSuffix;
1236 - pReader->nData -= n+nSuffix; 1148 - pReader->nData -= n+nSuffix;
1237 + pReader->pData += nSuffix; 1149 + pReader->pData += nSuffix;
1238 + pReader->nData -= nSuffix; 1150 + pReader->nData -= nSuffix;
1239 } 1151 }
1240 + return SQLITE_OK; 1152 + return SQLITE_OK;
1241 } 1153 }
1242 1154
1243 /* strcmp-style comparison of pReader's current term against pTerm. 1155 /* strcmp-style comparison of pReader's current term against pTerm.
1244 @@ -5077,6 +5312,9 @@ 1156 @@ -5133,14 +5326,19 @@ static int leavesReaderInit(fulltext_vtab *v,
1245 ** the leaf data was entirely contained in the root), or from the
1246 ** stream of blocks between iStartBlockid and iEndBlockid, inclusive.
1247 */
1248 +/* TODO(shess): Figure out a means of indicating how many leaves are
1249 +** expected, for purposes of detecting corruption.
1250 +*/
1251 static int leavesReaderInit(fulltext_vtab *v,
1252 int idx,
1253 sqlite_int64 iStartBlockid,
1254 @@ -5088,32 +5326,67 @@
1255 1157
1256 dataBufferInit(&pReader->rootData, 0); 1158 dataBufferInit(&pReader->rootData, 0);
1257 if( iStartBlockid==0 ){ 1159 if( iStartBlockid==0 ){
1258 + int rc; 1160 + int rc;
1259 + /* Corrupt if this can't be a leaf node. */ 1161 /* Corrupt if this can't be a leaf node. */
1260 + if( pRootData==NULL || nRootData<1 || pRootData[0]!='\0' ){ 1162 if( pRootData==NULL || nRootData<1 || pRootData[0]!='\0' ){
1261 + return SQLITE_CORRUPT_BKPT; 1163 return SQLITE_CORRUPT_BKPT;
1262 + } 1164 }
1263 /* Entire leaf level fit in root data. */ 1165 /* Entire leaf level fit in root data. */
1264 dataBufferReplace(&pReader->rootData, pRootData, nRootData); 1166 dataBufferReplace(&pReader->rootData, pRootData, nRootData);
1265 - leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, 1167 - leafReaderInit(pReader->rootData.pData, pReader->rootData.nData,
1266 - &pReader->leafReader); 1168 - &pReader->leafReader);
1267 + rc = leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, 1169 + rc = leafReaderInit(pReader->rootData.pData, pReader->rootData.nData,
1268 + &pReader->leafReader); 1170 + &pReader->leafReader);
1269 + if( rc!=SQLITE_OK ){ 1171 + if( rc!=SQLITE_OK ){
1270 + dataBufferDestroy(&pReader->rootData); 1172 + dataBufferDestroy(&pReader->rootData);
1271 + return rc; 1173 + return rc;
1272 + } 1174 + }
1273 }else{ 1175 }else{
1274 sqlite3_stmt *s; 1176 sqlite3_stmt *s;
1275 int rc = sql_get_leaf_statement(v, idx, &s); 1177 int rc = sql_get_leaf_statement(v, idx, &s);
1276 if( rc!=SQLITE_OK ) return rc; 1178 @@ -5174,7 +5372,7 @@ static int leavesReaderInit(fulltext_vtab *v,
1179 if( pLeafData==NULL || nLeafData<1 || pLeafData[0]!='\0' ){
1180 rc = SQLITE_CORRUPT_BKPT;
1181 }else{
1182 - leafReaderInit(pLeafData, nLeafData, &pReader->leafReader);
1183 + rc = leafReaderInit(pLeafData, nLeafData, &pReader->leafReader);
1184 }
1185 }
1277 1186
1278 rc = sqlite3_bind_int64(s, 1, iStartBlockid); 1187 @@ -5197,11 +5395,12 @@ static int leavesReaderInit(fulltext_vtab *v,
1279 - if( rc!=SQLITE_OK ) return rc;
1280 + if( rc!=SQLITE_OK ) goto err;
1281
1282 rc = sqlite3_bind_int64(s, 2, iEndBlockid);
1283 - if( rc!=SQLITE_OK ) return rc;
1284 + if( rc!=SQLITE_OK ) goto err;
1285
1286 rc = sqlite3_step(s);
1287 +
1288 + /* Corrupt if interior node referenced missing leaf node. */
1289 if( rc==SQLITE_DONE ){
1290 - pReader->eof = 1;
1291 - return SQLITE_OK;
1292 + rc = SQLITE_CORRUPT_BKPT;
1293 + goto err;
1294 + }
1295 +
1296 + if( rc!=SQLITE_ROW ) goto err;
1297 + rc = SQLITE_OK;
1298 +
1299 + /* Corrupt if leaf data isn't a blob. */
1300 + if( sqlite3_column_type(s, 0)!=SQLITE_BLOB ){
1301 + rc = SQLITE_CORRUPT_BKPT;
1302 + }else{
1303 + const char *pLeafData = sqlite3_column_blob(s, 0);
1304 + int nLeafData = sqlite3_column_bytes(s, 0);
1305 +
1306 + /* Corrupt if this can't be a leaf node. */
1307 + if( pLeafData==NULL || nLeafData<1 || pLeafData[0]!='\0' ){
1308 + rc = SQLITE_CORRUPT_BKPT;
1309 + }else{
1310 + rc = leafReaderInit(pLeafData, nLeafData, &pReader->leafReader);
1311 + }
1312 + }
1313 +
1314 + err:
1315 + if( rc!=SQLITE_OK ){
1316 + if( idx==-1 ){
1317 + sqlite3_finalize(s);
1318 + }else{
1319 + sqlite3_reset(s);
1320 + }
1321 + return rc;
1322 }
1323 - if( rc!=SQLITE_ROW ) return rc;
1324
1325 pReader->pStmt = s;
1326 - leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0),
1327 - sqlite3_column_bytes(pReader->pStmt, 0),
1328 - &pReader->leafReader);
1329 }
1330 return SQLITE_OK;
1331 }
1332 @@ -5122,11 +5395,12 @@
1333 ** end of the current leaf, step forward to the next leaf block. 1188 ** end of the current leaf, step forward to the next leaf block.
1334 */ 1189 */
1335 static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){ 1190 static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){
1336 + int rc; 1191 + int rc;
1337 assert( !leavesReaderAtEnd(pReader) ); 1192 assert( !leavesReaderAtEnd(pReader) );
1338 - leafReaderStep(&pReader->leafReader); 1193 - leafReaderStep(&pReader->leafReader);
1339 + rc = leafReaderStep(&pReader->leafReader); 1194 + rc = leafReaderStep(&pReader->leafReader);
1340 + if( rc!=SQLITE_OK ) return rc; 1195 + if( rc!=SQLITE_OK ) return rc;
1341 1196
1342 if( leafReaderAtEnd(&pReader->leafReader) ){ 1197 if( leafReaderAtEnd(&pReader->leafReader) ){
1343 - int rc; 1198 - int rc;
1344 if( pReader->rootData.pData ){ 1199 if( pReader->rootData.pData ){
1345 pReader->eof = 1; 1200 pReader->eof = 1;
1346 return SQLITE_OK; 1201 return SQLITE_OK;
1347 @@ -5136,10 +5410,25 @@ 1202 @@ -5216,6 +5415,7 @@ static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){
1348 pReader->eof = 1; 1203 if( sqlite3_column_type(pReader->pStmt, 0)!=SQLITE_BLOB ){
1349 return rc==SQLITE_DONE ? SQLITE_OK : rc; 1204 return SQLITE_CORRUPT_BKPT;
1350 } 1205 }else{
1351 - leafReaderDestroy(&pReader->leafReader);
1352 - leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0),
1353 - sqlite3_column_bytes(pReader->pStmt, 0),
1354 - &pReader->leafReader);
1355 +
1356 + /* Corrupt if leaf data isn't a blob. */
1357 + if( sqlite3_column_type(pReader->pStmt, 0)!=SQLITE_BLOB ){
1358 + return SQLITE_CORRUPT_BKPT;
1359 + }else{
1360 + LeafReader tmp; 1206 + LeafReader tmp;
1361 + const char *pLeafData = sqlite3_column_blob(pReader->pStmt, 0); 1207 const char *pLeafData = sqlite3_column_blob(pReader->pStmt, 0);
1362 + int nLeafData = sqlite3_column_bytes(pReader->pStmt, 0); 1208 int nLeafData = sqlite3_column_bytes(pReader->pStmt, 0);
1363 + 1209
1364 + /* Corrupt if this can't be a leaf node. */ 1210 @@ -5224,8 +5424,10 @@ static int leavesReaderStep(fulltext_vtab *v, LeavesReade r *pReader){
1365 + if( pLeafData==NULL || nLeafData<1 || pLeafData[0]!='\0' ){ 1211 return SQLITE_CORRUPT_BKPT;
1366 + return SQLITE_CORRUPT_BKPT; 1212 }
1367 + } 1213
1368 +
1369 + rc = leafReaderInit(pLeafData, nLeafData, &tmp); 1214 + rc = leafReaderInit(pLeafData, nLeafData, &tmp);
1370 + if( rc!=SQLITE_OK ) return rc; 1215 + if( rc!=SQLITE_OK ) return rc;
1371 + leafReaderDestroy(&pReader->leafReader); 1216 leafReaderDestroy(&pReader->leafReader);
1217 - leafReaderInit(pLeafData, nLeafData, &pReader->leafReader);
1372 + pReader->leafReader = tmp; 1218 + pReader->leafReader = tmp;
1373 + } 1219 }
1374 } 1220 }
1375 return SQLITE_OK; 1221 return SQLITE_OK;
1376 } 1222 @@ -5334,13 +5536,26 @@ static int leavesReadersMerge(fulltext_vtab *v,
1377 @@ -5200,8 +5489,19 @@
1378 sqlite_int64 iEnd = sqlite3_column_int64(s, 1);
1379 const char *pRootData = sqlite3_column_blob(s, 2);
1380 int nRootData = sqlite3_column_bytes(s, 2);
1381 + sqlite_int64 iIndex = sqlite3_column_int64(s, 3);
1382 +
1383 + /* Corrupt if we get back different types than we stored. */
1384 + /* Also corrupt if the index is not sequential starting at 0. */
1385 + if( sqlite3_column_type(s, 0)!=SQLITE_INTEGER ||
1386 + sqlite3_column_type(s, 1)!=SQLITE_INTEGER ||
1387 + sqlite3_column_type(s, 2)!=SQLITE_BLOB ||
1388 + i!=iIndex ||
1389 + i>=MERGE_COUNT ){
1390 + rc = SQLITE_CORRUPT_BKPT;
1391 + break;
1392 + }
1393
1394 - assert( i<MERGE_COUNT );
1395 rc = leavesReaderInit(v, i, iStart, iEnd, pRootData, nRootData,
1396 &pReaders[i]);
1397 if( rc!=SQLITE_OK ) break;
1398 @@ -5212,6 +5512,7 @@
1399 while( i-->0 ){
1400 leavesReaderDestroy(&pReaders[i]);
1401 }
1402 + sqlite3_reset(s); /* So we don't leave a lock. */
1403 return rc;
1404 }
1405
1406 @@ -5235,13 +5536,26 @@
1407 DLReader dlReaders[MERGE_COUNT]; 1223 DLReader dlReaders[MERGE_COUNT];
1408 const char *pTerm = leavesReaderTerm(pReaders); 1224 const char *pTerm = leavesReaderTerm(pReaders);
1409 int i, nTerm = leavesReaderTermBytes(pReaders); 1225 int i, nTerm = leavesReaderTermBytes(pReaders);
1410 + int rc; 1226 + int rc;
1411 1227
1412 assert( nReaders<=MERGE_COUNT ); 1228 assert( nReaders<=MERGE_COUNT );
1413 1229
1414 for(i=0; i<nReaders; i++){ 1230 for(i=0; i<nReaders; i++){
1415 - dlrInit(&dlReaders[i], DL_DEFAULT, 1231 - dlrInit(&dlReaders[i], DL_DEFAULT,
1416 - leavesReaderData(pReaders+i), 1232 - leavesReaderData(pReaders+i),
1417 - leavesReaderDataBytes(pReaders+i)); 1233 - leavesReaderDataBytes(pReaders+i));
1418 + const char *pData = leavesReaderData(pReaders+i); 1234 + const char *pData = leavesReaderData(pReaders+i);
1419 + if( pData==NULL ){ 1235 + if( pData==NULL ){
1420 + rc = SQLITE_CORRUPT_BKPT; 1236 + rc = SQLITE_CORRUPT_BKPT;
1421 + break; 1237 + break;
1422 + } 1238 + }
1423 + rc = dlrInit(&dlReaders[i], DL_DEFAULT, 1239 + rc = dlrInit(&dlReaders[i], DL_DEFAULT,
1424 + pData, 1240 + pData,
1425 + leavesReaderDataBytes(pReaders+i)); 1241 + leavesReaderDataBytes(pReaders+i));
1426 + if( rc!=SQLITE_OK ) break; 1242 + if( rc!=SQLITE_OK ) break;
1427 + } 1243 + }
1428 + if( rc!=SQLITE_OK ){ 1244 + if( rc!=SQLITE_OK ){
1429 + while( i-->0 ){ 1245 + while( i-->0 ){
1430 + dlrDestroy(&dlReaders[i]); 1246 + dlrDestroy(&dlReaders[i]);
1431 + } 1247 + }
1432 + return rc; 1248 + return rc;
1433 } 1249 }
1434 1250
1435 return leafWriterStepMerge(v, pWriter, pTerm, nTerm, dlReaders, nReaders); 1251 return leafWriterStepMerge(v, pWriter, pTerm, nTerm, dlReaders, nReaders);
1436 @@ -5295,10 +5609,14 @@ 1252 @@ -5444,12 +5659,14 @@ static int segmentMerge(fulltext_vtab *v, int iLevel){
1437 memset(&lrs, '\0', sizeof(lrs));
1438 rc = leavesReadersInit(v, iLevel, lrs, &i);
1439 if( rc!=SQLITE_OK ) return rc;
1440 - assert( i==MERGE_COUNT );
1441
1442 leafWriterInit(iLevel+1, idx, &writer);
1443
1444 + if( i!=MERGE_COUNT ){
1445 + rc = SQLITE_CORRUPT_BKPT;
1446 + goto err;
1447 + }
1448 +
1449 /* Since leavesReaderReorder() pushes readers at eof to the end,
1450 ** when the first reader is empty, all will be empty.
1451 */
1452 @@ -5341,12 +5659,14 @@
1453 } 1253 }
1454 1254
1455 /* Accumulate the union of *acc and *pData into *acc. */ 1255 /* Accumulate the union of *acc and *pData into *acc. */
1456 -static void docListAccumulateUnion(DataBuffer *acc, 1256 -static void docListAccumulateUnion(DataBuffer *acc,
1457 - const char *pData, int nData) { 1257 - const char *pData, int nData) {
1458 +static int docListAccumulateUnion(DataBuffer *acc, 1258 +static int docListAccumulateUnion(DataBuffer *acc,
1459 + const char *pData, int nData) { 1259 + const char *pData, int nData) {
1460 DataBuffer tmp = *acc; 1260 DataBuffer tmp = *acc;
1461 + int rc; 1261 + int rc;
1462 dataBufferInit(acc, tmp.nData+nData); 1262 dataBufferInit(acc, tmp.nData+nData);
1463 - docListUnion(tmp.pData, tmp.nData, pData, nData, acc); 1263 - docListUnion(tmp.pData, tmp.nData, pData, nData, acc);
1464 + rc = docListUnion(tmp.pData, tmp.nData, pData, nData, acc); 1264 + rc = docListUnion(tmp.pData, tmp.nData, pData, nData, acc);
1465 dataBufferDestroy(&tmp); 1265 dataBufferDestroy(&tmp);
1466 + return rc; 1266 + return rc;
1467 } 1267 }
1468 1268
1469 /* TODO(shess) It might be interesting to explore different merge 1269 /* TODO(shess) It might be interesting to explore different merge
1470 @@ -5388,8 +5708,13 @@ 1270 @@ -5491,8 +5708,13 @@ static int loadSegmentLeavesInt(fulltext_vtab *v, LeavesR eader *pReader,
1471 int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix); 1271 int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix);
1472 if( c>0 ) break; /* Past any possible matches. */ 1272 if( c>0 ) break; /* Past any possible matches. */
1473 if( c==0 ){ 1273 if( c==0 ){
1474 + int iBuffer, nData; 1274 + int iBuffer, nData;
1475 const char *pData = leavesReaderData(pReader); 1275 const char *pData = leavesReaderData(pReader);
1476 - int iBuffer, nData = leavesReaderDataBytes(pReader); 1276 - int iBuffer, nData = leavesReaderDataBytes(pReader);
1477 + if( pData==NULL ){ 1277 + if( pData==NULL ){
1478 + rc = SQLITE_CORRUPT_BKPT; 1278 + rc = SQLITE_CORRUPT_BKPT;
1479 + break; 1279 + break;
1480 + } 1280 + }
1481 + nData = leavesReaderDataBytes(pReader); 1281 + nData = leavesReaderDataBytes(pReader);
1482 1282
1483 /* Find the first empty buffer. */ 1283 /* Find the first empty buffer. */
1484 for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){ 1284 for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){
1485 @@ -5435,11 +5760,13 @@ 1285 @@ -5538,11 +5760,13 @@ static int loadSegmentLeavesInt(fulltext_vtab *v, Leaves Reader *pReader,
1486 ** with pData/nData. 1286 ** with pData/nData.
1487 */ 1287 */
1488 dataBufferSwap(p, pAcc); 1288 dataBufferSwap(p, pAcc);
1489 - docListAccumulateUnion(pAcc, pData, nData); 1289 - docListAccumulateUnion(pAcc, pData, nData);
1490 + rc = docListAccumulateUnion(pAcc, pData, nData); 1290 + rc = docListAccumulateUnion(pAcc, pData, nData);
1491 + if( rc!=SQLITE_OK ) goto err; 1291 + if( rc!=SQLITE_OK ) goto err;
1492 1292
1493 /* Accumulate remaining doclists into pAcc. */ 1293 /* Accumulate remaining doclists into pAcc. */
1494 for(++p; p<pAcc; ++p){ 1294 for(++p; p<pAcc; ++p){
1495 - docListAccumulateUnion(pAcc, p->pData, p->nData); 1295 - docListAccumulateUnion(pAcc, p->pData, p->nData);
1496 + rc = docListAccumulateUnion(pAcc, p->pData, p->nData); 1296 + rc = docListAccumulateUnion(pAcc, p->pData, p->nData);
1497 + if( rc!=SQLITE_OK ) goto err; 1297 + if( rc!=SQLITE_OK ) goto err;
1498 1298
1499 /* dataBufferReset() could allow a large doclist to blow up 1299 /* dataBufferReset() could allow a large doclist to blow up
1500 ** our memory requirements. 1300 ** our memory requirements.
1501 @@ -5464,13 +5791,15 @@ 1301 @@ -5567,13 +5791,15 @@ static int loadSegmentLeavesInt(fulltext_vtab *v, Leaves Reader *pReader,
1502 if( out->nData==0 ){ 1302 if( out->nData==0 ){
1503 dataBufferSwap(out, &(pBuffers[iBuffer])); 1303 dataBufferSwap(out, &(pBuffers[iBuffer]));
1504 }else{ 1304 }else{
1505 - docListAccumulateUnion(out, pBuffers[iBuffer].pData, 1305 - docListAccumulateUnion(out, pBuffers[iBuffer].pData,
1506 - pBuffers[iBuffer].nData); 1306 - pBuffers[iBuffer].nData);
1507 + rc = docListAccumulateUnion(out, pBuffers[iBuffer].pData, 1307 + rc = docListAccumulateUnion(out, pBuffers[iBuffer].pData,
1508 + pBuffers[iBuffer].nData); 1308 + pBuffers[iBuffer].nData);
1509 + if( rc!=SQLITE_OK ) break; 1309 + if( rc!=SQLITE_OK ) break;
1510 } 1310 }
1511 } 1311 }
1512 } 1312 }
1513 } 1313 }
1514 1314
1515 +err: 1315 +err:
1516 while( nBuffers-- ){ 1316 while( nBuffers-- ){
1517 dataBufferDestroy(&(pBuffers[nBuffers])); 1317 dataBufferDestroy(&(pBuffers[nBuffers]));
1518 } 1318 }
1519 @@ -5529,20 +5858,26 @@ 1319 @@ -5632,20 +5858,26 @@ static int loadSegmentLeaves(fulltext_vtab *v,
1520 ** node. Consider whether breaking symmetry is worthwhile. I suspect 1320 ** node. Consider whether breaking symmetry is worthwhile. I suspect
1521 ** it is not worthwhile. 1321 ** it is not worthwhile.
1522 */ 1322 */
1523 -static void getChildrenContaining(const char *pData, int nData, 1323 -static void getChildrenContaining(const char *pData, int nData,
1524 - const char *pTerm, int nTerm, int isPrefix, 1324 - const char *pTerm, int nTerm, int isPrefix,
1525 - sqlite_int64 *piStartChild, 1325 - sqlite_int64 *piStartChild,
1526 - sqlite_int64 *piEndChild){ 1326 - sqlite_int64 *piEndChild){
1527 +static int getChildrenContaining(const char *pData, int nData, 1327 +static int getChildrenContaining(const char *pData, int nData,
1528 + const char *pTerm, int nTerm, int isPrefix, 1328 + const char *pTerm, int nTerm, int isPrefix,
1529 + sqlite_int64 *piStartChild, 1329 + sqlite_int64 *piStartChild,
(...skipping 12 matching lines...) Expand all
1542 if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break; 1342 if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break;
1543 - interiorReaderStep(&reader); 1343 - interiorReaderStep(&reader);
1544 + rc = interiorReaderStep(&reader); 1344 + rc = interiorReaderStep(&reader);
1545 + if( rc!=SQLITE_OK ){ 1345 + if( rc!=SQLITE_OK ){
1546 + interiorReaderDestroy(&reader); 1346 + interiorReaderDestroy(&reader);
1547 + return rc; 1347 + return rc;
1548 + } 1348 + }
1549 } 1349 }
1550 *piStartChild = interiorReaderCurrentBlockid(&reader); 1350 *piStartChild = interiorReaderCurrentBlockid(&reader);
1551 1351
1552 @@ -5552,7 +5887,11 @@ 1352 @@ -5655,7 +5887,11 @@ static void getChildrenContaining(const char *pData, int nData,
1553 */ 1353 */
1554 while( !interiorReaderAtEnd(&reader) ){ 1354 while( !interiorReaderAtEnd(&reader) ){
1555 if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break; 1355 if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break;
1556 - interiorReaderStep(&reader); 1356 - interiorReaderStep(&reader);
1557 + rc = interiorReaderStep(&reader); 1357 + rc = interiorReaderStep(&reader);
1558 + if( rc!=SQLITE_OK ){ 1358 + if( rc!=SQLITE_OK ){
1559 + interiorReaderDestroy(&reader); 1359 + interiorReaderDestroy(&reader);
1560 + return rc; 1360 + return rc;
1561 + } 1361 + }
1562 } 1362 }
1563 *piEndChild = interiorReaderCurrentBlockid(&reader); 1363 *piEndChild = interiorReaderCurrentBlockid(&reader);
1564 1364
1565 @@ -5561,6 +5900,7 @@ 1365 @@ -5664,6 +5900,7 @@ static void getChildrenContaining(const char *pData, int n Data,
1566 /* Children must ascend, and if !prefix, both must be the same. */ 1366 /* Children must ascend, and if !prefix, both must be the same. */
1567 assert( *piEndChild>=*piStartChild ); 1367 assert( *piEndChild>=*piStartChild );
1568 assert( isPrefix || *piStartChild==*piEndChild ); 1368 assert( isPrefix || *piStartChild==*piEndChild );
1569 + return rc; 1369 + return rc;
1570 } 1370 }
1571 1371
1572 /* Read block at iBlockid and pass it with other params to 1372 /* Read block at iBlockid and pass it with other params to
1573 @@ -5588,11 +5928,31 @@ 1373 @@ -5709,8 +5946,12 @@ static int loadAndGetChildrenContaining(
1574 if( rc!=SQLITE_OK ) return rc; 1374 return SQLITE_CORRUPT_BKPT;
1375 }
1575 1376
1576 rc = sqlite3_step(s); 1377 - getChildrenContaining(pData, nData, pTerm, nTerm,
1577 - if( rc==SQLITE_DONE ) return SQLITE_ERROR; 1378 - isPrefix, piStartChild, piEndChild);
1578 + /* Corrupt if interior node references missing child node. */
1579 + if( rc==SQLITE_DONE ) return SQLITE_CORRUPT_BKPT;
1580 if( rc!=SQLITE_ROW ) return rc;
1581
1582 - getChildrenContaining(sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0),
1583 - pTerm, nTerm, isPrefix, piStartChild, piEndChild);
1584 + /* Corrupt if child node isn't a blob. */
1585 + if( sqlite3_column_type(s, 0)!=SQLITE_BLOB ){
1586 + sqlite3_reset(s); /* So we don't leave a lock. */
1587 + return SQLITE_CORRUPT_BKPT;
1588 + }else{
1589 + const char *pData = sqlite3_column_blob(s, 0);
1590 + int nData = sqlite3_column_bytes(s, 0);
1591 +
1592 + /* Corrupt if child is not a valid interior node. */
1593 + if( pData==NULL || nData<1 || pData[0]=='\0' ){
1594 + sqlite3_reset(s); /* So we don't leave a lock. */
1595 + return SQLITE_CORRUPT_BKPT;
1596 + }
1597 +
1598 + rc = getChildrenContaining(pData, nData, pTerm, nTerm, 1379 + rc = getChildrenContaining(pData, nData, pTerm, nTerm,
1599 + isPrefix, piStartChild, piEndChild); 1380 + isPrefix, piStartChild, piEndChild);
1600 + if( rc!=SQLITE_OK ){ 1381 + if( rc!=SQLITE_OK ){
1601 + sqlite3_reset(s); 1382 + sqlite3_reset(s);
1602 + return rc; 1383 + return rc;
1603 + } 1384 + }
1604 + } 1385 }
1605 1386
1606 /* We expect only one row. We must execute another sqlite3_step() 1387 /* We expect only one row. We must execute another sqlite3_step()
1607 * to complete the iteration; otherwise the table will remain 1388 @@ -5741,8 +5982,9 @@ static int loadSegmentInt(fulltext_vtab *v, const char *pD ata, int nData,
1608 @@ -5622,8 +5982,9 @@
1609 /* Process pData as an interior node, then loop down the tree 1389 /* Process pData as an interior node, then loop down the tree
1610 ** until we find the set of leaf nodes to scan for the term. 1390 ** until we find the set of leaf nodes to scan for the term.
1611 */ 1391 */
1612 - getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, 1392 - getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix,
1613 - &iStartChild, &iEndChild); 1393 - &iStartChild, &iEndChild);
1614 + rc = getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, 1394 + rc = getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix,
1615 + &iStartChild, &iEndChild); 1395 + &iStartChild, &iEndChild);
1616 + if( rc!=SQLITE_OK ) return rc; 1396 + if( rc!=SQLITE_OK ) return rc;
1617 while( iStartChild>iLeavesEnd ){ 1397 while( iStartChild>iLeavesEnd ){
1618 sqlite_int64 iNextStart, iNextEnd; 1398 sqlite_int64 iNextStart, iNextEnd;
1619 rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix, 1399 rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix,
1620 @@ -5675,7 +6036,8 @@ 1400 @@ -5812,16 +6054,21 @@ static int loadSegment(fulltext_vtab *v, const char *pDa ta, int nData,
1621 DataBuffer result;
1622 int rc;
1623
1624 - assert( nData>1 );
1625 + /* Corrupt if segment root can't be valid. */
1626 + if( pData==NULL || nData<1 ) return SQLITE_CORRUPT_BKPT;
1627
1628 /* This code should never be called with buffered updates. */
1629 assert( v->nPendingData<0 );
1630 @@ -5692,16 +6054,21 @@
1631 DataBuffer merged; 1401 DataBuffer merged;
1632 DLReader readers[2]; 1402 DLReader readers[2];
1633 1403
1634 - dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); 1404 - dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData);
1635 - dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); 1405 - dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData);
1636 - dataBufferInit(&merged, out->nData+result.nData); 1406 - dataBufferInit(&merged, out->nData+result.nData);
1637 - docListMerge(&merged, readers, 2); 1407 - docListMerge(&merged, readers, 2);
1638 - dataBufferDestroy(out); 1408 - dataBufferDestroy(out);
1639 - *out = merged; 1409 - *out = merged;
1640 - dlrDestroy(&readers[0]); 1410 - dlrDestroy(&readers[0]);
1641 - dlrDestroy(&readers[1]); 1411 - dlrDestroy(&readers[1]);
1642 + rc = dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); 1412 + rc = dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData);
1643 + if( rc==SQLITE_OK ){ 1413 + if( rc==SQLITE_OK ){
1644 + rc = dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); 1414 + rc = dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData);
1645 + if( rc==SQLITE_OK ){ 1415 + if( rc==SQLITE_OK ){
1646 + dataBufferInit(&merged, out->nData+result.nData); 1416 + dataBufferInit(&merged, out->nData+result.nData);
1647 + rc = docListMerge(&merged, readers, 2); 1417 + rc = docListMerge(&merged, readers, 2);
1648 + dataBufferDestroy(out); 1418 + dataBufferDestroy(out);
1649 + *out = merged; 1419 + *out = merged;
1650 + dlrDestroy(&readers[1]); 1420 + dlrDestroy(&readers[1]);
1651 + } 1421 + }
1652 + dlrDestroy(&readers[0]); 1422 + dlrDestroy(&readers[0]);
1653 + } 1423 + }
1654 } 1424 }
1655 } 1425 }
1656 + 1426 +
1657 dataBufferDestroy(&result); 1427 dataBufferDestroy(&result);
1658 return rc; 1428 return rc;
1659 } 1429 }
1660 @@ -5729,11 +6096,20 @@ 1430 @@ -5862,6 +6109,7 @@ static int termSelect(fulltext_vtab *v, int iColumn,
1661 const char *pData = sqlite3_column_blob(s, 2);
1662 const int nData = sqlite3_column_bytes(s, 2);
1663 const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1);
1664 +
1665 + /* Corrupt if we get back different types than we stored. */
1666 + if( sqlite3_column_type(s, 1)!=SQLITE_INTEGER ||
1667 + sqlite3_column_type(s, 2)!=SQLITE_BLOB ){
1668 + rc = SQLITE_CORRUPT_BKPT;
1669 + goto err;
1670 + }
1671 +
1672 rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix,
1673 &doclist);
1674 if( rc!=SQLITE_OK ) goto err; 1431 if( rc!=SQLITE_OK ) goto err;
1675 } 1432 }
1676 if( rc==SQLITE_DONE ){ 1433 if( rc==SQLITE_DONE ){
1677 + rc = SQLITE_OK; 1434 + rc = SQLITE_OK;
1678 if( doclist.nData!=0 ){ 1435 if( doclist.nData!=0 ){
1679 /* TODO(shess) The old term_select_all() code applied the column 1436 /* TODO(shess) The old term_select_all() code applied the column
1680 ** restrict as we merged segments, leading to smaller buffers. 1437 ** restrict as we merged segments, leading to smaller buffers.
1681 @@ -5741,13 +6117,13 @@ 1438 @@ -5869,10 +6117,9 @@ static int termSelect(fulltext_vtab *v, int iColumn,
1682 ** system is checked in. 1439 ** system is checked in.
1683 */ 1440 */
1684 if( iColumn==v->nColumn) iColumn = -1; 1441 if( iColumn==v->nColumn) iColumn = -1;
1685 - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, 1442 - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
1686 - iColumn, iType, out); 1443 - iColumn, iType, out);
1687 + rc = docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, 1444 + rc = docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
1688 + iColumn, iType, out); 1445 + iColumn, iType, out);
1689 } 1446 }
1690 - rc = SQLITE_OK; 1447 - rc = SQLITE_OK;
1691 } 1448 }
1692 1449
1693 err: 1450 err:
1694 + sqlite3_reset(s); /* So we don't leave a lock. */ 1451 @@ -6218,6 +6465,7 @@ static int optimizeInternal(fulltext_vtab *v,
1695 dataBufferDestroy(&doclist);
1696 return rc;
1697 }
1698 @@ -6089,6 +6465,7 @@
1699 LeafWriter *pWriter){ 1452 LeafWriter *pWriter){
1700 int i, rc = SQLITE_OK; 1453 int i, rc = SQLITE_OK;
1701 DataBuffer doclist, merged, tmp; 1454 DataBuffer doclist, merged, tmp;
1702 + const char *pData; 1455 + const char *pData;
1703 1456
1704 /* Order the readers. */ 1457 /* Order the readers. */
1705 i = nReaders; 1458 i = nReaders;
1706 @@ -6109,14 +6486,21 @@ 1459 @@ -6238,14 +6486,21 @@ static int optimizeInternal(fulltext_vtab *v,
1707 if( 0!=optLeavesReaderTermCmp(&readers[0], &readers[i]) ) break; 1460 if( 0!=optLeavesReaderTermCmp(&readers[0], &readers[i]) ) break;
1708 } 1461 }
1709 1462
1710 + pData = optLeavesReaderData(&readers[0]); 1463 + pData = optLeavesReaderData(&readers[0]);
1711 + if( pData==NULL ){ 1464 + if( pData==NULL ){
1712 + rc = SQLITE_CORRUPT_BKPT; 1465 + rc = SQLITE_CORRUPT_BKPT;
1713 + break; 1466 + break;
1714 + } 1467 + }
1715 + 1468 +
1716 /* Special-case for no merge. */ 1469 /* Special-case for no merge. */
1717 if( i==1 ){ 1470 if( i==1 ){
1718 /* Trim deletions from the doclist. */ 1471 /* Trim deletions from the doclist. */
1719 dataBufferReset(&merged); 1472 dataBufferReset(&merged);
1720 - docListTrim(DL_DEFAULT, 1473 - docListTrim(DL_DEFAULT,
1721 - optLeavesReaderData(&readers[0]), 1474 - optLeavesReaderData(&readers[0]),
1722 - optLeavesReaderDataBytes(&readers[0]), 1475 - optLeavesReaderDataBytes(&readers[0]),
1723 - -1, DL_DEFAULT, &merged); 1476 - -1, DL_DEFAULT, &merged);
1724 + rc = docListTrim(DL_DEFAULT, 1477 + rc = docListTrim(DL_DEFAULT,
1725 + pData, 1478 + pData,
1726 + optLeavesReaderDataBytes(&readers[0]), 1479 + optLeavesReaderDataBytes(&readers[0]),
1727 + -1, DL_DEFAULT, &merged); 1480 + -1, DL_DEFAULT, &merged);
1728 + if( rc!= SQLITE_OK ) break; 1481 + if( rc!= SQLITE_OK ) break;
1729 }else{ 1482 }else{
1730 DLReader dlReaders[MERGE_COUNT]; 1483 DLReader dlReaders[MERGE_COUNT];
1731 int iReader, nReaders; 1484 int iReader, nReaders;
1732 @@ -6124,9 +6508,10 @@ 1485 @@ -6253,9 +6508,10 @@ static int optimizeInternal(fulltext_vtab *v,
1733 /* Prime the pipeline with the first reader's doclist. After 1486 /* Prime the pipeline with the first reader's doclist. After
1734 ** one pass index 0 will reference the accumulated doclist. 1487 ** one pass index 0 will reference the accumulated doclist.
1735 */ 1488 */
1736 - dlrInit(&dlReaders[0], DL_DEFAULT, 1489 - dlrInit(&dlReaders[0], DL_DEFAULT,
1737 - optLeavesReaderData(&readers[0]), 1490 - optLeavesReaderData(&readers[0]),
1738 - optLeavesReaderDataBytes(&readers[0])); 1491 - optLeavesReaderDataBytes(&readers[0]));
1739 + rc = dlrInit(&dlReaders[0], DL_DEFAULT, 1492 + rc = dlrInit(&dlReaders[0], DL_DEFAULT,
1740 + pData, 1493 + pData,
1741 + optLeavesReaderDataBytes(&readers[0])); 1494 + optLeavesReaderDataBytes(&readers[0]));
1742 + if( rc!=SQLITE_OK ) break; 1495 + if( rc!=SQLITE_OK ) break;
1743 iReader = 1; 1496 iReader = 1;
1744 1497
1745 assert( iReader<i ); /* Must execute the loop at least once. */ 1498 assert( iReader<i ); /* Must execute the loop at least once. */
1746 @@ -6134,24 +6519,35 @@ 1499 @@ -6263,24 +6519,35 @@ static int optimizeInternal(fulltext_vtab *v,
1747 /* Merge 16 inputs per pass. */ 1500 /* Merge 16 inputs per pass. */
1748 for( nReaders=1; iReader<i && nReaders<MERGE_COUNT; 1501 for( nReaders=1; iReader<i && nReaders<MERGE_COUNT;
1749 iReader++, nReaders++ ){ 1502 iReader++, nReaders++ ){
1750 - dlrInit(&dlReaders[nReaders], DL_DEFAULT, 1503 - dlrInit(&dlReaders[nReaders], DL_DEFAULT,
1751 - optLeavesReaderData(&readers[iReader]), 1504 - optLeavesReaderData(&readers[iReader]),
1752 - optLeavesReaderDataBytes(&readers[iReader])); 1505 - optLeavesReaderDataBytes(&readers[iReader]));
1753 + pData = optLeavesReaderData(&readers[iReader]); 1506 + pData = optLeavesReaderData(&readers[iReader]);
1754 + if( pData == NULL ){ 1507 + if( pData == NULL ){
1755 + rc = SQLITE_CORRUPT_BKPT; 1508 + rc = SQLITE_CORRUPT_BKPT;
1756 + break; 1509 + break;
(...skipping 24 matching lines...) Expand all
1781 1534
1782 + if( rc!=SQLITE_OK ) goto err; 1535 + if( rc!=SQLITE_OK ) goto err;
1783 + 1536 +
1784 /* Accumulated doclist to reader 0 for next pass. */ 1537 /* Accumulated doclist to reader 0 for next pass. */
1785 - dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData); 1538 - dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData);
1786 + rc = dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData); 1539 + rc = dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData);
1787 + if( rc!=SQLITE_OK ) goto err; 1540 + if( rc!=SQLITE_OK ) goto err;
1788 } 1541 }
1789 1542
1790 /* Destroy reader that was left in the pipeline. */ 1543 /* Destroy reader that was left in the pipeline. */
1791 @@ -6159,8 +6555,9 @@ 1544 @@ -6288,8 +6555,9 @@ static int optimizeInternal(fulltext_vtab *v,
1792 1545
1793 /* Trim deletions from the doclist. */ 1546 /* Trim deletions from the doclist. */
1794 dataBufferReset(&merged); 1547 dataBufferReset(&merged);
1795 - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, 1548 - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
1796 - -1, DL_DEFAULT, &merged); 1549 - -1, DL_DEFAULT, &merged);
1797 + rc = docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, 1550 + rc = docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
1798 + -1, DL_DEFAULT, &merged); 1551 + -1, DL_DEFAULT, &merged);
1799 + if( rc!=SQLITE_OK ) goto err; 1552 + if( rc!=SQLITE_OK ) goto err;
1800 } 1553 }
1801 1554
1802 /* Only pass doclists with hits (skip if all hits deleted). */ 1555 /* Only pass doclists with hits (skip if all hits deleted). */
1803 @@ -6240,6 +6637,14 @@ 1556 @@ -6628,16 +6896,19 @@ static void createDoclistResult(sqlite3_context *pContex t,
1804 const char *pRootData = sqlite3_column_blob(s, 2);
1805 int nRootData = sqlite3_column_bytes(s, 2);
1806
1807 + /* Corrupt if we get back different types than we stored. */
1808 + if( sqlite3_column_type(s, 0)!=SQLITE_INTEGER ||
1809 + sqlite3_column_type(s, 1)!=SQLITE_INTEGER ||
1810 + sqlite3_column_type(s, 2)!=SQLITE_BLOB ){
1811 + rc = SQLITE_CORRUPT_BKPT;
1812 + break;
1813 + }
1814 +
1815 assert( i<nReaders );
1816 rc = leavesReaderInit(v, -1, iStart, iEnd, pRootData, nRootData,
1817 &readers[i].reader);
1818 @@ -6253,6 +6658,8 @@
1819 if( rc==SQLITE_DONE ){
1820 assert( i==nReaders );
1821 rc = optimizeInternal(v, readers, nReaders, &writer);
1822 + }else{
1823 + sqlite3_reset(s); /* So we don't leave a lock. */
1824 }
1825
1826 while( i-- > 0 ){
1827 @@ -6316,9 +6723,18 @@
1828 const sqlite_int64 iEndBlockid = sqlite3_column_int64(s, 1);
1829 const char *pRootData = sqlite3_column_blob(s, 2);
1830 const int nRootData = sqlite3_column_bytes(s, 2);
1831 + int rc;
1832 LeavesReader reader;
1833 - int rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid,
1834 - pRootData, nRootData, &reader);
1835 +
1836 + /* Corrupt if we get back different types than we stored. */
1837 + if( sqlite3_column_type(s, 0)!=SQLITE_INTEGER ||
1838 + sqlite3_column_type(s, 1)!=SQLITE_INTEGER ||
1839 + sqlite3_column_type(s, 2)!=SQLITE_BLOB ){
1840 + return SQLITE_CORRUPT_BKPT;
1841 + }
1842 +
1843 + rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid,
1844 + pRootData, nRootData, &reader);
1845 if( rc!=SQLITE_OK ) return rc;
1846
1847 while( rc==SQLITE_OK && !leavesReaderAtEnd(&reader) ){
1848 @@ -6480,16 +6896,19 @@
1849 const char *pData, int nData){ 1557 const char *pData, int nData){
1850 DataBuffer dump; 1558 DataBuffer dump;
1851 DLReader dlReader; 1559 DLReader dlReader;
1852 + int rc; 1560 + int rc;
1853 1561
1854 assert( pData!=NULL && nData>0 ); 1562 assert( pData!=NULL && nData>0 );
1855 1563
1856 + rc = dlrInit(&dlReader, DL_DEFAULT, pData, nData); 1564 + rc = dlrInit(&dlReader, DL_DEFAULT, pData, nData);
1857 + if( rc!=SQLITE_OK ) return rc; 1565 + if( rc!=SQLITE_OK ) return;
1858 dataBufferInit(&dump, 0); 1566 dataBufferInit(&dump, 0);
1859 - dlrInit(&dlReader, DL_DEFAULT, pData, nData); 1567 - dlrInit(&dlReader, DL_DEFAULT, pData, nData);
1860 - for( ; !dlrAtEnd(&dlReader); dlrStep(&dlReader) ){ 1568 - for( ; !dlrAtEnd(&dlReader); dlrStep(&dlReader) ){
1861 + for( ; rc==SQLITE_OK && !dlrAtEnd(&dlReader); rc = dlrStep(&dlReader) ){ 1569 + for( ; rc==SQLITE_OK && !dlrAtEnd(&dlReader); rc = dlrStep(&dlReader) ){
1862 char buf[256]; 1570 char buf[256];
1863 PLReader plReader; 1571 PLReader plReader;
1864 1572
1865 - plrInit(&plReader, &dlReader); 1573 - plrInit(&plReader, &dlReader);
1866 + rc = plrInit(&plReader, &dlReader); 1574 + rc = plrInit(&plReader, &dlReader);
1867 + if( rc!=SQLITE_OK ) break; 1575 + if( rc!=SQLITE_OK ) break;
1868 if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){ 1576 if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){
1869 sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader)); 1577 sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader));
1870 dataBufferAppend(&dump, buf, strlen(buf)); 1578 dataBufferAppend(&dump, buf, strlen(buf));
1871 @@ -6500,7 +6919,8 @@ 1579 @@ -6648,7 +6919,8 @@ static void createDoclistResult(sqlite3_context *pContext,
1872 dlrDocid(&dlReader), iColumn); 1580 dlrDocid(&dlReader), iColumn);
1873 dataBufferAppend(&dump, buf, strlen(buf)); 1581 dataBufferAppend(&dump, buf, strlen(buf));
1874 1582
1875 - for( ; !plrAtEnd(&plReader); plrStep(&plReader) ){ 1583 - for( ; !plrAtEnd(&plReader); plrStep(&plReader) ){
1876 + for( ; !plrAtEnd(&plReader); rc = plrStep(&plReader) ){ 1584 + for( ; !plrAtEnd(&plReader); rc = plrStep(&plReader) ){
1877 + if( rc!=SQLITE_OK ) break; 1585 + if( rc!=SQLITE_OK ) break;
1878 if( plrColumn(&plReader)!=iColumn ){ 1586 if( plrColumn(&plReader)!=iColumn ){
1879 iColumn = plrColumn(&plReader); 1587 iColumn = plrColumn(&plReader);
1880 sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn); 1588 sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn);
1881 @@ -6521,6 +6941,7 @@ 1589 @@ -6669,6 +6941,7 @@ static void createDoclistResult(sqlite3_context *pContext,
1882 dataBufferAppend(&dump, buf, strlen(buf)); 1590 dataBufferAppend(&dump, buf, strlen(buf));
1883 } 1591 }
1884 plrDestroy(&plReader); 1592 plrDestroy(&plReader);
1885 + if( rc!= SQLITE_OK ) break; 1593 + if( rc!= SQLITE_OK ) break;
1886 1594
1887 assert( dump.nData>0 ); 1595 assert( dump.nData>0 );
1888 dump.nData--; /* Overwrite trailing space. */ 1596 dump.nData--; /* Overwrite trailing space. */
1889 @@ -6529,6 +6950,10 @@ 1597 @@ -6677,6 +6950,10 @@ static void createDoclistResult(sqlite3_context *pContext ,
1890 } 1598 }
1891 } 1599 }
1892 dlrDestroy(&dlReader); 1600 dlrDestroy(&dlReader);
1893 + if( rc!=SQLITE_OK ){ 1601 + if( rc!=SQLITE_OK ){
1894 + dataBufferDestroy(&dump); 1602 + dataBufferDestroy(&dump);
1895 + return rc; 1603 + return;
1896 + } 1604 + }
1897 1605
1898 assert( dump.nData>0 ); 1606 assert( dump.nData>0 );
1899 dump.nData--; /* Overwrite trailing space. */ 1607 dump.nData--; /* Overwrite trailing space. */
1900 @@ -6540,6 +6965,7 @@ 1608 --
1901 sqlite3_result_text(pContext, dump.pData, dump.nData, sqlite3_free); 1609 2.2.1
1902 dump.pData = NULL; 1610
1903 dump.nData = dump.nCapacity = 0;
1904 + return SQLITE_OK;
1905 }
1906
1907 /* Implements dump_doclist() for use in inspecting the fts2 index from
1908 @@ -6822,7 +7248,11 @@
1909 ** module with sqlite.
1910 */
1911 if( SQLITE_OK==rc
1912 +#if GEARS_FTS2_CHANGES && !SQLITE_TEST
1913 + /* fts2_tokenizer() disabled for security reasons. */
1914 +#else
1915 && SQLITE_OK==(rc = sqlite3Fts2InitHashTable(db, pHash, "fts2_tokenizer"))
1916 +#endif
1917 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
1918 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1))
1919 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", -1))
1920 diff -ru ext-orig/fts2/fts2_icu.c ext/fts2/fts2_icu.c
1921 --- ext-orig/fts2/fts2_icu.c» 2009-09-03 13:32:06.000000000 -0700
1922 +++ ext/fts2/fts2_icu.c»2009-09-18 14:39:41.000000000 -0700
1923 @@ -198,7 +198,7 @@
1924
1925 while( iStart<iEnd ){
1926 int iWhite = iStart;
1927 - U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
1928 + U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
1929 if( u_isspace(c) ){
1930 iStart = iWhite;
1931 }else{
1932 diff -ru ext-orig/fts2/fts2_tokenizer.c ext/fts2/fts2_tokenizer.c
1933 --- ext-orig/fts2/fts2_tokenizer.c» 2009-09-03 13:32:06.000000000 -0700
1934 +++ ext/fts2/fts2_tokenizer.c» 2009-09-18 14:39:41.000000000 -0700
1935 @@ -28,11 +28,14 @@
1936
1937 #include "sqlite3.h"
1938 #include "sqlite3ext.h"
1939 -SQLITE_EXTENSION_INIT1
1940 +#ifndef SQLITE_CORE
1941 + SQLITE_EXTENSION_INIT1
1942 +#endif
1943
1944 #include "fts2_hash.h"
1945 #include "fts2_tokenizer.h"
1946 #include <assert.h>
1947 +#include <stddef.h>
1948
1949 /*
1950 ** Implementation of the SQL scalar function for accessing the underlying
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698