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

            Bug ID: 101525
           Summary: "out of the bounds" warning for an Innocuous memset
                    call with LTO
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Hi-Angel at yandex dot ru
  Target Milestone: ---

Created attachment 51176
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51176&action=edit
preprocessed file that triggers the warnings

Given this memset call

    static bool
    append_space_for_newline (struct it *it, bool default_face_p) {
    […]
          memset (&it->position, 0, sizeof it->position);

GCC complains:

    xdisp.c: In function ‘append_space_for_newline’:
    xdisp.c:21906:7: warning: ‘memset’ offset [2352, 2359] from the object at
‘it_122(D)’ is out of the bounds of referenced subobject ‘charpos’ with type
‘long int’ at offset 2344 [-Warray-bounds]
    21906 |       memset (&it->position, 0, sizeof it->position);
          |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from composite.h:29,
                     from xdisp.c:441:
    dispextern.h:214:13: note: subobject ‘charpos’ declared here
      214 |   ptrdiff_t charpos;
          |             ^~~~~~~

I can't see any overflow on this line. Either way, further experiments show
that removing the function content below the offending line makes warnings
disappear. Since it's impossible to make overflow disappear by removing a code
below the overflow, the warning disappearance suggests the warning is a GCC bug
and should not be there.

----------------------------------------

I couldn't reduce it down to a minimal testcase because removing irrelevant
code makes warning go away, so instead in steps-to-reproduce I use a
preprocessed source. I had to compress it to be able to attach. The original is
a `src/xdisp.c` file in Emacs project at commit `6ebe8b03d80`.

# Steps to reproduce

1. Download `xdisp.preprocessed.c.zst`
2. Unpack with: zstd -d xdisp.preprocessed.c.zst
3. Compile with: gcc -c -Warray-bounds=2 -flto=2 -O3 -ffat-lto-objects 
xdisp.preprocessed.c

## Expected

No warnings for `append_space_for_newline` function

## Actual

There is a warning:

    xdisp.c: In function ‘append_space_for_newline’:
    xdisp.c:21906:7: warning: ‘memset’ offset [2352, 2359] from the object at
‘it_140(D)’ is out of the bounds of referenced subobject ‘charpos’ with type
‘long int’ at offset 2344 [-Warray-bounds]
    In file included from composite.h:29,
                     from xdisp.c:441:
    dispextern.h:214:13: note: subobject ‘charpos’ declared here

Reply via email to