3 ms·
There is no undefined behavior here? Unsigned overflow wraps around and this is correct. Always has been in C++ for as long as I can remember.
by banachtarski 10y ago
There is no undefined behavior here? Unsigned overflow wraps around and this is correct. Always has been in C++ for as long as I can remember.
- detaro 10y agoI don't think pointer arithmetic is defined to behave as unsigned. Unsigned is the exception in that it's overflow behavior is defined.
- kentonv 10y agoUnsigned overflow wraps around, but signed and pointer overflow are UB. In this case, the code was depending on pointer overflow wrapping around.
- banachtarski 10y agoedit: oh gosh I totally misread something > Since farPointer.offset is an unsigned number, the compiler is able to conclude that target < segmentStart always evaluates false. Thus, the compiler removes this part of the check. Unfortunately, in the case of overflow, this is exactly the part of the check that we need. This certainly seems like a compiler bug!