Skip to content

Commit

Permalink
Parser: avoid overflow when enum backed by large _BitInt
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas authored and Vexu committed May 3, 2024
1 parent d226852 commit 333089c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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'" \

0 comments on commit 333089c

Please sign in to comment.