Say my size_type
is uint64_t
, and I have the following loop (where sz
is of size_type
too)
for ( size_type i= 0; i < sz; ++i ) {
//something
if ( i+1 == sz ) { //<-- here
///
}
}
Now when compiling this with flags -fno-omit-frame-pointer -fsanitize=undefined -O2 -fsanitize=address
, I get a runtime error which says that in the place I marked here
in the code snippet above, 2147483647 + 1 cannot fit into integer
, and it is true that sz
is something tad larger than 2^31-1
. However, everything should be OK, beause uint64_t
can hold the value, and by conversion rules i+1
should be promoted to uint64_t
. What am I missing?
EDIT: Isn't it so that uint64_t
always has 64
bits? Then, 2147...
-value is just 32
bits, and we should still be OK. I'm now running my thing without sanitizers, and no crash has occurred so far.
EDIT:
clang version 8.0.0-3~ubuntu18.04.1 (tags/RELEASE_800/final) Target: x86_64-pc-linux-gnu Thread model: posix
and also I have linker flags as follows
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=address")
maybe that is interfering as well? I know the compiler explorer cannot reproduce the error, which is strange and calls for further investigation on my part.