From 0cfed00b525498a20f8e6ddfa5886daacbb56282 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Wed, 11 Apr 2018 23:30:05 -0500 Subject: [PATCH] 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 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); } --- Parser.pas | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Parser.pas b/Parser.pas index 971e597..e535029 100644 --- a/Parser.pas +++ b/Parser.pas @@ -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}