| | @@ -325,11 +325,11 @@ |
| 325 | 325 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 326 | 326 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 327 | 327 | */ |
| 328 | 328 | #define SQLITE_VERSION "3.8.11" |
| 329 | 329 | #define SQLITE_VERSION_NUMBER 3008011 |
| 330 | | -#define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899" |
| 330 | +#define SQLITE_SOURCE_ID "2015-07-03 17:54:49 030f60a7ba171650ce8c0ac32dc166eab80aca32" |
| 331 | 331 | |
| 332 | 332 | /* |
| 333 | 333 | ** CAPI3REF: Run-Time Library Version Numbers |
| 334 | 334 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 335 | 335 | ** |
| | @@ -8354,10 +8354,20 @@ |
| 8354 | 8354 | # define SQLITE_NOINLINE __declspec(noinline) |
| 8355 | 8355 | #else |
| 8356 | 8356 | # define SQLITE_NOINLINE |
| 8357 | 8357 | #endif |
| 8358 | 8358 | |
| 8359 | +/* |
| 8360 | +** Make sure that the compiler intrinsics we desire are enabled when |
| 8361 | +** compiling with an appropriate version of MSVC. |
| 8362 | +*/ |
| 8363 | +#if defined(_MSC_VER) && _MSC_VER>=1300 |
| 8364 | +# include <intrin.h> |
| 8365 | +# pragma intrinsic(_byteswap_ushort) |
| 8366 | +# pragma intrinsic(_byteswap_ulong) |
| 8367 | +#endif |
| 8368 | + |
| 8359 | 8369 | /* |
| 8360 | 8370 | ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. |
| 8361 | 8371 | ** 0 means mutexes are permanently disable and the library is never |
| 8362 | 8372 | ** threadsafe. 1 means the library is serialized which is the highest |
| 8363 | 8373 | ** level of threadsafety. 2 means the library is multithreaded - multiple |
| | @@ -10323,11 +10333,13 @@ |
| 10323 | 10333 | #endif |
| 10324 | 10334 | |
| 10325 | 10335 | /* Functions used to query pager state and configuration. */ |
| 10326 | 10336 | SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*); |
| 10327 | 10337 | SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*); |
| 10328 | | -SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); |
| 10338 | +#ifdef SQLITE_DEBUG |
| 10339 | +SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); |
| 10340 | +#endif |
| 10329 | 10341 | SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*); |
| 10330 | 10342 | SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int); |
| 10331 | 10343 | SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*); |
| 10332 | 10344 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); |
| 10333 | 10345 | SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); |
| | @@ -24920,24 +24932,31 @@ |
| 24920 | 24932 | SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){ |
| 24921 | 24933 | #if SQLITE_BYTEORDER==4321 |
| 24922 | 24934 | u32 x; |
| 24923 | 24935 | memcpy(&x,p,4); |
| 24924 | 24936 | return x; |
| 24925 | | -#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) |
| 24937 | +#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 24926 | 24938 | u32 x; |
| 24927 | 24939 | memcpy(&x,p,4); |
| 24928 | 24940 | return __builtin_bswap32(x); |
| 24941 | +#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 24942 | + u32 x; |
| 24943 | + memcpy(&x,p,4); |
| 24944 | + return _byteswap_ulong(x); |
| 24929 | 24945 | #else |
| 24930 | 24946 | testcase( p[0]&0x80 ); |
| 24931 | 24947 | return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 24932 | 24948 | #endif |
| 24933 | 24949 | } |
| 24934 | 24950 | SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 24935 | 24951 | #if SQLITE_BYTEORDER==4321 |
| 24936 | 24952 | memcpy(p,&v,4); |
| 24937 | | -#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) |
| 24953 | +#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 24938 | 24954 | u32 x = __builtin_bswap32(v); |
| 24955 | + memcpy(p,&x,4); |
| 24956 | +#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 24957 | + u32 x = _byteswap_ulong(v); |
| 24939 | 24958 | memcpy(p,&x,4); |
| 24940 | 24959 | #else |
| 24941 | 24960 | p[0] = (u8)(v>>24); |
| 24942 | 24961 | p[1] = (u8)(v>>16); |
| 24943 | 24962 | p[2] = (u8)(v>>8); |
| | @@ -48503,16 +48522,18 @@ |
| 48503 | 48522 | */ |
| 48504 | 48523 | SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){ |
| 48505 | 48524 | return pPager->readOnly; |
| 48506 | 48525 | } |
| 48507 | 48526 | |
| 48527 | +#ifdef SQLITE_DEBUG |
| 48508 | 48528 | /* |
| 48509 | 48529 | ** Return the number of references to the pager. |
| 48510 | 48530 | */ |
| 48511 | 48531 | SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){ |
| 48512 | 48532 | return sqlite3PcacheRefCount(pPager->pPCache); |
| 48513 | 48533 | } |
| 48534 | +#endif |
| 48514 | 48535 | |
| 48515 | 48536 | /* |
| 48516 | 48537 | ** Return the approximate number of bytes of memory currently |
| 48517 | 48538 | ** used by the pager and its associated cache. |
| 48518 | 48539 | */ |
| | @@ -53247,10 +53268,11 @@ |
| 53247 | 53268 | int nErr; /* Number of messages written to zErrMsg so far */ |
| 53248 | 53269 | int mallocFailed; /* A memory allocation error has occurred */ |
| 53249 | 53270 | const char *zPfx; /* Error message prefix */ |
| 53250 | 53271 | int v1, v2; /* Values for up to two %d fields in zPfx */ |
| 53251 | 53272 | StrAccum errMsg; /* Accumulate the error message text here */ |
| 53273 | + u32 *heap; /* Min-heap used for analyzing cell coverage */ |
| 53252 | 53274 | }; |
| 53253 | 53275 | |
| 53254 | 53276 | /* |
| 53255 | 53277 | ** Routines to read or write a two- and four-byte big-endian integer values. |
| 53256 | 53278 | */ |
| | @@ -53266,10 +53288,12 @@ |
| 53266 | 53288 | */ |
| 53267 | 53289 | #if SQLITE_BYTEORDER==4321 |
| 53268 | 53290 | # define get2byteAligned(x) (*(u16*)(x)) |
| 53269 | 53291 | #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000 |
| 53270 | 53292 | # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) |
| 53293 | +#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 53294 | +# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x)) |
| 53271 | 53295 | #else |
| 53272 | 53296 | # define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) |
| 53273 | 53297 | #endif |
| 53274 | 53298 | |
| 53275 | 53299 | /************** End of btreeInt.h ********************************************/ |
| | @@ -54596,10 +54620,13 @@ |
| 54596 | 54620 | ){ |
| 54597 | 54621 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54598 | 54622 | assert( pPage->leaf==0 ); |
| 54599 | 54623 | assert( pPage->noPayload ); |
| 54600 | 54624 | assert( pPage->childPtrSize==4 ); |
| 54625 | +#ifndef SQLITE_DEBUG |
| 54626 | + UNUSED_PARAMETER(pPage); |
| 54627 | +#endif |
| 54601 | 54628 | pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
| 54602 | 54629 | pInfo->nPayload = 0; |
| 54603 | 54630 | pInfo->nLocal = 0; |
| 54604 | 54631 | pInfo->iOverflow = 0; |
| 54605 | 54632 | pInfo->pPayload = 0; |
| | @@ -54793,10 +54820,12 @@ |
| 54793 | 54820 | ** the (CellInfo.nSize) value found by doing a full parse of the |
| 54794 | 54821 | ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 54795 | 54822 | ** this function verifies that this invariant is not violated. */ |
| 54796 | 54823 | CellInfo debuginfo; |
| 54797 | 54824 | pPage->xParseCell(pPage, pCell, &debuginfo); |
| 54825 | +#else |
| 54826 | + UNUSED_PARAMETER(pPage); |
| 54798 | 54827 | #endif |
| 54799 | 54828 | |
| 54800 | 54829 | assert( pPage->childPtrSize==4 ); |
| 54801 | 54830 | pEnd = pIter + 9; |
| 54802 | 54831 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| | @@ -57143,11 +57172,11 @@ |
| 57143 | 57172 | ** pages are in use. |
| 57144 | 57173 | */ |
| 57145 | 57174 | static int autoVacuumCommit(BtShared *pBt){ |
| 57146 | 57175 | int rc = SQLITE_OK; |
| 57147 | 57176 | Pager *pPager = pBt->pPager; |
| 57148 | | - VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
| 57177 | + VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) |
| 57149 | 57178 | |
| 57150 | 57179 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 57151 | 57180 | invalidateAllOverflowCache(pBt); |
| 57152 | 57181 | assert(pBt->autoVacuum); |
| 57153 | 57182 | if( !pBt->incrVacuum ){ |
| | @@ -62478,36 +62507,42 @@ |
| 62478 | 62507 | ** |
| 62479 | 62508 | ** These checks are done: |
| 62480 | 62509 | ** |
| 62481 | 62510 | ** 1. Make sure that cells and freeblocks do not overlap |
| 62482 | 62511 | ** but combine to completely cover the page. |
| 62483 | | -** NO 2. Make sure cell keys are in order. |
| 62484 | | -** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 62485 | | -** NO 4. Make sure no key is greater than or equal to zUpperBound. |
| 62486 | | -** 5. Check the integrity of overflow pages. |
| 62487 | | -** 6. Recursively call checkTreePage on all children. |
| 62488 | | -** 7. Verify that the depth of all children is the same. |
| 62489 | | -** 8. Make sure this page is at least 33% full or else it is |
| 62490 | | -** the root of the tree. |
| 62512 | +** 2. Make sure integer cell keys are in order. |
| 62513 | +** 3. Check the integrity of overflow pages. |
| 62514 | +** 4. Recursively call checkTreePage on all children. |
| 62515 | +** 5. Verify that the depth of all children is the same. |
| 62491 | 62516 | */ |
| 62492 | 62517 | static int checkTreePage( |
| 62493 | 62518 | IntegrityCk *pCheck, /* Context for the sanity check */ |
| 62494 | 62519 | int iPage, /* Page number of the page to check */ |
| 62495 | | - i64 *pnParentMinKey, |
| 62496 | | - i64 *pnParentMaxKey |
| 62520 | + i64 *piMinKey, /* Write minimum integer primary key here */ |
| 62521 | + i64 maxKey /* Error if integer primary key greater than this */ |
| 62497 | 62522 | ){ |
| 62498 | | - MemPage *pPage; |
| 62499 | | - int i, rc, depth, d2, pgno, cnt; |
| 62500 | | - int hdr, cellStart; |
| 62501 | | - int nCell; |
| 62502 | | - u8 *data; |
| 62503 | | - BtShared *pBt; |
| 62504 | | - int usableSize; |
| 62505 | | - u32 *heap = 0; |
| 62506 | | - u32 x, prev = 0; |
| 62507 | | - i64 nMinKey = 0; |
| 62508 | | - i64 nMaxKey = 0; |
| 62523 | + MemPage *pPage = 0; /* The page being analyzed */ |
| 62524 | + int i; /* Loop counter */ |
| 62525 | + int rc; /* Result code from subroutine call */ |
| 62526 | + int depth = -1, d2; /* Depth of a subtree */ |
| 62527 | + int pgno; /* Page number */ |
| 62528 | + int nFrag; /* Number of fragmented bytes on the page */ |
| 62529 | + int hdr; /* Offset to the page header */ |
| 62530 | + int cellStart; /* Offset to the start of the cell pointer array */ |
| 62531 | + int nCell; /* Number of cells */ |
| 62532 | + int doCoverageCheck = 1; /* True if cell coverage checking should be done */ |
| 62533 | + int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey |
| 62534 | + ** False if IPK must be strictly less than maxKey */ |
| 62535 | + u8 *data; /* Page content */ |
| 62536 | + u8 *pCell; /* Cell content */ |
| 62537 | + u8 *pCellIdx; /* Next element of the cell pointer array */ |
| 62538 | + BtShared *pBt; /* The BtShared object that owns pPage */ |
| 62539 | + u32 pc; /* Address of a cell */ |
| 62540 | + u32 usableSize; /* Usable size of the page */ |
| 62541 | + u32 contentOffset; /* Offset to the start of the cell content area */ |
| 62542 | + u32 *heap = 0; /* Min-heap used for checking cell coverage */ |
| 62543 | + u32 x, prev = 0; /* Next and previous entry on the min-heap */ |
| 62509 | 62544 | const char *saved_zPfx = pCheck->zPfx; |
| 62510 | 62545 | int saved_v1 = pCheck->v1; |
| 62511 | 62546 | int saved_v2 = pCheck->v2; |
| 62512 | 62547 | |
| 62513 | 62548 | /* Check that the page exists |
| | @@ -62519,11 +62554,10 @@ |
| 62519 | 62554 | pCheck->zPfx = "Page %d: "; |
| 62520 | 62555 | pCheck->v1 = iPage; |
| 62521 | 62556 | if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
| 62522 | 62557 | checkAppendMsg(pCheck, |
| 62523 | 62558 | "unable to get the page. error code=%d", rc); |
| 62524 | | - depth = -1; |
| 62525 | 62559 | goto end_of_check; |
| 62526 | 62560 | } |
| 62527 | 62561 | |
| 62528 | 62562 | /* Clear MemPage.isInit to make sure the corruption detection code in |
| 62529 | 62563 | ** btreeInitPage() is executed. */ |
| | @@ -62530,208 +62564,198 @@ |
| 62530 | 62564 | pPage->isInit = 0; |
| 62531 | 62565 | if( (rc = btreeInitPage(pPage))!=0 ){ |
| 62532 | 62566 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
| 62533 | 62567 | checkAppendMsg(pCheck, |
| 62534 | 62568 | "btreeInitPage() returns error code %d", rc); |
| 62535 | | - releasePage(pPage); |
| 62536 | | - depth = -1; |
| 62537 | 62569 | goto end_of_check; |
| 62538 | 62570 | } |
| 62571 | + data = pPage->aData; |
| 62572 | + hdr = pPage->hdrOffset; |
| 62539 | 62573 | |
| 62540 | | - /* Check out all the cells. |
| 62541 | | - */ |
| 62542 | | - depth = 0; |
| 62543 | | - for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
| 62544 | | - u8 *pCell; |
| 62545 | | - u32 sz; |
| 62574 | + /* Set up for cell analysis */ |
| 62575 | + pCheck->zPfx = "On tree page %d cell %d: "; |
| 62576 | + contentOffset = get2byteNotZero(&data[hdr+5]); |
| 62577 | + assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 62578 | + |
| 62579 | + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 62580 | + ** number of cells on the page. */ |
| 62581 | + nCell = get2byte(&data[hdr+3]); |
| 62582 | + assert( pPage->nCell==nCell ); |
| 62583 | + |
| 62584 | + /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
| 62585 | + ** immediately follows the b-tree page header. */ |
| 62586 | + cellStart = hdr + 12 - 4*pPage->leaf; |
| 62587 | + assert( pPage->aCellIdx==&data[cellStart] ); |
| 62588 | + pCellIdx = &data[cellStart + 2*(nCell-1)]; |
| 62589 | + |
| 62590 | + if( !pPage->leaf ){ |
| 62591 | + /* Analyze the right-child page of internal pages */ |
| 62592 | + pgno = get4byte(&data[hdr+8]); |
| 62593 | +#ifndef SQLITE_OMIT_AUTOVACUUM |
| 62594 | + if( pBt->autoVacuum ){ |
| 62595 | + pCheck->zPfx = "On page %d at right child: "; |
| 62596 | + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 62597 | + } |
| 62598 | +#endif |
| 62599 | + depth = checkTreePage(pCheck, pgno, &maxKey, maxKey); |
| 62600 | + keyCanBeEqual = 0; |
| 62601 | + }else{ |
| 62602 | + /* For leaf pages, the coverage check will occur in the same loop |
| 62603 | + ** as the other cell checks, so initialize the heap. */ |
| 62604 | + heap = pCheck->heap; |
| 62605 | + heap[0] = 0; |
| 62606 | + } |
| 62607 | + |
| 62608 | + /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 62609 | + ** integer offsets to the cell contents. */ |
| 62610 | + for(i=nCell-1; i>=0 && pCheck->mxErr; i--){ |
| 62546 | 62611 | CellInfo info; |
| 62547 | 62612 | |
| 62548 | | - /* Check payload overflow pages |
| 62549 | | - */ |
| 62550 | | - pCheck->zPfx = "On tree page %d cell %d: "; |
| 62551 | | - pCheck->v1 = iPage; |
| 62613 | + /* Check cell size */ |
| 62552 | 62614 | pCheck->v2 = i; |
| 62553 | | - pCell = findCell(pPage,i); |
| 62615 | + assert( pCellIdx==&data[cellStart + i*2] ); |
| 62616 | + pc = get2byteAligned(pCellIdx); |
| 62617 | + pCellIdx -= 2; |
| 62618 | + if( pc<contentOffset || pc>usableSize-4 ){ |
| 62619 | + checkAppendMsg(pCheck, "Offset %d out of range %d..%d", |
| 62620 | + pc, contentOffset, usableSize-4); |
| 62621 | + doCoverageCheck = 0; |
| 62622 | + continue; |
| 62623 | + } |
| 62624 | + pCell = &data[pc]; |
| 62554 | 62625 | pPage->xParseCell(pPage, pCell, &info); |
| 62555 | | - sz = info.nPayload; |
| 62556 | | - /* For intKey pages, check that the keys are in order. |
| 62557 | | - */ |
| 62626 | + if( pc+info.nSize>usableSize ){ |
| 62627 | + checkAppendMsg(pCheck, "Extends off end of page"); |
| 62628 | + doCoverageCheck = 0; |
| 62629 | + continue; |
| 62630 | + } |
| 62631 | + |
| 62632 | + /* Check for integer primary key out of range */ |
| 62558 | 62633 | if( pPage->intKey ){ |
| 62559 | | - if( i==0 ){ |
| 62560 | | - nMinKey = nMaxKey = info.nKey; |
| 62561 | | - }else if( info.nKey <= nMaxKey ){ |
| 62562 | | - checkAppendMsg(pCheck, |
| 62563 | | - "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
| 62564 | | - } |
| 62565 | | - nMaxKey = info.nKey; |
| 62566 | | - } |
| 62567 | | - if( (sz>info.nLocal) |
| 62568 | | - && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
| 62569 | | - ){ |
| 62570 | | - int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
| 62571 | | - Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 62634 | + if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){ |
| 62635 | + checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); |
| 62636 | + } |
| 62637 | + maxKey = info.nKey; |
| 62638 | + } |
| 62639 | + |
| 62640 | + /* Check the content overflow list */ |
| 62641 | + if( info.nPayload>info.nLocal ){ |
| 62642 | + int nPage; /* Number of pages on the overflow chain */ |
| 62643 | + Pgno pgnoOvfl; /* First page of the overflow chain */ |
| 62644 | + assert( pc + info.iOverflow <= usableSize ); |
| 62645 | + nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4); |
| 62646 | + pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 62572 | 62647 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 62573 | 62648 | if( pBt->autoVacuum ){ |
| 62574 | 62649 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); |
| 62575 | 62650 | } |
| 62576 | 62651 | #endif |
| 62577 | 62652 | checkList(pCheck, 0, pgnoOvfl, nPage); |
| 62578 | 62653 | } |
| 62579 | 62654 | |
| 62580 | | - /* Check sanity of left child page. |
| 62581 | | - */ |
| 62582 | 62655 | if( !pPage->leaf ){ |
| 62656 | + /* Check sanity of left child page for internal pages */ |
| 62583 | 62657 | pgno = get4byte(pCell); |
| 62584 | 62658 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 62585 | 62659 | if( pBt->autoVacuum ){ |
| 62586 | 62660 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 62587 | 62661 | } |
| 62588 | 62662 | #endif |
| 62589 | | - d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey); |
| 62590 | | - if( i>0 && d2!=depth ){ |
| 62663 | + d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey); |
| 62664 | + keyCanBeEqual = 0; |
| 62665 | + if( d2!=depth ){ |
| 62591 | 62666 | checkAppendMsg(pCheck, "Child page depth differs"); |
| 62592 | | - } |
| 62593 | | - depth = d2; |
| 62594 | | - } |
| 62595 | | - } |
| 62596 | | - |
| 62597 | | - if( !pPage->leaf ){ |
| 62598 | | - pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 62599 | | - pCheck->zPfx = "On page %d at right child: "; |
| 62600 | | - pCheck->v1 = iPage; |
| 62601 | | -#ifndef SQLITE_OMIT_AUTOVACUUM |
| 62602 | | - if( pBt->autoVacuum ){ |
| 62603 | | - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 62604 | | - } |
| 62605 | | -#endif |
| 62606 | | - checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey); |
| 62607 | | - } |
| 62608 | | - |
| 62609 | | - /* For intKey leaf pages, check that the min/max keys are in order |
| 62610 | | - ** with any left/parent/right pages. |
| 62611 | | - */ |
| 62612 | | - pCheck->zPfx = "Page %d: "; |
| 62613 | | - pCheck->v1 = iPage; |
| 62614 | | - if( pPage->leaf && pPage->intKey ){ |
| 62615 | | - /* if we are a left child page */ |
| 62616 | | - if( pnParentMinKey ){ |
| 62617 | | - /* if we are the left most child page */ |
| 62618 | | - if( !pnParentMaxKey ){ |
| 62619 | | - if( nMaxKey > *pnParentMinKey ){ |
| 62620 | | - checkAppendMsg(pCheck, |
| 62621 | | - "Rowid %lld out of order (max larger than parent min of %lld)", |
| 62622 | | - nMaxKey, *pnParentMinKey); |
| 62623 | | - } |
| 62624 | | - }else{ |
| 62625 | | - if( nMinKey <= *pnParentMinKey ){ |
| 62626 | | - checkAppendMsg(pCheck, |
| 62627 | | - "Rowid %lld out of order (min less than parent min of %lld)", |
| 62628 | | - nMinKey, *pnParentMinKey); |
| 62629 | | - } |
| 62630 | | - if( nMaxKey > *pnParentMaxKey ){ |
| 62631 | | - checkAppendMsg(pCheck, |
| 62632 | | - "Rowid %lld out of order (max larger than parent max of %lld)", |
| 62633 | | - nMaxKey, *pnParentMaxKey); |
| 62634 | | - } |
| 62635 | | - *pnParentMinKey = nMaxKey; |
| 62636 | | - } |
| 62637 | | - /* else if we're a right child page */ |
| 62638 | | - } else if( pnParentMaxKey ){ |
| 62639 | | - if( nMinKey <= *pnParentMaxKey ){ |
| 62640 | | - checkAppendMsg(pCheck, |
| 62641 | | - "Rowid %lld out of order (min less than parent max of %lld)", |
| 62642 | | - nMinKey, *pnParentMaxKey); |
| 62643 | | - } |
| 62644 | | - } |
| 62645 | | - } |
| 62667 | + depth = d2; |
| 62668 | + } |
| 62669 | + }else{ |
| 62670 | + /* Populate the coverage-checking heap for leaf pages */ |
| 62671 | + btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1)); |
| 62672 | + } |
| 62673 | + } |
| 62674 | + *piMinKey = maxKey; |
| 62646 | 62675 | |
| 62647 | 62676 | /* Check for complete coverage of the page |
| 62648 | 62677 | */ |
| 62649 | | - data = pPage->aData; |
| 62650 | | - hdr = pPage->hdrOffset; |
| 62651 | | - heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); |
| 62652 | | - pCheck->zPfx = 0; |
| 62653 | | - if( heap==0 ){ |
| 62654 | | - pCheck->mallocFailed = 1; |
| 62655 | | - }else{ |
| 62656 | | - int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 62657 | | - assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 62658 | | - heap[0] = 0; |
| 62659 | | - btreeHeapInsert(heap, contentOffset-1); |
| 62660 | | - /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 62661 | | - ** number of cells on the page. */ |
| 62662 | | - nCell = get2byte(&data[hdr+3]); |
| 62663 | | - /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
| 62664 | | - ** immediately follows the b-tree page header. */ |
| 62665 | | - cellStart = hdr + 12 - 4*pPage->leaf; |
| 62666 | | - /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 62667 | | - ** integer offsets to the cell contents. */ |
| 62668 | | - for(i=0; i<nCell; i++){ |
| 62669 | | - int pc = get2byteAligned(&data[cellStart+i*2]); |
| 62670 | | - u32 size = 65536; |
| 62671 | | - if( pc<=usableSize-4 ){ |
| 62672 | | - size = pPage->xCellSize(pPage, &data[pc]); |
| 62673 | | - } |
| 62674 | | - if( (int)(pc+size-1)>=usableSize ){ |
| 62675 | | - pCheck->zPfx = 0; |
| 62676 | | - checkAppendMsg(pCheck, |
| 62677 | | - "Corruption detected in cell %d on page %d",i,iPage); |
| 62678 | | - }else{ |
| 62678 | + pCheck->zPfx = 0; |
| 62679 | + if( doCoverageCheck && pCheck->mxErr>0 ){ |
| 62680 | + /* For leaf pages, the min-heap has already been initialized and the |
| 62681 | + ** cells have already been inserted. But for internal pages, that has |
| 62682 | + ** not yet been done, so do it now */ |
| 62683 | + if( !pPage->leaf ){ |
| 62684 | + heap = pCheck->heap; |
| 62685 | + heap[0] = 0; |
| 62686 | + for(i=nCell-1; i>=0; i--){ |
| 62687 | + u32 size; |
| 62688 | + pc = get2byteAligned(&data[cellStart+i*2]); |
| 62689 | + size = pPage->xCellSize(pPage, &data[pc]); |
| 62679 | 62690 | btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); |
| 62680 | 62691 | } |
| 62681 | 62692 | } |
| 62682 | | - /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
| 62693 | + /* Add the freeblocks to the min-heap |
| 62694 | + ** |
| 62695 | + ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
| 62683 | 62696 | ** is the offset of the first freeblock, or zero if there are no |
| 62684 | | - ** freeblocks on the page. */ |
| 62697 | + ** freeblocks on the page. |
| 62698 | + */ |
| 62685 | 62699 | i = get2byte(&data[hdr+1]); |
| 62686 | 62700 | while( i>0 ){ |
| 62687 | 62701 | int size, j; |
| 62688 | | - assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 62702 | + assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 62689 | 62703 | size = get2byte(&data[i+2]); |
| 62690 | | - assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 62704 | + assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */ |
| 62691 | 62705 | btreeHeapInsert(heap, (i<<16)|(i+size-1)); |
| 62692 | 62706 | /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a |
| 62693 | 62707 | ** big-endian integer which is the offset in the b-tree page of the next |
| 62694 | 62708 | ** freeblock in the chain, or zero if the freeblock is the last on the |
| 62695 | 62709 | ** chain. */ |
| 62696 | 62710 | j = get2byte(&data[i]); |
| 62697 | 62711 | /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 62698 | 62712 | ** increasing offset. */ |
| 62699 | 62713 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 62700 | | - assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 62714 | + assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 62701 | 62715 | i = j; |
| 62702 | 62716 | } |
| 62703 | | - cnt = 0; |
| 62704 | | - assert( heap[0]>0 ); |
| 62705 | | - assert( (heap[1]>>16)==0 ); |
| 62706 | | - btreeHeapPull(heap,&prev); |
| 62717 | + /* Analyze the min-heap looking for overlap between cells and/or |
| 62718 | + ** freeblocks, and counting the number of untracked bytes in nFrag. |
| 62719 | + ** |
| 62720 | + ** Each min-heap entry is of the form: (start_address<<16)|end_address. |
| 62721 | + ** There is an implied first entry the covers the page header, the cell |
| 62722 | + ** pointer index, and the gap between the cell pointer index and the start |
| 62723 | + ** of cell content. |
| 62724 | + ** |
| 62725 | + ** The loop below pulls entries from the min-heap in order and compares |
| 62726 | + ** the start_address against the previous end_address. If there is an |
| 62727 | + ** overlap, that means bytes are used multiple times. If there is a gap, |
| 62728 | + ** that gap is added to the fragmentation count. |
| 62729 | + */ |
| 62730 | + nFrag = 0; |
| 62731 | + prev = contentOffset - 1; /* Implied first min-heap entry */ |
| 62707 | 62732 | while( btreeHeapPull(heap,&x) ){ |
| 62708 | | - if( (prev&0xffff)+1>(x>>16) ){ |
| 62733 | + if( (prev&0xffff)>=(x>>16) ){ |
| 62709 | 62734 | checkAppendMsg(pCheck, |
| 62710 | 62735 | "Multiple uses for byte %u of page %d", x>>16, iPage); |
| 62711 | 62736 | break; |
| 62712 | 62737 | }else{ |
| 62713 | | - cnt += (x>>16) - (prev&0xffff) - 1; |
| 62738 | + nFrag += (x>>16) - (prev&0xffff) - 1; |
| 62714 | 62739 | prev = x; |
| 62715 | 62740 | } |
| 62716 | 62741 | } |
| 62717 | | - cnt += usableSize - (prev&0xffff) - 1; |
| 62742 | + nFrag += usableSize - (prev&0xffff) - 1; |
| 62718 | 62743 | /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments |
| 62719 | 62744 | ** is stored in the fifth field of the b-tree page header. |
| 62720 | 62745 | ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
| 62721 | 62746 | ** number of fragmented free bytes within the cell content area. |
| 62722 | 62747 | */ |
| 62723 | | - if( heap[0]==0 && cnt!=data[hdr+7] ){ |
| 62748 | + if( heap[0]==0 && nFrag!=data[hdr+7] ){ |
| 62724 | 62749 | checkAppendMsg(pCheck, |
| 62725 | 62750 | "Fragmentation of %d bytes reported as %d on page %d", |
| 62726 | | - cnt, data[hdr+7], iPage); |
| 62751 | + nFrag, data[hdr+7], iPage); |
| 62727 | 62752 | } |
| 62728 | 62753 | } |
| 62729 | | - sqlite3PageFree(heap); |
| 62730 | | - releasePage(pPage); |
| 62731 | 62754 | |
| 62732 | 62755 | end_of_check: |
| 62756 | + releasePage(pPage); |
| 62733 | 62757 | pCheck->zPfx = saved_zPfx; |
| 62734 | 62758 | pCheck->v1 = saved_v1; |
| 62735 | 62759 | pCheck->v2 = saved_v2; |
| 62736 | 62760 | return depth+1; |
| 62737 | 62761 | } |
| | @@ -62757,42 +62781,48 @@ |
| 62757 | 62781 | int nRoot, /* Number of entries in aRoot[] */ |
| 62758 | 62782 | int mxErr, /* Stop reporting errors after this many */ |
| 62759 | 62783 | int *pnErr /* Write number of errors seen to this variable */ |
| 62760 | 62784 | ){ |
| 62761 | 62785 | Pgno i; |
| 62762 | | - int nRef; |
| 62763 | 62786 | IntegrityCk sCheck; |
| 62764 | 62787 | BtShared *pBt = p->pBt; |
| 62788 | + int savedDbFlags = pBt->db->flags; |
| 62765 | 62789 | char zErr[100]; |
| 62790 | + VVA_ONLY( int nRef ); |
| 62766 | 62791 | |
| 62767 | 62792 | sqlite3BtreeEnter(p); |
| 62768 | 62793 | assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
| 62769 | | - nRef = sqlite3PagerRefcount(pBt->pPager); |
| 62794 | + assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 ); |
| 62770 | 62795 | sCheck.pBt = pBt; |
| 62771 | 62796 | sCheck.pPager = pBt->pPager; |
| 62772 | 62797 | sCheck.nPage = btreePagecount(sCheck.pBt); |
| 62773 | 62798 | sCheck.mxErr = mxErr; |
| 62774 | 62799 | sCheck.nErr = 0; |
| 62775 | 62800 | sCheck.mallocFailed = 0; |
| 62776 | 62801 | sCheck.zPfx = 0; |
| 62777 | 62802 | sCheck.v1 = 0; |
| 62778 | 62803 | sCheck.v2 = 0; |
| 62779 | | - *pnErr = 0; |
| 62804 | + sCheck.aPgRef = 0; |
| 62805 | + sCheck.heap = 0; |
| 62806 | + sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
| 62780 | 62807 | if( sCheck.nPage==0 ){ |
| 62781 | | - sqlite3BtreeLeave(p); |
| 62782 | | - return 0; |
| 62808 | + goto integrity_ck_cleanup; |
| 62783 | 62809 | } |
| 62784 | 62810 | |
| 62785 | 62811 | sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); |
| 62786 | 62812 | if( !sCheck.aPgRef ){ |
| 62787 | | - *pnErr = 1; |
| 62788 | | - sqlite3BtreeLeave(p); |
| 62789 | | - return 0; |
| 62813 | + sCheck.mallocFailed = 1; |
| 62814 | + goto integrity_ck_cleanup; |
| 62790 | 62815 | } |
| 62816 | + sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); |
| 62817 | + if( sCheck.heap==0 ){ |
| 62818 | + sCheck.mallocFailed = 1; |
| 62819 | + goto integrity_ck_cleanup; |
| 62820 | + } |
| 62821 | + |
| 62791 | 62822 | i = PENDING_BYTE_PAGE(pBt); |
| 62792 | 62823 | if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); |
| 62793 | | - sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
| 62794 | 62824 | |
| 62795 | 62825 | /* Check the integrity of the freelist |
| 62796 | 62826 | */ |
| 62797 | 62827 | sCheck.zPfx = "Main freelist: "; |
| 62798 | 62828 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| | @@ -62799,21 +62829,23 @@ |
| 62799 | 62829 | get4byte(&pBt->pPage1->aData[36])); |
| 62800 | 62830 | sCheck.zPfx = 0; |
| 62801 | 62831 | |
| 62802 | 62832 | /* Check all the tables. |
| 62803 | 62833 | */ |
| 62834 | + testcase( pBt->db->flags & SQLITE_CellSizeCk ); |
| 62835 | + pBt->db->flags &= ~SQLITE_CellSizeCk; |
| 62804 | 62836 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
| 62837 | + i64 notUsed; |
| 62805 | 62838 | if( aRoot[i]==0 ) continue; |
| 62806 | 62839 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 62807 | 62840 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 62808 | 62841 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); |
| 62809 | 62842 | } |
| 62810 | 62843 | #endif |
| 62811 | | - sCheck.zPfx = "List of tree roots: "; |
| 62812 | | - checkTreePage(&sCheck, aRoot[i], NULL, NULL); |
| 62813 | | - sCheck.zPfx = 0; |
| 62844 | + checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); |
| 62814 | 62845 | } |
| 62846 | + pBt->db->flags = savedDbFlags; |
| 62815 | 62847 | |
| 62816 | 62848 | /* Make sure every page in the file is referenced |
| 62817 | 62849 | */ |
| 62818 | 62850 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
| 62819 | 62851 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| | @@ -62833,32 +62865,24 @@ |
| 62833 | 62865 | checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); |
| 62834 | 62866 | } |
| 62835 | 62867 | #endif |
| 62836 | 62868 | } |
| 62837 | 62869 | |
| 62838 | | - /* Make sure this analysis did not leave any unref() pages. |
| 62839 | | - ** This is an internal consistency check; an integrity check |
| 62840 | | - ** of the integrity check. |
| 62841 | | - */ |
| 62842 | | - if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
| 62843 | | - checkAppendMsg(&sCheck, |
| 62844 | | - "Outstanding page count goes from %d to %d during this analysis", |
| 62845 | | - nRef, sqlite3PagerRefcount(pBt->pPager) |
| 62846 | | - ); |
| 62847 | | - } |
| 62848 | | - |
| 62849 | 62870 | /* Clean up and report errors. |
| 62850 | 62871 | */ |
| 62851 | | - sqlite3BtreeLeave(p); |
| 62872 | +integrity_ck_cleanup: |
| 62873 | + sqlite3PageFree(sCheck.heap); |
| 62852 | 62874 | sqlite3_free(sCheck.aPgRef); |
| 62853 | 62875 | if( sCheck.mallocFailed ){ |
| 62854 | 62876 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 62855 | | - *pnErr = sCheck.nErr+1; |
| 62856 | | - return 0; |
| 62877 | + sCheck.nErr++; |
| 62857 | 62878 | } |
| 62858 | 62879 | *pnErr = sCheck.nErr; |
| 62859 | 62880 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 62881 | + /* Make sure this analysis did not leave any unref() pages. */ |
| 62882 | + assert( nRef==sqlite3PagerRefcount(pBt->pPager) ); |
| 62883 | + sqlite3BtreeLeave(p); |
| 62860 | 62884 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
| 62861 | 62885 | } |
| 62862 | 62886 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 62863 | 62887 | |
| 62864 | 62888 | /* |
| | @@ -63822,13 +63846,17 @@ |
| 63822 | 63846 | ** |
| 63823 | 63847 | ** It is assumed that the mutex associated with the BtShared object |
| 63824 | 63848 | ** corresponding to the source database is held when this function is |
| 63825 | 63849 | ** called. |
| 63826 | 63850 | */ |
| 63827 | | -SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){ |
| 63828 | | - sqlite3_backup *p; /* Iterator variable */ |
| 63829 | | - for(p=pBackup; p; p=p->pNext){ |
| 63851 | +static SQLITE_NOINLINE void backupUpdate( |
| 63852 | + sqlite3_backup *p, |
| 63853 | + Pgno iPage, |
| 63854 | + const u8 *aData |
| 63855 | +){ |
| 63856 | + assert( p!=0 ); |
| 63857 | + do{ |
| 63830 | 63858 | assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) ); |
| 63831 | 63859 | if( !isFatalError(p->rc) && iPage<p->iNext ){ |
| 63832 | 63860 | /* The backup process p has already copied page iPage. But now it |
| 63833 | 63861 | ** has been modified by a transaction on the source pager. Copy |
| 63834 | 63862 | ** the new data into the backup. |
| | @@ -63841,11 +63869,14 @@ |
| 63841 | 63869 | assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED ); |
| 63842 | 63870 | if( rc!=SQLITE_OK ){ |
| 63843 | 63871 | p->rc = rc; |
| 63844 | 63872 | } |
| 63845 | 63873 | } |
| 63846 | | - } |
| 63874 | + }while( (p = p->pNext)!=0 ); |
| 63875 | +} |
| 63876 | +SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){ |
| 63877 | + if( pBackup ) backupUpdate(pBackup, iPage, aData); |
| 63847 | 63878 | } |
| 63848 | 63879 | |
| 63849 | 63880 | /* |
| 63850 | 63881 | ** Restart the backup process. This is called when the pager layer |
| 63851 | 63882 | ** detects that the database has been modified by an external database |
| | @@ -112047,11 +112078,11 @@ |
| 112047 | 112078 | ){ |
| 112048 | 112079 | int i, j; /* Loop counters */ |
| 112049 | 112080 | WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */ |
| 112050 | 112081 | Vdbe *v; /* The virtual machine under construction */ |
| 112051 | 112082 | int isAgg; /* True for select lists like "count(*)" */ |
| 112052 | | - ExprList *pEList; /* List of columns to extract. */ |
| 112083 | + ExprList *pEList = 0; /* List of columns to extract. */ |
| 112053 | 112084 | SrcList *pTabList; /* List of tables to select from */ |
| 112054 | 112085 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 112055 | 112086 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 112056 | 112087 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
| 112057 | 112088 | int rc = 1; /* Value to return from this function */ |
| 112058 | 112089 | |