/ Check-in [b80119bd]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix another buffer overrun that could occur when quering a corrupt database using an fts5vocab table.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b80119bd6044c0993210115732b6ae7b4b0b29c1471b0723bef24f2052c8e671
User & Date: dan 2019-01-31 14:37:18
Context
2019-01-31
15:38
Add the SQLITE_CONFIG_MEMDB_MAXSIZE configuration option for configuring the default maximum size of an in-memory database created using sqlite3_deserialize(). This is necessary to make the interface reasonably testable. check-in: cb72ee04 user: drh tags: trunk
14:37
Fix another buffer overrun that could occur when quering a corrupt database using an fts5vocab table. check-in: b80119bd user: dan tags: trunk
02:42
If the amalgamation file "sqlite3.c" is generated using the SQLITE_ENABLE_UPDATE_DELETE_LIMIT option, but then subsequently (mis-)compiled without that option, and then a particular syntax error could cause a memory leak, but for this fix. check-in: 1bb17c86 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts5/fts5_index.c.

  1648   1648   */
  1649   1649   static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
  1650   1650     u8 *a = pIter->pLeaf->p;        /* Buffer to read data from */
  1651   1651     int iOff = pIter->iLeafOffset;  /* Offset to read at */
  1652   1652     int nNew;                       /* Bytes of new data */
  1653   1653   
  1654   1654     iOff += fts5GetVarint32(&a[iOff], nNew);
  1655         -  if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n ){
         1655  +  if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
  1656   1656       p->rc = FTS5_CORRUPT;
  1657   1657       return;
  1658   1658     }
  1659   1659     pIter->term.n = nKeep;
  1660   1660     fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
  1661   1661     assert( pIter->term.n<=pIter->term.nSpace );
  1662   1662     iOff += nNew;

Changes to ext/fts5/fts5_vocab.c.

   480    480       if( sqlite3Fts5IterEof(pCsr->pIter) ){
   481    481         pCsr->bEof = 1;
   482    482       }else{
   483    483         const char *zTerm;
   484    484         int nTerm;
   485    485   
   486    486         zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
          487  +      assert( nTerm>=0 );
   487    488         if( pCsr->nLeTerm>=0 ){
   488    489           int nCmp = MIN(nTerm, pCsr->nLeTerm);
   489    490           int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
   490    491           if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
   491    492             pCsr->bEof = 1;
   492    493             return SQLITE_OK;
   493    494           }

Changes to ext/fts5/test/fts5corrupt3.test.

  6636   6636   |   4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 5d 69 71 a5   uild....opti]iq.
  6637   6637   | end crash-44a8305b4bd86f.db
  6638   6638   }]} {}
  6639   6639   
  6640   6640   do_catchsql_test 48.1 {
  6641   6641     INSERT INTO t1(t1) VALUES('integrity-check');
  6642   6642   } {1 {database disk image is malformed}}
         6643  +
         6644  +#--------------------------------------------------------------------------
         6645  +reset_db
         6646  +do_test 49.0 {
         6647  +  sqlite3 db {}
         6648  +  db deserialize [decode_hexdb {
         6649  +| size 40960 pagesize 4096 filename crash-fd87385402ecf5.db
         6650  +| page 1 offset 0
         6651  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         6652  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a   .....@  ........
         6653  +|     32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04   ................
         6654  +|     48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00   ................
         6655  +|     96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c   ..........n....L
         6656  +|    112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50   .....$...r.....P
         6657  +|    128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00   .....n..........
         6658  +|   2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d   ..............C.
         6659  +|   2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43   .....utablet4t4C
         6660  +|   2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41   REATE VIRTUAL TA
         6661  +|   2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73   BLE t4 USING fts
         6662  +|   2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f   5vocab('t2', 'ro
         6663  +|   2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65   w')C......utable
         6664  +|   3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55   t3t3CREATE VIRTU
         6665  +|   3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e   AL TABLE t3 USIN
         6666  +|   3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27   G fts5vocab('t1'
         6667  +|   3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d   , 'row')V.......
         6668  +|   3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32   tablet2_configt2
         6669  +|   3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54   _config.CREATE T
         6670  +|   3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27   ABLE 't2_config'
         6671  +|   3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   (k PRIMARY KEY, 
         6672  +|   3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44   v) WITHOUT ROWID
         6673  +|   3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32   ^...!!...tablet2
         6674  +|   3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65   _contentt2_conte
         6675  +|   3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20   nt.CREATE TABLE 
         6676  +|   3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20   't2_content'(id 
         6677  +|   3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20   INTEGER PRIMARY 
         6678  +|   3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29   KEY, c0, c1, c2)
         6679  +|   3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32   i.......-tablet2
         6680  +|   3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54   _idxt2_idx.CREAT
         6681  +|   3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27   E TABLE 't2_idx'
         6682  +|   3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67   (segid, term, pg
         6683  +|   3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28   no, PRIMARY KEY(
         6684  +|   3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49   segid, term)) WI
         6685  +|   3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b   THOUT ROWIDU....
         6686  +|   3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61   ....tablet2_data
         6687  +|   3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54   t2_data.CREATE T
         6688  +|   3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69   ABLE 't2_data'(i
         6689  +|   3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52   d INTEGER PRIMAR
         6690  +|   3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f   Y KEY, block BLO
         6691  +|   3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65   B)X........table
         6692  +|   3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55   t2t2CREATE VIRTU
         6693  +|   3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e   AL TABLE t2 USIN
         6694  +|   3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22   G fts5('a',[b],.
         6695  +|   3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63   c.,detail=none,c
         6696  +|   3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17   olumnsize=0)V...
         6697  +|   3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66   ....tablet1_conf
         6698  +|   3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41   igt1_config.CREA
         6699  +|   3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e   TE TABLE 't1_con
         6700  +|   3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b   fig'(k PRIMARY K
         6701  +|   3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52   EY, v) WITHOUT R
         6702  +|   3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62   OWID[...!!...tab
         6703  +|   3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64   let1_docsizet1_d
         6704  +|   3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41   ocsize.CREATE TA
         6705  +|   3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27   BLE 't1_docsize'
         6706  +|   3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d   (id INTEGER PRIM
         6707  +|   3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42   ARY KEY, sz BLOB
         6708  +|   3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74   )^...!!...tablet
         6709  +|   3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74   1_contentt1_cont
         6710  +|   3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45   ent.CREATE TABLE
         6711  +|   3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64    't1_content'(id
         6712  +|   3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59    INTEGER PRIMARY
         6713  +|   3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32    KEY, c0, c1, c2
         6714  +|   3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74   )i.......-tablet
         6715  +|   3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41   1_idxt1_idx.CREA
         6716  +|   3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78   TE TABLE 't1_idx
         6717  +|   3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70   '(segid, term, p
         6718  +|   3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59   gno, PRIMARY KEY
         6719  +|   3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57   (segid, term)) W
         6720  +|   3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17   ITHOUT ROWIDU...
         6721  +|   3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74   .....tablet1_dat
         6722  +|   3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20   at1_data.CREATE 
         6723  +|   3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28   TABLE 't1_data'(
         6724  +|   3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41   id INTEGER PRIMA
         6725  +|   3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c   RY KEY, block BL
         6726  +|   4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c   OB)[.......#tabl
         6727  +|   4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54   et1t1CREATE VIRT
         6728  +|   4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49   UAL TABLE t1 USI
         6729  +|   4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e   NG fts5(a,b unin
         6730  +|   4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65   dexed,c,tokenize
         6731  +|   4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29   =.porter ascii.)
         6732  +| page 2 offset 4096
         6733  +|      0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c   ..h............|
         6734  +|     16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00   .*..............
         6735  +|   3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00   .......0........
         6736  +|   3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80   ..........7.....
         6737  +|   3888: 01 03 00 74 00 20 68 20 69 0d 00 00 00 03 0f e8   ...t. h i.......
         6738  +|   3904: 00 0f f8 0f f0 0f e8 00 00 00 00 00 00 00 00 00   ................
         6739  +| page 5 offset 16384
         6740  +|   4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 00   ................
         6741  +|   4080: 60 20 30 d6 20 30 00 30 60 10 30 01 20 30 00 30   ` 0. 0.0`.0. 0.0
         6742  +| page 6 offset 20480
         6743  +|      0: a0 00 00 00 10 ff 40 00 ff 00 00 00 00 00 00 00   ......@.........
         6744  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         6745  +| page 7 offset 24576
         6746  +|      0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00   ................
         6747  +|   3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84   ..............A.
         6748  +|   4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61   ............4.0a
         6749  +|   4016: 01 01 00 00 00 00 00 00 00 00 00 11 87 89 06 26   ...............&
         6750  +|   4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01   .d...e...f...g..
         6751  +|   4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06   ...h.....i......
         6752  +|   4064: 06 04 44 00 06 06 07 01 03 00 14 03 09 09 09 0f   ..D.............
         6753  +|   4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01   ...$............
         6754  +| page 8 offset 28672
         6755  +|      0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00   ................
         6756  +|   4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02   ................
         6757  +| page 9 offset 32768
         6758  +|      0: 0d 00 00 00 9d 0f be 00 0f ea 0f d4 0f be 00 00   ................
         6759  +|   4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03   ................
         6760  +|   4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67   .....a b cg h ig
         6761  +|   4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69    h i.......g h i
         6762  +|   4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17   a b cg h i......
         6763  +|   4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69   .a b cd e fg h i
         6764  +| page 10 offset 36864
         6765  +|      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
         6766  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         6767  +| end crash-fd87385402ecf5.db
         6768  +}]} {}
         6769  +
         6770  +do_catchsql_test 49.1 {
         6771  +  SELECT term FROM t4 WHERE term LIKE 'oase';
         6772  +} {1 {database disk image is malformed}}
  6643   6773   
  6644   6774   
  6645   6775   sqlite3_fts5_may_be_corrupt 0
  6646   6776   finish_test
  6647   6777