Skip to content

C front-end: zero-width bit-fields must not have a declarator #7227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions regression/ansi-c/bitfields2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <limits.h>

#define CONCAT(a, b) a##b
#define CONCAT2(a, b) CONCAT(a, b)

#define STATIC_ASSERT(condition) \
int CONCAT2(some_array, __LINE__)[(condition) ? 1 : -1]

#if CHAR_BIT == 8
struct bits
{
char a : 4;
char b : 4;
char c : 4;
char d : 4;
char : 0; // this is ok: no declarator
int i;
};

STATIC_ASSERT(sizeof(struct bits) == 2 * sizeof(int));

# pragma pack(1)
struct packed_bits
{
char a : 4;
char b : 4;
char c : 4;
char d : 4;
char x : 0; // this is not permitted
int i;
};
# pragma pack()

STATIC_ASSERT(sizeof(struct packed_bits) == sizeof(int) + 2);
#endif

int main()
{
}
9 changes: 9 additions & 0 deletions regression/ansi-c/bitfields2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c

zero-width bit-field with declarator not permitted$
CONVERSION ERROR
^EXIT=(64|1)$
^SIGNAL=0$
--
^warning: ignoring
10 changes: 10 additions & 0 deletions src/ansi-c/c_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,16 @@ void c_typecheck_baset::typecheck_compound_body(
throw 0;
}

if(
new_component.type().id() == ID_c_bit_field &&
to_c_bit_field_type(new_component.type()).get_width() == 0 &&
!new_component.get_name().empty())
{
throw invalid_source_file_exceptiont{
"zero-width bit-field with declarator not permitted",
source_location};
}

components.push_back(new_component);
}
}
Expand Down