Skip to content

A file shorter than one page opens as a new empty database (read-write re-initializes it) #506

Closed

Description

@adityamukho

Found by the corrupt_file fuzz target (#387 PR B) in its first minutes.

What happens

A database file truncated to fewer than 4096 bytes (one page) opens without error as a new, empty database:

  • v3: FileBackend::open computes page_count = file_len / PAGE_SIZE, which is 0, so PersistentFactStorage::select_meta returns Opened::Fresh. A read-write open then runs init_empty and writes a fresh meta page over what is left of page 0. A read-only open shows an empty database.
  • main (v2.x): FileBackend::open treats file_len < PAGE_SIZE as a new file and writes a new header over it.

Either way, a damaged file is answered with wrong data (no facts, tx count 0) instead of an error, and the read-write case overwrites what was left of it.

Reproducer (fuzzer input, corrupt_file layout): 24 ae 08 00 00 00 ae 89. That is a read-write open of the template truncated to 2222 bytes, with no byte edits. Any length 1..4095 behaves the same.

Why it is not just a torn initial write

The only legitimate way to get a non-empty file under one page is a crash during the very first meta write of a brand-new database, and that file holds no data. A real database truncated by a bad copy, a full disk during a copy, or filesystem damage takes the same branch and is silently replaced.

Options

  1. Refuse any file of 1..4095 bytes with a new STG code ("file shorter than one page"). A crash in the first write of a new database then needs the file deleted by hand. That is rare, and it is clearly reported.
  2. Accept it as fresh only when the bytes present are a prefix of exactly what init_empty writes (generation 1 empty meta), or all zero. Refuse anything else.

Option 2 keeps crash-on-create recoverable and rejects everything else. Both apply to read-only opens as well.

A zero-length file stays a new database, as in SQLite.

Done when

  • Fix on v3, with a regression test in tests/ and the input above as a corrupt_file seed.
  • The corrupt_file target drops its temporary skip of sub-page lengths.
  • v2.x backport decided (same behavior on main).

Activity

  1. added this to the v3.0.0 milestone on Oct 10, 2026
  2. added
    stability
    data-integrity
    on Oct 10, 2026
  3. adityamukho commented on Oct 10, 2026

    @adityamukho
    CollaboratorAuthor

    Decision (2026-10-10): option 2. A file of 1..4095 bytes is accepted as a new database only when its bytes are all zero or a prefix of exactly what init_empty writes (the generation-1 empty meta), the same rule is_torn_initial_meta already applies to a full page 0. Anything else is refused with a new STG code, for read-write and read-only opens. A zero-length file stays a new database.

  4. added 3 commits that reference this issue on Oct 10, 2026
  5. adityamukho commented on Oct 11, 2026

    @adityamukho
    CollaboratorAuthor

    Fixed in v2.0.5 (released 2026-10-11, PR #509): a file of 1–4,095 bytes is refused with STG-045 unless it is a torn first write. Also fixed on v3 for v3.0.0. Listed under "Fixed in v2.0.5" in #421.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    data-integritystability

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions