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

            Bug ID: 113922
           Summary: -Wstringop-overflow with FORTIFY_SOURCE=3 and O{1,2,3}
                    generates a false positive for 0-sized structs
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sergiodj at sergiodj dot net
  Target Milestone: ---

Hi,

Consider the following example program:

#include <stdio.h>
#include <unistd.h>

int main(void) {
    struct test_st {};
    int fd = 0;
    int count = 0;

    struct test_st test_info[16];

    count = read(fd, test_info, sizeof(test_info));
    return(0);
}

When compiling it with GCC 13.2 using -D_FORTIFY_SOURCE=3 and -O1, I see:

a.c: In function ‘main’:
a.c:15:13: warning: ‘read’ writing 1 byte into a region of size 0 overflows the
destination [-Wstringop-overflow=]
   15 |     count = read(fd, test_info, sizeof(test_info));
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:10:20: note: destination object ‘test_info’ of size 0
   10 |     struct test_st test_info[16];
      |                    ^~~~~~~~~
In file included from /usr/include/unistd.h:1214,
                 from a.c:3:
/usr/include/x86_64-linux-gnu/bits/unistd.h:26:1: note: in a call to function
‘read’ declared with attribute ‘access (write_only, 2)’
   26 | read (int __fd, void *__buf, size_t __nbytes)
      | ^~~~

GCC allows empty structs on C code and they are correctly sized 0, but
-Wstringop-overflow still thinks the code is trying to read 1 byte into the
array, which is not correct.

I know there are a lot of false positives reported against -Wstringop-overflow,
but I couldn't find an exact duplicate of this one.

Reply via email to