https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78436
Bug ID: 78436
Summary: incorrect write to larger-than-type bitfield (signed
char x:9)
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: babokin at gmail dot com
Target Milestone: ---
Created attachment 40091
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40091&action=edit
reproducer
The test is correct C++ program with bitfield larger than its base type, i.e.
signed char : 9.
-O2 produces incorrect result.
The C++14 standard states explicitly the following (9.3.1, [class.bit]):
The value of the integral constant expression may be larger than the number of
bits in the object representation (3.9) of the bit-field’s type; in such cases
the extra bits are used as padding bits and do not participate in the value
representation (3.9) of the bit-field.
> g++ -O0 -w -o no_opt bitfield.cpp main.cpp;./no_opt
0
> g++ -O2 -w -o opt bitfield.cpp main.cpp;./opt
-1
> cat bitfield.h
struct S {
long int : 23;
long int m0 : 24;
long int m1 : 10;
long int m2 : 24;
signed char m3 : 9;
};
extern struct S s1;
> cat bitfield.cpp
#include "bitfield.h"
void foo() {
s1.m3 = 0;
s1.m2 = -1193165L;
}
> cat main.cpp
#include <stdio.h>
#include "bitfield.h"
struct S s1;
void init () {
s1.m0 = 0L;
s1.m1 = 0L;
s1.m2 = 0L;
s1.m3 = 0;
}
void foo();
int main() {
init();
foo();
printf("%d\n",s1.m3);
return 0;
}
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/home/dybaboki/gcc/bin/libexec/gcc/x86_64-pc-linux-gnu/7.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure --with-arch=corei7 --with-cpu=corei7
--enable-clocale=gnu --with-system-zlib --enable-shared --with-demangler-in-ld
--enable-cloog-backend=isl --with-fpmath=sse --prefix=/home/dybaboki/gcc/bin
--enable-languages=c,c++,fortran,lto : (reconfigured) ../gcc/configure
--with-arch=corei7 --with-cpu=corei7 --enable-clocale=gnu --with-system-zlib
--enable-shared --with-demangler-in-ld --enable-cloog-backend=isl
--with-fpmath=sse --prefix=/home/dybaboki/gcc/bin
--enable-languages=c,c++,fortran,lto : (reconfigured) ../gcc/configure
--with-arch=corei7 --with-cpu=corei7 --enable-clocale=gnu --with-system-zlib
--enable-shared --with-demangler-in-ld --enable-cloog-backend=isl
--with-fpmath=sse --prefix=/home/dybaboki/gcc/bin
--enable-languages=c,c++,fortran,lto : (reconfigured) ../gcc_github/configure
--with-arch=corei7 --with-cpu=corei7 --enable-clocale=gnu --with-system-zlib
--enable-shared --with-demangler-in-ld --enable-cloog-backend=isl
--with-fpmath=sse --prefix=/home/dybaboki/gcc/bin
--enable-languages=c,c++,fortran,lto --no-create --no-recursion :
(reconfigured) ../gcc/configure --with-arch=corei7 --with-cpu=corei7
--enable-clocale=gnu --with-system-zlib --enable-shared --with-demangler-in-ld
--enable-cloog-backend=isl --with-fpmath=sse --prefix=/home/dybaboki/gcc/bin
--enable-languages=c,c++,fortran,lto --no-create --no-recursion
Thread model: posix
gcc version 7.0.0 20161119 (experimental) (GCC)