https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94206
Bug ID: 94206
Summary: Wrong optimization: memset of n-bit integer types
(from bit-fields) is truncated to n bits (instead of
sizeof)
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: ch3root at openwall dot com
Target Milestone: ---
Bit-fields of different widths have their own types in gcc. And `memset` seems
to be optimized strangely for them. Even at `-O0`!
This is not standard C due to the use of `typeof` but this topic is starting to
be more interesting in light of N2472 "Adding Fundamental Type for N-bit
Integers".
----------------------------------------------------------------------
#include <string.h>
#include <stdio.h>
struct {
unsigned long x:33;
} s;
typedef __typeof__(s.x + 0) uint33;
int main()
{
uint33 x;
printf("sizeof = %zu\n", sizeof x);
memset(&x, -1, sizeof x);
unsigned long u;
memcpy(&u, &x, sizeof u);
printf("%lx\n", u);
}
----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
sizeof = 8
1ffffffff
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
sizeof = 8
1ffffffff
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.1 20200317 (experimental)
----------------------------------------------------------------------
The right result is `ffffffffffffffff`.