authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2025-04-09 14:25:37+10:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-04-28 20:48:19+02:00
logb5c22777f839f926ff3e272c9285d4d3e6402c0d
tree7172953a0c344657e5449ed61683e51bcb65b0a3
parent7e68999f79228a693fb1d42d6281b7d7f3e2a9de
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

sema: do checked cast when resolving aggregate size


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

src/Sema.zig+18-2
...@@ -35540,7 +35540,15 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {...@@ -35540,7 +35540,15 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
35540 offsets[i] = @intCast(aligns[i].forward(offset));35540 offsets[i] = @intCast(aligns[i].forward(offset));
35541 offset = offsets[i] + sizes[i];35541 offset = offsets[i] + sizes[i];
35542 }35542 }
35543 struct_type.setLayoutResolved(ip, @intCast(big_align.forward(offset)), big_align);35543 const size = std.math.cast(u32, big_align.forward(offset)) orelse {
35544 const msg = try sema.errMsg(
35545 ty.srcLoc(zcu),
35546 "struct layout requires size {d}, this compiler implementation supports up to {d}",
35547 .{ big_align.forward(offset), std.math.maxInt(u32) },
35548 );
35549 return sema.failWithOwnedErrorMsg(null, msg);
35550 };
35551 struct_type.setLayoutResolved(ip, size, big_align);
35544 _ = try ty.comptimeOnlySema(pt);35552 _ = try ty.comptimeOnlySema(pt);
35545}35553}
3554635554
...@@ -35815,7 +35823,15 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {...@@ -35815,7 +35823,15 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {
35815 break :layout .{ size, max_align.max(tag_align), padding };35823 break :layout .{ size, max_align.max(tag_align), padding };
35816 } else .{ max_align.forward(max_size), max_align, 0 };35824 } else .{ max_align.forward(max_size), max_align, 0 };
3581735825
35818 union_type.setHaveLayout(ip, @intCast(size), padding, alignment);35826 const casted_size = std.math.cast(u32, size) orelse {
35827 const msg = try sema.errMsg(
35828 ty.srcLoc(pt.zcu),
35829 "union layout requires size {d}, this compiler implementation supports up to {d}",
35830 .{ size, std.math.maxInt(u32) },
35831 );
35832 return sema.failWithOwnedErrorMsg(null, msg);
35833 };
35834 union_type.setHaveLayout(ip, casted_size, padding, alignment);
3581935835
35820 if (union_type.flagsUnordered(ip).assumed_runtime_bits and !(try ty.hasRuntimeBitsSema(pt))) {35836 if (union_type.flagsUnordered(ip).assumed_runtime_bits and !(try ty.hasRuntimeBitsSema(pt))) {
35821 const msg = try sema.errMsg(35837 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