bsnes-emu / bsnes

bsnes is a Super Nintendo (SNES) emulator focused on performance, features, and ease of use.
Other
1.67k stars 154 forks source link

Fix critical undefined bit-shift length operation #177

Closed Screwtapello closed 3 years ago

Screwtapello commented 3 years ago

Natural/Integer.bit() (BitRange) was shifting by whatever type the source was to match the target bit length. But this breaks when the target type is u64/s64 and the source type is u32/s32 or smaller. Shifting by >=32 becomes undefined behavior. We have to cast the input source to the target type first, so that the source<<shift result is valid. This is safe here regardless of source's signedness, because it's only used in =, &=, ^=, |= operations.

Cherry-picked from https://github.com/higan-emu/higan/pull/174/files