New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 44618 - [clang-tidy] bugprone-infinite-loop: detects clearly finite loops as infinite
Summary: [clang-tidy] bugprone-infinite-loop: detects clearly finite loops as infinite
Status: RESOLVED FIXED
Alias: None
Product: clang-tools-extra
Classification: Unclassified
Component: clang-tidy (show other bugs)
Version: unspecified
Hardware: PC Linux
: P normal
Assignee: Ádám Balogh
URL:
Keywords:
Depends on:
Blocks: release-10.0.0
  Show dependency tree
 
Reported: 2020-01-22 01:49 PST by Dennis Felsing
Modified: 2020-01-29 13:02 PST (History)
6 users (show)

See Also:
Fixed By Commit(s): rG70f4c6e7b14f225f9628fbdab3620ce037613351


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dennis Felsing 2020-01-22 01:49:17 PST
Hi,

The new bugprone-infinite-loop check is not detecting some easy patterns of finite loops, thus leading to false positives. Not sure if this can easily be fixed:

$ clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 10.0.175git
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: broadwell
$ cat .clang-tidy
Checks: bugprone-infinite-loop
$ cat x.cpp
#include <iostream>

int main(int /*argc*/, char** /*argv*/)
{
    char foobar[] = "foobar";
    char *pattern = foobar;
    while (char c = *pattern) {
        if (c == 'o') {
            std::cout << "o" << std::endl;
        }

        ++pattern;
    }

    return 0;
}
$ clang-tidy x.cpp
/home/d067158/x.cpp:7:5: warning: this loop is infinite; none of its condition variables (c) are updated in the loop body [clang-tidy-bugprone-infinite-loop]
    while (char c = *pattern) {
    ^
$ ./x
o
o

Best regards
Dennis
Comment 1 Ádám Balogh 2020-01-23 03:13:27 PST
Reduced test case:

void false_positive() {
  int n = 10;
  while (int m = n) {
    --n;
  }
}

However, no false positive for this:

void no_false_positive() {
  int n = 10, m;
  while (m = n) {
    --n;
  }
}
Comment 2 Ádám Balogh 2020-01-23 06:11:04 PST
Fix uploaded for review: https://reviews.llvm.org/D73270
Comment 3 Hans Wennborg 2020-01-29 13:02:05 PST
(In reply to Ádám Balogh from comment #2)
> Fix uploaded for review: https://reviews.llvm.org/D73270

That was committed, and later cherry-picked to 10.x in d275de35f8bdb92e7b5789ebdb96df99fab504ab