4 ms·
Signed overflow is ub in C
by zokier 9y ago
Signed overflow is ub in C
- deleted 9y ago[deleted]
- dpark 9y agoI'm aware of that. The question was whether any practical architectures will do the "wrong" thing here.
- moefh 9y agoCompilers will do the "wrong" thing. For example, gcc optimizes out the "x >= 0" check in for (int x = 0; x >= 0; x++) making it an infinite loop, because it assumes "x++" can't overflow.
- dom0 9y agoSigned overflow is IB, not UB.
- mikeash 9y agoAlas, no. Signed overflow is UB and compilers can and will assume that it cannot happen when performing optimizations.
- astrange 9y agoWhich is a good thing, assuming you did reasonable testing with ubsan etc. Having to assume things that never happen is a big problem for optimization!
- mikeash 9y agoThat's a good point. Seems like we could do a bit better than the current state of the art, though. If non-optimized builds trapped on overflow, that would at least give you a better chance of detecting these problems before the optimizer starts throwing out code you meant to keep.