<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - warn when loop condition comparison uses different size operands"
   href="https://llvm.org/bugs/show_bug.cgi?id=31705">31705</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>warn when loop condition comparison uses different size operands
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>nivek.research@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>