Skip to content

Commit 9720295

Browse files
authored
Merge pull request #7227 from tautschnig/bugfixes/zero-width-bitfields
C front-end: zero-width bit-fields must not have a declarator
2 parents 42e3680 + 50c43e2 commit 9720295

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

regression/ansi-c/bitfields2/main.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <limits.h>
2+
3+
#define CONCAT(a, b) a##b
4+
#define CONCAT2(a, b) CONCAT(a, b)
5+
6+
#define STATIC_ASSERT(condition) \
7+
int CONCAT2(some_array, __LINE__)[(condition) ? 1 : -1]
8+
9+
#if CHAR_BIT == 8
10+
struct bits
11+
{
12+
char a : 4;
13+
char b : 4;
14+
char c : 4;
15+
char d : 4;
16+
char : 0; // this is ok: no declarator
17+
int i;
18+
};
19+
20+
STATIC_ASSERT(sizeof(struct bits) == 2 * sizeof(int));
21+
22+
# pragma pack(1)
23+
struct packed_bits
24+
{
25+
char a : 4;
26+
char b : 4;
27+
char c : 4;
28+
char d : 4;
29+
char x : 0; // this is not permitted
30+
int i;
31+
};
32+
# pragma pack()
33+
34+
STATIC_ASSERT(sizeof(struct packed_bits) == sizeof(int) + 2);
35+
#endif
36+
37+
int main()
38+
{
39+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
zero-width bit-field with declarator not permitted$
5+
CONVERSION ERROR
6+
^EXIT=(64|1)$
7+
^SIGNAL=0$
8+
--
9+
^warning: ignoring

src/ansi-c/c_typecheck_type.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,16 @@ void c_typecheck_baset::typecheck_compound_body(
964964
throw 0;
965965
}
966966

967+
if(
968+
new_component.type().id() == ID_c_bit_field &&
969+
to_c_bit_field_type(new_component.type()).get_width() == 0 &&
970+
!new_component.get_name().empty())
971+
{
972+
throw invalid_source_file_exceptiont{
973+
"zero-width bit-field with declarator not permitted",
974+
source_location};
975+
}
976+
967977
components.push_back(new_component);
968978
}
969979
}

0 commit comments

Comments
 (0)