Skip to content

Pass -fwrapv to GCC compilation flags

Hanicef requested to merge Hanicef/SRB2:use-fwrapv-gcc-flag into next

in case you weren't aware, integer overflows and underflows are undefined behavior in C, and that of course means that anything goes with them. that said, though, GCC has been (ab)using this for optimization reasons, which is something that has lead to countless security holes and in the past, and also the well-known (or, at least if you work with this stuff a lot) article "GCC undefined behaviors are getting wild".

the problem, however, is that we rely on integer overflows and underflows a ton. in fact, angle_t actively exploits integer overflows to be able to perform rotations beyond 360 degrees. this puts us in a dangerous spot since it's very possible that GCC can start "optimizing" away important checks which can cause runtime bugs that only happens in release builds -- hell, this might've already happened without any of us being aware of it!

fortunately, there is a switch that fixes this: -fwrapv. it tells GCC to assume that numbers are in two's complement, and optimizes accordingly. this disables some performance optimizations and enables others, but at it's core, it fixes the integer overflow problems and stops any optimization bugs of this category from appearing.

Merge request reports

Loading