Fix segmentation faults on 32-bit ARM devices running GNU/Linux #15
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.
I tried to run SRB2Kart on my ODROID-XU4, which is a 32-bit ARMv7h device, and I would consistently get segmentation faults when Z_MallocAlign() was called with an alignbits value greater than or equal to 32. I found that the 1<<alignbits call would overflow and evaluate to 0. This would then have 1 subtracted from it, causing an underflow and setting extrabytes equal to 0xFFFFFFFF. This excessively high value for extrabytes causes weird memory corruption errors that eventually lead to a segmentation fault.
I have corrected this issue by not subtracting 1 when an overflow is guaranteed to have occurred.
Weirdly enough, this problem does not occur on 64-bit Intel, even though an alignbits value of 64 would still cause an overflow. extrabytes is still set to 0 even when alignbits is 64. This probably has to do with differences in the CPU overflow flag when bit shifting too much to the left.
Another interesting thing is that https://github.com/STJr/SRB2 apparently had some memory management updates (STJr/SRB2@cee5eb4) that removed the left bit shift operation entirely, but these updates have not made their way into SRB2Kart yet.