authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2025-04-09 14:25:37+10:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-04-28 16:48:45+01:00
log365ed0ed68b6f6c39b3b8f5373fbba25fe09a518
tree48a6cb2d7b9f0a7eba8f85acac7e4f0dc6b8b1b4
parent1f6336794b519d827eb2da583e238e95c6f7e0e5

sema: do checked cast when resolving aggregate size


2 files changed, 49 insertions(+), 2 deletions(-)

src/Sema.zig+18-2
...@@ -34985,7 +34985,15 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {...@@ -34985,7 +34985,15 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
34985 offsets[i] = @intCast(aligns[i].forward(offset));34985 offsets[i] = @intCast(aligns[i].forward(offset));
34986 offset = offsets[i] + sizes[i];34986 offset = offsets[i] + sizes[i];
34987 }34987 }
34988 struct_type.setLayoutResolved(ip, @intCast(big_align.forward(offset)), big_align);34988 const size = std.math.cast(u32, big_align.forward(offset)) orelse {
34989 const msg = try sema.errMsg(
34990 ty.srcLoc(zcu),
34991 "struct layout requires size {d}, this compiler implementation supports up to {d}",
34992 .{ big_align.forward(offset), std.math.maxInt(u32) },
34993 );
34994 return sema.failWithOwnedErrorMsg(null, msg);
34995 };
34996 struct_type.setLayoutResolved(ip, size, big_align);
34989 _ = try ty.comptimeOnlySema(pt);34997 _ = try ty.comptimeOnlySema(pt);
34990}34998}
3499134999
...@@ -35260,7 +35268,15 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {...@@ -35260,7 +35268,15 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {
35260 break :layout .{ size, max_align.max(tag_align), padding };35268 break :layout .{ size, max_align.max(tag_align), padding };
35261 } else .{ max_align.forward(max_size), max_align, 0 };35269 } else .{ max_align.forward(max_size), max_align, 0 };
3526235270
35263 union_type.setHaveLayout(ip, @intCast(size), padding, alignment);35271 const casted_size = std.math.cast(u32, size) orelse {
35272 const msg = try sema.errMsg(
35273 ty.srcLoc(pt.zcu),
35274 "union layout requires size {d}, this compiler implementation supports up to {d}",
35275 .{ size, std.math.maxInt(u32) },
35276 );
35277 return sema.failWithOwnedErrorMsg(null, msg);
35278 };
35279 union_type.setHaveLayout(ip, casted_size, padding, alignment);
3526435280
35265 if (union_type.flagsUnordered(ip).assumed_runtime_bits and !(try ty.hasRuntimeBitsSema(pt))) {35281 if (union_type.flagsUnordered(ip).assumed_runtime_bits and !(try ty.hasRuntimeBitsSema(pt))) {
35266 const msg = try sema.errMsg(35282 const msg = try sema.errMsg(
test/cases/compile_errors/aggregate_too_large.zig created+31
...@@ -0,0 +1,31 @@
1const S = struct {
2 data: [1 << 32]u8,
3};
4
5const T = struct {
6 d1: [1 << 31]u8,
7 d2: [1 << 31]u8,
8};
9
10const U = union {
11 a: u32,
12 b: [1 << 32]u8,
13};
14
15const V = union {
16 a: u32,
17 b: T,
18};
19
20comptime {
21 _ = S;
22 _ = T;
23 _ = U;
24 _ = V;
25}
26
27// error
28//
29// :1:11: error: struct layout requires size 4294967296, this compiler implementation supports up to 4294967295
30// :5:11: error: struct layout requires size 4294967296, this compiler implementation supports up to 4294967295
31// :10:11: error: union layout requires size 4294967300, this compiler implementation supports up to 4294967295