3 ms·
Yes (x - y < x) can't detect overflow because it overflows too, but what's wrong with (UINT_MAX - x < y)?
by inlined 10y ago
Yes (x - y < x) can't detect overflow because it overflows too, but what's wrong with (UINT_MAX - x < y)?
- hghdfgv 10y agoWhat do you mean? (UINT_MAX - x < y) is wrong, if x and y are both of type int, it should be: (INT_MAX - x < y), which is defined and will yield true, if an overflow on x+y would happen.
- Kristine1975 10y agoIt seems to me that you're mixing (signed) int (x and y) and unsigned int (UINT_MAX). As for securely implementing overflow checks for signed addition, this seems to be useful: http://blog.regehr.org/archives/1139 http://blog.regehr.org/archives/1139