From 9a34e3c5ce34662be956bdb1d0ad988316d16a94 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Sun, 24 Mar 2024 01:20:19 -0700 Subject: decl: Check alignment range We store alignment as int, so check that it fits to avoid implementation-defined behavior. Also, fix printf format specifier and drop unneeded parentheses. --- decl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/decl.c b/decl.c index 98ac497..0a5cf6d 100644 --- a/decl.c +++ b/decl.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -446,8 +447,8 @@ declspecs(struct scope *s, enum storageclass *sc, enum funcspec *fs, int *align) expect(TLPAREN, "after 'alignas'"); other = typename(s, NULL); i = other ? other->align : intconstexpr(s, false); - if (i & (i - 1)) - error(&tok.loc, "invalid alignment: %d", i); + if (i & i - 1 || i > INT_MAX) + error(&tok.loc, "invalid alignment: %llu", i); if (i > *align) *align = i; expect(TRPAREN, "to close 'alignas' specifier"); -- cgit v1.2.3