Skip to content
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

Parser: avoid overflow when enum backed by large _BitInt #698

Merged
merged 1 commit into from
May 3, 2024
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
4 changes: 2 additions & 2 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2701,8 +2701,6 @@ const Enumerator = struct {
return;
}
if (try e.res.val.add(e.res.val, Value.one, e.res.ty, p.comp)) {
const byte_size = e.res.ty.sizeof(p.comp).?;
const bit_size: u8 = @intCast(if (e.res.ty.isUnsignedInt(p.comp)) byte_size * 8 else byte_size * 8 - 1);
if (e.fixed) {
try p.errStr(.enum_not_representable_fixed, tok, try p.typeStr(e.res.ty));
return;
Expand All @@ -2711,6 +2709,8 @@ const Enumerator = struct {
try p.errTok(.enumerator_overflow, tok);
break :blk larger;
} else blk: {
const signed = !e.res.ty.isUnsignedInt(p.comp);
const bit_size: u8 = @intCast(e.res.ty.bitSizeof(p.comp).? - @intFromBool(signed));
try p.errExtra(.enum_not_representable, tok, .{ .pow_2_as_string = bit_size });
break :blk Type{ .specifier = .ulong_long };
};
Expand Down
7 changes: 7 additions & 0 deletions test/cases/_BitInt.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ int z = 0uwb;
int x = 1'2;
_Static_assert(((int)-18446744073709551616WB) == 0);

enum E: _BitInt(512) {
A=6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042047WB,
B,
};

#define EXPECTED_ERRORS "_BitInt.c:3:1: warning: '_BitInt' in C17 and earlier is a Clang extension' [-Wbit-int-extension]" \
"_BitInt.c:13:1: error: _BitInt of bit sizes greater than 65535 not supported" \
"_BitInt.c:14:8: error: signed _BitInt must have a bit size of at least 2" \
Expand All @@ -30,3 +35,5 @@ _Static_assert(((int)-18446744073709551616WB) == 0);
"_BitInt.c:18:25: warning: '_BitInt' suffix for literals is a C23 extension [-Wc23-extensions]" \
"_BitInt.c:18:25: warning: '_BitInt' suffix for literals is a C23 extension [-Wc23-extensions]" \
"_BitInt.c:22:10: error: expected ';', found 'a character literal'" \
"_BitInt.c:27:5: error: enumerator value is not representable in the underlying type '_BitInt'" \

Loading