Repository navigation
Fix undefined behavior in RLE encode/decode size header handling - #399
Merged
Merged
Conversation
load() returns uint8_t, which promotes to int; 'load() << byte * 8' performs a shift of >= 32 bits on a 32-bit int when byte >= 4, which is undefined behavior. On the encode side, 'input.size() >> byte * 8' has the same problem, since input.size() is a 32-bit uint. At -O2/-O3 (the performance build profile), Apple Clang miscompiles nall::Decode::RLE based on this UB and emits a brk trap at runtime: saving states appears to work, but loading a state — or even just selecting one in the State Manager, which RLE-decodes the preview image — crashes bsnes. -O1/-Og builds happen to generate working code, which made this look like a macOS-specific regression. Cast to uint64_t before shifting so all eight size-header bytes are assembled and written correctly at any optimization level. Fixes bsnes-emu#396.
Contributor
|
Thank you very much for looking into this! I'll try to review and merge this soon. |
Contributor
|
Building latest version on Linux with g++ 16: seems to work fine, as expected Thank you for your contribution! |
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.
load()returnsuint8_t, which promotes toint;load() << byte * 8performs a shift of >= 32 bits on a 32-bitintwhenbyte >= 4, which is undefined behavior. On the encode side,input.size() >> byte * 8has the same problem, sinceinput.size()is a 32-bituint.At
-O2/-O3(theperformancebuild profile), Apple Clang miscompilesnall::Decode::RLEbased on this UB and emits abrktrap at runtime: saving states appears to work, but loading a state — or even just selecting one in the State Manager, which RLE-decodes the preview image — crashes bsnes.-O1/-Ogbuilds happen to generate working code, which made this look like a macOS-specific regression (see #396 #296).This was reproduced independently of the UI: decoding a valid
.bststate file with the exactupdateSelection()preview code crashes at-O2/-O3and works at-O1/-Os; casting touint64_tbefore shifting fixes all optimization levels.Cast to
uint64_tbefore shifting so all eight size-header bytes are assembled and written correctly at any optimization level.Fixes #396 #296