Skip to content

Commit 8c59fd3

Browse files
committed
Check enum range for typed enums
1 parent 6ca6328 commit 8c59fd3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

tccgen.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,13 +4431,19 @@ static void struct_decl(CType *type, int u)
44314431
/* non empty enums are not allowed */
44324432
ps = &s->next;
44334433
if (u == VT_ENUM) {
4434-
long long ll = 0, pl = 0, nl = 0;
4434+
long long ll = 0, pl = 0, nl = 0, ni = 0, pi = 0;
4435+
unsigned long long mu = 0;
44354436
CType t;
44364437
t.ref = s;
44374438
/* enum symbols have static storage */
44384439
t.t = VT_INT|VT_STATIC|VT_ENUM_VAL;
4439-
if (bt)
4440+
if (bt) {
44404441
t.t = bt|VT_STATIC|VT_ENUM_VAL;
4442+
mu = 1llu << (type_size(&t, &align) * 8 - 1);
4443+
pi = mu - 1;
4444+
ni = -mu;
4445+
mu = (mu << 1) - 1u;
4446+
}
44414447
for(;;) {
44424448
v = tok;
44434449
if (v < TOK_UIDENT)
@@ -4451,6 +4457,10 @@ static void struct_decl(CType *type, int u)
44514457
next();
44524458
ll = expr_const64();
44534459
}
4460+
if (bt && (t.t & VT_UNSIGNED ? (unsigned long long)ll > mu
4461+
: ll < ni || ll > pi))
4462+
tcc_error("enumerator value outside the range of underlying type '%s'",
4463+
get_tok_str(v, NULL));
44544464
ss = sym_push(v, &t, VT_CONST, 0);
44554465
ss->enum_val = ll;
44564466
*ps = ss, ps = &ss->next;

0 commit comments

Comments
 (0)