https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106904
Bug ID: 106904
Summary: Incorrect -Wstringop-overflow with partial memcpy()
into a nested structure
Product: gcc
Version: 12.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: zfigura at codeweavers dot com
Target Milestone: ---
Created attachment 53562
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53562&action=edit
minimal test case
I encountered a warning while trying to compile 32-bit wine 7.17 with gcc 12.2,
specificially at this line here:
https://source.winehq.org/git/wine.git/blob/wine-7.17:/dlls/win32u/message.c#l359
The relevant code copies a smaller structure into a larger one of a different
type. (This may be a violation of aliasing rules, but adding
-fno-strict-aliasing doesn't change anything.)
I was able to reproduce this with a minimal test case. This is a very weird set
of conditions, but I couldn't seem to reduce this test case any further.
Changing the type of "ps" to "struct packed_windowpos" makes the error go away;
so does changing the first argument of the memcpy to "ps".
leslie@terabithia:~$ gcc --version
gcc (Debian 12.2.0-1) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
leslie@terabithia:~$ gcc -m32 test.c -c -o test.o -Wall -O2
test.c: In function ‘func’:
test.c:26:5: warning: writing 8 bytes into a region of size 4
[-Wstringop-overflow=]
26 | __builtin_memcpy(&ps->wp, &wp, sizeof(wp));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.c:9:9: note: destination object ‘hwnd’ of size 4
9 | int hwnd;
| ^~~~