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

            Bug ID: 128831
           Summary: -fanalyzer false positive with
                    -ftrivial-auto-var-init=pattern
           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 ran into this problem when compiling TZDB with gcc (GCC) 16.2.1 20261029 (Red
Hat 16.2.1-3) on x86-64. Compile the following stripped-down program u.i with
"gcc -fsanitize=address -ftrivial-auto-var-init=pattern -fanalyzer -S u.i":

  struct zone {                                                                 
    long z_stdoff, xxx;                                                         
    char z_format_specifier;                                                    
    bool z_isdst;                                                               
    long z_save, yyy;                                                           
  };                                                                            

  void                                                                          
  stringzone(void)                                                              
  {                                                                             
    struct zone zstr[2];                                                        
    struct zone *dstzp = &zstr[1];                                              
  }                                                                             

The output is as follows, and is obviously bogus as there is no buffer
overflow:

u.i: In function ‘stringzone’:
u.i:11:17: warning: stack-based buffer overflow [CWE-121]
[-Wanalyzer-out-of-bounds]
   11 |     struct zone zstr[2];
      |                 ^~~~
  ‘stringzone’: events 1-2
   11 |     struct zone zstr[2];
      |                 ^~~~
      |                 |
      |                 (1) capacity: 80 bytes
      |                 (2) following ‘true’ branch... ─>─┐
      |                                                   │
  ‘stringzone’: events 3-4
      |                                                   │
      |┌──────────────────────────────────────────────────┘
   11 |│    struct zone zstr[2];
      |│                ^~~~
      |│                |
      |└───────────────>(3) ...to here
      |                 (4) ⚠️  out-of-bounds write from byte 98 till byte 103
but ‘zstr’ ends at byte 80
u.i:11:17: note: write of 6 bytes to beyond the end of ‘zstr’
u.i:11:17: note: valid subscripts for ‘zstr’ are ‘[0]’ to ‘[1]’

                                            ┌────────────────────────────┐
                                            │write of ‘char[6]’ (6 bytes)│
                                            └────────────────────────────┘
                                                          │
                                                          │
                                                          v
  ┌───────────────┬───────────────┐         ┌────────────────────────────┐
  │      [0]      │      [1]      │         │                            │
  ├───────────────┴───────────────┤         │     after valid range      │
  │‘zstr’ (type: ‘struct zone[2]’)│         │                            │
  └───────────────────────────────┘         └────────────────────────────┘
  ├───────────────┬───────────────┤├───┬───┤├─────────────┬──────────────┤
                  │                    │                  │
        ╭─────────┴────────╮      ╭────┴───╮  ╭───────────┴──────────╮
        │capacity: 80 bytes│      │18 bytes│  │⚠️  overflow of 6 bytes│
        ╰──────────────────╯      ╰────────╯  ╰──────────────────────╯

Reply via email to