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

            Bug ID: 110878
           Summary: -Wstringop-overread incorrectly warns about arguments
                    to functions with static array parameter declarations
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: campbell+gcc-bugzilla at mumble dot net
  Target Milestone: ---

Isolated from code passing a pointer into an array and the length of the array
as separate arguments, where each function has the minimum length of the array
encoded in its parameter declaration, and uses runtime conditionals to
guarantee the minimum is met:

// bar(p, n) may access p[0], p[1], ..., p[n-1], and requires n >= 128
void bar(unsigned char[static 128], unsigned);

// foo(p, n) may access p[0], p[1], ..., p[n-1], and requires n >= 16
void
foo(unsigned char p[static 16], unsigned n)
{

        if (n % 128)
                n -= n % 128;
        if (n)
                bar(p, n);
}

<source>: In function 'foo':
<source>:12:17: error: 'bar' accessing 128 bytes in a region of size 16
[-Werror=stringop-overflow=]
   12 |                 bar(p, n);
      |                 ^~~~~~~~~
<source>:12:17: note: referencing argument 1 of type 'unsigned char[128]'
<source>:2:6: note: in a call to function 'bar'
    2 | void bar(unsigned char[static 128], unsigned n);
      |      ^~~
cc1: all warnings being treated as errors
Compiler returned: 1

Reproduced in GCC 10.5, 11.4, and 12.3.  Not reproduced in any earlier versions
of GCC.

Using `if (n >= 128)' doesn't change anything, presumably because GCC doesn't
know the connection between p and n.

Reply via email to