Skip to content

Size makemaz() DFS stack for the full maze grid - #9

Merged
Critlist merged 3 commits into
mainfrom
fix/makemaz-stack-overflow
Oct 16, 2026
Merged

Critlist merged 3 commits into
mainfrom
fix/makemaz-stack-overflow

Conversation

@Critlist

Copy link
Copy Markdown
Owner

Bug: maze levels (the bottom of the dungeon, depth 25–29) crash on about 7% of seeds. Repro: ./mklev lvtest b 30 16 → *** stack smashing detected *** (ASan: stack-buffer-overflow at src/root/mklev.c:446, stack[++sp]). This happens with signed and unsigned char alike. It's unrelated to #8.

Cause: Kenny Woodland's makemaz() (the "this is all Kenny's fault" function) is a depth-first maze carver with an explicit stack, int stack[200]. Maze cells sit at odd coordinates: 8 rows (3..17, MAZY) × 37 columns (3..75, MAZX) = 296 cells. Each cell is pushed at most once (it's marked typ=2 as soon as it reaches the top), and stack[0] is unused, so the worst case needs 8·37+1 = 297 entries. Measured depth over 3000 seeds: min 90, median 159, max 232, and 207/3000 (6.9%) went past index 199.

Fix: int stack[8*37+1];, with the bound explained in a Modern: comment and the original declaration kept under #if 0. original/ is untouched, and the algorithm is unchanged.

Verification (Debug builds; mklev <file> <type> <depth> <seed>):

Test Before After
Maze b, depths 25–29 × seeds 1–2000, ASan+UBSan 715 / 10000 errors 0 / 10000
Same, plain build 590 aborts (+125 overflows that exited 0 with corrupted state) 0 / 10000
Same, -funsigned-char (simulated aarch64), depths 25/27/29 × 1000 174 / 3000 aborts 0 / 3000
Byte-identical level files for seeds that never overflowed (setarch -R, since saved structs contain heap pointers) — 9285 / 9285 identical
Normal a (depths 3, 15) and above-maze n (24, 28) × 300 seeds, ASan 599/600 OK 599/600 OK (same seed, see below)
Full game starts at 80×24 — ✓

Not fixed here (pre-existing, separate): seed 215 hangs or segfaults on a/n levels on the old and new builds alike (it never reaches makemaz()). Worth its own issue.

Brouwer's Hack 1.0.3 (restoHack) isn't affected. It replaced this explicit stack with a recursive walkfrom().

🤖 Generated with Claude Code

Critlist and others added 3 commits October 10, 2026 12:17
makemaz() builds the bottom-level maze with an explicit depth-first
stack, int stack[200]. Maze cells sit at odd coordinates, 8 rows x 37
columns = 296 cells; each is pushed at most once and stack[0] is unused,
so the deepest path needs 8*37+1 entries. Measured over 3000 seeds the
depth ranged 90-232, and 6.9% of maze levels ran past index 199,
smashing the stack (SIGABRT with the stack protector, silent corruption
otherwise).

Size the array from the grid bound; keep the original declaration
under #if 0. The algorithm is unchanged: with ASLR disabled, 9285/9285
non-overflowing seeds produce byte-identical level files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Critlist
Critlist merged commit dfa56b8 into main Oct 16, 2026
5 checks passed
neuhaus pushed a commit to neuhaus/protoHack that referenced this pull request Nov 15, 2026
Size makemaz() DFS stack for the full maze grid
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