Fix invalid static initialization of bitfields occupying three bytes.

An extra, fourth byte was being generated for the bitfield(s). This would cause all subsequent members of the struct and any enclosing object not to be initialized at the proper locations, which would generally corrupt their values.

The following program illustrates the issue:

#include <stdio.h>

struct X {
    int a:9;
    int b:9;
    int c;
} x = {123,234,12345};

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

View File

@ -1765,12 +1765,19 @@ var
iPtr^.bitsize := 0;
iPtr^.isStructOrUnion := false;
iPtr^.iVal := bitvalue;
if bitcount > 16 then
iPtr^.itype := cgULong
else if bitcount > 8 then
if bitcount <= 8 then
iPtr^.itype := cgUByte
else if bitcount <= 16 then
iPtr^.itype := cgUWord
else
iPtr^.itype := cgUByte;
else if bitcount > 24 then
iPtr^.itype := cgULong
else begin {3-byte bitfield: split into two parts}
iPtr^.itype := cgUWord;
iPtr^.iVal := bitvalue & $0000FFFF;
bitcount := bitcount - 16;
bitvalue := bitvalue >> 16;
InitializeBitField;
end;
bitcount := 0; {reset the bit field values}
bitvalue := 0;
end; {if}