[llvm-bugs] [Bug 31705] New: warn when loop condition comparison uses different size operands
via llvm-bugs
llvm-bugs at lists.llvm.org
Fri Jan 20 07:29:37 PST 2017
https://llvm.org/bugs/show_bug.cgi?id=31705
Bug ID: 31705
Summary: warn when loop condition comparison uses different
size operands
Product: clang
Version: unspecified
Hardware: Macintosh
OS: MacOS X
Status: NEW
Severity: enhancement
Priority: P
Component: Static Analyzer
Assignee: kremenek at apple.com
Reporter: nivek.research at gmail.com
CC: llvm-bugs at lists.llvm.org
Classification: Unclassified
I recently encountered a infinite loop where the variable in a for loop
overflowed before reaching the termination value. For example, the 16-bit
unsigned int below is compared to a 64-bit unsigned long (on macOS) and since
the 16-bit value will overflow after 65535 it never reaches 65536 and the loop
will never terminates. I think it would be a great check for either the
compiler or more probably the static analyzer to warn that loop may not
terminate because of comparison may always be false due to overflow. I believe
the check would be good for for, while and do until loops.
#include <cstddef>
#include <cstdint>
#include <cstdio>
int main() {
size_t limit = 65536;
for (uint16_t index = 0; index < limit; index++) {
}
printf("completedi\n");
}
I believe similar warnings could be reported for
uint16_t index = 0;
while (index < limit) {
index++;
}
and
do {
index++;
} while (index < limit);
The compiler does produce a warning if limit is replaced by a constant value:
main.cpp:11:15: warning: comparison of constant 65536 with expression of type
'uint16_t' (aka 'unsigned short') is always true
[-Wtautological-constant-out-of-range-compare]
while (index < 65536) {
~~~~~ ^ ~~~~~
So perhaps this can just be enhanced.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170120/ad88b7a3/attachment.html>
More information about the llvm-bugs
mailing list