History

Fix bitfields init : tiny solution

tccgen.c: Check struct/union size for bitfield. tests/tcctest.c: Add test code.

herman ten brugge committed 6 years ago

Commit 8fd7a381 parent 89ea6242 files · +9 −3Browse files

Unified · Split

2 files
  1. tccgen.c +6 −3
  2. tests/tcctest.c +3 −0
tccgen.c+6−3
@@ -7339,7 +7339,7 @@ static void init_putz(Section *sec, unsigned long c, int size) · 7338 unmodified lines
73397339 'al' contains the already initialized length of the
73407340 current container (starting at c). This returns the new length of that. */
73417341 static int decl_designator(CType *type, Section *sec, unsigned long c,
7342− Sym **cur_field, int flags, int al)
7342+ Sym **cur_field, int flags, int al, int size)
73437343 {
73447344 Sym *s, *f;
73457345 int index, index_last, align, l, nb_elems, elem_size;
@@ -7428,8 +7428,11 @@ static int decl_designator(CType *type, Section *sec, unsigned long c, · 82 unmodified lines
74287428 ensures that it even works with designators) */
74297429 if (!(flags & DIF_SIZE_ONLY)) {
74307430 int zlen = c - (corig + al);
7431− if (type->t & VT_BITFIELD) /* must include current field too */
7431+ if (type->t & VT_BITFIELD) { /* must include current field too */
74327432 zlen += type_size(type, &align);
7433+ if (al + zlen > size)
7434+ zlen = size - al;
7435+ }
74337436 if (zlen > 0)
74347437 init_putz(sec, corig + al, zlen);
74357438 }
@@ -7783,7 +7786,7 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 347 unmodified lines
77837786 do_init_list:
77847787 len = 0;
77857788 while (tok != '}' || (flags & DIF_HAVE_ELEM)) {
7786− len = decl_designator(type, sec, c, &f, flags, len);
7789+ len = decl_designator(type, sec, c, &f, flags, len, n*size1);
77877790 flags &= ~DIF_HAVE_ELEM;
77887791 if (type->t & VT_ARRAY) {
77897792 ++indexsym.c;
tests/tcctest.c+3−0
@@ -1746,6 +1746,8 @@ struct bfa_SS {int a,b; struct bf_SS c[3]; int d,e; }; · 1745 unmodified lines
17461746 struct bfa_SS bfa_init = { .c[1].bit = 1 };
17471747 struct bf_SS bfaa_init[3] = { [1].bit = 1 };
17481748 struct bf_SS bfaa_vinit[] = { [2].bit = 1 };
1749+struct b2_SS {long long int field : 52; long long int pad : 12; };
1750+struct b2_SS bf_init2 = {0xFFF000FFF000FLL, 0x123};
17491751
17501752 extern int external_inited = 42;
17511753
@@ -1771,6 +1773,7 @@ void init_test(void) · 19 unmodified lines
17711773 struct bfa_SS bfa_finit = { .c[1].bit = 1 };
17721774 struct bf_SS bfaa_finit[3] = { [1].bit = 1 };
17731775 struct bf_SS bfaa_fvinit[] = { [2].bit = 1 };
1776+ struct b2_SS bf_finit2 = {0xFFF000FFF000FLL, 0x123};
17741777
17751778 printf("sinit1=%d\n", sinit1);
17761779 printf("sinit2=%d\n", sinit2);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t