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

            Bug ID: 128988
           Summary: -Wanalyzer-deref-before-check with UBSAN reports false
                    positive
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: b.buschinski at googlemail dot com
  Target Milestone: ---

With gcc 17 (trunk).

Compiler explorer: https://godbolt.org/z/4EMcr9oqj

For the code:
```
struct foo {
    int a;
};

__attribute__((nonnull)) int test_f(struct foo * f) {
    if (f->a == 1) {
        return 1;
    }
    return 0;
}

int test(struct foo * f)
{
    if (f->a >= 0) {
        return test_f(f);
    }
    return 7;
}

```

when compiled with -fanalyzer AND -fsanitize=undefined

it reports an error:

```
<source>: In function 'test':
<source>:15:16: error: check of 'f' for NULL after already dereferencing it
[-Werror=analyzer-deref-before-check]
   15 |         return test_f(f);
      |                ^~~~~~~~~
  'test': event 1
   14 |     if (f->a >= 0) {
      |         ~^~~
      |          |
      |          (1) pointer 'f' is dereferenced here
  'test': events 2-4
   14 |     if (f->a >= 0) {
      |        ^
      |        |
      |        (2) following 'true' branch... ─>─┐
      |                                          │
      |                                          │
      |┌─────────────────────────────────────────┘
   15 |│        return test_f(f);
      |│               ~~~~~~~~~
      |│               |
      |└──────────────>(3) ...to here
      |                (4) ⚠️  pointer 'f' is checked for NULL here but it was
already dereferenced at (1)
cc1: all warnings being treated as errors
Compiler returned: 1
```

The error(or warning) does not show up when compiled without UBSAN.

As there is no explicit null check and only the implicit
__attribute__((nonnull)), I would say this is a false-positive.

Reply via email to