Fix a case where during a rollback triggered by an IO or malloc error an unjournalled region of the database could be written to (with it's original data). This was causing an assert in test_journal.c to fail. Add a test case in ioerr2.test to trigger this case. (CVS 6286)

FossilOrigin-Name: 315a6692f9a03a470871cce4f74567683546f343
This commit is contained in:
danielk1977
2009-02-12 09:11:56 +00:00
parent ce14f6245e
commit f7f33fb08a
5 changed files with 62 additions and 17 deletions
+9 -5
View File
@@ -18,7 +18,7 @@
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.566 2009/02/04 19:16:23 drh Exp $
** @(#) $Id: pager.c,v 1.567 2009/02/12 09:11:56 danielk1977 Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
@@ -1416,6 +1416,7 @@ static u32 pager_cksum(Pager *pPager, const u8 *aData){
static int pager_playback_one_page(
Pager *pPager, /* The pager being played back */
int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
int isUnsync, /* True if reading from unsynced main journal */
i64 *pOffset, /* Offset of record to playback */
int isSavepnt, /* True for a savepoint rollback */
Bitvec *pDone /* Bitvec of pages already played back */
@@ -1511,6 +1512,7 @@ static int pager_playback_one_page(
if( (pPager->state>=PAGER_EXCLUSIVE)
&& (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
&& isOpen(pPager->fd)
&& !isUnsync
){
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
@@ -1951,6 +1953,7 @@ static int pager_playback(Pager *pPager, int isHot){
** occurs.
*/
while( 1 ){
int isUnsync = 0;
/* Read the next journal header from the journal file. If there are
** not enough bytes left in the journal file for a complete header, or
@@ -1997,6 +2000,7 @@ static int pager_playback(Pager *pPager, int isHot){
if( nRec==0 && !isHot &&
pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
isUnsync = 1;
}
/* If this is the first header read from the journal, truncate the
@@ -2018,7 +2022,7 @@ static int pager_playback(Pager *pPager, int isHot){
pager_reset(pPager);
needPagerReset = 0;
}
rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 0, 0);
rc = pager_playback_one_page(pPager,1,isUnsync,&pPager->journalOff,0,0);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_DONE ){
rc = SQLITE_OK;
@@ -2160,7 +2164,7 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
pPager->journalOff = pSavepoint->iOffset;
while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
}
assert( rc!=SQLITE_DONE );
}else{
@@ -2195,7 +2199,7 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
}
for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
rc = pager_playback_one_page(pPager, 1, &pPager->journalOff, 1, pDone);
rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
}
assert( rc!=SQLITE_DONE );
}
@@ -2210,7 +2214,7 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
assert( offset==ii*(4+pPager->pageSize) );
rc = pager_playback_one_page(pPager, 0, &offset, 1, pDone);
rc = pager_playback_one_page(pPager, 0, 0, &offset, 1, pDone);
}
assert( rc!=SQLITE_DONE );
}
+19 -1
View File
@@ -15,7 +15,7 @@
** correctly populates and syncs a journal file before writing to a
** corresponding database file.
**
** $Id: test_journal.c,v 1.10 2009/02/11 07:38:12 danielk1977 Exp $
** $Id: test_journal.c,v 1.11 2009/02/12 09:11:56 danielk1977 Exp $
*/
#if SQLITE_TEST /* This file is used for testing only */
@@ -206,6 +206,15 @@ struct JtGlobal {
};
static struct JtGlobal g = {0, 0};
extern int sqlite3_io_error_pending;
static void stop_ioerr_simulation(int *piSave){
*piSave = sqlite3_io_error_pending;
sqlite3_io_error_pending = -1;
}
static void start_ioerr_simulation(int iSave){
sqlite3_io_error_pending = iSave;
}
/*
** The jt_file pointed to by the argument may or may not be a file-handle
** open on a main database file. If it is, and a transaction is currently
@@ -343,6 +352,9 @@ static int openTransaction(jt_file *pMain, jt_file *pJournal){
rc = SQLITE_IOERR_NOMEM;
}else if( pMain->nPage>0 ){
u32 iTrunk;
int iSave;
stop_ioerr_simulation(&iSave);
/* Read the database free-list. Add the page-number for each free-list
** leaf to the jt_file.pWritable bitvec.
@@ -372,6 +384,8 @@ static int openTransaction(jt_file *pMain, jt_file *pJournal){
pMain->aCksum[ii] = genCksum(aData, pMain->nPagesize);
}
}
start_ioerr_simulation(iSave);
}
sqlite3_free(aData);
@@ -465,12 +479,15 @@ static int readJournalFile(jt_file *p, jt_file *pMain){
sqlite3_int64 iOff = 0;
sqlite3_int64 iSize = p->iMaxOff;
unsigned char *aPage;
int iSave;
aPage = sqlite3_malloc(pMain->nPagesize);
if( !aPage ){
return SQLITE_IOERR_NOMEM;
}
stop_ioerr_simulation(&iSave);
while( rc==SQLITE_OK && iOff<iSize ){
u32 nRec, nPage, nSector, nPagesize;
u32 ii;
@@ -522,6 +539,7 @@ static int readJournalFile(jt_file *p, jt_file *pMain){
}
finish_rjf:
start_ioerr_simulation(iSave);
sqlite3_free(aPage);
if( rc==SQLITE_IOERR_SHORT_READ ){
rc = SQLITE_OK;