| | @@ -1162,11 +1162,11 @@ |
| 1162 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1164 | */ |
| 1165 | 1165 | #define SQLITE_VERSION "3.27.0" |
| 1166 | 1166 | #define SQLITE_VERSION_NUMBER 3027000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2019-01-21 17:57:31 505ed9a47825240979338a24044559613fbbd2a7850bdff70c7164da054ec63d" |
| 1167 | +#define SQLITE_SOURCE_ID "2019-01-27 02:45:32 9cf8ebd141aa2eb661d457624c76433bd9e4abfdef04aa52e28bc169172calt1" |
| 1168 | 1168 | |
| 1169 | 1169 | /* |
| 1170 | 1170 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1171 | 1171 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1172 | 1172 | ** |
| | @@ -1860,10 +1860,19 @@ |
| 1860 | 1860 | ** current transaction. This hint is not guaranteed to be accurate but it |
| 1861 | 1861 | ** is often close. The underlying VFS might choose to preallocate database |
| 1862 | 1862 | ** file space based on this hint in order to help writes to the database |
| 1863 | 1863 | ** file run faster. |
| 1864 | 1864 | ** |
| 1865 | +** <li>[[SQLITE_FCNTL_SIZE_LIMIT]] |
| 1866 | +** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that |
| 1867 | +** implements [sqlite3_deserialize()] to set an upper bound on the size |
| 1868 | +** of the in-memory database. The argument is a pointer to a [sqlite3_int64]. |
| 1869 | +** If the integer pointed to is negative, then it is filled in with the |
| 1870 | +** current limit. Otherwise the limit is set to the larger of the value |
| 1871 | +** of the integer pointed to and the current database size. The integer |
| 1872 | +** pointed to is set to the new limit. |
| 1873 | +** |
| 1865 | 1874 | ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]] |
| 1866 | 1875 | ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS |
| 1867 | 1876 | ** extends and truncates the database file in chunks of a size specified |
| 1868 | 1877 | ** by the user. The fourth argument to [sqlite3_file_control()] should |
| 1869 | 1878 | ** point to an integer (type int) containing the new chunk-size to use |
| | @@ -2168,10 +2177,11 @@ |
| 2168 | 2177 | #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31 |
| 2169 | 2178 | #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32 |
| 2170 | 2179 | #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33 |
| 2171 | 2180 | #define SQLITE_FCNTL_LOCK_TIMEOUT 34 |
| 2172 | 2181 | #define SQLITE_FCNTL_DATA_VERSION 35 |
| 2182 | +#define SQLITE_FCNTL_SIZE_LIMIT 36 |
| 2173 | 2183 | |
| 2174 | 2184 | /* deprecated names */ |
| 2175 | 2185 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 2176 | 2186 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 2177 | 2187 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -12272,16 +12282,12 @@ |
| 12272 | 12282 | ** should be greater than or equal to zero and smaller than the value |
| 12273 | 12283 | ** output by xInstCount(). |
| 12274 | 12284 | ** |
| 12275 | 12285 | ** Usually, output parameter *piPhrase is set to the phrase number, *piCol |
| 12276 | 12286 | ** to the column in which it occurs and *piOff the token offset of the |
| 12277 | | -** first token of the phrase. The exception is if the table was created |
| 12278 | | -** with the offsets=0 option specified. In this case *piOff is always |
| 12279 | | -** set to -1. |
| 12280 | | -** |
| 12281 | | -** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM) |
| 12282 | | -** if an error occurs. |
| 12287 | +** first token of the phrase. Returns SQLITE_OK if successful, or an error |
| 12288 | +** code (i.e. SQLITE_NOMEM) if an error occurs. |
| 12283 | 12289 | ** |
| 12284 | 12290 | ** This API can be quite slow if used with an FTS5 table created with the |
| 12285 | 12291 | ** "detail=none" or "detail=column" option. |
| 12286 | 12292 | ** |
| 12287 | 12293 | ** xRowid: |
| | @@ -46564,16 +46570,22 @@ |
| 46564 | 46570 | |
| 46565 | 46571 | /* An open file */ |
| 46566 | 46572 | struct MemFile { |
| 46567 | 46573 | sqlite3_file base; /* IO methods */ |
| 46568 | 46574 | sqlite3_int64 sz; /* Size of the file */ |
| 46569 | | - sqlite3_int64 szMax; /* Space allocated to aData */ |
| 46575 | + sqlite3_int64 szAlloc; /* Space allocated to aData */ |
| 46576 | + sqlite3_int64 szMax; /* Maximum allowed size of the file */ |
| 46570 | 46577 | unsigned char *aData; /* content of the file */ |
| 46571 | 46578 | int nMmap; /* Number of memory mapped pages */ |
| 46572 | 46579 | unsigned mFlags; /* Flags */ |
| 46573 | 46580 | int eLock; /* Most recent lock against this file */ |
| 46574 | 46581 | }; |
| 46582 | + |
| 46583 | +/* The default maximum size of an in-memory database */ |
| 46584 | +#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE |
| 46585 | +# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824 |
| 46586 | +#endif |
| 46575 | 46587 | |
| 46576 | 46588 | /* |
| 46577 | 46589 | ** Methods for MemFile |
| 46578 | 46590 | */ |
| 46579 | 46591 | static int memdbClose(sqlite3_file*); |
| | @@ -46690,14 +46702,19 @@ |
| 46690 | 46702 | static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){ |
| 46691 | 46703 | unsigned char *pNew; |
| 46692 | 46704 | if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){ |
| 46693 | 46705 | return SQLITE_FULL; |
| 46694 | 46706 | } |
| 46707 | + if( newSz>p->szMax ){ |
| 46708 | + return SQLITE_FULL; |
| 46709 | + } |
| 46710 | + newSz *= 2; |
| 46711 | + if( newSz>p->szMax ) newSz = p->szMax; |
| 46695 | 46712 | pNew = sqlite3_realloc64(p->aData, newSz); |
| 46696 | 46713 | if( pNew==0 ) return SQLITE_NOMEM; |
| 46697 | 46714 | p->aData = pNew; |
| 46698 | | - p->szMax = newSz; |
| 46715 | + p->szAlloc = newSz; |
| 46699 | 46716 | return SQLITE_OK; |
| 46700 | 46717 | } |
| 46701 | 46718 | |
| 46702 | 46719 | /* |
| 46703 | 46720 | ** Write data to an memdb-file. |
| | @@ -46707,14 +46724,15 @@ |
| 46707 | 46724 | const void *z, |
| 46708 | 46725 | int iAmt, |
| 46709 | 46726 | sqlite_int64 iOfst |
| 46710 | 46727 | ){ |
| 46711 | 46728 | MemFile *p = (MemFile *)pFile; |
| 46729 | + if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY; |
| 46712 | 46730 | if( iOfst+iAmt>p->sz ){ |
| 46713 | 46731 | int rc; |
| 46714 | | - if( iOfst+iAmt>p->szMax |
| 46715 | | - && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK |
| 46732 | + if( iOfst+iAmt>p->szAlloc |
| 46733 | + && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK |
| 46716 | 46734 | ){ |
| 46717 | 46735 | return rc; |
| 46718 | 46736 | } |
| 46719 | 46737 | if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz); |
| 46720 | 46738 | p->sz = iOfst+iAmt; |
| | @@ -46756,10 +46774,15 @@ |
| 46756 | 46774 | /* |
| 46757 | 46775 | ** Lock an memdb-file. |
| 46758 | 46776 | */ |
| 46759 | 46777 | static int memdbLock(sqlite3_file *pFile, int eLock){ |
| 46760 | 46778 | MemFile *p = (MemFile *)pFile; |
| 46779 | + if( eLock>SQLITE_LOCK_SHARED |
| 46780 | + && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0 |
| 46781 | + ){ |
| 46782 | + return SQLITE_READONLY; |
| 46783 | + } |
| 46761 | 46784 | p->eLock = eLock; |
| 46762 | 46785 | return SQLITE_OK; |
| 46763 | 46786 | } |
| 46764 | 46787 | |
| 46765 | 46788 | #if 0 /* Never used because memdbAccess() always returns false */ |
| | @@ -46779,10 +46802,23 @@ |
| 46779 | 46802 | MemFile *p = (MemFile *)pFile; |
| 46780 | 46803 | int rc = SQLITE_NOTFOUND; |
| 46781 | 46804 | if( op==SQLITE_FCNTL_VFSNAME ){ |
| 46782 | 46805 | *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz); |
| 46783 | 46806 | rc = SQLITE_OK; |
| 46807 | + } |
| 46808 | + if( op==SQLITE_FCNTL_SIZE_LIMIT ){ |
| 46809 | + sqlite3_int64 iLimit = *(sqlite3_int64*)pArg; |
| 46810 | + if( iLimit<p->sz ){ |
| 46811 | + if( iLimit<0 ){ |
| 46812 | + iLimit = p->szMax; |
| 46813 | + }else{ |
| 46814 | + iLimit = p->sz; |
| 46815 | + } |
| 46816 | + } |
| 46817 | + p->szMax = iLimit; |
| 46818 | + *(sqlite3_int64*)pArg = iLimit; |
| 46819 | + rc = SQLITE_OK; |
| 46784 | 46820 | } |
| 46785 | 46821 | return rc; |
| 46786 | 46822 | } |
| 46787 | 46823 | |
| 46788 | 46824 | #if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
| | @@ -46810,12 +46846,17 @@ |
| 46810 | 46846 | sqlite3_int64 iOfst, |
| 46811 | 46847 | int iAmt, |
| 46812 | 46848 | void **pp |
| 46813 | 46849 | ){ |
| 46814 | 46850 | MemFile *p = (MemFile *)pFile; |
| 46815 | | - p->nMmap++; |
| 46816 | | - *pp = (void*)(p->aData + iOfst); |
| 46851 | + if( iOfst+iAmt>p->sz ){ |
| 46852 | + assert( CORRUPT_DB ); |
| 46853 | + *pp = 0; |
| 46854 | + }else{ |
| 46855 | + p->nMmap++; |
| 46856 | + *pp = (void*)(p->aData + iOfst); |
| 46857 | + } |
| 46817 | 46858 | return SQLITE_OK; |
| 46818 | 46859 | } |
| 46819 | 46860 | |
| 46820 | 46861 | /* Release a memory-mapped page */ |
| 46821 | 46862 | static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
| | @@ -46841,10 +46882,11 @@ |
| 46841 | 46882 | memset(p, 0, sizeof(*p)); |
| 46842 | 46883 | p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; |
| 46843 | 46884 | assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */ |
| 46844 | 46885 | *pOutFlags = flags | SQLITE_OPEN_MEMORY; |
| 46845 | 46886 | p->base.pMethods = &memdb_io_methods; |
| 46887 | + p->szMax = SQLITE_MEMDB_DEFAULT_MAXSIZE; |
| 46846 | 46888 | return SQLITE_OK; |
| 46847 | 46889 | } |
| 46848 | 46890 | |
| 46849 | 46891 | #if 0 /* Only used to delete rollback journals, master journals, and WAL |
| 46850 | 46892 | ** files, none of which exist in memdb. So this routine is never used */ |
| | @@ -47090,11 +47132,15 @@ |
| 47090 | 47132 | if( p==0 ){ |
| 47091 | 47133 | rc = SQLITE_ERROR; |
| 47092 | 47134 | }else{ |
| 47093 | 47135 | p->aData = pData; |
| 47094 | 47136 | p->sz = szDb; |
| 47137 | + p->szAlloc = szBuf; |
| 47095 | 47138 | p->szMax = szBuf; |
| 47139 | + if( p->szMax<SQLITE_MEMDB_DEFAULT_MAXSIZE ){ |
| 47140 | + p->szMax = SQLITE_MEMDB_DEFAULT_MAXSIZE; |
| 47141 | + } |
| 47096 | 47142 | p->mFlags = mFlags; |
| 47097 | 47143 | rc = SQLITE_OK; |
| 47098 | 47144 | } |
| 47099 | 47145 | |
| 47100 | 47146 | end_deserialize: |
| | @@ -63737,15 +63783,16 @@ |
| 63737 | 63783 | ){ |
| 63738 | 63784 | int rc; /* Status code */ |
| 63739 | 63785 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
| 63740 | 63786 | |
| 63741 | 63787 | if( pKey ){ |
| 63788 | + KeyInfo *pKeyInfo = pCur->pKeyInfo; |
| 63742 | 63789 | assert( nKey==(i64)(int)nKey ); |
| 63743 | | - pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo); |
| 63790 | + pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); |
| 63744 | 63791 | if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT; |
| 63745 | | - sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey); |
| 63746 | | - if( pIdxKey->nField==0 ){ |
| 63792 | + sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey); |
| 63793 | + if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){ |
| 63747 | 63794 | rc = SQLITE_CORRUPT_BKPT; |
| 63748 | 63795 | goto moveto_done; |
| 63749 | 63796 | } |
| 63750 | 63797 | }else{ |
| 63751 | 63798 | pIdxKey = 0; |
| | @@ -68408,11 +68455,11 @@ |
| 68408 | 68455 | nCell = (int)pCur->info.nKey; |
| 68409 | 68456 | testcase( nCell<0 ); /* True if key size is 2^32 or more */ |
| 68410 | 68457 | testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ |
| 68411 | 68458 | testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ |
| 68412 | 68459 | testcase( nCell==2 ); /* Minimum legal index key size */ |
| 68413 | | - if( nCell<2 ){ |
| 68460 | + if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){ |
| 68414 | 68461 | rc = SQLITE_CORRUPT_PAGE(pPage); |
| 68415 | 68462 | goto moveto_finish; |
| 68416 | 68463 | } |
| 68417 | 68464 | pCellKey = sqlite3Malloc( nCell+18 ); |
| 68418 | 68465 | if( pCellKey==0 ){ |
| | @@ -69042,11 +69089,11 @@ |
| 69042 | 69089 | *ppPage = 0; |
| 69043 | 69090 | } |
| 69044 | 69091 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
| 69045 | 69092 | } |
| 69046 | 69093 | |
| 69047 | | - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 69094 | + assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 69048 | 69095 | |
| 69049 | 69096 | end_allocate_page: |
| 69050 | 69097 | releasePage(pTrunk); |
| 69051 | 69098 | releasePage(pPrevTrunk); |
| 69052 | 69099 | assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 ); |
| | @@ -69626,20 +69673,85 @@ |
| 69626 | 69673 | } |
| 69627 | 69674 | #endif |
| 69628 | 69675 | } |
| 69629 | 69676 | } |
| 69630 | 69677 | |
| 69678 | +/* |
| 69679 | +** The following parameters determine how many adjacent pages get involved |
| 69680 | +** in a balancing operation. NN is the number of neighbors on either side |
| 69681 | +** of the page that participate in the balancing operation. NB is the |
| 69682 | +** total number of pages that participate, including the target page and |
| 69683 | +** NN neighbors on either side. |
| 69684 | +** |
| 69685 | +** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 69686 | +** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 69687 | +** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 69688 | +** The value of NN appears to give the best results overall. |
| 69689 | +** |
| 69690 | +** (Later:) The description above makes it seem as if these values are |
| 69691 | +** tunable - as if you could change them and recompile and it would all work. |
| 69692 | +** But that is unlikely. NB has been 3 since the inception of SQLite and |
| 69693 | +** we have never tested any other value. |
| 69694 | +*/ |
| 69695 | +#define NN 1 /* Number of neighbors on either side of pPage */ |
| 69696 | +#define NB 3 /* (NN*2+1): Total pages involved in the balance */ |
| 69697 | + |
| 69631 | 69698 | /* |
| 69632 | 69699 | ** A CellArray object contains a cache of pointers and sizes for a |
| 69633 | 69700 | ** consecutive sequence of cells that might be held on multiple pages. |
| 69701 | +** |
| 69702 | +** The cells in this array are the divider cell or cells from the pParent |
| 69703 | +** page plus up to three child pages. There are a total of nCell cells. |
| 69704 | +** |
| 69705 | +** pRef is a pointer to one of the pages that contributes cells. This is |
| 69706 | +** used to access information such as MemPage.intKey and MemPage.pBt->pageSize |
| 69707 | +** which should be common to all pages that contribute cells to this array. |
| 69708 | +** |
| 69709 | +** apCell[] and szCell[] hold, respectively, pointers to the start of each |
| 69710 | +** cell and the size of each cell. Some of the apCell[] pointers might refer |
| 69711 | +** to overflow cells. In other words, some apCel[] pointers might not point |
| 69712 | +** to content area of the pages. |
| 69713 | +** |
| 69714 | +** A szCell[] of zero means the size of that cell has not yet been computed. |
| 69715 | +** |
| 69716 | +** The cells come from as many as four different pages: |
| 69717 | +** |
| 69718 | +** ----------- |
| 69719 | +** | Parent | |
| 69720 | +** ----------- |
| 69721 | +** / | \ |
| 69722 | +** / | \ |
| 69723 | +** --------- --------- --------- |
| 69724 | +** |Child-1| |Child-2| |Child-3| |
| 69725 | +** --------- --------- --------- |
| 69726 | +** |
| 69727 | +** The order of cells is in the array is: |
| 69728 | +** |
| 69729 | +** 1. All cells from Child-1 in order |
| 69730 | +** 2. The first divider cell from Parent |
| 69731 | +** 3. All cells from Child-2 in order |
| 69732 | +** 4. The second divider cell from Parent |
| 69733 | +** 5. All cells from Child-3 in order |
| 69734 | +** |
| 69735 | +** The apEnd[] array holds pointer to the end of page for Child-1, the |
| 69736 | +** Parent, Child-2, the Parent (again), and Child-3. The ixNx[] array |
| 69737 | +** holds the number of cells contained in each of these 5 stages, and |
| 69738 | +** all stages to the left. Hence: |
| 69739 | +** ixNx[0] = Number of cells in Child-1. |
| 69740 | +** ixNx[1] = Number of cells in Child-1 plus 1 for first divider. |
| 69741 | +** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. |
| 69742 | +** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells |
| 69743 | +** ixNx[4] = Total number of cells. |
| 69634 | 69744 | */ |
| 69635 | 69745 | typedef struct CellArray CellArray; |
| 69636 | 69746 | struct CellArray { |
| 69637 | 69747 | int nCell; /* Number of cells in apCell[] */ |
| 69638 | 69748 | MemPage *pRef; /* Reference page */ |
| 69639 | 69749 | u8 **apCell; /* All cells begin balanced */ |
| 69640 | 69750 | u16 *szCell; /* Local size of all cells in apCell[] */ |
| 69751 | + u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */ |
| 69752 | + int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */ |
| 69641 | 69753 | }; |
| 69642 | 69754 | |
| 69643 | 69755 | /* |
| 69644 | 69756 | ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been |
| 69645 | 69757 | ** computed. |
| | @@ -69686,41 +69798,62 @@ |
| 69686 | 69798 | ** |
| 69687 | 69799 | ** The MemPage.nFree field is invalidated by this function. It is the |
| 69688 | 69800 | ** responsibility of the caller to set it correctly. |
| 69689 | 69801 | */ |
| 69690 | 69802 | static int rebuildPage( |
| 69691 | | - MemPage *pPg, /* Edit this page */ |
| 69803 | + CellArray *pCArray, /* Content to be added to page pPg */ |
| 69804 | + int iFirst, /* First cell in pCArray to use */ |
| 69692 | 69805 | int nCell, /* Final number of cells on page */ |
| 69693 | | - u8 **apCell, /* Array of cells */ |
| 69694 | | - u16 *szCell /* Array of cell sizes */ |
| 69806 | + MemPage *pPg /* The page to be reconstructed */ |
| 69695 | 69807 | ){ |
| 69696 | 69808 | const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
| 69697 | 69809 | u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
| 69698 | 69810 | const int usableSize = pPg->pBt->usableSize; |
| 69699 | 69811 | u8 * const pEnd = &aData[usableSize]; |
| 69700 | | - int i; |
| 69812 | + int i = iFirst; /* Which cell to copy from pCArray*/ |
| 69813 | + int j; /* Start of cell content area */ |
| 69814 | + int iEnd = i+nCell; /* Loop terminator */ |
| 69701 | 69815 | u8 *pCellptr = pPg->aCellIdx; |
| 69702 | 69816 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 69703 | 69817 | u8 *pData; |
| 69818 | + int k; /* Current slot in pCArray->apEnd[] */ |
| 69819 | + u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */ |
| 69704 | 69820 | |
| 69705 | | - i = get2byte(&aData[hdr+5]); |
| 69706 | | - memcpy(&pTmp[i], &aData[i], usableSize - i); |
| 69821 | + assert( i<iEnd ); |
| 69822 | + j = get2byte(&aData[hdr+5]); |
| 69823 | + memcpy(&pTmp[j], &aData[j], usableSize - j); |
| 69824 | + |
| 69825 | + for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){} |
| 69826 | + pSrcEnd = pCArray->apEnd[k]; |
| 69707 | 69827 | |
| 69708 | 69828 | pData = pEnd; |
| 69709 | | - for(i=0; i<nCell; i++){ |
| 69710 | | - u8 *pCell = apCell[i]; |
| 69829 | + while( 1/*exit by break*/ ){ |
| 69830 | + u8 *pCell = pCArray->apCell[i]; |
| 69831 | + u16 sz = pCArray->szCell[i]; |
| 69832 | + assert( sz>0 ); |
| 69711 | 69833 | if( SQLITE_WITHIN(pCell,aData,pEnd) ){ |
| 69712 | | - if( ((uptr)(pCell+szCell[i]))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT; |
| 69834 | + if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT; |
| 69713 | 69835 | pCell = &pTmp[pCell - aData]; |
| 69836 | + }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd |
| 69837 | + && (uptr)(pCell)<(uptr)pSrcEnd |
| 69838 | + ){ |
| 69839 | + return SQLITE_CORRUPT_BKPT; |
| 69714 | 69840 | } |
| 69715 | | - pData -= szCell[i]; |
| 69841 | + |
| 69842 | + pData -= sz; |
| 69716 | 69843 | put2byte(pCellptr, (pData - aData)); |
| 69717 | 69844 | pCellptr += 2; |
| 69718 | 69845 | if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; |
| 69719 | | - memcpy(pData, pCell, szCell[i]); |
| 69720 | | - assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); |
| 69721 | | - testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); |
| 69846 | + memcpy(pData, pCell, sz); |
| 69847 | + assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); |
| 69848 | + testcase( sz!=pPg->xCellSize(pPg,pCell) ); |
| 69849 | + i++; |
| 69850 | + if( i>=iEnd ) break; |
| 69851 | + if( pCArray->ixNx[k]<=i ){ |
| 69852 | + k++; |
| 69853 | + pSrcEnd = pCArray->apEnd[k]; |
| 69854 | + } |
| 69722 | 69855 | } |
| 69723 | 69856 | |
| 69724 | 69857 | /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
| 69725 | 69858 | pPg->nCell = nCell; |
| 69726 | 69859 | pPg->nOverflow = 0; |
| | @@ -69731,16 +69864,15 @@ |
| 69731 | 69864 | aData[hdr+7] = 0x00; |
| 69732 | 69865 | return SQLITE_OK; |
| 69733 | 69866 | } |
| 69734 | 69867 | |
| 69735 | 69868 | /* |
| 69736 | | -** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 69737 | | -** contains the size in bytes of each such cell. This function attempts to |
| 69738 | | -** add the cells stored in the array to page pPg. If it cannot (because |
| 69739 | | -** the page needs to be defragmented before the cells will fit), non-zero |
| 69740 | | -** is returned. Otherwise, if the cells are added successfully, zero is |
| 69741 | | -** returned. |
| 69869 | +** The pCArray objects contains pointers to b-tree cells and the cell sizes. |
| 69870 | +** This function attempts to add the cells stored in the array to page pPg. |
| 69871 | +** If it cannot (because the page needs to be defragmented before the cells |
| 69872 | +** will fit), non-zero is returned. Otherwise, if the cells are added |
| 69873 | +** successfully, zero is returned. |
| 69742 | 69874 | ** |
| 69743 | 69875 | ** Argument pCellptr points to the first entry in the cell-pointer array |
| 69744 | 69876 | ** (part of page pPg) to populate. After cell apCell[0] is written to the |
| 69745 | 69877 | ** page body, a 16-bit offset is written to pCellptr. And so on, for each |
| 69746 | 69878 | ** cell in the array. It is the responsibility of the caller to ensure |
| | @@ -69758,22 +69890,27 @@ |
| 69758 | 69890 | ** cells in apCell[], then the cells do not fit and non-zero is returned. |
| 69759 | 69891 | */ |
| 69760 | 69892 | static int pageInsertArray( |
| 69761 | 69893 | MemPage *pPg, /* Page to add cells to */ |
| 69762 | 69894 | u8 *pBegin, /* End of cell-pointer array */ |
| 69763 | | - u8 **ppData, /* IN/OUT: Page content -area pointer */ |
| 69895 | + u8 **ppData, /* IN/OUT: Page content-area pointer */ |
| 69764 | 69896 | u8 *pCellptr, /* Pointer to cell-pointer area */ |
| 69765 | 69897 | int iFirst, /* Index of first cell to add */ |
| 69766 | 69898 | int nCell, /* Number of cells to add to pPg */ |
| 69767 | 69899 | CellArray *pCArray /* Array of cells */ |
| 69768 | 69900 | ){ |
| 69769 | | - int i; |
| 69770 | | - u8 *aData = pPg->aData; |
| 69771 | | - u8 *pData = *ppData; |
| 69772 | | - int iEnd = iFirst + nCell; |
| 69901 | + int i = iFirst; /* Loop counter - cell index to insert */ |
| 69902 | + u8 *aData = pPg->aData; /* Complete page */ |
| 69903 | + u8 *pData = *ppData; /* Content area. A subset of aData[] */ |
| 69904 | + int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */ |
| 69905 | + int k; /* Current slot in pCArray->apEnd[] */ |
| 69906 | + u8 *pEnd; /* Maximum extent of cell data */ |
| 69773 | 69907 | assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
| 69774 | | - for(i=iFirst; i<iEnd; i++){ |
| 69908 | + if( iEnd<=iFirst ) return 0; |
| 69909 | + for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){} |
| 69910 | + pEnd = pCArray->apEnd[k]; |
| 69911 | + while( 1 /*Exit by break*/ ){ |
| 69775 | 69912 | int sz, rc; |
| 69776 | 69913 | u8 *pSlot; |
| 69777 | 69914 | sz = cachedCellSize(pCArray, i); |
| 69778 | 69915 | if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ |
| 69779 | 69916 | if( (pData - pBegin)<sz ) return 1; |
| | @@ -69784,24 +69921,37 @@ |
| 69784 | 69921 | ** database. But they might for a corrupt database. Hence use memmove() |
| 69785 | 69922 | ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ |
| 69786 | 69923 | assert( (pSlot+sz)<=pCArray->apCell[i] |
| 69787 | 69924 | || pSlot>=(pCArray->apCell[i]+sz) |
| 69788 | 69925 | || CORRUPT_DB ); |
| 69926 | + if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd |
| 69927 | + && (uptr)(pCArray->apCell[i])<(uptr)pEnd |
| 69928 | + ){ |
| 69929 | + assert( CORRUPT_DB ); |
| 69930 | + (void)SQLITE_CORRUPT_BKPT; |
| 69931 | + return 1; |
| 69932 | + } |
| 69789 | 69933 | memmove(pSlot, pCArray->apCell[i], sz); |
| 69790 | 69934 | put2byte(pCellptr, (pSlot - aData)); |
| 69791 | 69935 | pCellptr += 2; |
| 69936 | + i++; |
| 69937 | + if( i>=iEnd ) break; |
| 69938 | + if( pCArray->ixNx[k]<=i ){ |
| 69939 | + k++; |
| 69940 | + pEnd = pCArray->apEnd[k]; |
| 69941 | + } |
| 69792 | 69942 | } |
| 69793 | 69943 | *ppData = pData; |
| 69794 | 69944 | return 0; |
| 69795 | 69945 | } |
| 69796 | 69946 | |
| 69797 | 69947 | /* |
| 69798 | | -** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 69799 | | -** contains the size in bytes of each such cell. This function adds the |
| 69800 | | -** space associated with each cell in the array that is currently stored |
| 69801 | | -** within the body of pPg to the pPg free-list. The cell-pointers and other |
| 69802 | | -** fields of the page are not updated. |
| 69948 | +** The pCArray object contains pointers to b-tree cells and their sizes. |
| 69949 | +** |
| 69950 | +** This function adds the space associated with each cell in the array |
| 69951 | +** that is currently stored within the body of pPg to the pPg free-list. |
| 69952 | +** The cell-pointers and other fields of the page are not updated. |
| 69803 | 69953 | ** |
| 69804 | 69954 | ** This function returns the total number of cells added to the free-list. |
| 69805 | 69955 | */ |
| 69806 | 69956 | static int pageFreeArray( |
| 69807 | 69957 | MemPage *pPg, /* Page to edit */ |
| | @@ -69847,13 +69997,13 @@ |
| 69847 | 69997 | } |
| 69848 | 69998 | return nRet; |
| 69849 | 69999 | } |
| 69850 | 70000 | |
| 69851 | 70001 | /* |
| 69852 | | -** apCell[] and szCell[] contains pointers to and sizes of all cells in the |
| 69853 | | -** pages being balanced. The current page, pPg, has pPg->nCell cells starting |
| 69854 | | -** with apCell[iOld]. After balancing, this page should hold nNew cells |
| 70002 | +** pCArray contains pointers to and sizes of all cells in the pages being |
| 70003 | +** balanced. The current page, pPg, has pPg->nCell cells starting with |
| 70004 | +** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells |
| 69855 | 70005 | ** starting at apCell[iNew]. |
| 69856 | 70006 | ** |
| 69857 | 70007 | ** This routine makes the necessary adjustments to pPg so that it contains |
| 69858 | 70008 | ** the correct cells after being balanced. |
| 69859 | 70009 | ** |
| | @@ -69949,27 +70099,12 @@ |
| 69949 | 70099 | |
| 69950 | 70100 | return SQLITE_OK; |
| 69951 | 70101 | editpage_fail: |
| 69952 | 70102 | /* Unable to edit this page. Rebuild it from scratch instead. */ |
| 69953 | 70103 | populateCellCache(pCArray, iNew, nNew); |
| 69954 | | - return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); |
| 69955 | | -} |
| 69956 | | - |
| 69957 | | -/* |
| 69958 | | -** The following parameters determine how many adjacent pages get involved |
| 69959 | | -** in a balancing operation. NN is the number of neighbors on either side |
| 69960 | | -** of the page that participate in the balancing operation. NB is the |
| 69961 | | -** total number of pages that participate, including the target page and |
| 69962 | | -** NN neighbors on either side. |
| 69963 | | -** |
| 69964 | | -** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 69965 | | -** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 69966 | | -** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 69967 | | -** The value of NN appears to give the best results overall. |
| 69968 | | -*/ |
| 69969 | | -#define NN 1 /* Number of neighbors on either side of pPage */ |
| 69970 | | -#define NB (NN*2+1) /* Total pages involved in the balance */ |
| 70104 | + return rebuildPage(pCArray, iNew, nNew, pPg); |
| 70105 | +} |
| 69971 | 70106 | |
| 69972 | 70107 | |
| 69973 | 70108 | #ifndef SQLITE_OMIT_QUICKBALANCE |
| 69974 | 70109 | /* |
| 69975 | 70110 | ** This version of balance() handles the common special case where |
| | @@ -70016,16 +70151,26 @@ |
| 70016 | 70151 | |
| 70017 | 70152 | u8 *pOut = &pSpace[4]; |
| 70018 | 70153 | u8 *pCell = pPage->apOvfl[0]; |
| 70019 | 70154 | u16 szCell = pPage->xCellSize(pPage, pCell); |
| 70020 | 70155 | u8 *pStop; |
| 70156 | + CellArray b; |
| 70021 | 70157 | |
| 70022 | 70158 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 70023 | | - assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 70159 | + assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 70024 | 70160 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 70025 | | - rc = rebuildPage(pNew, 1, &pCell, &szCell); |
| 70026 | | - if( NEVER(rc) ) return rc; |
| 70161 | + b.nCell = 1; |
| 70162 | + b.pRef = pPage; |
| 70163 | + b.apCell = &pCell; |
| 70164 | + b.szCell = &szCell; |
| 70165 | + b.apEnd[0] = pPage->aDataEnd; |
| 70166 | + b.ixNx[0] = 2; |
| 70167 | + rc = rebuildPage(&b, 0, 1, pNew); |
| 70168 | + if( NEVER(rc) ){ |
| 70169 | + releasePage(pNew); |
| 70170 | + return rc; |
| 70171 | + } |
| 70027 | 70172 | pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
| 70028 | 70173 | |
| 70029 | 70174 | /* If this is an auto-vacuum database, update the pointer map |
| 70030 | 70175 | ** with entries for the new page, and any pointer from the |
| 70031 | 70176 | ** cell on the page to an overflow page. If either of these |
| | @@ -70501,10 +70646,14 @@ |
| 70501 | 70646 | ** |
| 70502 | 70647 | */ |
| 70503 | 70648 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 70504 | 70649 | for(i=0; i<nOld; i++){ |
| 70505 | 70650 | MemPage *p = apOld[i]; |
| 70651 | + b.apEnd[i*2] = p->aDataEnd; |
| 70652 | + b.apEnd[i*2+1] = pParent->aDataEnd; |
| 70653 | + b.ixNx[i*2] = cntOld[i]; |
| 70654 | + b.ixNx[i*2+1] = cntOld[i]+1; |
| 70506 | 70655 | szNew[i] = usableSpace - p->nFree; |
| 70507 | 70656 | for(j=0; j<p->nOverflow; j++){ |
| 70508 | 70657 | szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); |
| 70509 | 70658 | } |
| 70510 | 70659 | cntNew[i] = cntOld[i]; |
| | @@ -71182,11 +71331,15 @@ |
| 71182 | 71331 | iAmt = nData; |
| 71183 | 71332 | } |
| 71184 | 71333 | if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){ |
| 71185 | 71334 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 71186 | 71335 | if( rc ) return rc; |
| 71187 | | - memcpy(pDest, ((u8*)pX->pData) + iOffset, iAmt); |
| 71336 | + /* In a corrupt database, it is possible for the source and destination |
| 71337 | + ** buffers to overlap. This is harmless since the database is already |
| 71338 | + ** corrupt but it does cause valgrind and ASAN warnings. So use |
| 71339 | + ** memmove(). */ |
| 71340 | + memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt); |
| 71188 | 71341 | } |
| 71189 | 71342 | } |
| 71190 | 71343 | return SQLITE_OK; |
| 71191 | 71344 | } |
| 71192 | 71345 | |
| | @@ -71596,10 +71749,11 @@ |
| 71596 | 71749 | ** from the internal node. The 'previous' entry is used for this instead |
| 71597 | 71750 | ** of the 'next' entry, as the previous entry is always a part of the |
| 71598 | 71751 | ** sub-tree headed by the child page of the cell being deleted. This makes |
| 71599 | 71752 | ** balancing the tree following the delete operation easier. */ |
| 71600 | 71753 | if( !pPage->leaf ){ |
| 71754 | + pCur->skipNext = 0; |
| 71601 | 71755 | rc = sqlite3BtreePrevious(pCur, 0); |
| 71602 | 71756 | assert( rc!=SQLITE_DONE ); |
| 71603 | 71757 | if( rc ) return rc; |
| 71604 | 71758 | } |
| 71605 | 71759 | |
| | @@ -74199,11 +74353,11 @@ |
| 74199 | 74353 | ** |
| 74200 | 74354 | ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) |
| 74201 | 74355 | ** if unable to complete the resizing. |
| 74202 | 74356 | */ |
| 74203 | 74357 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ |
| 74204 | | - assert( szNew>0 ); |
| 74358 | + assert( CORRUPT_DB || szNew>0 ); |
| 74205 | 74359 | assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); |
| 74206 | 74360 | if( pMem->szMalloc<szNew ){ |
| 74207 | 74361 | return sqlite3VdbeMemGrow(pMem, szNew, 0); |
| 74208 | 74362 | } |
| 74209 | 74363 | assert( (pMem->flags & MEM_Dyn)==0 ); |
| | @@ -75486,13 +75640,15 @@ |
| 75486 | 75640 | else if( op==TK_FUNCTION && pCtx!=0 ){ |
| 75487 | 75641 | rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx); |
| 75488 | 75642 | } |
| 75489 | 75643 | #endif |
| 75490 | 75644 | else if( op==TK_TRUEFALSE ){ |
| 75491 | | - pVal = valueNew(db, pCtx); |
| 75492 | | - pVal->flags = MEM_Int; |
| 75493 | | - pVal->u.i = pExpr->u.zToken[4]==0; |
| 75645 | + pVal = valueNew(db, pCtx); |
| 75646 | + if( pVal ){ |
| 75647 | + pVal->flags = MEM_Int; |
| 75648 | + pVal->u.i = pExpr->u.zToken[4]==0; |
| 75649 | + } |
| 75494 | 75650 | } |
| 75495 | 75651 | |
| 75496 | 75652 | *ppVal = pVal; |
| 75497 | 75653 | return rc; |
| 75498 | 75654 | |
| | @@ -76483,11 +76639,11 @@ |
| 76483 | 76639 | while( (pOp = opIterNext(&sIter))!=0 ){ |
| 76484 | 76640 | int opcode = pOp->opcode; |
| 76485 | 76641 | if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename |
| 76486 | 76642 | || opcode==OP_VDestroy |
| 76487 | 76643 | || ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
| 76488 | | - && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
| 76644 | + && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort)) |
| 76489 | 76645 | ){ |
| 76490 | 76646 | hasAbort = 1; |
| 76491 | 76647 | break; |
| 76492 | 76648 | } |
| 76493 | 76649 | if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1; |
| | @@ -79639,11 +79795,11 @@ |
| 79639 | 79795 | int nKey, /* Size of the binary record */ |
| 79640 | 79796 | const void *pKey, /* The binary record */ |
| 79641 | 79797 | UnpackedRecord *p /* Populate this structure before returning. */ |
| 79642 | 79798 | ){ |
| 79643 | 79799 | const unsigned char *aKey = (const unsigned char *)pKey; |
| 79644 | | - int d; |
| 79800 | + u32 d; |
| 79645 | 79801 | u32 idx; /* Offset in aKey[] to read from */ |
| 79646 | 79802 | u16 u; /* Unsigned loop counter */ |
| 79647 | 79803 | u32 szHdr; |
| 79648 | 79804 | Mem *pMem = p->aMem; |
| 79649 | 79805 | |
| | @@ -79650,11 +79806,11 @@ |
| 79650 | 79806 | p->default_rc = 0; |
| 79651 | 79807 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 79652 | 79808 | idx = getVarint32(aKey, szHdr); |
| 79653 | 79809 | d = szHdr; |
| 79654 | 79810 | u = 0; |
| 79655 | | - while( idx<szHdr && d<=nKey ){ |
| 79811 | + while( idx<szHdr && d<=(u32)nKey ){ |
| 79656 | 79812 | u32 serial_type; |
| 79657 | 79813 | |
| 79658 | 79814 | idx += getVarint32(&aKey[idx], serial_type); |
| 79659 | 79815 | pMem->enc = pKeyInfo->enc; |
| 79660 | 79816 | pMem->db = pKeyInfo->db; |
| | @@ -79663,11 +79819,11 @@ |
| 79663 | 79819 | pMem->z = 0; |
| 79664 | 79820 | d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
| 79665 | 79821 | pMem++; |
| 79666 | 79822 | if( (++u)>=p->nField ) break; |
| 79667 | 79823 | } |
| 79668 | | - if( d>nKey && u ){ |
| 79824 | + if( d>(u32)nKey && u ){ |
| 79669 | 79825 | assert( CORRUPT_DB ); |
| 79670 | 79826 | /* In a corrupt record entry, the last pMem might have been set up using |
| 79671 | 79827 | ** uninitialized memory. Overwrite its value with NULL, to prevent |
| 79672 | 79828 | ** warnings from MSAN. */ |
| 79673 | 79829 | sqlite3VdbeMemSetNull(pMem-1); |
| | @@ -79747,11 +79903,12 @@ |
| 79747 | 79903 | */ |
| 79748 | 79904 | d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
| 79749 | 79905 | |
| 79750 | 79906 | /* Do the comparison |
| 79751 | 79907 | */ |
| 79752 | | - rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]); |
| 79908 | + rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], |
| 79909 | + pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0); |
| 79753 | 79910 | if( rc!=0 ){ |
| 79754 | 79911 | assert( mem1.szMalloc==0 ); /* See comment below */ |
| 79755 | 79912 | if( pKeyInfo->aSortOrder[i] ){ |
| 79756 | 79913 | rc = -rc; /* Invert the result for DESC sort order. */ |
| 79757 | 79914 | } |
| | @@ -80178,14 +80335,16 @@ |
| 80178 | 80335 | rc = +1; |
| 80179 | 80336 | }else{ |
| 80180 | 80337 | mem1.n = (serial_type - 12) / 2; |
| 80181 | 80338 | testcase( (d1+mem1.n)==(unsigned)nKey1 ); |
| 80182 | 80339 | testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); |
| 80183 | | - if( (d1+mem1.n) > (unsigned)nKey1 ){ |
| 80340 | + if( (d1+mem1.n) > (unsigned)nKey1 |
| 80341 | + || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i |
| 80342 | + ){ |
| 80184 | 80343 | pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 80185 | 80344 | return 0; /* Corruption */ |
| 80186 | | - }else if( (pKeyInfo = pPKey2->pKeyInfo)->aColl[i] ){ |
| 80345 | + }else if( pKeyInfo->aColl[i] ){ |
| 80187 | 80346 | mem1.enc = pKeyInfo->enc; |
| 80188 | 80347 | mem1.db = pKeyInfo->db; |
| 80189 | 80348 | mem1.flags = MEM_Str; |
| 80190 | 80349 | mem1.z = (char*)&aKey1[d1]; |
| 80191 | 80350 | rc = vdbeCompareMemString( |
| | @@ -83382,10 +83541,11 @@ |
| 83382 | 83541 | ** accordingly. |
| 83383 | 83542 | */ |
| 83384 | 83543 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
| 83385 | 83544 | assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); |
| 83386 | 83545 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
| 83546 | + ExpandBlob(pMem); |
| 83387 | 83547 | if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ |
| 83388 | 83548 | return 0; |
| 83389 | 83549 | } |
| 83390 | 83550 | if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){ |
| 83391 | 83551 | return MEM_Int; |
| | @@ -89257,10 +89417,21 @@ |
| 89257 | 89417 | memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); |
| 89258 | 89418 | p->aOp = aOp = pProgram->aOp; |
| 89259 | 89419 | p->nOp = pProgram->nOp; |
| 89260 | 89420 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 89261 | 89421 | p->anExec = 0; |
| 89422 | +#endif |
| 89423 | +#ifdef SQLITE_DEBUG |
| 89424 | + /* Verify that second and subsequent executions of the same trigger do not |
| 89425 | + ** try to reuse register values from the first use. */ |
| 89426 | + { |
| 89427 | + int i; |
| 89428 | + for(i=0; i<p->nMem; i++){ |
| 89429 | + aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ |
| 89430 | + aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */ |
| 89431 | + } |
| 89432 | + } |
| 89262 | 89433 | #endif |
| 89263 | 89434 | pOp = &aOp[-1]; |
| 89264 | 89435 | |
| 89265 | 89436 | break; |
| 89266 | 89437 | } |
| | @@ -94431,10 +94602,26 @@ |
| 94431 | 94602 | /* #include "sqliteInt.h" */ |
| 94432 | 94603 | /* #include <stdlib.h> */ |
| 94433 | 94604 | /* #include <string.h> */ |
| 94434 | 94605 | |
| 94435 | 94606 | |
| 94607 | +#if !defined(SQLITE_OMIT_WINDOWFUNC) |
| 94608 | +/* |
| 94609 | +** Walk all expressions linked into the list of Window objects passed |
| 94610 | +** as the second argument. |
| 94611 | +*/ |
| 94612 | +static int walkWindowList(Walker *pWalker, Window *pList){ |
| 94613 | + Window *pWin; |
| 94614 | + for(pWin=pList; pWin; pWin=pWin->pNextWin){ |
| 94615 | + if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort; |
| 94616 | + if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort; |
| 94617 | + if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort; |
| 94618 | + } |
| 94619 | + return WRC_Continue; |
| 94620 | +} |
| 94621 | +#endif |
| 94622 | + |
| 94436 | 94623 | /* |
| 94437 | 94624 | ** Walk an expression tree. Invoke the callback once for each node |
| 94438 | 94625 | ** of the expression, while descending. (In other words, the callback |
| 94439 | 94626 | ** is invoked before visiting children.) |
| 94440 | 94627 | ** |
| | @@ -94470,14 +94657,11 @@ |
| 94470 | 94657 | }else if( pExpr->x.pList ){ |
| 94471 | 94658 | if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |
| 94472 | 94659 | } |
| 94473 | 94660 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 94474 | 94661 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 94475 | | - Window *pWin = pExpr->y.pWin; |
| 94476 | | - if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort; |
| 94477 | | - if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort; |
| 94478 | | - if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort; |
| 94662 | + if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; |
| 94479 | 94663 | } |
| 94480 | 94664 | #endif |
| 94481 | 94665 | } |
| 94482 | 94666 | break; |
| 94483 | 94667 | } |
| | @@ -94513,10 +94697,20 @@ |
| 94513 | 94697 | if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; |
| 94514 | 94698 | if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; |
| 94515 | 94699 | if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; |
| 94516 | 94700 | if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; |
| 94517 | 94701 | if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; |
| 94702 | +#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE) |
| 94703 | + { |
| 94704 | + Parse *pParse = pWalker->pParse; |
| 94705 | + if( pParse && IN_RENAME_OBJECT ){ |
| 94706 | + int rc = walkWindowList(pWalker, p->pWinDefn); |
| 94707 | + assert( rc==WRC_Continue ); |
| 94708 | + return rc; |
| 94709 | + } |
| 94710 | + } |
| 94711 | +#endif |
| 94518 | 94712 | return WRC_Continue; |
| 94519 | 94713 | } |
| 94520 | 94714 | |
| 94521 | 94715 | /* |
| 94522 | 94716 | ** Walk the parse trees associated with all subqueries in the |
| | @@ -95442,14 +95636,14 @@ |
| 95442 | 95636 | sqlite3WalkExprList(pWalker, pList); |
| 95443 | 95637 | if( is_agg ){ |
| 95444 | 95638 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 95445 | 95639 | if( pExpr->y.pWin ){ |
| 95446 | 95640 | Select *pSel = pNC->pWinSelect; |
| 95641 | + sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); |
| 95447 | 95642 | sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition); |
| 95448 | 95643 | sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy); |
| 95449 | 95644 | sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); |
| 95450 | | - sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); |
| 95451 | 95645 | if( 0==pSel->pWin |
| 95452 | 95646 | || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) |
| 95453 | 95647 | ){ |
| 95454 | 95648 | pExpr->y.pWin->pNextWin = pSel->pWin; |
| 95455 | 95649 | pSel->pWin = pExpr->y.pWin; |
| | @@ -95744,38 +95938,35 @@ |
| 95744 | 95938 | } |
| 95745 | 95939 | if( !db->mallocFailed ){ |
| 95746 | 95940 | assert(pDup); |
| 95747 | 95941 | iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); |
| 95748 | 95942 | } |
| 95749 | | - if( IN_RENAME_OBJECT ){ |
| 95750 | | - if( iCol>0 ){ |
| 95751 | | - pItem->done = 1; |
| 95752 | | - continue; |
| 95753 | | - } |
| 95754 | | - }else{ |
| 95943 | + if( !IN_RENAME_OBJECT ){ |
| 95755 | 95944 | sqlite3ExprDelete(db, pDup); |
| 95756 | 95945 | } |
| 95757 | 95946 | } |
| 95758 | 95947 | } |
| 95759 | 95948 | if( iCol>0 ){ |
| 95760 | 95949 | /* Convert the ORDER BY term into an integer column number iCol, |
| 95761 | 95950 | ** taking care to preserve the COLLATE clause if it exists */ |
| 95762 | | - Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
| 95763 | | - if( pNew==0 ) return 1; |
| 95764 | | - pNew->flags |= EP_IntValue; |
| 95765 | | - pNew->u.iValue = iCol; |
| 95766 | | - if( pItem->pExpr==pE ){ |
| 95767 | | - pItem->pExpr = pNew; |
| 95768 | | - }else{ |
| 95769 | | - Expr *pParent = pItem->pExpr; |
| 95770 | | - assert( pParent->op==TK_COLLATE ); |
| 95771 | | - while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; |
| 95772 | | - assert( pParent->pLeft==pE ); |
| 95773 | | - pParent->pLeft = pNew; |
| 95774 | | - } |
| 95775 | | - sqlite3ExprDelete(db, pE); |
| 95776 | | - pItem->u.x.iOrderByCol = (u16)iCol; |
| 95951 | + if( !IN_RENAME_OBJECT ){ |
| 95952 | + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
| 95953 | + if( pNew==0 ) return 1; |
| 95954 | + pNew->flags |= EP_IntValue; |
| 95955 | + pNew->u.iValue = iCol; |
| 95956 | + if( pItem->pExpr==pE ){ |
| 95957 | + pItem->pExpr = pNew; |
| 95958 | + }else{ |
| 95959 | + Expr *pParent = pItem->pExpr; |
| 95960 | + assert( pParent->op==TK_COLLATE ); |
| 95961 | + while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; |
| 95962 | + assert( pParent->pLeft==pE ); |
| 95963 | + pParent->pLeft = pNew; |
| 95964 | + } |
| 95965 | + sqlite3ExprDelete(db, pE); |
| 95966 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 95967 | + } |
| 95777 | 95968 | pItem->done = 1; |
| 95778 | 95969 | }else{ |
| 95779 | 95970 | moreToDo = 1; |
| 95780 | 95971 | } |
| 95781 | 95972 | } |
| | @@ -96119,10 +96310,21 @@ |
| 96119 | 96310 | "the GROUP BY clause"); |
| 96120 | 96311 | return WRC_Abort; |
| 96121 | 96312 | } |
| 96122 | 96313 | } |
| 96123 | 96314 | } |
| 96315 | + |
| 96316 | + if( IN_RENAME_OBJECT ){ |
| 96317 | + Window *pWin; |
| 96318 | + for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){ |
| 96319 | + if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy) |
| 96320 | + || sqlite3ResolveExprListNames(&sNC, pWin->pPartition) |
| 96321 | + ){ |
| 96322 | + return WRC_Abort; |
| 96323 | + } |
| 96324 | + } |
| 96325 | + } |
| 96124 | 96326 | |
| 96125 | 96327 | /* If this is part of a compound SELECT, check that it has the right |
| 96126 | 96328 | ** number of expressions in the select list. */ |
| 96127 | 96329 | if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){ |
| 96128 | 96330 | sqlite3SelectWrongNumTermsError(pParse, p->pNext); |
| | @@ -101683,10 +101885,11 @@ |
| 101683 | 101885 | w.xExprCallback = analyzeAggregate; |
| 101684 | 101886 | w.xSelectCallback = analyzeAggregatesInSelect; |
| 101685 | 101887 | w.xSelectCallback2 = analyzeAggregatesInSelectEnd; |
| 101686 | 101888 | w.walkerDepth = 0; |
| 101687 | 101889 | w.u.pNC = pNC; |
| 101890 | + w.pParse = 0; |
| 101688 | 101891 | assert( pNC->pSrcList!=0 ); |
| 101689 | 101892 | sqlite3WalkExpr(&w, pExpr); |
| 101690 | 101893 | } |
| 101691 | 101894 | |
| 101692 | 101895 | /* |
| | @@ -120153,22 +120356,26 @@ |
| 120153 | 120356 | /* ePragFlg: */ PragFlg_Result0, |
| 120154 | 120357 | /* ColNames: */ 0, 0, |
| 120155 | 120358 | /* iArg: */ 0 }, |
| 120156 | 120359 | #endif |
| 120157 | 120360 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120361 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120158 | 120362 | {/* zName: */ "count_changes", |
| 120159 | 120363 | /* ePragTyp: */ PragTyp_FLAG, |
| 120160 | 120364 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120161 | 120365 | /* ColNames: */ 0, 0, |
| 120162 | 120366 | /* iArg: */ SQLITE_CountRows }, |
| 120367 | +#endif |
| 120163 | 120368 | #endif |
| 120164 | 120369 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN |
| 120370 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120165 | 120371 | {/* zName: */ "data_store_directory", |
| 120166 | 120372 | /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY, |
| 120167 | 120373 | /* ePragFlg: */ PragFlg_NoColumns1, |
| 120168 | 120374 | /* ColNames: */ 0, 0, |
| 120169 | 120375 | /* iArg: */ 0 }, |
| 120376 | +#endif |
| 120170 | 120377 | #endif |
| 120171 | 120378 | #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) |
| 120172 | 120379 | {/* zName: */ "data_version", |
| 120173 | 120380 | /* ePragTyp: */ PragTyp_HEADER_VALUE, |
| 120174 | 120381 | /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0, |
| | @@ -120180,16 +120387,18 @@ |
| 120180 | 120387 | /* ePragTyp: */ PragTyp_DATABASE_LIST, |
| 120181 | 120388 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0, |
| 120182 | 120389 | /* ColNames: */ 35, 3, |
| 120183 | 120390 | /* iArg: */ 0 }, |
| 120184 | 120391 | #endif |
| 120185 | | -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) |
| 120392 | +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 120393 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120186 | 120394 | {/* zName: */ "default_cache_size", |
| 120187 | 120395 | /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE, |
| 120188 | 120396 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1, |
| 120189 | 120397 | /* ColNames: */ 45, 1, |
| 120190 | 120398 | /* iArg: */ 0 }, |
| 120399 | +#endif |
| 120191 | 120400 | #endif |
| 120192 | 120401 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120193 | 120402 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 120194 | 120403 | {/* zName: */ "defer_foreign_keys", |
| 120195 | 120404 | /* ePragTyp: */ PragTyp_FLAG, |
| | @@ -120197,15 +120406,17 @@ |
| 120197 | 120406 | /* ColNames: */ 0, 0, |
| 120198 | 120407 | /* iArg: */ SQLITE_DeferFKs }, |
| 120199 | 120408 | #endif |
| 120200 | 120409 | #endif |
| 120201 | 120410 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120411 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120202 | 120412 | {/* zName: */ "empty_result_callbacks", |
| 120203 | 120413 | /* ePragTyp: */ PragTyp_FLAG, |
| 120204 | 120414 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120205 | 120415 | /* ColNames: */ 0, 0, |
| 120206 | 120416 | /* iArg: */ SQLITE_NullCallback }, |
| 120417 | +#endif |
| 120207 | 120418 | #endif |
| 120208 | 120419 | #if !defined(SQLITE_OMIT_UTF16) |
| 120209 | 120420 | {/* zName: */ "encoding", |
| 120210 | 120421 | /* ePragTyp: */ PragTyp_ENCODING, |
| 120211 | 120422 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| | @@ -120241,15 +120452,19 @@ |
| 120241 | 120452 | /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0, |
| 120242 | 120453 | /* ColNames: */ 0, 0, |
| 120243 | 120454 | /* iArg: */ BTREE_FREE_PAGE_COUNT }, |
| 120244 | 120455 | #endif |
| 120245 | 120456 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120457 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120246 | 120458 | {/* zName: */ "full_column_names", |
| 120247 | 120459 | /* ePragTyp: */ PragTyp_FLAG, |
| 120248 | 120460 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120249 | 120461 | /* ColNames: */ 0, 0, |
| 120250 | 120462 | /* iArg: */ SQLITE_FullColNames }, |
| 120463 | +#endif |
| 120464 | +#endif |
| 120465 | +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120251 | 120466 | {/* zName: */ "fullfsync", |
| 120252 | 120467 | /* ePragTyp: */ PragTyp_FLAG, |
| 120253 | 120468 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120254 | 120469 | /* ColNames: */ 0, 0, |
| 120255 | 120470 | /* iArg: */ SQLITE_FullFSync }, |
| | @@ -120472,15 +120687,17 @@ |
| 120472 | 120687 | /* ePragFlg: */ PragFlg_Result0, |
| 120473 | 120688 | /* ColNames: */ 0, 0, |
| 120474 | 120689 | /* iArg: */ 0 }, |
| 120475 | 120690 | #endif |
| 120476 | 120691 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 120692 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120477 | 120693 | {/* zName: */ "short_column_names", |
| 120478 | 120694 | /* ePragTyp: */ PragTyp_FLAG, |
| 120479 | 120695 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120480 | 120696 | /* ColNames: */ 0, 0, |
| 120481 | 120697 | /* iArg: */ SQLITE_ShortColNames }, |
| 120698 | +#endif |
| 120482 | 120699 | #endif |
| 120483 | 120700 | {/* zName: */ "shrink_memory", |
| 120484 | 120701 | /* ePragTyp: */ PragTyp_SHRINK_MEMORY, |
| 120485 | 120702 | /* ePragFlg: */ PragFlg_NoColumns, |
| 120486 | 120703 | /* ColNames: */ 0, 0, |
| | @@ -120529,15 +120746,19 @@ |
| 120529 | 120746 | {/* zName: */ "temp_store", |
| 120530 | 120747 | /* ePragTyp: */ PragTyp_TEMP_STORE, |
| 120531 | 120748 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120532 | 120749 | /* ColNames: */ 0, 0, |
| 120533 | 120750 | /* iArg: */ 0 }, |
| 120751 | +#endif |
| 120752 | +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 120753 | +#if !defined(SQLITE_OMIT_DEPRECATED) |
| 120534 | 120754 | {/* zName: */ "temp_store_directory", |
| 120535 | 120755 | /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY, |
| 120536 | 120756 | /* ePragFlg: */ PragFlg_NoColumns1, |
| 120537 | 120757 | /* ColNames: */ 0, 0, |
| 120538 | 120758 | /* iArg: */ 0 }, |
| 120759 | +#endif |
| 120539 | 120760 | #endif |
| 120540 | 120761 | #if defined(SQLITE_HAS_CODEC) |
| 120541 | 120762 | {/* zName: */ "textkey", |
| 120542 | 120763 | /* ePragTyp: */ PragTyp_KEY, |
| 120543 | 120764 | /* ePragFlg: */ 0, |
| | @@ -126101,13 +126322,13 @@ |
| 126101 | 126322 | |
| 126102 | 126323 | savedFlags = db->flags; |
| 126103 | 126324 | db->flags &= ~(u64)SQLITE_FullColNames; |
| 126104 | 126325 | db->flags |= SQLITE_ShortColNames; |
| 126105 | 126326 | sqlite3SelectPrep(pParse, pSelect, 0); |
| 126327 | + db->flags = savedFlags; |
| 126106 | 126328 | if( pParse->nErr ) return 0; |
| 126107 | 126329 | while( pSelect->pPrior ) pSelect = pSelect->pPrior; |
| 126108 | | - db->flags = savedFlags; |
| 126109 | 126330 | pTab = sqlite3DbMallocZero(db, sizeof(Table) ); |
| 126110 | 126331 | if( pTab==0 ){ |
| 126111 | 126332 | return 0; |
| 126112 | 126333 | } |
| 126113 | 126334 | /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside |
| | @@ -132377,10 +132598,11 @@ |
| 132377 | 132598 | |
| 132378 | 132599 | /* There is one entry in the aRegIdx[] array for each index on the table |
| 132379 | 132600 | ** being updated. Fill in aRegIdx[] with a register number that will hold |
| 132380 | 132601 | ** the key for accessing each index. |
| 132381 | 132602 | */ |
| 132603 | + if( onError==OE_Replace ) bReplace = 1; |
| 132382 | 132604 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 132383 | 132605 | int reg; |
| 132384 | 132606 | if( chngKey || hasFK>1 || pIdx==pPk |
| 132385 | 132607 | || indexWhereClauseMightChange(pIdx,aXRef,chngRowid) |
| 132386 | 132608 | ){ |
| | @@ -132390,13 +132612,11 @@ |
| 132390 | 132612 | reg = 0; |
| 132391 | 132613 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 132392 | 132614 | if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){ |
| 132393 | 132615 | reg = ++pParse->nMem; |
| 132394 | 132616 | pParse->nMem += pIdx->nColumn; |
| 132395 | | - if( (onError==OE_Replace) |
| 132396 | | - || (onError==OE_Default && pIdx->onError==OE_Replace) |
| 132397 | | - ){ |
| 132617 | + if( onError==OE_Default && pIdx->onError==OE_Replace ){ |
| 132398 | 132618 | bReplace = 1; |
| 132399 | 132619 | } |
| 132400 | 132620 | break; |
| 132401 | 132621 | } |
| 132402 | 132622 | } |
| | @@ -145794,10 +146014,11 @@ |
| 145794 | 146014 | VdbeCoverageIf(v, eCond==2); |
| 145795 | 146015 | sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg); |
| 145796 | 146016 | VdbeCoverageNeverNullIf(v, eCond==0); |
| 145797 | 146017 | VdbeCoverageNeverNullIf(v, eCond==1); |
| 145798 | 146018 | VdbeCoverageNeverNullIf(v, eCond==2); |
| 146019 | + sqlite3MayAbort(pParse); |
| 145799 | 146020 | sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort); |
| 145800 | 146021 | sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC); |
| 145801 | 146022 | sqlite3ReleaseTempReg(pParse, regZero); |
| 145802 | 146023 | } |
| 145803 | 146024 | |
| | @@ -150949,12 +151170,14 @@ |
| 150949 | 151170 | ** expr1 NOT IN () |
| 150950 | 151171 | ** |
| 150951 | 151172 | ** simplify to constants 0 (false) and 1 (true), respectively, |
| 150952 | 151173 | ** regardless of the value of expr1. |
| 150953 | 151174 | */ |
| 150954 | | - sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy490); |
| 150955 | | - yymsp[-4].minor.yy490 = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].minor.yy96],1); |
| 151175 | + if( IN_RENAME_OBJECT==0 ){ |
| 151176 | + sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy490); |
| 151177 | + yymsp[-4].minor.yy490 = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].minor.yy96],1); |
| 151178 | + } |
| 150956 | 151179 | }else if( yymsp[-1].minor.yy42->nExpr==1 ){ |
| 150957 | 151180 | /* Expressions of the form: |
| 150958 | 151181 | ** |
| 150959 | 151182 | ** expr1 IN (?1) |
| 150960 | 151183 | ** expr1 NOT IN (?2) |
| | @@ -158741,10 +158964,22 @@ |
| 158741 | 158964 | ** Terminator values for position-lists and column-lists. |
| 158742 | 158965 | */ |
| 158743 | 158966 | #define POS_COLUMN (1) /* Column-list terminator */ |
| 158744 | 158967 | #define POS_END (0) /* Position-list terminator */ |
| 158745 | 158968 | |
| 158969 | +/* |
| 158970 | +** The assert_fts3_nc() macro is similar to the assert() macro, except that it |
| 158971 | +** is used for assert() conditions that are true only if it can be |
| 158972 | +** guranteed that the database is not corrupt. |
| 158973 | +*/ |
| 158974 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 158975 | +SQLITE_API extern int sqlite3_fts3_may_be_corrupt; |
| 158976 | +# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x)) |
| 158977 | +#else |
| 158978 | +# define assert_fts3_nc(x) assert(x) |
| 158979 | +#endif |
| 158980 | + |
| 158746 | 158981 | /* |
| 158747 | 158982 | ** This section provides definitions to allow the |
| 158748 | 158983 | ** FTS3 extension to be compiled outside of the |
| 158749 | 158984 | ** amalgamation. |
| 158750 | 158985 | */ |
| | @@ -159265,10 +159500,18 @@ |
| 159265 | 159500 | SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; } |
| 159266 | 159501 | SQLITE_PRIVATE int sqlite3Fts3Never(int b) { assert( !b ); return b; } |
| 159267 | 159502 | # endif |
| 159268 | 159503 | #endif |
| 159269 | 159504 | |
| 159505 | +/* |
| 159506 | +** This variable is set to false when running tests for which the on disk |
| 159507 | +** structures should not be corrupt. Otherwise, true. If it is false, extra |
| 159508 | +** assert() conditions in the fts3 code are activated - conditions that are |
| 159509 | +** only true if it is guaranteed that the fts3 database is not corrupt. |
| 159510 | +*/ |
| 159511 | +SQLITE_API int sqlite3_fts3_may_be_corrupt = 1; |
| 159512 | + |
| 159270 | 159513 | /* |
| 159271 | 159514 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 159272 | 159515 | ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes. |
| 159273 | 159516 | ** The number of bytes written is returned. |
| 159274 | 159517 | */ |
| | @@ -161493,11 +161736,11 @@ |
| 161493 | 161736 | ** docids to grow. |
| 161494 | 161737 | ** |
| 161495 | 161738 | ** A symetric argument may be made if the doclists are in descending |
| 161496 | 161739 | ** order. |
| 161497 | 161740 | */ |
| 161498 | | - aOut = sqlite3_malloc64((sqlite3_int64)n1+n2+FTS3_VARINT_MAX-1); |
| 161741 | + aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING); |
| 161499 | 161742 | if( !aOut ) return SQLITE_NOMEM; |
| 161500 | 161743 | |
| 161501 | 161744 | p = aOut; |
| 161502 | 161745 | fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1); |
| 161503 | 161746 | fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2); |
| | @@ -161522,14 +161765,16 @@ |
| 161522 | 161765 | } |
| 161523 | 161766 | |
| 161524 | 161767 | if( rc!=SQLITE_OK ){ |
| 161525 | 161768 | sqlite3_free(aOut); |
| 161526 | 161769 | p = aOut = 0; |
| 161770 | + }else{ |
| 161771 | + assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 ); |
| 161772 | + memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING); |
| 161527 | 161773 | } |
| 161528 | 161774 | *paOut = aOut; |
| 161529 | 161775 | *pnOut = (int)(p-aOut); |
| 161530 | | - assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 ); |
| 161531 | 161776 | return rc; |
| 161532 | 161777 | } |
| 161533 | 161778 | |
| 161534 | 161779 | /* |
| 161535 | 161780 | ** This function does a "phrase" merge of two doclists. In a phrase merge, |
| | @@ -162803,11 +163048,10 @@ |
| 162803 | 163048 | */ |
| 162804 | 163049 | static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){ |
| 162805 | 163050 | Fts3Table *p = (Fts3Table*)pVtab; |
| 162806 | 163051 | UNUSED_PARAMETER(iSavepoint); |
| 162807 | 163052 | assert( p->inTransaction ); |
| 162808 | | - assert( p->mxSavepoint >= iSavepoint ); |
| 162809 | 163053 | TESTONLY( p->mxSavepoint = iSavepoint ); |
| 162810 | 163054 | sqlite3Fts3PendingTermsClear(p); |
| 162811 | 163055 | return SQLITE_OK; |
| 162812 | 163056 | } |
| 162813 | 163057 | |
| | @@ -169623,11 +169867,11 @@ |
| 169623 | 169867 | int iLangid, /* Language id */ |
| 169624 | 169868 | int iIndex, /* Index in p->aIndex[] */ |
| 169625 | 169869 | int iLevel /* Level of segments */ |
| 169626 | 169870 | ){ |
| 169627 | 169871 | sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */ |
| 169628 | | - assert( iLangid>=0 ); |
| 169872 | + assert_fts3_nc( iLangid>=0 ); |
| 169629 | 169873 | assert( p->nIndex>0 ); |
| 169630 | 169874 | assert( iIndex>=0 && iIndex<p->nIndex ); |
| 169631 | 169875 | |
| 169632 | 169876 | iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL; |
| 169633 | 169877 | return iBase + iLevel; |
| | @@ -171310,10 +171554,15 @@ |
| 171310 | 171554 | } |
| 171311 | 171555 | nData = pWriter->nData; |
| 171312 | 171556 | |
| 171313 | 171557 | nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm); |
| 171314 | 171558 | nSuffix = nTerm-nPrefix; |
| 171559 | + |
| 171560 | + /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of |
| 171561 | + ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when |
| 171562 | + ** compared with BINARY collation. This indicates corruption. */ |
| 171563 | + if( nSuffix<=0 ) return FTS_CORRUPT_VTAB; |
| 171315 | 171564 | |
| 171316 | 171565 | /* Figure out how many bytes are required by this new entry */ |
| 171317 | 171566 | nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */ |
| 171318 | 171567 | sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */ |
| 171319 | 171568 | nSuffix + /* Term suffix */ |
| | @@ -199230,16 +199479,12 @@ |
| 199230 | 199479 | ** should be greater than or equal to zero and smaller than the value |
| 199231 | 199480 | ** output by xInstCount(). |
| 199232 | 199481 | ** |
| 199233 | 199482 | ** Usually, output parameter *piPhrase is set to the phrase number, *piCol |
| 199234 | 199483 | ** to the column in which it occurs and *piOff the token offset of the |
| 199235 | | -** first token of the phrase. The exception is if the table was created |
| 199236 | | -** with the offsets=0 option specified. In this case *piOff is always |
| 199237 | | -** set to -1. |
| 199238 | | -** |
| 199239 | | -** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM) |
| 199240 | | -** if an error occurs. |
| 199484 | +** first token of the phrase. Returns SQLITE_OK if successful, or an error |
| 199485 | +** code (i.e. SQLITE_NOMEM) if an error occurs. |
| 199241 | 199486 | ** |
| 199242 | 199487 | ** This API can be quite slow if used with an FTS5 table created with the |
| 199243 | 199488 | ** "detail=none" or "detail=column" option. |
| 199244 | 199489 | ** |
| 199245 | 199490 | ** xRowid: |
| | @@ -199777,10 +200022,16 @@ |
| 199777 | 200022 | # define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x)) |
| 199778 | 200023 | #else |
| 199779 | 200024 | # define assert_nc(x) assert(x) |
| 199780 | 200025 | #endif |
| 199781 | 200026 | |
| 200027 | +/* |
| 200028 | +** A version of memcmp() that does not cause asan errors if one of the pointer |
| 200029 | +** parameters is NULL and the number of bytes to compare is zero. |
| 200030 | +*/ |
| 200031 | +#define fts5Memcmp(s1, s2, n) ((n)==0 ? 0 : memcmp((s1), (s2), (n))) |
| 200032 | + |
| 199782 | 200033 | /* Mark a function parameter as unused, to suppress nuisance compiler |
| 199783 | 200034 | ** warnings. */ |
| 199784 | 200035 | #ifndef UNUSED_PARAM |
| 199785 | 200036 | # define UNUSED_PARAM(X) (void)(X) |
| 199786 | 200037 | #endif |
| | @@ -199964,11 +200215,11 @@ |
| 199964 | 200215 | /* Write and decode big-endian 32-bit integer values */ |
| 199965 | 200216 | static void sqlite3Fts5Put32(u8*, int); |
| 199966 | 200217 | static int sqlite3Fts5Get32(const u8*); |
| 199967 | 200218 | |
| 199968 | 200219 | #define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32) |
| 199969 | | -#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0xFFFFFFFF) |
| 200220 | +#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF) |
| 199970 | 200221 | |
| 199971 | 200222 | typedef struct Fts5PoslistReader Fts5PoslistReader; |
| 199972 | 200223 | struct Fts5PoslistReader { |
| 199973 | 200224 | /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */ |
| 199974 | 200225 | const u8 *a; /* Position list to iterate through */ |
| | @@ -202320,25 +202571,26 @@ |
| 202320 | 202571 | int iOff = 0; |
| 202321 | 202572 | int iFirst = -1; |
| 202322 | 202573 | int nInst; |
| 202323 | 202574 | int nScore = 0; |
| 202324 | 202575 | int iLast = 0; |
| 202576 | + sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken; |
| 202325 | 202577 | |
| 202326 | 202578 | rc = pApi->xInstCount(pFts, &nInst); |
| 202327 | 202579 | for(i=0; i<nInst && rc==SQLITE_OK; i++){ |
| 202328 | 202580 | rc = pApi->xInst(pFts, i, &ip, &ic, &iOff); |
| 202329 | | - if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<(iPos+nToken) ){ |
| 202581 | + if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){ |
| 202330 | 202582 | nScore += (aSeen[ip] ? 1 : 1000); |
| 202331 | 202583 | aSeen[ip] = 1; |
| 202332 | 202584 | if( iFirst<0 ) iFirst = iOff; |
| 202333 | 202585 | iLast = iOff + pApi->xPhraseSize(pFts, ip); |
| 202334 | 202586 | } |
| 202335 | 202587 | } |
| 202336 | 202588 | |
| 202337 | 202589 | *pnScore = nScore; |
| 202338 | 202590 | if( piPos ){ |
| 202339 | | - int iAdj = iFirst - (nToken - (iLast-iFirst)) / 2; |
| 202591 | + sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2; |
| 202340 | 202592 | if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken; |
| 202341 | 202593 | if( iAdj<0 ) iAdj = 0; |
| 202342 | 202594 | *piPos = iAdj; |
| 202343 | 202595 | } |
| 202344 | 202596 | |
| | @@ -202882,11 +203134,11 @@ |
| 202882 | 203134 | if( iVal==1 ){ |
| 202883 | 203135 | fts5FastGetVarint32(a, i, iVal); |
| 202884 | 203136 | iOff = ((i64)iVal) << 32; |
| 202885 | 203137 | fts5FastGetVarint32(a, i, iVal); |
| 202886 | 203138 | } |
| 202887 | | - *piOff = iOff + (iVal-2); |
| 203139 | + *piOff = iOff + ((iVal-2) & 0x7FFFFFFF); |
| 202888 | 203140 | *pi = i; |
| 202889 | 203141 | return 0; |
| 202890 | 203142 | } |
| 202891 | 203143 | } |
| 202892 | 203144 | |
| | @@ -208054,11 +208306,11 @@ |
| 208054 | 208306 | ** |
| 208055 | 208307 | ** res = *pLeft - *pRight |
| 208056 | 208308 | */ |
| 208057 | 208309 | static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){ |
| 208058 | 208310 | int nCmp = MIN(pLeft->n, pRight->n); |
| 208059 | | - int res = memcmp(pLeft->p, pRight->p, nCmp); |
| 208311 | + int res = fts5Memcmp(pLeft->p, pRight->p, nCmp); |
| 208060 | 208312 | return (res==0 ? (pLeft->n - pRight->n) : res); |
| 208061 | 208313 | } |
| 208062 | 208314 | |
| 208063 | 208315 | static int fts5LeafFirstTermOff(Fts5Data *pLeaf){ |
| 208064 | 208316 | int ret; |
| | @@ -209982,11 +210234,11 @@ |
| 209982 | 210234 | assert( pRes->iFirst==i2 ); |
| 209983 | 210235 | }else if( p2->pLeaf==0 ){ |
| 209984 | 210236 | assert( pRes->iFirst==i1 ); |
| 209985 | 210237 | }else{ |
| 209986 | 210238 | int nMin = MIN(p1->term.n, p2->term.n); |
| 209987 | | - int res = memcmp(p1->term.p, p2->term.p, nMin); |
| 210239 | + int res = fts5Memcmp(p1->term.p, p2->term.p, nMin); |
| 209988 | 210240 | if( res==0 ) res = p1->term.n - p2->term.n; |
| 209989 | 210241 | |
| 209990 | 210242 | if( res==0 ){ |
| 209991 | 210243 | assert( pRes->bTermEq==1 ); |
| 209992 | 210244 | assert( p1->iRowid!=p2->iRowid ); |
| | @@ -211028,11 +211280,11 @@ |
| 211028 | 211280 | u32 mask; |
| 211029 | 211281 | memset(aUsed, 0, sizeof(aUsed)); |
| 211030 | 211282 | for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){ |
| 211031 | 211283 | for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){ |
| 211032 | 211284 | int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid; |
| 211033 | | - if( iId<=FTS5_MAX_SEGMENT ){ |
| 211285 | + if( iId<=FTS5_MAX_SEGMENT && iId>0 ){ |
| 211034 | 211286 | aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32); |
| 211035 | 211287 | } |
| 211036 | 211288 | } |
| 211037 | 211289 | } |
| 211038 | 211290 | |
| | @@ -211575,11 +211827,11 @@ |
| 211575 | 211827 | */ |
| 211576 | 211828 | static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){ |
| 211577 | 211829 | int i; |
| 211578 | 211830 | Fts5Buffer buf; |
| 211579 | 211831 | memset(&buf, 0, sizeof(Fts5Buffer)); |
| 211580 | | - for(i=0; i<pIter->nSeg; i++){ |
| 211832 | + for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){ |
| 211581 | 211833 | Fts5SegIter *pSeg = &pIter->aSeg[i]; |
| 211582 | 211834 | if( pSeg->pSeg==0 ){ |
| 211583 | 211835 | /* no-op */ |
| 211584 | 211836 | }else if( pSeg->pLeaf==0 ){ |
| 211585 | 211837 | /* All keys from this input segment have been transfered to the output. |
| | @@ -211595,37 +211847,45 @@ |
| 211595 | 211847 | u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00}; |
| 211596 | 211848 | |
| 211597 | 211849 | iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno); |
| 211598 | 211850 | pData = fts5DataRead(p, iLeafRowid); |
| 211599 | 211851 | if( pData ){ |
| 211600 | | - fts5BufferZero(&buf); |
| 211601 | | - fts5BufferGrow(&p->rc, &buf, pData->nn); |
| 211602 | | - fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr); |
| 211603 | | - fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n); |
| 211604 | | - fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p); |
| 211605 | | - fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff, &pData->p[iOff]); |
| 211606 | | - if( p->rc==SQLITE_OK ){ |
| 211607 | | - /* Set the szLeaf field */ |
| 211608 | | - fts5PutU16(&buf.p[2], (u16)buf.n); |
| 211609 | | - } |
| 211610 | | - |
| 211611 | | - /* Set up the new page-index array */ |
| 211612 | | - fts5BufferAppendVarint(&p->rc, &buf, 4); |
| 211613 | | - if( pSeg->iLeafPgno==pSeg->iTermLeafPgno |
| 211614 | | - && pSeg->iEndofDoclist<pData->szLeaf |
| 211615 | | - ){ |
| 211616 | | - int nDiff = pData->szLeaf - pSeg->iEndofDoclist; |
| 211617 | | - fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4); |
| 211618 | | - fts5BufferAppendBlob(&p->rc, &buf, |
| 211619 | | - pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff] |
| 211620 | | - ); |
| 211621 | | - } |
| 211622 | | - |
| 211623 | | - fts5DataRelease(pData); |
| 211624 | | - pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno; |
| 211625 | | - fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid); |
| 211626 | | - fts5DataWrite(p, iLeafRowid, buf.p, buf.n); |
| 211852 | + if( iOff>pData->szLeaf ){ |
| 211853 | + /* This can occur if the pages that the segments occupy overlap - if |
| 211854 | + ** a single page has been assigned to more than one segment. In |
| 211855 | + ** this case a prior iteration of this loop may have corrupted the |
| 211856 | + ** segment currently being trimmed. */ |
| 211857 | + p->rc = FTS5_CORRUPT; |
| 211858 | + }else{ |
| 211859 | + fts5BufferZero(&buf); |
| 211860 | + fts5BufferGrow(&p->rc, &buf, pData->nn); |
| 211861 | + fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr); |
| 211862 | + fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n); |
| 211863 | + fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p); |
| 211864 | + fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff]); |
| 211865 | + if( p->rc==SQLITE_OK ){ |
| 211866 | + /* Set the szLeaf field */ |
| 211867 | + fts5PutU16(&buf.p[2], (u16)buf.n); |
| 211868 | + } |
| 211869 | + |
| 211870 | + /* Set up the new page-index array */ |
| 211871 | + fts5BufferAppendVarint(&p->rc, &buf, 4); |
| 211872 | + if( pSeg->iLeafPgno==pSeg->iTermLeafPgno |
| 211873 | + && pSeg->iEndofDoclist<pData->szLeaf |
| 211874 | + ){ |
| 211875 | + int nDiff = pData->szLeaf - pSeg->iEndofDoclist; |
| 211876 | + fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4); |
| 211877 | + fts5BufferAppendBlob(&p->rc, &buf, |
| 211878 | + pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff] |
| 211879 | + ); |
| 211880 | + } |
| 211881 | + |
| 211882 | + pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno; |
| 211883 | + fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid); |
| 211884 | + fts5DataWrite(p, iLeafRowid, buf.p, buf.n); |
| 211885 | + } |
| 211886 | + fts5DataRelease(pData); |
| 211627 | 211887 | } |
| 211628 | 211888 | } |
| 211629 | 211889 | } |
| 211630 | 211890 | fts5BufferFree(&buf); |
| 211631 | 211891 | } |
| | @@ -211713,11 +211973,11 @@ |
| 211713 | 211973 | int nPos; /* position-list size field value */ |
| 211714 | 211974 | int nTerm; |
| 211715 | 211975 | const u8 *pTerm; |
| 211716 | 211976 | |
| 211717 | 211977 | pTerm = fts5MultiIterTerm(pIter, &nTerm); |
| 211718 | | - if( nTerm!=term.n || memcmp(pTerm, term.p, nTerm) ){ |
| 211978 | + if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){ |
| 211719 | 211979 | if( pnRem && writer.nLeafWritten>nRem ){ |
| 211720 | 211980 | break; |
| 211721 | 211981 | } |
| 211722 | 211982 | fts5BufferSet(&p->rc, &term, nTerm, pTerm); |
| 211723 | 211983 | bTermWritten =0; |
| | @@ -212469,10 +212729,11 @@ |
| 212469 | 212729 | /* WRITEPOSLISTSIZE */ |
| 212470 | 212730 | fts5BufferSafeAppendVarint(&out, tmp.n * 2); |
| 212471 | 212731 | fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n); |
| 212472 | 212732 | fts5DoclistIterNext(&i1); |
| 212473 | 212733 | fts5DoclistIterNext(&i2); |
| 212734 | + assert( out.n<=(p1->n+p2->n+9) ); |
| 212474 | 212735 | if( i1.aPoslist==0 || i2.aPoslist==0 ) break; |
| 212475 | 212736 | } |
| 212476 | 212737 | } |
| 212477 | 212738 | |
| 212478 | 212739 | if( i1.aPoslist ){ |
| | @@ -212570,11 +212831,11 @@ |
| 212570 | 212831 | } |
| 212571 | 212832 | fts5BufferFree(&aBuf[i]); |
| 212572 | 212833 | } |
| 212573 | 212834 | fts5MultiIterFree(p1); |
| 212574 | 212835 | |
| 212575 | | - pData = fts5IdxMalloc(p, sizeof(Fts5Data) + doclist.n); |
| 212836 | + pData = fts5IdxMalloc(p, sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING); |
| 212576 | 212837 | if( pData ){ |
| 212577 | 212838 | pData->p = (u8*)&pData[1]; |
| 212578 | 212839 | pData->nn = pData->szLeaf = doclist.n; |
| 212579 | 212840 | if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n); |
| 212580 | 212841 | fts5MultiIterNew2(p, pData, bDesc, ppIter); |
| | @@ -213336,11 +213597,11 @@ |
| 213336 | 213597 | iRowidOff = fts5LeafFirstRowidOff(pLeaf); |
| 213337 | 213598 | if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){ |
| 213338 | 213599 | p->rc = FTS5_CORRUPT; |
| 213339 | 213600 | }else{ |
| 213340 | 213601 | iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm); |
| 213341 | | - res = memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm)); |
| 213602 | + res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm)); |
| 213342 | 213603 | if( res==0 ) res = nTerm - nIdxTerm; |
| 213343 | 213604 | if( res<0 ) p->rc = FTS5_CORRUPT; |
| 213344 | 213605 | } |
| 213345 | 213606 | |
| 213346 | 213607 | fts5IntegrityCheckPgidx(p, pLeaf); |
| | @@ -215772,10 +216033,11 @@ |
| 215772 | 216033 | */ |
| 215773 | 216034 | static int fts5CacheInstArray(Fts5Cursor *pCsr){ |
| 215774 | 216035 | int rc = SQLITE_OK; |
| 215775 | 216036 | Fts5PoslistReader *aIter; /* One iterator for each phrase */ |
| 215776 | 216037 | int nIter; /* Number of iterators/phrases */ |
| 216038 | + int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol; |
| 215777 | 216039 | |
| 215778 | 216040 | nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr); |
| 215779 | 216041 | if( pCsr->aInstIter==0 ){ |
| 215780 | 216042 | sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter; |
| 215781 | 216043 | pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte); |
| | @@ -215825,10 +216087,14 @@ |
| 215825 | 216087 | |
| 215826 | 216088 | aInst = &pCsr->aInst[3 * (nInst-1)]; |
| 215827 | 216089 | aInst[0] = iBest; |
| 215828 | 216090 | aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos); |
| 215829 | 216091 | aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos); |
| 216092 | + if( aInst[1]<0 || aInst[1]>=nCol ){ |
| 216093 | + rc = FTS5_CORRUPT; |
| 216094 | + break; |
| 216095 | + } |
| 215830 | 216096 | sqlite3Fts5PoslistReaderNext(&aIter[iBest]); |
| 215831 | 216097 | } |
| 215832 | 216098 | } |
| 215833 | 216099 | |
| 215834 | 216100 | pCsr->nInstCount = nInst; |
| | @@ -216636,11 +216902,11 @@ |
| 216636 | 216902 | int nArg, /* Number of args */ |
| 216637 | 216903 | sqlite3_value **apUnused /* Function arguments */ |
| 216638 | 216904 | ){ |
| 216639 | 216905 | assert( nArg==0 ); |
| 216640 | 216906 | UNUSED_PARAM2(nArg, apUnused); |
| 216641 | | - sqlite3_result_text(pCtx, "fts5: 2019-01-21 16:12:20 6c33a303ebbb0f5193ead535280ba63118e14fb4f9977ce80dc716a0b082ec99", -1, SQLITE_TRANSIENT); |
| 216907 | + sqlite3_result_text(pCtx, "fts5: 2019-01-26 23:34:50 a3ea1a822d3a110f4f186f2fc8550f435c8c98635d058096b7be9d4df7066b8b", -1, SQLITE_TRANSIENT); |
| 216642 | 216908 | } |
| 216643 | 216909 | |
| 216644 | 216910 | /* |
| 216645 | 216911 | ** Return true if zName is the extension on one of the shadow tables used |
| 216646 | 216912 | ** by this module. |
| | @@ -221399,12 +221665,12 @@ |
| 221399 | 221665 | } |
| 221400 | 221666 | #endif /* SQLITE_CORE */ |
| 221401 | 221667 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 221402 | 221668 | |
| 221403 | 221669 | /************** End of stmt.c ************************************************/ |
| 221404 | | -#if __LINE__!=221404 |
| 221670 | +#if __LINE__!=221670 |
| 221405 | 221671 | #undef SQLITE_SOURCE_ID |
| 221406 | | -#define SQLITE_SOURCE_ID "2019-01-21 17:57:31 505ed9a47825240979338a24044559613fbbd2a7850bdff70c7164da054ealt2" |
| 221672 | +#define SQLITE_SOURCE_ID "2019-01-27 02:45:32 9cf8ebd141aa2eb661d457624c76433bd9e4abfdef04aa52e28bc169172calt2" |
| 221407 | 221673 | #endif |
| 221408 | 221674 | /* Return the source-id for this library */ |
| 221409 | 221675 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 221410 | 221676 | /************************** End of sqlite3.c ******************************/ |
| 221411 | 221677 | |