Fix problem with static initialization of bitfields followed by non-bitfield members.

The initialized bytes for the bitfield(s) could wind up improperly being placed after those for the non-bitfield, generally corrupting both values.

The following program illustrates the problem:

#include <stdio.h>

struct X {
    int a:9;
    int b;
} x = {42,123};

int main(void) {
    printf("x.a = %i, x.b = %i\n", x.a, x.b);
}
This commit is contained in:
Stephen Heumann 2018-04-11 23:30:05 -05:00
parent 04b0143eff
commit 0cfed00b52

View File

@ -2384,6 +2384,13 @@ var
and (token.kind <> rbracech) do begin
if ip^.isForwardDeclared then
ResolveForwardReference(ip);
if ip^.bitSize = 0 then
if bitCount > 0 then begin
InitializeBitField;
bitCount := (bitCount+7) div 8;
count := count-bitCount;
bitCount := 0;
end; {if}
InitializeTerm(ip^.itype, ip^.bitsize, ip^.bitdisp, false);
if ip^.bitSize <> 0 then begin
bitCount := bitCount + ip^.bitSize;
@ -2393,11 +2400,6 @@ var
end; {if}
end {if}
else begin
if bitCount > 0 then begin
bitCount := (bitCount+7) div 8;
count := count-bitCount;
bitCount := 0;
end; {if}
count := count-ip^.itype^.size;
end; {else}
{ writeln('Initializer: ', ip^.bitsize:10, ip^.bitdisp:10, bitCount:10); {debug}