From 43cb31f3a231ab7b80322f2990d693e78160c45b Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Thu, 2 May 2024 23:41:16 -0700 Subject: [PATCH] Type: avoid overflow computing size of large _BitInt's --- src/aro/Type.zig | 2 +- test/cases/_BitInt.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/aro/Type.zig b/src/aro/Type.zig index 0ff08ee8..1ca932a8 100644 --- a/src/aro/Type.zig +++ b/src/aro/Type.zig @@ -1001,7 +1001,7 @@ pub fn sizeof(ty: Type, comp: *const Compilation) ?u64 { .double => comp.target.c_type_byte_size(.double), .float128 => 16, .bit_int => { - return std.mem.alignForward(u64, (ty.data.int.bits + 7) / 8, ty.alignof(comp)); + return std.mem.alignForward(u64, (@as(u32, ty.data.int.bits) + 7) / 8, ty.alignof(comp)); }, // zig fmt: off .complex_char, .complex_schar, .complex_uchar, .complex_short, .complex_ushort, .complex_int, diff --git a/test/cases/_BitInt.c b/test/cases/_BitInt.c index 4f1e83d2..f815a73d 100644 --- a/test/cases/_BitInt.c +++ b/test/cases/_BitInt.c @@ -26,6 +26,7 @@ enum E: _BitInt(512) { A=6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042047WB, B, }; +_Static_assert(sizeof(_BitInt(65535)) == 8192, ""); #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" \