https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80872
Bug ID: 80872
Summary: There is no warning on accidental infinite loops
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: david at westcontrol dot com
Target Milestone: ---
Would it be possible to add warnings on accidental infinite loops, such as:
void foo(void) {
for (int i = 0; i <= 0x7fffffff; i++) {
// ...
}
}
The compiler (correctly) translates this to an infinite loop, in all versions
of gcc that I tested, and in both C and C++, with optimisation enabled. But no
warning is given, even with -Wall -Wextra. That includes the -Wtype-limits
warning, which I thought should trigger here. Perhaps the order of passes is
such that the code is simplified to an infinite loop before the type limits
checking is done?
Replacing " <= 0x7fffffff" with "< 0x80000000" gives triggers -Wsign-compare
but not -Wtype-limits, which is relevant because in C -Wsign-compare is in
-Wextra but not -Wall. Exceeding the limits of unsigned int in the literal
here correctly triggers -Wtype-limits.