1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Add the btreeGetUnusedPage() routine to btree.c, and use it to detect content

pages on the freelist and to cause that condition to trigger an SQLITE_CORRUPT.

FossilOrigin-Name: fe15d1f70360d6fef8ef1a111dd43e060d059623
This commit is contained in:
drh
2015-05-28 03:28:27 +00:00
parent 275fe3adf1
commit 7e8c6f1c2e
3 changed files with 48 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
C Do\snot\sattempt\sto\stake\sany\swrite\slock\son\sa\sread-only\sdatabase\son\sWindows. C Add\sthe\sbtreeGetUnusedPage()\sroutine\sto\sbtree.c,\sand\suse\sit\sto\sdetect\scontent\npages\son\sthe\sfreelist\sand\sto\scause\sthat\scondition\sto\strigger\san\sSQLITE_CORRUPT.
D 2015-05-28T00:54:35.202 D 2015-05-28T03:28:27.932
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 994bab32a3a69e0c35bd148b65cde49879772964 F Makefile.in 994bab32a3a69e0c35bd148b65cde49879772964
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240
F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3
F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d
F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79
F src/btree.c 84b1f0f4688c8da2c426a158502a77617ce5f286 F src/btree.c 7cd71f5579269590ebc864cb0e196fa02c52339a
F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1
F src/btreeInt.h 973a22a6fd61350b454ad614832b1f0a5e25a1e4 F src/btreeInt.h 973a22a6fd61350b454ad614832b1f0a5e25a1e4
F src/build.c 85a169a0a22f8b80caf513eaf2944d39b979f571 F src/build.c 85a169a0a22f8b80caf513eaf2944d39b979f571
@@ -1280,7 +1280,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 06959d4ada53b765cae4b192a691fced4b8aebbb P a47ff0cdab0f82398c68ea770053f193f4812a51
R b30cfcc10e16186636545a138869402d R 6b967d488166149b477f7892b5bb9a1c
U drh U drh
Z 3b24ef1efe39a20b744ff0c0269b43f9 Z 3a9fdccaff4c9c6bd17852b6f3db0358

View File

@@ -1 +1 @@
a47ff0cdab0f82398c68ea770053f193f4812a51 fe15d1f70360d6fef8ef1a111dd43e060d059623

View File

@@ -1723,10 +1723,10 @@ static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
/* /*
** Get a page from the pager. Initialize the MemPage.pBt and ** Get a page from the pager. Initialize the MemPage.pBt and
** MemPage.aData elements if needed. ** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
** **
** If the noContent flag is set, it means that we do not care about ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
** the content of the page at this time. So do not go to the disk ** about the content of the page at this time. So do not go to the disk
** to fetch the content. Just fill in the content with zeros for now. ** to fetch the content. Just fill in the content with zeros for now.
** If in the future we call sqlite3PagerWrite() on this page, that ** If in the future we call sqlite3PagerWrite() on this page, that
** means we have started to be concerned about content and the disk ** means we have started to be concerned about content and the disk
@@ -1828,6 +1828,36 @@ static void releasePage(MemPage *pPage){
} }
} }
/*
** Get an unused page.
**
** This works just like btreeGetPage() with the addition:
**
** * If the page is already in use for some other purpose, immediately
** release it and return an SQLITE_CURRUPT error.
** * Make sure the isInit flag is clear
*/
static int btreeGetUnusedPage(
BtShared *pBt, /* The btree */
Pgno pgno, /* Number of the page to fetch */
MemPage **ppPage, /* Return the page in this parameter */
int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
){
int rc = btreeGetPage(pBt, pgno, ppPage, flags);
if( rc==SQLITE_OK ){
if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
releasePage(*ppPage);
*ppPage = 0;
return SQLITE_CORRUPT_BKPT;
}
(*ppPage)->isInit = 0;
}else{
*ppPage = 0;
}
return rc;
}
/* /*
** During a rollback, when the pager reloads information into the cache ** During a rollback, when the pager reloads information into the cache
** so that the cache is restored to its original state at the start of ** so that the cache is restored to its original state at the start of
@@ -5345,7 +5375,7 @@ static int allocateBtreePage(
if( iTrunk>mxPage ){ if( iTrunk>mxPage ){
rc = SQLITE_CORRUPT_BKPT; rc = SQLITE_CORRUPT_BKPT;
}else{ }else{
rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
} }
if( rc ){ if( rc ){
pTrunk = 0; pTrunk = 0;
@@ -5410,7 +5440,7 @@ static int allocateBtreePage(
goto end_allocate_page; goto end_allocate_page;
} }
testcase( iNewTrunk==mxPage ); testcase( iNewTrunk==mxPage );
rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
goto end_allocate_page; goto end_allocate_page;
} }
@@ -5490,7 +5520,7 @@ static int allocateBtreePage(
} }
put4byte(&aData[4], k-1); put4byte(&aData[4], k-1);
noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){
rc = sqlite3PagerWrite((*ppPage)->pDbPage); rc = sqlite3PagerWrite((*ppPage)->pDbPage);
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
@@ -5538,7 +5568,7 @@ static int allocateBtreePage(
MemPage *pPg = 0; MemPage *pPg = 0;
TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){
rc = sqlite3PagerWrite(pPg->pDbPage); rc = sqlite3PagerWrite(pPg->pDbPage);
releasePage(pPg); releasePage(pPg);
@@ -5552,11 +5582,12 @@ static int allocateBtreePage(
*pPgno = pBt->nPage; *pPgno = pBt->nPage;
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
if( rc ) return rc; if( rc ) return rc;
rc = sqlite3PagerWrite((*ppPage)->pDbPage); rc = sqlite3PagerWrite((*ppPage)->pDbPage);
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
releasePage(*ppPage); releasePage(*ppPage);
*ppPage = 0;
} }
TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
} }
@@ -5566,17 +5597,8 @@ static int allocateBtreePage(
end_allocate_page: end_allocate_page:
releasePage(pTrunk); releasePage(pTrunk);
releasePage(pPrevTrunk); releasePage(pPrevTrunk);
if( rc==SQLITE_OK ){ assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
releasePage(*ppPage);
*ppPage = 0;
return SQLITE_CORRUPT_BKPT;
}
(*ppPage)->isInit = 0;
}else{
*ppPage = 0;
}
assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
return rc; return rc;
} }