Harden the dbstat extension against corrupt database files.

FossilOrigin-Name: a0d47f25ae7bdf98f5b853f23776b3bf86bea7c0dda386664c1e3b1c363c518f
This commit is contained in:
drh 2018-10-29 16:07:10 +00:00
parent d811d844cd
commit f888158c7f
3 changed files with 30 additions and 13 deletions

View File

@ -1,5 +1,5 @@
C Improvements\sto\sthe\sdbfuzz2.c\stest\smodule.
D 2018-10-27T21:06:44.652
C Harden\sthe\sdbstat\sextension\sagainst\scorrupt\sdatabase\sfiles.
D 2018-10-29T16:07:10.322
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F Makefile.in 15344f4e44dfd9ffb04e9867bdd352a8a5a86211b8919a6ca724e7063694320b
@ -454,7 +454,7 @@ F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
F src/ctime.c 56e2f32d2e5491c152352bd53cffc9979ee1e1b70df39ec97a90920ae420a950
F src/date.c ebe1dc7c8a347117bb02570f1a931c62dd78f4a2b1b516f4837d45b7d6426957
F src/dbpage.c 4aa7f26198934dbd002e69418220eae3dbc71b010bbac32bd78faf86b52ce6c3
F src/dbstat.c edabb82611143727511a45ca0859b8cd037851ebe756ae3db289859dd18b6f91
F src/dbstat.c 5f96184b8a751b7c92b959b55679f56e409c3b3bbe98eaf5e43189c16d4df082
F src/delete.c 107e28d3ef8bd72fd11953374ca9107cd74e8b09c3ded076a6048742d26ce7d2
F src/expr.c 5cee8fb79b1952689af80ed71ed16ad295f29d85de30c7592993b05cf1ec1e06
F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
@ -1774,7 +1774,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P a4a083ed8cdb106af661d2ee0203e21c576f5c2304419ce603826e4f2851c2e0
R e5a9cd9771ffc88fd093fdbd062e0c2a
P d60eff493b875366981c5a25000bb65cde9f6e628192914910790acc562c17b9
R 668f948a6c21a9c15cb9b96a8664b9e2
U drh
Z 7b11fae1033935d453c9b6c66817005d
Z 0ebd96da0851d12eb56b4ff281cd01aa

View File

@ -1 +1 @@
d60eff493b875366981c5a25000bb65cde9f6e628192914910790acc562c17b9
a0d47f25ae7bdf98f5b853f23776b3bf86bea7c0dda386664c1e3b1c363c518f

View File

@ -324,22 +324,33 @@ static int statDecodePage(Btree *pBt, StatPage *p){
u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
p->flags = aHdr[0];
if( p->flags==0x0A || p->flags==0x0D ){
isLeaf = 1;
nHdr = 8;
}else if( p->flags==0x05 || p->flags==0x02 ){
isLeaf = 0;
nHdr = 12;
}else{
goto statPageIsCorrupt;
}
if( p->iPgno==1 ) nHdr += 100;
p->nCell = get2byte(&aHdr[3]);
p->nMxPayload = 0;
isLeaf = (p->flags==0x0A || p->flags==0x0D);
nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
szPage = sqlite3BtreeGetPageSize(pBt);
nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
nUnused += (int)aHdr[7];
iOff = get2byte(&aHdr[1]);
while( iOff ){
int iNext;
if( iOff>=szPage ) goto statPageIsCorrupt;
nUnused += get2byte(&aData[iOff+2]);
iOff = get2byte(&aData[iOff]);
iNext = get2byte(&aData[iOff]);
if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
iOff = iNext;
}
p->nUnused = nUnused;
p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
szPage = sqlite3BtreeGetPageSize(pBt);
if( p->nCell ){
int i; /* Used to iterate through cells */
@ -356,6 +367,7 @@ static int statDecodePage(Btree *pBt, StatPage *p){
StatCell *pCell = &p->aCell[i];
iOff = get2byte(&aData[nHdr+i*2]);
if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
if( !isLeaf ){
pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
iOff += 4;
@ -372,8 +384,8 @@ static int statDecodePage(Btree *pBt, StatPage *p){
}
if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
if( nLocal<0 ) goto statPageIsCorrupt;
pCell->nLocal = nLocal;
assert( nLocal>=0 );
assert( nPayload>=(u32)nLocal );
assert( nLocal<=(nUsable-35) );
if( nPayload>(u32)nLocal ){
@ -402,6 +414,11 @@ static int statDecodePage(Btree *pBt, StatPage *p){
}
return SQLITE_OK;
statPageIsCorrupt:
p->flags = 0;
p->nCell = 0;
return SQLITE_OK;
}
/*