Fossil SCM

Update to the latest SQLite 3.8.11 alpha, including ifdefery to prevent OpenBSD from trying to use __builtin_bswap32(), which it does not support.

drh 2015-07-03 17:56 UTC trunk
Commit 548f96963c97254ab8a13bcc8687bceda8c777fc
2 files changed +222 -191 +1 -1
+222 -191
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325325
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326326
** [sqlite_version()] and [sqlite_source_id()].
327327
*/
328328
#define SQLITE_VERSION "3.8.11"
329329
#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"
331331
332332
/*
333333
** CAPI3REF: Run-Time Library Version Numbers
334334
** KEYWORDS: sqlite3_version, sqlite3_sourceid
335335
**
@@ -8354,10 +8354,20 @@
83548354
# define SQLITE_NOINLINE __declspec(noinline)
83558355
#else
83568356
# define SQLITE_NOINLINE
83578357
#endif
83588358
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
+
83598369
/*
83608370
** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
83618371
** 0 means mutexes are permanently disable and the library is never
83628372
** threadsafe. 1 means the library is serialized which is the highest
83638373
** level of threadsafety. 2 means the library is multithreaded - multiple
@@ -10323,11 +10333,13 @@
1032310333
#endif
1032410334
1032510335
/* Functions used to query pager state and configuration. */
1032610336
SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
1032710337
SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
10328
-SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
10338
+#ifdef SQLITE_DEBUG
10339
+SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
10340
+#endif
1032910341
SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
1033010342
SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
1033110343
SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
1033210344
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
1033310345
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
@@ -24920,24 +24932,31 @@
2492024932
SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
2492124933
#if SQLITE_BYTEORDER==4321
2492224934
u32 x;
2492324935
memcpy(&x,p,4);
2492424936
return x;
24925
-#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24937
+#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
2492624938
u32 x;
2492724939
memcpy(&x,p,4);
2492824940
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);
2492924945
#else
2493024946
testcase( p[0]&0x80 );
2493124947
return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
2493224948
#endif
2493324949
}
2493424950
SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
2493524951
#if SQLITE_BYTEORDER==4321
2493624952
memcpy(p,&v,4);
24937
-#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24953
+#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
2493824954
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);
2493924958
memcpy(p,&x,4);
2494024959
#else
2494124960
p[0] = (u8)(v>>24);
2494224961
p[1] = (u8)(v>>16);
2494324962
p[2] = (u8)(v>>8);
@@ -48503,16 +48522,18 @@
4850348522
*/
4850448523
SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
4850548524
return pPager->readOnly;
4850648525
}
4850748526
48527
+#ifdef SQLITE_DEBUG
4850848528
/*
4850948529
** Return the number of references to the pager.
4851048530
*/
4851148531
SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
4851248532
return sqlite3PcacheRefCount(pPager->pPCache);
4851348533
}
48534
+#endif
4851448535
4851548536
/*
4851648537
** Return the approximate number of bytes of memory currently
4851748538
** used by the pager and its associated cache.
4851848539
*/
@@ -53247,10 +53268,11 @@
5324753268
int nErr; /* Number of messages written to zErrMsg so far */
5324853269
int mallocFailed; /* A memory allocation error has occurred */
5324953270
const char *zPfx; /* Error message prefix */
5325053271
int v1, v2; /* Values for up to two %d fields in zPfx */
5325153272
StrAccum errMsg; /* Accumulate the error message text here */
53273
+ u32 *heap; /* Min-heap used for analyzing cell coverage */
5325253274
};
5325353275
5325453276
/*
5325553277
** Routines to read or write a two- and four-byte big-endian integer values.
5325653278
*/
@@ -53266,10 +53288,12 @@
5326653288
*/
5326753289
#if SQLITE_BYTEORDER==4321
5326853290
# define get2byteAligned(x) (*(u16*)(x))
5326953291
#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
5327053292
# 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))
5327153295
#else
5327253296
# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
5327353297
#endif
5327453298
5327553299
/************** End of btreeInt.h ********************************************/
@@ -54596,10 +54620,13 @@
5459654620
){
5459754621
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5459854622
assert( pPage->leaf==0 );
5459954623
assert( pPage->noPayload );
5460054624
assert( pPage->childPtrSize==4 );
54625
+#ifndef SQLITE_DEBUG
54626
+ UNUSED_PARAMETER(pPage);
54627
+#endif
5460154628
pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
5460254629
pInfo->nPayload = 0;
5460354630
pInfo->nLocal = 0;
5460454631
pInfo->iOverflow = 0;
5460554632
pInfo->pPayload = 0;
@@ -54793,10 +54820,12 @@
5479354820
** the (CellInfo.nSize) value found by doing a full parse of the
5479454821
** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
5479554822
** this function verifies that this invariant is not violated. */
5479654823
CellInfo debuginfo;
5479754824
pPage->xParseCell(pPage, pCell, &debuginfo);
54825
+#else
54826
+ UNUSED_PARAMETER(pPage);
5479854827
#endif
5479954828
5480054829
assert( pPage->childPtrSize==4 );
5480154830
pEnd = pIter + 9;
5480254831
while( (*pIter++)&0x80 && pIter<pEnd );
@@ -57143,11 +57172,11 @@
5714357172
** pages are in use.
5714457173
*/
5714557174
static int autoVacuumCommit(BtShared *pBt){
5714657175
int rc = SQLITE_OK;
5714757176
Pager *pPager = pBt->pPager;
57148
- VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
57177
+ VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
5714957178
5715057179
assert( sqlite3_mutex_held(pBt->mutex) );
5715157180
invalidateAllOverflowCache(pBt);
5715257181
assert(pBt->autoVacuum);
5715357182
if( !pBt->incrVacuum ){
@@ -62478,36 +62507,42 @@
6247862507
**
6247962508
** These checks are done:
6248062509
**
6248162510
** 1. Make sure that cells and freeblocks do not overlap
6248262511
** 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.
6249162516
*/
6249262517
static int checkTreePage(
6249362518
IntegrityCk *pCheck, /* Context for the sanity check */
6249462519
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 */
6249762522
){
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 */
6250962544
const char *saved_zPfx = pCheck->zPfx;
6251062545
int saved_v1 = pCheck->v1;
6251162546
int saved_v2 = pCheck->v2;
6251262547
6251362548
/* Check that the page exists
@@ -62519,11 +62554,10 @@
6251962554
pCheck->zPfx = "Page %d: ";
6252062555
pCheck->v1 = iPage;
6252162556
if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
6252262557
checkAppendMsg(pCheck,
6252362558
"unable to get the page. error code=%d", rc);
62524
- depth = -1;
6252562559
goto end_of_check;
6252662560
}
6252762561
6252862562
/* Clear MemPage.isInit to make sure the corruption detection code in
6252962563
** btreeInitPage() is executed. */
@@ -62530,208 +62564,198 @@
6253062564
pPage->isInit = 0;
6253162565
if( (rc = btreeInitPage(pPage))!=0 ){
6253262566
assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
6253362567
checkAppendMsg(pCheck,
6253462568
"btreeInitPage() returns error code %d", rc);
62535
- releasePage(pPage);
62536
- depth = -1;
6253762569
goto end_of_check;
6253862570
}
62571
+ data = pPage->aData;
62572
+ hdr = pPage->hdrOffset;
6253962573
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--){
6254662611
CellInfo info;
6254762612
62548
- /* Check payload overflow pages
62549
- */
62550
- pCheck->zPfx = "On tree page %d cell %d: ";
62551
- pCheck->v1 = iPage;
62613
+ /* Check cell size */
6255262614
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];
6255462625
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 */
6255862633
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]);
6257262647
#ifndef SQLITE_OMIT_AUTOVACUUM
6257362648
if( pBt->autoVacuum ){
6257462649
checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
6257562650
}
6257662651
#endif
6257762652
checkList(pCheck, 0, pgnoOvfl, nPage);
6257862653
}
6257962654
62580
- /* Check sanity of left child page.
62581
- */
6258262655
if( !pPage->leaf ){
62656
+ /* Check sanity of left child page for internal pages */
6258362657
pgno = get4byte(pCell);
6258462658
#ifndef SQLITE_OMIT_AUTOVACUUM
6258562659
if( pBt->autoVacuum ){
6258662660
checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
6258762661
}
6258862662
#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 ){
6259162666
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;
6264662675
6264762676
/* Check for complete coverage of the page
6264862677
*/
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]);
6267962690
btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
6268062691
}
6268162692
}
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
6268362696
** is the offset of the first freeblock, or zero if there are no
62684
- ** freeblocks on the page. */
62697
+ ** freeblocks on the page.
62698
+ */
6268562699
i = get2byte(&data[hdr+1]);
6268662700
while( i>0 ){
6268762701
int size, j;
62688
- assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */
62702
+ assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
6268962703
size = get2byte(&data[i+2]);
62690
- assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */
62704
+ assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
6269162705
btreeHeapInsert(heap, (i<<16)|(i+size-1));
6269262706
/* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
6269362707
** big-endian integer which is the offset in the b-tree page of the next
6269462708
** freeblock in the chain, or zero if the freeblock is the last on the
6269562709
** chain. */
6269662710
j = get2byte(&data[i]);
6269762711
/* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
6269862712
** increasing offset. */
6269962713
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() */
6270162715
i = j;
6270262716
}
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 */
6270762732
while( btreeHeapPull(heap,&x) ){
62708
- if( (prev&0xffff)+1>(x>>16) ){
62733
+ if( (prev&0xffff)>=(x>>16) ){
6270962734
checkAppendMsg(pCheck,
6271062735
"Multiple uses for byte %u of page %d", x>>16, iPage);
6271162736
break;
6271262737
}else{
62713
- cnt += (x>>16) - (prev&0xffff) - 1;
62738
+ nFrag += (x>>16) - (prev&0xffff) - 1;
6271462739
prev = x;
6271562740
}
6271662741
}
62717
- cnt += usableSize - (prev&0xffff) - 1;
62742
+ nFrag += usableSize - (prev&0xffff) - 1;
6271862743
/* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
6271962744
** is stored in the fifth field of the b-tree page header.
6272062745
** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
6272162746
** number of fragmented free bytes within the cell content area.
6272262747
*/
62723
- if( heap[0]==0 && cnt!=data[hdr+7] ){
62748
+ if( heap[0]==0 && nFrag!=data[hdr+7] ){
6272462749
checkAppendMsg(pCheck,
6272562750
"Fragmentation of %d bytes reported as %d on page %d",
62726
- cnt, data[hdr+7], iPage);
62751
+ nFrag, data[hdr+7], iPage);
6272762752
}
6272862753
}
62729
- sqlite3PageFree(heap);
62730
- releasePage(pPage);
6273162754
6273262755
end_of_check:
62756
+ releasePage(pPage);
6273362757
pCheck->zPfx = saved_zPfx;
6273462758
pCheck->v1 = saved_v1;
6273562759
pCheck->v2 = saved_v2;
6273662760
return depth+1;
6273762761
}
@@ -62757,42 +62781,48 @@
6275762781
int nRoot, /* Number of entries in aRoot[] */
6275862782
int mxErr, /* Stop reporting errors after this many */
6275962783
int *pnErr /* Write number of errors seen to this variable */
6276062784
){
6276162785
Pgno i;
62762
- int nRef;
6276362786
IntegrityCk sCheck;
6276462787
BtShared *pBt = p->pBt;
62788
+ int savedDbFlags = pBt->db->flags;
6276562789
char zErr[100];
62790
+ VVA_ONLY( int nRef );
6276662791
6276762792
sqlite3BtreeEnter(p);
6276862793
assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
62769
- nRef = sqlite3PagerRefcount(pBt->pPager);
62794
+ assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 );
6277062795
sCheck.pBt = pBt;
6277162796
sCheck.pPager = pBt->pPager;
6277262797
sCheck.nPage = btreePagecount(sCheck.pBt);
6277362798
sCheck.mxErr = mxErr;
6277462799
sCheck.nErr = 0;
6277562800
sCheck.mallocFailed = 0;
6277662801
sCheck.zPfx = 0;
6277762802
sCheck.v1 = 0;
6277862803
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);
6278062807
if( sCheck.nPage==0 ){
62781
- sqlite3BtreeLeave(p);
62782
- return 0;
62808
+ goto integrity_ck_cleanup;
6278362809
}
6278462810
6278562811
sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
6278662812
if( !sCheck.aPgRef ){
62787
- *pnErr = 1;
62788
- sqlite3BtreeLeave(p);
62789
- return 0;
62813
+ sCheck.mallocFailed = 1;
62814
+ goto integrity_ck_cleanup;
6279062815
}
62816
+ sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
62817
+ if( sCheck.heap==0 ){
62818
+ sCheck.mallocFailed = 1;
62819
+ goto integrity_ck_cleanup;
62820
+ }
62821
+
6279162822
i = PENDING_BYTE_PAGE(pBt);
6279262823
if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
62793
- sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
6279462824
6279562825
/* Check the integrity of the freelist
6279662826
*/
6279762827
sCheck.zPfx = "Main freelist: ";
6279862828
checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
@@ -62799,21 +62829,23 @@
6279962829
get4byte(&pBt->pPage1->aData[36]));
6280062830
sCheck.zPfx = 0;
6280162831
6280262832
/* Check all the tables.
6280362833
*/
62834
+ testcase( pBt->db->flags & SQLITE_CellSizeCk );
62835
+ pBt->db->flags &= ~SQLITE_CellSizeCk;
6280462836
for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
62837
+ i64 notUsed;
6280562838
if( aRoot[i]==0 ) continue;
6280662839
#ifndef SQLITE_OMIT_AUTOVACUUM
6280762840
if( pBt->autoVacuum && aRoot[i]>1 ){
6280862841
checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
6280962842
}
6281062843
#endif
62811
- sCheck.zPfx = "List of tree roots: ";
62812
- checkTreePage(&sCheck, aRoot[i], NULL, NULL);
62813
- sCheck.zPfx = 0;
62844
+ checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
6281462845
}
62846
+ pBt->db->flags = savedDbFlags;
6281562847
6281662848
/* Make sure every page in the file is referenced
6281762849
*/
6281862850
for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
6281962851
#ifdef SQLITE_OMIT_AUTOVACUUM
@@ -62833,32 +62865,24 @@
6283362865
checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
6283462866
}
6283562867
#endif
6283662868
}
6283762869
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
-
6284962870
/* Clean up and report errors.
6285062871
*/
62851
- sqlite3BtreeLeave(p);
62872
+integrity_ck_cleanup:
62873
+ sqlite3PageFree(sCheck.heap);
6285262874
sqlite3_free(sCheck.aPgRef);
6285362875
if( sCheck.mallocFailed ){
6285462876
sqlite3StrAccumReset(&sCheck.errMsg);
62855
- *pnErr = sCheck.nErr+1;
62856
- return 0;
62877
+ sCheck.nErr++;
6285762878
}
6285862879
*pnErr = sCheck.nErr;
6285962880
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);
6286062884
return sqlite3StrAccumFinish(&sCheck.errMsg);
6286162885
}
6286262886
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
6286362887
6286462888
/*
@@ -63822,13 +63846,17 @@
6382263846
**
6382363847
** It is assumed that the mutex associated with the BtShared object
6382463848
** corresponding to the source database is held when this function is
6382563849
** called.
6382663850
*/
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{
6383063858
assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
6383163859
if( !isFatalError(p->rc) && iPage<p->iNext ){
6383263860
/* The backup process p has already copied page iPage. But now it
6383363861
** has been modified by a transaction on the source pager. Copy
6383463862
** the new data into the backup.
@@ -63841,11 +63869,14 @@
6384163869
assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
6384263870
if( rc!=SQLITE_OK ){
6384363871
p->rc = rc;
6384463872
}
6384563873
}
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);
6384763878
}
6384863879
6384963880
/*
6385063881
** Restart the backup process. This is called when the pager layer
6385163882
** detects that the database has been modified by an external database
@@ -112047,11 +112078,11 @@
112047112078
){
112048112079
int i, j; /* Loop counters */
112049112080
WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
112050112081
Vdbe *v; /* The virtual machine under construction */
112051112082
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. */
112053112084
SrcList *pTabList; /* List of tables to select from */
112054112085
Expr *pWhere; /* The WHERE clause. May be NULL */
112055112086
ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
112056112087
Expr *pHaving; /* The HAVING clause. May be NULL */
112057112088
int rc = 1; /* Value to return from this function */
112058112089
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -8354,10 +8354,20 @@
8354 # define SQLITE_NOINLINE __declspec(noinline)
8355 #else
8356 # define SQLITE_NOINLINE
8357 #endif
8358
 
 
 
 
 
 
 
 
 
 
8359 /*
8360 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
8361 ** 0 means mutexes are permanently disable and the library is never
8362 ** threadsafe. 1 means the library is serialized which is the highest
8363 ** level of threadsafety. 2 means the library is multithreaded - multiple
@@ -10323,11 +10333,13 @@
10323 #endif
10324
10325 /* Functions used to query pager state and configuration. */
10326 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
10327 SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
10328 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
 
 
10329 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
10330 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
10331 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
10332 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
10333 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
@@ -24920,24 +24932,31 @@
24920 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
24921 #if SQLITE_BYTEORDER==4321
24922 u32 x;
24923 memcpy(&x,p,4);
24924 return x;
24925 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24926 u32 x;
24927 memcpy(&x,p,4);
24928 return __builtin_bswap32(x);
 
 
 
 
24929 #else
24930 testcase( p[0]&0x80 );
24931 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
24932 #endif
24933 }
24934 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
24935 #if SQLITE_BYTEORDER==4321
24936 memcpy(p,&v,4);
24937 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24938 u32 x = __builtin_bswap32(v);
 
 
 
24939 memcpy(p,&x,4);
24940 #else
24941 p[0] = (u8)(v>>24);
24942 p[1] = (u8)(v>>16);
24943 p[2] = (u8)(v>>8);
@@ -48503,16 +48522,18 @@
48503 */
48504 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
48505 return pPager->readOnly;
48506 }
48507
 
48508 /*
48509 ** Return the number of references to the pager.
48510 */
48511 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
48512 return sqlite3PcacheRefCount(pPager->pPCache);
48513 }
 
48514
48515 /*
48516 ** Return the approximate number of bytes of memory currently
48517 ** used by the pager and its associated cache.
48518 */
@@ -53247,10 +53268,11 @@
53247 int nErr; /* Number of messages written to zErrMsg so far */
53248 int mallocFailed; /* A memory allocation error has occurred */
53249 const char *zPfx; /* Error message prefix */
53250 int v1, v2; /* Values for up to two %d fields in zPfx */
53251 StrAccum errMsg; /* Accumulate the error message text here */
 
53252 };
53253
53254 /*
53255 ** Routines to read or write a two- and four-byte big-endian integer values.
53256 */
@@ -53266,10 +53288,12 @@
53266 */
53267 #if SQLITE_BYTEORDER==4321
53268 # define get2byteAligned(x) (*(u16*)(x))
53269 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
53270 # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
 
 
53271 #else
53272 # define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
53273 #endif
53274
53275 /************** End of btreeInt.h ********************************************/
@@ -54596,10 +54620,13 @@
54596 ){
54597 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54598 assert( pPage->leaf==0 );
54599 assert( pPage->noPayload );
54600 assert( pPage->childPtrSize==4 );
 
 
 
54601 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54602 pInfo->nPayload = 0;
54603 pInfo->nLocal = 0;
54604 pInfo->iOverflow = 0;
54605 pInfo->pPayload = 0;
@@ -54793,10 +54820,12 @@
54793 ** the (CellInfo.nSize) value found by doing a full parse of the
54794 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54795 ** this function verifies that this invariant is not violated. */
54796 CellInfo debuginfo;
54797 pPage->xParseCell(pPage, pCell, &debuginfo);
 
 
54798 #endif
54799
54800 assert( pPage->childPtrSize==4 );
54801 pEnd = pIter + 9;
54802 while( (*pIter++)&0x80 && pIter<pEnd );
@@ -57143,11 +57172,11 @@
57143 ** pages are in use.
57144 */
57145 static int autoVacuumCommit(BtShared *pBt){
57146 int rc = SQLITE_OK;
57147 Pager *pPager = pBt->pPager;
57148 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
57149
57150 assert( sqlite3_mutex_held(pBt->mutex) );
57151 invalidateAllOverflowCache(pBt);
57152 assert(pBt->autoVacuum);
57153 if( !pBt->incrVacuum ){
@@ -62478,36 +62507,42 @@
62478 **
62479 ** These checks are done:
62480 **
62481 ** 1. Make sure that cells and freeblocks do not overlap
62482 ** 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.
62491 */
62492 static int checkTreePage(
62493 IntegrityCk *pCheck, /* Context for the sanity check */
62494 int iPage, /* Page number of the page to check */
62495 i64 *pnParentMinKey,
62496 i64 *pnParentMaxKey
62497 ){
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;
 
 
 
 
 
 
 
 
 
 
62509 const char *saved_zPfx = pCheck->zPfx;
62510 int saved_v1 = pCheck->v1;
62511 int saved_v2 = pCheck->v2;
62512
62513 /* Check that the page exists
@@ -62519,11 +62554,10 @@
62519 pCheck->zPfx = "Page %d: ";
62520 pCheck->v1 = iPage;
62521 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
62522 checkAppendMsg(pCheck,
62523 "unable to get the page. error code=%d", rc);
62524 depth = -1;
62525 goto end_of_check;
62526 }
62527
62528 /* Clear MemPage.isInit to make sure the corruption detection code in
62529 ** btreeInitPage() is executed. */
@@ -62530,208 +62564,198 @@
62530 pPage->isInit = 0;
62531 if( (rc = btreeInitPage(pPage))!=0 ){
62532 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
62533 checkAppendMsg(pCheck,
62534 "btreeInitPage() returns error code %d", rc);
62535 releasePage(pPage);
62536 depth = -1;
62537 goto end_of_check;
62538 }
 
 
62539
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;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62546 CellInfo info;
62547
62548 /* Check payload overflow pages
62549 */
62550 pCheck->zPfx = "On tree page %d cell %d: ";
62551 pCheck->v1 = iPage;
62552 pCheck->v2 = i;
62553 pCell = findCell(pPage,i);
 
 
 
 
 
 
 
 
 
62554 pPage->xParseCell(pPage, pCell, &info);
62555 sz = info.nPayload;
62556 /* For intKey pages, check that the keys are in order.
62557 */
 
 
 
 
62558 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]);
62572 #ifndef SQLITE_OMIT_AUTOVACUUM
62573 if( pBt->autoVacuum ){
62574 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
62575 }
62576 #endif
62577 checkList(pCheck, 0, pgnoOvfl, nPage);
62578 }
62579
62580 /* Check sanity of left child page.
62581 */
62582 if( !pPage->leaf ){
 
62583 pgno = get4byte(pCell);
62584 #ifndef SQLITE_OMIT_AUTOVACUUM
62585 if( pBt->autoVacuum ){
62586 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
62587 }
62588 #endif
62589 d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey);
62590 if( i>0 && d2!=depth ){
 
62591 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 }
62646
62647 /* Check for complete coverage of the page
62648 */
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{
62679 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
62680 }
62681 }
62682 /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
 
 
62683 ** is the offset of the first freeblock, or zero if there are no
62684 ** freeblocks on the page. */
 
62685 i = get2byte(&data[hdr+1]);
62686 while( i>0 ){
62687 int size, j;
62688 assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */
62689 size = get2byte(&data[i+2]);
62690 assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */
62691 btreeHeapInsert(heap, (i<<16)|(i+size-1));
62692 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
62693 ** big-endian integer which is the offset in the b-tree page of the next
62694 ** freeblock in the chain, or zero if the freeblock is the last on the
62695 ** chain. */
62696 j = get2byte(&data[i]);
62697 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
62698 ** increasing offset. */
62699 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
62700 assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */
62701 i = j;
62702 }
62703 cnt = 0;
62704 assert( heap[0]>0 );
62705 assert( (heap[1]>>16)==0 );
62706 btreeHeapPull(heap,&prev);
 
 
 
 
 
 
 
 
 
 
 
62707 while( btreeHeapPull(heap,&x) ){
62708 if( (prev&0xffff)+1>(x>>16) ){
62709 checkAppendMsg(pCheck,
62710 "Multiple uses for byte %u of page %d", x>>16, iPage);
62711 break;
62712 }else{
62713 cnt += (x>>16) - (prev&0xffff) - 1;
62714 prev = x;
62715 }
62716 }
62717 cnt += usableSize - (prev&0xffff) - 1;
62718 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
62719 ** is stored in the fifth field of the b-tree page header.
62720 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
62721 ** number of fragmented free bytes within the cell content area.
62722 */
62723 if( heap[0]==0 && cnt!=data[hdr+7] ){
62724 checkAppendMsg(pCheck,
62725 "Fragmentation of %d bytes reported as %d on page %d",
62726 cnt, data[hdr+7], iPage);
62727 }
62728 }
62729 sqlite3PageFree(heap);
62730 releasePage(pPage);
62731
62732 end_of_check:
 
62733 pCheck->zPfx = saved_zPfx;
62734 pCheck->v1 = saved_v1;
62735 pCheck->v2 = saved_v2;
62736 return depth+1;
62737 }
@@ -62757,42 +62781,48 @@
62757 int nRoot, /* Number of entries in aRoot[] */
62758 int mxErr, /* Stop reporting errors after this many */
62759 int *pnErr /* Write number of errors seen to this variable */
62760 ){
62761 Pgno i;
62762 int nRef;
62763 IntegrityCk sCheck;
62764 BtShared *pBt = p->pBt;
 
62765 char zErr[100];
 
62766
62767 sqlite3BtreeEnter(p);
62768 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
62769 nRef = sqlite3PagerRefcount(pBt->pPager);
62770 sCheck.pBt = pBt;
62771 sCheck.pPager = pBt->pPager;
62772 sCheck.nPage = btreePagecount(sCheck.pBt);
62773 sCheck.mxErr = mxErr;
62774 sCheck.nErr = 0;
62775 sCheck.mallocFailed = 0;
62776 sCheck.zPfx = 0;
62777 sCheck.v1 = 0;
62778 sCheck.v2 = 0;
62779 *pnErr = 0;
 
 
62780 if( sCheck.nPage==0 ){
62781 sqlite3BtreeLeave(p);
62782 return 0;
62783 }
62784
62785 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
62786 if( !sCheck.aPgRef ){
62787 *pnErr = 1;
62788 sqlite3BtreeLeave(p);
62789 return 0;
62790 }
 
 
 
 
 
 
62791 i = PENDING_BYTE_PAGE(pBt);
62792 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
62793 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
62794
62795 /* Check the integrity of the freelist
62796 */
62797 sCheck.zPfx = "Main freelist: ";
62798 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
@@ -62799,21 +62829,23 @@
62799 get4byte(&pBt->pPage1->aData[36]));
62800 sCheck.zPfx = 0;
62801
62802 /* Check all the tables.
62803 */
 
 
62804 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
 
62805 if( aRoot[i]==0 ) continue;
62806 #ifndef SQLITE_OMIT_AUTOVACUUM
62807 if( pBt->autoVacuum && aRoot[i]>1 ){
62808 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
62809 }
62810 #endif
62811 sCheck.zPfx = "List of tree roots: ";
62812 checkTreePage(&sCheck, aRoot[i], NULL, NULL);
62813 sCheck.zPfx = 0;
62814 }
 
62815
62816 /* Make sure every page in the file is referenced
62817 */
62818 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
62819 #ifdef SQLITE_OMIT_AUTOVACUUM
@@ -62833,32 +62865,24 @@
62833 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
62834 }
62835 #endif
62836 }
62837
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 /* Clean up and report errors.
62850 */
62851 sqlite3BtreeLeave(p);
 
62852 sqlite3_free(sCheck.aPgRef);
62853 if( sCheck.mallocFailed ){
62854 sqlite3StrAccumReset(&sCheck.errMsg);
62855 *pnErr = sCheck.nErr+1;
62856 return 0;
62857 }
62858 *pnErr = sCheck.nErr;
62859 if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
 
 
 
62860 return sqlite3StrAccumFinish(&sCheck.errMsg);
62861 }
62862 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
62863
62864 /*
@@ -63822,13 +63846,17 @@
63822 **
63823 ** It is assumed that the mutex associated with the BtShared object
63824 ** corresponding to the source database is held when this function is
63825 ** called.
63826 */
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){
 
 
 
 
63830 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
63831 if( !isFatalError(p->rc) && iPage<p->iNext ){
63832 /* The backup process p has already copied page iPage. But now it
63833 ** has been modified by a transaction on the source pager. Copy
63834 ** the new data into the backup.
@@ -63841,11 +63869,14 @@
63841 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
63842 if( rc!=SQLITE_OK ){
63843 p->rc = rc;
63844 }
63845 }
63846 }
 
 
 
63847 }
63848
63849 /*
63850 ** Restart the backup process. This is called when the pager layer
63851 ** detects that the database has been modified by an external database
@@ -112047,11 +112078,11 @@
112047 ){
112048 int i, j; /* Loop counters */
112049 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
112050 Vdbe *v; /* The virtual machine under construction */
112051 int isAgg; /* True for select lists like "count(*)" */
112052 ExprList *pEList; /* List of columns to extract. */
112053 SrcList *pTabList; /* List of tables to select from */
112054 Expr *pWhere; /* The WHERE clause. May be NULL */
112055 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
112056 Expr *pHaving; /* The HAVING clause. May be NULL */
112057 int rc = 1; /* Value to return from this function */
112058
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-07-03 17:54:49 030f60a7ba171650ce8c0ac32dc166eab80aca32"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -8354,10 +8354,20 @@
8354 # define SQLITE_NOINLINE __declspec(noinline)
8355 #else
8356 # define SQLITE_NOINLINE
8357 #endif
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
8369 /*
8370 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
8371 ** 0 means mutexes are permanently disable and the library is never
8372 ** threadsafe. 1 means the library is serialized which is the highest
8373 ** level of threadsafety. 2 means the library is multithreaded - multiple
@@ -10323,11 +10333,13 @@
10333 #endif
10334
10335 /* Functions used to query pager state and configuration. */
10336 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
10337 SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
10338 #ifdef SQLITE_DEBUG
10339 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
10340 #endif
10341 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
10342 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
10343 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
10344 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
10345 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
@@ -24920,24 +24932,31 @@
24932 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
24933 #if SQLITE_BYTEORDER==4321
24934 u32 x;
24935 memcpy(&x,p,4);
24936 return x;
24937 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
24938 u32 x;
24939 memcpy(&x,p,4);
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);
24945 #else
24946 testcase( p[0]&0x80 );
24947 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
24948 #endif
24949 }
24950 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
24951 #if SQLITE_BYTEORDER==4321
24952 memcpy(p,&v,4);
24953 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
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);
24958 memcpy(p,&x,4);
24959 #else
24960 p[0] = (u8)(v>>24);
24961 p[1] = (u8)(v>>16);
24962 p[2] = (u8)(v>>8);
@@ -48503,16 +48522,18 @@
48522 */
48523 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
48524 return pPager->readOnly;
48525 }
48526
48527 #ifdef SQLITE_DEBUG
48528 /*
48529 ** Return the number of references to the pager.
48530 */
48531 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
48532 return sqlite3PcacheRefCount(pPager->pPCache);
48533 }
48534 #endif
48535
48536 /*
48537 ** Return the approximate number of bytes of memory currently
48538 ** used by the pager and its associated cache.
48539 */
@@ -53247,10 +53268,11 @@
53268 int nErr; /* Number of messages written to zErrMsg so far */
53269 int mallocFailed; /* A memory allocation error has occurred */
53270 const char *zPfx; /* Error message prefix */
53271 int v1, v2; /* Values for up to two %d fields in zPfx */
53272 StrAccum errMsg; /* Accumulate the error message text here */
53273 u32 *heap; /* Min-heap used for analyzing cell coverage */
53274 };
53275
53276 /*
53277 ** Routines to read or write a two- and four-byte big-endian integer values.
53278 */
@@ -53266,10 +53288,12 @@
53288 */
53289 #if SQLITE_BYTEORDER==4321
53290 # define get2byteAligned(x) (*(u16*)(x))
53291 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
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))
53295 #else
53296 # define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
53297 #endif
53298
53299 /************** End of btreeInt.h ********************************************/
@@ -54596,10 +54620,13 @@
54620 ){
54621 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54622 assert( pPage->leaf==0 );
54623 assert( pPage->noPayload );
54624 assert( pPage->childPtrSize==4 );
54625 #ifndef SQLITE_DEBUG
54626 UNUSED_PARAMETER(pPage);
54627 #endif
54628 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54629 pInfo->nPayload = 0;
54630 pInfo->nLocal = 0;
54631 pInfo->iOverflow = 0;
54632 pInfo->pPayload = 0;
@@ -54793,10 +54820,12 @@
54820 ** the (CellInfo.nSize) value found by doing a full parse of the
54821 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54822 ** this function verifies that this invariant is not violated. */
54823 CellInfo debuginfo;
54824 pPage->xParseCell(pPage, pCell, &debuginfo);
54825 #else
54826 UNUSED_PARAMETER(pPage);
54827 #endif
54828
54829 assert( pPage->childPtrSize==4 );
54830 pEnd = pIter + 9;
54831 while( (*pIter++)&0x80 && pIter<pEnd );
@@ -57143,11 +57172,11 @@
57172 ** pages are in use.
57173 */
57174 static int autoVacuumCommit(BtShared *pBt){
57175 int rc = SQLITE_OK;
57176 Pager *pPager = pBt->pPager;
57177 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
57178
57179 assert( sqlite3_mutex_held(pBt->mutex) );
57180 invalidateAllOverflowCache(pBt);
57181 assert(pBt->autoVacuum);
57182 if( !pBt->incrVacuum ){
@@ -62478,36 +62507,42 @@
62507 **
62508 ** These checks are done:
62509 **
62510 ** 1. Make sure that cells and freeblocks do not overlap
62511 ** but combine to completely cover the page.
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.
 
 
 
 
62516 */
62517 static int checkTreePage(
62518 IntegrityCk *pCheck, /* Context for the sanity check */
62519 int iPage, /* Page number of the page to check */
62520 i64 *piMinKey, /* Write minimum integer primary key here */
62521 i64 maxKey /* Error if integer primary key greater than this */
62522 ){
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 */
62544 const char *saved_zPfx = pCheck->zPfx;
62545 int saved_v1 = pCheck->v1;
62546 int saved_v2 = pCheck->v2;
62547
62548 /* Check that the page exists
@@ -62519,11 +62554,10 @@
62554 pCheck->zPfx = "Page %d: ";
62555 pCheck->v1 = iPage;
62556 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
62557 checkAppendMsg(pCheck,
62558 "unable to get the page. error code=%d", rc);
 
62559 goto end_of_check;
62560 }
62561
62562 /* Clear MemPage.isInit to make sure the corruption detection code in
62563 ** btreeInitPage() is executed. */
@@ -62530,208 +62564,198 @@
62564 pPage->isInit = 0;
62565 if( (rc = btreeInitPage(pPage))!=0 ){
62566 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
62567 checkAppendMsg(pCheck,
62568 "btreeInitPage() returns error code %d", rc);
 
 
62569 goto end_of_check;
62570 }
62571 data = pPage->aData;
62572 hdr = pPage->hdrOffset;
62573
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--){
62611 CellInfo info;
62612
62613 /* Check cell size */
 
 
 
62614 pCheck->v2 = 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];
62625 pPage->xParseCell(pPage, pCell, &info);
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 */
62633 if( pPage->intKey ){
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]);
62647 #ifndef SQLITE_OMIT_AUTOVACUUM
62648 if( pBt->autoVacuum ){
62649 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
62650 }
62651 #endif
62652 checkList(pCheck, 0, pgnoOvfl, nPage);
62653 }
62654
 
 
62655 if( !pPage->leaf ){
62656 /* Check sanity of left child page for internal pages */
62657 pgno = get4byte(pCell);
62658 #ifndef SQLITE_OMIT_AUTOVACUUM
62659 if( pBt->autoVacuum ){
62660 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
62661 }
62662 #endif
62663 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
62664 keyCanBeEqual = 0;
62665 if( d2!=depth ){
62666 checkAppendMsg(pCheck, "Child page depth differs");
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;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62675
62676 /* Check for complete coverage of the page
62677 */
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]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62690 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
62691 }
62692 }
62693 /* Add the freeblocks to the min-heap
62694 **
62695 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
62696 ** is the offset of the first freeblock, or zero if there are no
62697 ** freeblocks on the page.
62698 */
62699 i = get2byte(&data[hdr+1]);
62700 while( i>0 ){
62701 int size, j;
62702 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
62703 size = get2byte(&data[i+2]);
62704 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
62705 btreeHeapInsert(heap, (i<<16)|(i+size-1));
62706 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
62707 ** big-endian integer which is the offset in the b-tree page of the next
62708 ** freeblock in the chain, or zero if the freeblock is the last on the
62709 ** chain. */
62710 j = get2byte(&data[i]);
62711 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
62712 ** increasing offset. */
62713 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
62714 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
62715 i = j;
62716 }
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 */
62732 while( btreeHeapPull(heap,&x) ){
62733 if( (prev&0xffff)>=(x>>16) ){
62734 checkAppendMsg(pCheck,
62735 "Multiple uses for byte %u of page %d", x>>16, iPage);
62736 break;
62737 }else{
62738 nFrag += (x>>16) - (prev&0xffff) - 1;
62739 prev = x;
62740 }
62741 }
62742 nFrag += usableSize - (prev&0xffff) - 1;
62743 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
62744 ** is stored in the fifth field of the b-tree page header.
62745 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
62746 ** number of fragmented free bytes within the cell content area.
62747 */
62748 if( heap[0]==0 && nFrag!=data[hdr+7] ){
62749 checkAppendMsg(pCheck,
62750 "Fragmentation of %d bytes reported as %d on page %d",
62751 nFrag, data[hdr+7], iPage);
62752 }
62753 }
 
 
62754
62755 end_of_check:
62756 releasePage(pPage);
62757 pCheck->zPfx = saved_zPfx;
62758 pCheck->v1 = saved_v1;
62759 pCheck->v2 = saved_v2;
62760 return depth+1;
62761 }
@@ -62757,42 +62781,48 @@
62781 int nRoot, /* Number of entries in aRoot[] */
62782 int mxErr, /* Stop reporting errors after this many */
62783 int *pnErr /* Write number of errors seen to this variable */
62784 ){
62785 Pgno i;
 
62786 IntegrityCk sCheck;
62787 BtShared *pBt = p->pBt;
62788 int savedDbFlags = pBt->db->flags;
62789 char zErr[100];
62790 VVA_ONLY( int nRef );
62791
62792 sqlite3BtreeEnter(p);
62793 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
62794 assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 );
62795 sCheck.pBt = pBt;
62796 sCheck.pPager = pBt->pPager;
62797 sCheck.nPage = btreePagecount(sCheck.pBt);
62798 sCheck.mxErr = mxErr;
62799 sCheck.nErr = 0;
62800 sCheck.mallocFailed = 0;
62801 sCheck.zPfx = 0;
62802 sCheck.v1 = 0;
62803 sCheck.v2 = 0;
62804 sCheck.aPgRef = 0;
62805 sCheck.heap = 0;
62806 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
62807 if( sCheck.nPage==0 ){
62808 goto integrity_ck_cleanup;
 
62809 }
62810
62811 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
62812 if( !sCheck.aPgRef ){
62813 sCheck.mallocFailed = 1;
62814 goto integrity_ck_cleanup;
 
62815 }
62816 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
62817 if( sCheck.heap==0 ){
62818 sCheck.mallocFailed = 1;
62819 goto integrity_ck_cleanup;
62820 }
62821
62822 i = PENDING_BYTE_PAGE(pBt);
62823 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 
62824
62825 /* Check the integrity of the freelist
62826 */
62827 sCheck.zPfx = "Main freelist: ";
62828 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
@@ -62799,21 +62829,23 @@
62829 get4byte(&pBt->pPage1->aData[36]));
62830 sCheck.zPfx = 0;
62831
62832 /* Check all the tables.
62833 */
62834 testcase( pBt->db->flags & SQLITE_CellSizeCk );
62835 pBt->db->flags &= ~SQLITE_CellSizeCk;
62836 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
62837 i64 notUsed;
62838 if( aRoot[i]==0 ) continue;
62839 #ifndef SQLITE_OMIT_AUTOVACUUM
62840 if( pBt->autoVacuum && aRoot[i]>1 ){
62841 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
62842 }
62843 #endif
62844 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
 
 
62845 }
62846 pBt->db->flags = savedDbFlags;
62847
62848 /* Make sure every page in the file is referenced
62849 */
62850 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
62851 #ifdef SQLITE_OMIT_AUTOVACUUM
@@ -62833,32 +62865,24 @@
62865 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
62866 }
62867 #endif
62868 }
62869
 
 
 
 
 
 
 
 
 
 
 
62870 /* Clean up and report errors.
62871 */
62872 integrity_ck_cleanup:
62873 sqlite3PageFree(sCheck.heap);
62874 sqlite3_free(sCheck.aPgRef);
62875 if( sCheck.mallocFailed ){
62876 sqlite3StrAccumReset(&sCheck.errMsg);
62877 sCheck.nErr++;
 
62878 }
62879 *pnErr = sCheck.nErr;
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);
62884 return sqlite3StrAccumFinish(&sCheck.errMsg);
62885 }
62886 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
62887
62888 /*
@@ -63822,13 +63846,17 @@
63846 **
63847 ** It is assumed that the mutex associated with the BtShared object
63848 ** corresponding to the source database is held when this function is
63849 ** called.
63850 */
63851 static SQLITE_NOINLINE void backupUpdate(
63852 sqlite3_backup *p,
63853 Pgno iPage,
63854 const u8 *aData
63855 ){
63856 assert( p!=0 );
63857 do{
63858 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
63859 if( !isFatalError(p->rc) && iPage<p->iNext ){
63860 /* The backup process p has already copied page iPage. But now it
63861 ** has been modified by a transaction on the source pager. Copy
63862 ** the new data into the backup.
@@ -63841,11 +63869,14 @@
63869 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
63870 if( rc!=SQLITE_OK ){
63871 p->rc = rc;
63872 }
63873 }
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);
63878 }
63879
63880 /*
63881 ** Restart the backup process. This is called when the pager layer
63882 ** detects that the database has been modified by an external database
@@ -112047,11 +112078,11 @@
112078 ){
112079 int i, j; /* Loop counters */
112080 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
112081 Vdbe *v; /* The virtual machine under construction */
112082 int isAgg; /* True for select lists like "count(*)" */
112083 ExprList *pEList = 0; /* List of columns to extract. */
112084 SrcList *pTabList; /* List of tables to select from */
112085 Expr *pWhere; /* The WHERE clause. May be NULL */
112086 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
112087 Expr *pHaving; /* The HAVING clause. May be NULL */
112088 int rc = 1; /* Value to return from this function */
112089
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111111
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112112
** [sqlite_version()] and [sqlite_source_id()].
113113
*/
114114
#define SQLITE_VERSION "3.8.11"
115115
#define SQLITE_VERSION_NUMBER 3008011
116
-#define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
116
+#define SQLITE_SOURCE_ID "2015-07-03 17:54:49 030f60a7ba171650ce8c0ac32dc166eab80aca32"
117117
118118
/*
119119
** CAPI3REF: Run-Time Library Version Numbers
120120
** KEYWORDS: sqlite3_version, sqlite3_sourceid
121121
**
122122
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
122
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-07-03 17:54:49 030f60a7ba171650ce8c0ac32dc166eab80aca32"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
122

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button