Index: ext/misc/scrub.c ================================================================== --- ext/misc/scrub.c +++ ext/misc/scrub.c @@ -72,10 +72,11 @@ sqlite3 *dbDest; /* Destination database connection */ sqlite3_file *pDest; /* Destination file handle */ u32 szPage; /* Page size */ u32 szUsable; /* Usable bytes on each page */ u32 nPage; /* Number of pages */ + u32 iLastPage; /* Page number of last page written so far*/ u8 *page1; /* Content of page 1 */ }; /* Store an error message */ static void scrubBackupErr(ScrubState *p, const char *zFormat, ...){ @@ -128,10 +129,11 @@ rc = p->pDest->pMethods->xWrite(p->pDest, pData, p->szPage, iOff); if( rc!=SQLITE_OK ){ scrubBackupErr(p, "write failed for page %d", pgno); p->rcErr = SQLITE_IOERR; } + if( pgno>p->iLastPage ) p->iLastPage = pgno; } /* Prepare a statement against the "db" database. */ static sqlite3_stmt *scrubBackupPrepare( ScrubState *p, /* Backup context */ @@ -538,10 +540,24 @@ while( sqlite3_step(pStmt)==SQLITE_ROW ){ i = (u32)sqlite3_column_int(pStmt, 0); scrubBackupBtree(&s, i, 0); } sqlite3_finalize(pStmt); + + /* If the last page of the input db file is a free-list leaf, then the + ** backup file on disk is still smaller than the size indicated within + ** the database header. In this case, write a page of zeroes to the + ** last page of the backup database so that SQLite does not mistakenly + ** think the db is corrupt. */ + if( s.iLastPage