Skip to content

Fix pre-commit Formatting#711

Closed
Sebastian-Griesbach wants to merge 6 commits into
Farama-Foundation:mainfrom
Sebastian-Griesbach:mr_bugfix
Closed

Fix pre-commit Formatting#711
Sebastian-Griesbach wants to merge 6 commits into
Farama-Foundation:mainfrom
Sebastian-Griesbach:mr_bugfix

Conversation

@Sebastian-Griesbach

Copy link
Copy Markdown

This PR is an edit of a preexisting PR: #668
Please cherry pick commit 373a575

The relevant commit only fixes formatting but does not change the code.

@jkterry1

Copy link
Copy Markdown
Member

@Sebastian-Griesbach can you please also rebase off main?

josephdviviano and others added 6 commits July 13, 2026 14:51
…dation#11)

The player position registers (myPOSP0, myPOSP1) are int16_t but used
as indices into ourPlayerPositionResetWhenTable[8][160][160]. If a
position escapes the valid [0, 159] range, the table access is
out-of-bounds, causing a segfault.

Three changes:

1. Clamp positions before the table lookups in RESP0/RESP1 (cases 0x10,
   0x11 in TIA::poke). This is the direct crash site from issue Farama-Foundation#11.

2. Replace the single-step HMOVE wrapping (if >= 160 then -= 160) with
   proper modular arithmetic that handles any value. The original
   correction only works for deltas in [-15, +8]; modulo is safe for
   all inputs.

3. Validate positions after state restore (TIA::load). The deserialized
   values were cast to int16_t with no bounds check, so a corrupted or
   stale save state could inject invalid positions.

All five position registers (P0, P1, M0, M1, BL) are fixed in each
location.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Injects out-of-bounds values into serialized TIA position registers
and verifies the emulator survives after restoring the corrupted state.
Without the bounds-validation fix in TIA::load, this would segfault.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Revert the HMOVE position wrapping from modulo arithmetic back to the
original if/else single-step correction. Motion table values are bounded
to [-15, +8], so positions in [0, 159] land in [-15, 167] after motion
— always within one correction of the valid range. The RESP and load()
clamps guarantee the [0, 159] precondition, making modulo unnecessary
on this hot path.

Expand test_restore_state_with_corrupted_positions with 11 comprehensive
edge cases (boundary values, int16_t extremes, exact multiples) and add
test_hmove_single_step_wrapping_bounds to exhaustively verify the
single-step wrapping contract and document its limits.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Sebastian-Griesbach

Copy link
Copy Markdown
Author

Done

@jkterry1

Copy link
Copy Markdown
Member

Closing in favor of the base PR, I really apologize for the confusion

@jkterry1 jkterry1 closed this Jul 13, 2026
@jkterry1

Copy link
Copy Markdown
Member

Thanks so much for the help with it though :)

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.

3 participants