Skip to content

fix(tools): bound sgd5_effects database reads and guard program_identity truncation - #5743

Open
Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:fix/sgd5-effects-bounds-and-identity
Open

Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:fix/sgd5-effects-bounds-and-identity

Conversation

@Persudes

Copy link
Copy Markdown
Contributor

Hardens database boundary enforcement and truncated-stream handling in tools/sgd5_effects.py:

Bugs fixed

  1. Unbounded group/header/effect-count reads crossing limit (program_groups, read_database)

    • program_groups(stream, limit) read the 6 group counts, program label lengths, label bytes, 1-byte tags, 4-byte size fields, and 4-byte trailers without checking stream.tell() + n <= limit. When another database followed in the same stream (shaders.rpf holds two SGD5 databases back-to-back), a truncated/corrupt DB1 could read across limit into DB2 instead of raising ValueError.
    • program_groups also iterated up to count without checking whether count * MIN_PROGRAM_BYTES could fit in limit - stream.tell().
    • read_database did not validate layout (database >= 0, first_group >= database + 4, first_group < end), and read the 4-byte effect count before checking stream.tell() + 4 < end.
    • Fix: validate layout, add read_bounded(stream, size, limit, message) and MIN_PROGRAM_BYTES (176), bound every count/header/trailer read in program_groups against limit, and check stream.tell() + 4 >= end before reading effect count in read_database.
  2. IndexError crashes in program_identity() on empty/truncated streams or bad offsets

    • stream.read(1)[0] and window[pos - 1] raised uncaught IndexError on empty streams, truncated labels/windows, or offsets with length < 2, instead of ValueError (which main() handles cleanly).
    • Fix: reject start < 0, not raw_length or raw_length[0] < 2, and len(window) < window_size with explicit ValueError.

Branch base & merge check

  • Branched from d0835dd9 (last commit before .github/workflows/ edits, since this OAuth token lacks workflow scope).
  • git merge-tree --write-tree upstream/main HEAD (upstream/main at 561b65a8): exit 0 (clean merge).

Environment & Verification

  • Environment: Windows 11 x64, Python 3.15.2 (D:\DevTools\python\python.exe). Synthetic databases only; no copyrighted game files or GPU used.
  • Red-first (synthetic unit tests before fixing tools/sgd5_effects.py):
    • python -I -B tests/test_sgd5_effects.py -v -> FAILED (failures=1, errors=2, skipped=1) (AssertionError: ValueError not raised on program_groups(..., 20) crossing limit, plus IndexError: index out of range on empty/truncated program_identity streams).
  • After fix:
    • python -I -B tests/test_sgd5_effects.py -v -> Ran 13 tests in 0.011s — OK (skipped=1) (test_xbox_package_counts skipped without LEONIDA_GAME_DIR).
    • python -I -B tests/test_ci_registration.py -> Ran 8 tests — OK.
    • python -I -B tests/test_text_write_line_endings.py -> Ran 3 tests — OK.
    • python -I -B scripts/check_no_game_data.py -> exit 0.
    • git diff --check, LF / no-BOM, and git merge-tree --write-tree upstream/main HEAD -> clean (0).

Related coordination board claim: #5581 (comment)

AI-assisted (Gemini, Antigravity); no copyrighted game files or dumps used.

@Kev-Ball

Copy link
Copy Markdown

Ran the package-gated test at 0a669af5 on CachyOS with my Xbox 1.0.2.1 copy. All 13 tests passed, including the retail counts for both databases.

I also compared the changed functions directly against the PR parent. In each database, program_groups returned the same offsets in the same order and left the stream at the same position. Calling program_identity for every program returned identical dictionaries for all 24,804 entries in the first database and all 1,699 in the second. The complete read_database(..., list_effects=True) results also matched. The stricter bounds accepted every retail program in this build.

CPU-only. AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 0a669af5 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_sgd5_effects.py -> OK (skipped=1)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@editrice

Copy link
Copy Markdown

Independent synthetic boundary review at exact head 0a669af5891c3b095cb6ca5051ca45c6d9ea0266 on Windows 11 / CPython 3.13.7: pass. I placed two hand-built SGD5 databases back-to-back and instrumented the stream to record every read or forward seek beyond the first database's declared limit. Cutting the program-group boundary at every byte produced 233 ValueErrors and one exact-boundary success. Testing every whole-database end produced 553 rejected ends and one complete success, with zero crossings into the adjacent database. The complete first database still decoded its one synthetic program/effect. program_identity accepted valid first/last records and rejected negative, EOF, past-EOF, and 27 truncated-window cases.

On the exact parent 561b65a8, the first cut already read or sought into the adjacent database (8→12, 12→13, 13→18, 18→19, 19→23), so the negative control distinguishes this change. The focused stock tests/test_sgd5_effects.py suite also passed (13 run, one game-gated skip). No game files or GPU used. AI-assisted.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Head 0a669af58; merge-base d0835dd9 (2,729 commits behind 561b65a). Cherry-pick onto 561b65a is clean.

Commands: python -I -m pytest tests/test_sgd5_effects.py -q -> 12 passed, 1 skipped, 47 subtests on the head and on the rebased tree; git diff --check clean.

Read the bounds: every group count, label, tag, size and trailer read is now bounded by limit; count * 176 is rejected before looping; the layout is validated; program_identity turns short/negative windows into ValueError. No regression for valid databases seen in the tests.

Verdict: approve after rebase. Overlaps #5717 (same tool, different functions; merge-tree clean) and #5788 (magic scan); no duplicate.

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 0a669af5 on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main dde82f27: pass

  • python -I tests/test_sgd5_effects.py: Ran 13 tests, OK (skipped=1). With only the non-test files reverted to main: FAILED (failures=1, errors=2, skipped=1), so the new tests exercise the change.
  • compileall and scripts/check_no_game_data.py on the changed files: clean.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting; claimed in #8047.

@sds-228 sds-228 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Focused suite passes at this head: 12 passed, 1 skipped, 47 subtests. The pre-change source fails the new boundary checks. Group, label and effect-count reads are bounded by the database limit, and short program windows are now usage errors.

AI-assisted (OpenCode).

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.

7 participants