https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118013

            Bug ID: 118013
           Summary: bogus "infinite loop" warning
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: bruno at clisp dot org
  Target Milestone: ---

Created attachment 59844
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59844&action=edit
test case i.c

Compiling the attached file i.c with gcc 14.2.0 (without or with optimization)
produces a "warning: infinite loop". But there is no infinite loop, because:
- If at line 93, j < nsubst, line 90 has incremented i.
- If at line 93, j == nsubst, i gets incremented.

$ gcc-version -Wall -fanalyzer -S i.c
i.c: In function ‘subst_string’:
i.c:72:18: warning: infinite loop [CWE-835] [-Wanalyzer-infinite-loop]
   72 |           if (str[i] == '\0')
      |                  ^
  ‘subst_string_list’: event 1
    |
    |  101 | subst_string_list (string_list_ty *slp,
    |      | ^~~~~~~~~~~~~~~~~
    |      | |
    |      | (1) entry to ‘subst_string_list’
    |
    +--> ‘subst_string’: events 2-11
           |
           |   52 | subst_string (const char *str,
           |      | ^~~~~~~~~~~~
           |      | |
           |      | (2) entry to ‘subst_string’
           |......
           |   72 |           if (str[i] == '\0')
           |      |              ~   ~
           |      |              |   |
           |      |              |   (3) infinite loop here
           |      |              |   (11) ...to here
           |      |              (4) if it ever follows ‘false’ branch, it will
always do so...
           |      |              (9) ...to here
           |      |              (10) looping back...
           |   73 |             break;
           |   74 |           for (j = 0; j < nsubst; j++)
           |      |                ~~~~~  ~~~~~~~~~~
           |      |                  |      |
           |      |                  |      (6) when ‘j >= nsubst’: always
following ‘false’ branch...
           |      |                  (5) ...to here
           |......
           |   93 |           if (j == nsubst)
           |      |              ~
           |      |              |
           |      |              (7) ...to here
           |      |              (8) when ‘j != nsubst’: always following
‘false’ branch...
           |

Reply via email to