Re:Using signed or unsigned int by default in C (2015)
by Leherenn on Tuesday August 23, 2022 @03:04PM
(#32569686)
For what it's worth (though you likely know it), you can add "-fsanitize=unsigned-integer-overflow" to trigger a diagnostic even if unsigned integer overflow is not UB.
Of course, this only works if your code does not rely on unsigned integer overflow or if you suppress each case one by one.
I would tend to say this is a good practice to enable it, because the number of times where you rely on the overflow should be very low and well documented.
Re:Using signed or unsigned int by default in C (2015)
by FartyMcFarter on Thursday August 25, 2022 @08:04AM
(#32592558)
I had forgotten about that one - that's a great solution, and I agree that in most programs unsigned overflow is rarely desirable, so explicit annotation makes sense for those cases.
There's an interesting caveat to this one - if you rely on sanitizers like ubsan to catch overflow errors, using unsigned won't work. It will happily and silently overflow.