Index: src/btree.c ================================================================== --- src/btree.c +++ src/btree.c @@ -7,11 +7,11 @@ ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.543 2008/11/27 02:22:11 drh Exp $ +** $Id: btree.c,v 1.544 2008/12/05 20:01:43 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. ** Including a description of file format and an overview of operation. */ @@ -5535,10 +5535,11 @@ releasePage(apOld[i]); } for(i=0; inOverflow = 0; /* releasePage(pParent); */ TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", pPage->pgno, nOld, nNew, nCell)); @@ -5608,11 +5609,11 @@ assemblePage(pPage, pChild->nCell, apCell, szCell); /* Copy the right-pointer of the child to the parent. */ assert( sqlite3PagerIswriteable(pPage->pDbPage) ); put4byte(&pPage->aData[pPage->hdrOffset+8], get4byte(&pChild->aData[pChild->hdrOffset+8])); - freePage(pChild); + rc = freePage(pChild); TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); }else{ /* The child has more information that will fit on the root. ** The tree is already balanced. Do nothing. */ TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); @@ -5626,11 +5627,11 @@ TRACE(("BALANCE: transfer child %d into root %d\n", pChild->pgno, pPage->pgno)); } assert( pPage->nOverflow==0 ); #ifndef SQLITE_OMIT_AUTOVACUUM - if( ISAUTOVACUUM ){ + if( ISAUTOVACUUM && rc==SQLITE_OK ){ rc = setChildPtrmaps(pPage); } #endif releasePage(pChild); } @@ -5732,18 +5733,21 @@ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); if( pCur->iPage==0 ){ rc = sqlite3PagerWrite(pPage->pDbPage); if( rc==SQLITE_OK && pPage->nOverflow>0 ){ rc = balance_deeper(pCur); + assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); } if( rc==SQLITE_OK && pPage->nCell==0 ){ rc = balance_shallower(pCur); + assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); } }else{ if( pPage->nOverflow>0 || (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ rc = balance_nonroot(pCur); + assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); } } return rc; } @@ -5908,12 +5912,19 @@ pCur->validNKey = 0; }else{ assert( pPage->leaf ); } rc = insertCell(pPage, idx, newCell, szNew, 0, 0); - if( rc!=SQLITE_OK ) goto end_insert; - rc = balance(pCur, 1); + if( rc==SQLITE_OK ){ + rc = balance(pCur, 1); + } + + /* Must make sure nOverflow is reset to zero even if the balance() + ** fails. Internal data structure corruption will result otherwise. */ + assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); + pPage->nOverflow = 0; + if( rc==SQLITE_OK ){ moveToRoot(pCur); } end_insert: return rc;