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

            Bug ID: 106939
           Summary: Linker-defined symbols are stained due to 'array
                    subscript is partly outside array bounds' warning
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: neoxic at icloud dot com
  Target Milestone: ---

Working with embedded applications, one often deals with manipulating memory
regions via symbols defined in a linker script. Please consider the following
snippet:

-----------------------------------------------------
#include <string.h>

extern char _src, _dst; // Defined by the linker

int main(void) {
    memcpy(&_dst, &_src + 1024, 1111);
    return 0;
}
-----------------------------------------------------

Compilation with GCC 12.2.0 yields lots of warnings:

-----------------------------------------------------
$ cc -O2 -Wall -Wextra -fno-strict-aliasing -fwrapv a.c
a.c: In function ‘main’:
a.c:6:29: warning: array subscript 1024 is outside array bounds of ‘char[1]’
[-Warray-bounds]
    6 |         memcpy(&_dst, &_src + 1024, 1111);
      |                       ~~~~~~^~~~~~
a.c:3:13: note: at offset 1024 into object ‘_src’ of size 1
    3 | extern char _src, _dst; // Defined by the linker
      |             ^~~~
a.c:6:9: warning: ‘memcpy’ forming offset [1, 1110] is out of the bounds [0, 1]
of object ‘_dst’ with type ‘char’ [-Warray-bounds]
    6 |         memcpy(&_dst, &_src + 1024, 1111);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:3:19: note: ‘_dst’ declared here
    3 | extern char _src, _dst; // Defined by the linker
      |                   ^~~~
-----------------------------------------------------

These symbols are used as mere address anchors, they don't contain anything.
But no matter what I do like changing their type, casting their addresses, etc,
I can't get rid of the pesky sticky warnings now. Is there a clean way to work
around these warning without turning off Warray-bounds?

Reply via email to