SQLite

View Ticket
Login
2012-12-21
16:16 Closed ticket [0cfd98ee]: Potential corruption of backup db when backing up a db last written by sqlite<3.7.0 plus 5 other changes (artifact: 82635d7d user: dan)
16:15
Ensure the database size field in the db header of a backup database is set correctly. Fix for [0cfd98ee201]. (check-in: ff6857b6 user: dan tags: trunk)
16:01 New ticket [0cfd98ee] Potential corruption of backup db when backing up a db last written by sqlite<3.7.0. (artifact: d23e9114 user: dan)

Ticket Hash: 0cfd98ee201a2aa8f3f494bcc90d2977dc4d1990
Title: Potential corruption of backup db when backing up a db last written by sqlite<3.7.0
Status: Closed Type: Code_Defect
Severity: Important Priority: Immediate
Subsystem: Pager Resolution: Fixed
Last Modified: 2012-12-21 16:16:53
Version Found In: 3.7.15
Description:
Versions of sqlite 3.7.0 and newer store the size of the database image in the database file header. Earlier versions do not. When a new version writes to the db, it updates the database size field and sets a flag in the header to indicate that the last version to write the db updated the db size field. An earlier version does not update the db size field, but does clear the flag.

If the flag is set, SQLite trusts the db size field in the header. So if it is set incorrectly, the database is considered corrupt.

The current problem comes about as follows:

  1. A 'new' version of sqlite writes to the database file, setting the db size field.
  2. An 'old' version of sqlite writes the db and changes the file size. This means the db size field is now invalid, but the flag is cleared so the db is not corrupt.
  3. A 'new' version of sqlite does a backup of the db. In the backup, the db size field remains unchanged, but the "written by a new version" flag is set. So the backup is now corrupted.

Note: The "flag" mentioned above is not actually a flag, it is a second copy of the change counter that is only updated by post-3.7.0 versions of sqlite. The flag is considered set if that second copy matches the first.

User Comments:
dan added on 2012-12-21 16:16:53:
Fixed by [ff6857b6ed].