Skip to content

opus_tags: validate Ogg checksums before replacing tags - #6561

Merged
shayderrr merged 1 commit into
ItsNotRexed:mainfrom
frnandoooo:fix/opus-retag-crc
Nov 23, 2026
Merged

shayderrr merged 1 commit into
ItsNotRexed:mainfrom
frnandoooo:fix/opus-retag-crc

Conversation

@frnandoooo

Copy link
Copy Markdown
Contributor

opus_tags accepted Ogg pages with corrupt checksums and could replace their tags, making the damaged page look newly valid. Validate Ogg page CRCs before publishing a replacement, including the copied audio tail, and reject truncated tails. Corrupt streams are counted as skipped and left byte-identical in both normal and dry-run mode; other named streams can still be tagged.

Uses the existing awc_opus Ogg CRC implementation. Validation adds one linear checksum pass over the file, with scratch memory bounded by one Ogg page. No codec or audio re-encoding changes.

Validation:

Claim: #6414 (comment) (new item; ID pending). No PR dependencies.

Windows, Python 3.14.10; CPU only, no GPU/driver required and no game files. AI-assisted: Codex implementation and testing; Claude static patch review. Review follow-ups were checked locally (existing Opus error-key normalization and preserved Unicode test text). Claude did not execute tests.

Common checks: compileall (make_game.py tools tests), python -I make_game.py --help, and git diff --check passed. The repository-wide text-newline check has one existing failure at tools/f64_omod_probe.py:268; reproduced on clean upstream 696cce455 as well as this branch. That file is untouched.

@autowert66

Copy link
Copy Markdown

Checked on macOS 27.2.1 / Apple M1 Max, Python 3.15.2, CPU only: replicated, with one better number than the PR's own run.

  • tests/test_opus_tags.py: 23/23 OK here — the single failure in the PR's Windows run (test_cli_reports_incomplete_directory_discovery, a Windows path-format case) does not reproduce on macOS, so the full suite is green on this platform.
  • Negative control replicated exactly: with main's unguarded tools/opus_tags.py, the new regressions fail the same 9 subcases the PR counts — test_retag_rejects_bad_crc_on_header_tags_and_audio_pages (pages 0/1/2, corrupt positions 22/46/69/98/121/251) and test_corrupt_stream_is_skipped_unchanged_in_normal_and_dry_run (dry_run true/false) — and all 23 pass with the change. The guard is load-bearing for precisely the cases it names.
  • The Ogg CRC reuse from awc_opus keeps the change to validation only; nothing here touches codec paths.

AI-assisted (OpenCode / DeepSeek V4.1 Flash + GLM-5.3): the agent ran the battery and the negative control sandboxed.

@cloph-dsp

Copy link
Copy Markdown
Contributor

Independent Windows check of head 2bb4302 with Python 3.14.6: python -I tests/test_opus_tags.py -q ran 23 tests, 22 OK, 1 failure. The failure is the pre-existing test_cli_reports_incomplete_directory_discovery path case: the assertion expects a plain Windows path while the tool prints it repr-doubled (already covered by #6049, #5927 and #5760). All new CRC/truncation tests and the corrupt-stream skip tests pass. git diff --check on the two files in this PR is clean.

CPU only, synthetic fixtures, no game data. AI-assisted (Hy4 flash).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 2bb4302 merged with main 042c5f3 on Linux/Python 3.12.16: pass

Merge was clean (tools/opus_tags.py, tests/test_opus_tags.py).

  • python -I tests/test_opus_tags.py: Ran 23 tests, OK.
  • Negative control (tools/opus_tags.py restored from upstream/main, tests from the PR): Ran 23 tests, FAILED (failures=9): the six per-page cases of test_retag_rejects_bad_crc_on_header_tags_and_audio_pages, test_retag_rejects_truncated_audio_tail, and both dry-run and normal runs of test_corrupt_stream_is_skipped_unchanged_in_normal_and_dry_run. PR version restored afterwards.

Diff read: retag now iterates through checked_pages, which recomputes each page's CRC with the checksum field zeroed (one page copied at a time), and also walks the audio pages after the tags page before returning anything, so a damaged or truncated stream raises ValueError and the folder run counts it as "skipped: Ogg checksum mismatch" with the file untouched. The tests build their Ogg streams in a temporary folder (synthetic, no game audio).

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

tools/opus_tags.py adds checked_pages(), which verifies every Ogg page's checksum with ogg_crc before its bytes are copied, and retag() now exhausts the audio tail before publishing a replacement. Corrupt or truncated files raise ValueError and are skipped unchanged by tag_folder, including dry runs. tests/test_opus_tags.py covers flipped checksum bytes on header/tags/audio pages, a truncated tail, and the skipped-file stats.

Why merge

Stops a bad CRC from being silently rewritten into a new file; validation happens before any write and tests cover both detection paths.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

tools/opus_tags.py adds checked_pages(), which verifies every Ogg page's checksum with ogg_crc before its bytes are copied, and retag() now exhausts the audio tail before publishing a replacement. Corrupt or truncated files raise ValueError and are skipped unchanged by tag_folder, including dry runs. tests/test_opus_tags.py covers flipped checksum bytes on header/tags/audio pages, a truncated tail, and the skipped-file stats.

Why merge

Stops a bad CRC from being silently rewritten into a new file; validation happens before any write and tests cover both detection paths.

Verdict: Approve

@shayderrr
shayderrr merged commit a90a79b into ItsNotRexed:main Nov 23, 2026
0 of 2 checks passed
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.

5 participants