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

            Bug ID: 128788
           Summary: -fanalyzer -fsanitize=undefined strcmp false positive
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: eggert at cs dot ucla.edu
  Target Milestone: ---

I found this bug when compiling Time Zone Database code with gcc (GCC) 16.2.1
20261029 (Red Hat 16.2.1-3) on x86-64. Compile the following stripped-down
program u.i:

  struct tm
  {
    const char *tm_zone;
  };

  int strcmp (const char *, const char *)
    __attribute__ ((__nothrow__ , __leaf__))
    __attribute__ ((__pure__))
    __attribute__ ((__nonnull__ (1, 2)));

  int
  format_utc_offset (struct tm const *tm)
  {
    return tm->tm_zone[0] == '-' || strcmp(tm->tm_zone, "zzz");
  }

with the command:

  gcc -fanalyzer -fsanitize=undefined -S u.i

This generates the following output, which is obviously a false positive. If I
omit "-fsanitize=undefined" the false positive is not generated. It seems that
-fanalyzer gets confused by extra code that -fsanitize=undefined is generating.

u.i: In function ‘format_utc_offset’:
u.i:14:37: warning: check of ‘*tm.tm_zone’ for NULL after already dereferencing
it [-Wanalyzer-deref-before-check]
   14 |     return tm->tm_zone[0] == '-' || strcmp(tm->tm_zone, "zzz");
      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
  ‘format_utc_offset’: events 1-2
   14 |     return tm->tm_zone[0] == '-' || strcmp(tm->tm_zone, "zzz");
      |            ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                       |          |
      |                       |          (2) following ‘false’ branch... ─>─┐
      |                       (1) pointer ‘*tm.tm_zone’ is dereferenced here
      |                                                                     │
  ‘format_utc_offset’: event 3
      |                                                                     │
      |┌────────────────────────────────────────────────────────────────────┘
   14 |│    return tm->tm_zone[0] == '-' || strcmp(tm->tm_zone, "zzz");
      |│                                           ~~^~~~~~~~~
      |│                                             |
      |└────────────────────────────────────────────>(3) ...to here
  ‘format_utc_offset’: event 4
   14 |     return tm->tm_zone[0] == '-' || strcmp(tm->tm_zone, "zzz");
      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                     |
      |                                     (4) ⚠️  pointer ‘*tm.tm_zone’ is
checked for NULL here but it was already dereferenced at (1)

Reply via email to