Skip to content

apps/maze: fix possible stack overflow#2

Merged
ringtailsoftware merged 1 commit intoringtailsoftware:mainfrom
1player:main
Dec 14, 2025
Merged

apps/maze: fix possible stack overflow#2
ringtailsoftware merged 1 commit intoringtailsoftware:mainfrom
1player:main

Conversation

@1player
Copy link
Copy Markdown
Contributor

@1player 1player commented Dec 14, 2025

I used your maze example to test my very own RISC-V emulator, which for some reason kept crashing when being compiled with anything greater than -O0, and I've spent 3 hours trying to figure out where the bug is — and it is in this code snippet! :)

The original code this app is inspired from, mentioned at the top of the file, uses rand(), which returns a number >= 0.
mulberry32() on the other hand can return a negative number, in which case will overflow the accesses in the dirs array. Apparently this wasn't reproducible unless optimizations were turned on.

I know it's such a small part of your project, but perhaps this'll save some headache to anyone using your examples to test their RISC-V emulators!

The original code this app is inspired from uses rand(), which returns
a number >= 0.
mulberry32() on the other hand can return a negative number, in which
case will overflow the accesses in the dirs array. Fix that by forcing
the result to be unsigned.
@ringtailsoftware ringtailsoftware merged commit b7269c2 into ringtailsoftware:main Dec 14, 2025
1 check failed
@ringtailsoftware
Copy link
Copy Markdown
Owner

Thank you! I got really confused by this and had assumed it was something weird about the optimiser.
Mystery solved.

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.

2 participants