Fix pline recursion loop when descending/ascending stairs - #6
Merged
Conversation
pru()'s "track last displayed @" tracking (added to keep redraws in sync on modern terminals) has no notion of which level it belongs to. After dodown()/doup() swap in a new level's map data, the tracked position still refers to the previous level, so pru() calls newsym() on foreign coordinates that are frequently unconnected rock (typ==0). That hits newsym()'s default case and its original 1982 "Bad newsym" diagnostic, which calls pline(), which can re-enter pru() — and since the tracking state was only updated *after* the newsym() call, the re-entrant call sees the same stale state and repeats the same newsym() call forever, overflowing the stack. Fixes this with two changes: dodown()/doup() now invalidate the tracking via a new clearpru() so stale cross-level coordinates are never passed to newsym() in the first place, and pru() now updates its tracking state before calling newsym() instead of after, so even a re-entrant call can no longer repeat the same call. Verified with a standalone harness linking the old and fixed hack.pri.c against the same repro scenario: the old code reliably SIGSEGVs after ~7000 recursive frames, the fixed code returns cleanly. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pru()→newsym()→pline()→pru()recursion that could crash the game with a stack overflow when taking stairs.pru()'s "track last displayed @" tracking (a modern addition for redraw sync on modern terminals) has no notion of level identity, so afterdodown()/doup()it holds the previous level's last position.pru()then callsnewsym()on that foreign coordinate, which is frequently unconnected rock (typ==0) — hittingnewsym()'s original-1982pline("Bad newsym...")diagnostic.pline()can re-enterpru(), and since the tracking state was only updated after thenewsym()call, the re-entrant call sees the same stale state and repeats forever.docs/CODING_STANDARDS.md:dodown()/doup()now call a newclearpru()right after moving the player onto the new level, so stale cross-level coordinates are never handed tonewsym().pru()now updates its tracking state before callingnewsym()instead of after, so even a re-entrant call can no longer repeat the same call — makes the recursion structurally impossible regardless of cause.Test plan
cmake --build build) succeeds cleanly.hack.pri.c, reproducing the exact reported call sequence (pruat old position → level-data corrupted the way an unconnected new-level tile would be →pruat new position withflags.dscrset):🤖 Generated with Claude Code