After a recent compiler upgrade, I noticed GCC emitting array index out of bounds
warnings for code like the following:
#define my_assert(c) \
do { \
if (!(c)) fprintf(stderr, "Assertion failed: %s\n", #c); \
} while (0)
/* ... */
std::array<uint32_t, 32> registers{};
my_assert(i < registers.size());
return registers[i];
Presumably this is because during an inlining pass it unrolls the code in this fashion:
std::array<uint32_t, 32> registers{};
if (i < registers.size()) {
return registers[i];
} else {
// i >= registers.size()
fprintf(stderr, "Assertion failed: %s\n", "i < registers.size()");
return registers[i];
}
The second branch is supposed to be unreachable in the course of normal operation. The name of the assert macro is admittedly suboptimal here, because it does not abort if the condition is false, but GCC is being extremely unhelpful by warning on this case, particularly because the warning only happens with optimizations enabled. clang does not emit a warning at any optimization level.