8000 fix(storage): refuse a sub-page file that is not a torn first write (#506, v2.x backport) by adityamukho · Pull Request #509 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content

fix(storage): refuse a sub-page file that is not a torn first write (#506, v2.x backport) - #509

Merged
adityamukho merged 1 commit into
mainfrom
fix/506-short-file-v2
Oct 10, 2026
Merged

adityamukho merged 1 commit into
mainfrom
fix/506-short-file-v2

Conversation

@adityamukho
Copy link
Copy Markdown
Collaborator

v2.x backport of #508 (merged to v3). Part of #506, a data-integrity issue that stays open until the fix is in a published release.

Problem

FileBackend::open set is_new = file_len < PAGE_SIZE and wrote a new header over any file under 4 KiB. A database cut below one page by an interrupted copy, or an unrelated file at the path, was silently replaced.

Fix

When 0 < file_len < PAGE_SIZE, open reads the bytes and accepts the file as new only if they are zeros or a prefix of the initial header page open itself writes (FileHeader::new() padded with zeros). Anything else is refused with STG-045 and the file is left as it was. A zero-length file is still a new database. The code number matches v3 (STG-028–044 exist only on v3, and codes are never reused), so the code means the same thing on both lines.

v2.x has no read-only open and no corrupt_file fuzzer, so neither is part of this backport.

Tests

  • tests/edge_cases_test.rs::test_sub_page_file_refused_unless_torn_first_write: a saved database cut to 2,222 and 20 bytes, and foreign bytes, each give STG-045, with the file unchanged and no WAL created. 4- and 10-byte header prefixes and 300 zeros open, take a write, and read it back after reopen. The test fails without the fix ("opened" instead of STG-045).
  • cargo test: 1,216 passed, 8 ignored. cargo clippy --all-features -- -D warnings is clean.

Docs: ERROR_REFERENCE.md (STG-045), CHANGELOG.md (Unreleased), TEST_COVERAGE.md, and the test counts in CLAUDE.md and README.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_012wXxZguv7PgKQjMMH4yJ5h

…506)

Backport of #508 to v2.x. A file of 1..4095 bytes made FileBackend::open
report a new database and write a fresh header over it. It now opens as new
only when its bytes are zeros or a prefix of the initial header page;
anything else is STG-045 (same number and meaning as on v3), file unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012wXxZguv7PgKQjMMH4yJ5h
@codecov
codecov Bot commented Oct 10, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@adityamukho
adityamukho merged commit 0da66eb into main Oct 10, 2026
21 checks passed
@adityamukho
adityamukho deleted the fix/506-short-file-v2 branch October 10, 2026 05:53
adityamukho added a commit that referenced this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0