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

            Bug ID: 100834
           Summary: False positive of -Wstringop-overflow= with -Os
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: florian.bezdeka at siemens dot com
  Target Milestone: ---

Compiling the testcase below with "gcc test.c" works, while "gcc -Os test.c"
produces a warning:

test.c: In function ‘main’:
test.c:59:9: warning: ‘memset’ writing 128 bytes into a region of size 0
overflows the destination [-Wstringop-overflow=]
   59 |         memset(&cpu_data->guest_regs, 0, sizeof(cpu_data->guest_regs));
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Tested gcc versions:

gcc 11.1.1 (Fedora 34): Affected
gcc 10.3.1 (Fedora 33): OK
gcc 10.2.1 (Debian 11): OK


test.c:

#include <stddef.h>

#define PAGE_SIZE       4096
#define STACK_SIZE      PAGE_SIZE

union registers {
        struct {
                unsigned long r15;
                unsigned long r14;
                unsigned long r13;
                unsigned long r12;
                unsigned long r11;
                unsigned long r10;
                unsigned long r9;
                unsigned long r8;
                unsigned long rdi;
                unsigned long rsi;
                unsigned long rbp;
                unsigned long unused;
                unsigned long rbx;
                unsigned long rdx;
                unsigned long rcx;
                unsigned long rax;
        };
        unsigned long by_index[16];
};

struct per_cpu {
        union {
                /** Stack used while in hypervisor mode. */
                unsigned char stack[STACK_SIZE];
                struct {
                        unsigned char __fill[STACK_SIZE - sizeof(union
registers)];
                        /** Guest registers saved on stack during VM exit. */
                        union registers guest_regs;
                };
        };

} __attribute__((aligned(PAGE_SIZE)));

void *memset(void *s, int c, size_t n)
{
        unsigned char *p = s;

        while (n-- > 0)
                *p++ = c;
        return s;
}

static inline struct per_cpu *this_cpu_data(void)
{
        return (struct per_cpu *) 0xdeadbeef;
}

int main(int argc, char **argv)
{
        struct per_cpu *cpu_data = this_cpu_data();

        memset(&cpu_data->guest_regs, 0, sizeof(cpu_data->guest_regs));
}
  • [Bug tree-optimization/100... florian.bezdeka at siemens dot com via Gcc-bugs

Reply via email to