Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> unsigned integer overflow is what does the wrong thing in C

You may want to rethink this statement



No, I am correct. Unsigned integer overflow has wrapping overflow semantics. Absolutely idiotic.


So... unsigned overflow is defined, while signed is straight up UB... and for you it's the former that's wrong?


Yes. Most calculations won't do the right thing with wrapping overflow and you'd be better of by them being undefined and tested via the relevant tooling (e.g. ubsan). Those that do should be written in a way to make that explicit.


Compilers can make commutative optimizations with signed integer overflow that is impossible with wrapping semantics. This is why Zig and Carbon both make unsigned integer overflow undefined as well. There is absolutely no advantage in most cases to wrapping semantics unless it is actually required for the algorithm (like some hashing or PRNG functions, for example).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: