2 ms·
Tools like UBsan [1] can detect integer overflow in debug builds, and are used internally at Google to run automated tests. So if you use a signed integer, the
by sltkr 11mo ago
Tools like UBsan [1] can detect integer overflow in debug builds, and are used internally at Google to run automated tests.
So if you use a signed integer, there is a chance that overflows are caught in tests.
1. https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
- im3w1l 11mo agoI feel like there should be a "sign-queer" integer. It's range is the intersection of unsigned and signed integers, in other words the high bit must be unset. Wrapping around on either end is illegal and should be diagnosed in debug builds. In production builds it may either be silently treated as an unsigned integer or generate a diagnostic. Implicitly casting to either signed or unsigned integer is allowed and does not generate a warning.
- sltkr 11mo agoThat's the behavior you get in Zig, if you declare a variable of type `u31`: an unsigned 31-bit integer that's implicitly convertible to `u32` or `i32`. Also in Zig overflow is illegal for both signed and unsigned integers, and guaranteed to be detected when building in Safe mode (but not in Fast mode). There is a separate set of operators for wrapping arithmetic.