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

            Bug ID: 108935
           Summary: Incorrect warning for infinite recursion
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: zach-gcc at cs dot stanford.edu
  Target Milestone: ---

The following code does not have infinite recursion:

```
typedef struct {
    unsigned idx;
    int vals[512];
} foo_t;

int ended(foo_t* f) {
    return f->idx >= 512;
}
unsigned foo(foo_t* f) {
    if (ended(f)) {
        return f->idx;
    }
    do {
        f->idx++;
    } while(!ended(f) && !f->vals[f->idx]);
    return foo(f);
}
```

but -fanalyzer reports infinite recursion (called with `gcc -fanalyzer -c
test.c`). The function always makes progress towards f->idx reaching 512, which
is the termination condition. It is bounded to at most 512 recursive calls. I
can even add `f->idx += 1000` and the warning is still reported.

I've also noticed the following example causes a similar warning, but only when
`foo` is also invoked.

```
typedef struct {
    unsigned done;
} foo_t;

unsigned foo(foo_t* f) {
    if (f->done) {
        return f->done;
    }
    f->done = 1;
    return foo(f);
}

int main() {
    foo_t f = (foo_t){
        .done = 0,
    };
    // must be called to cause warning
    foo(&f);
}
```

Tested on version GCC 13 20230203 (commit hash a37a0cb303d). Thanks.

Reply via email to