mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 23:23:07 +00:00
sema: do checked cast when resolving aggregate size
This commit is contained in:
parent
1f6336794b
commit
365ed0ed68
20
src/Sema.zig
20
src/Sema.zig
@ -34985,7 +34985,15 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
|
|||||||
offsets[i] = @intCast(aligns[i].forward(offset));
|
offsets[i] = @intCast(aligns[i].forward(offset));
|
||||||
offset = offsets[i] + sizes[i];
|
offset = offsets[i] + sizes[i];
|
||||||
}
|
}
|
||||||
struct_type.setLayoutResolved(ip, @intCast(big_align.forward(offset)), big_align);
|
const size = std.math.cast(u32, big_align.forward(offset)) orelse {
|
||||||
|
const msg = try sema.errMsg(
|
||||||
|
ty.srcLoc(zcu),
|
||||||
|
"struct layout requires size {d}, this compiler implementation supports up to {d}",
|
||||||
|
.{ big_align.forward(offset), std.math.maxInt(u32) },
|
||||||
|
);
|
||||||
|
return sema.failWithOwnedErrorMsg(null, msg);
|
||||||
|
};
|
||||||
|
struct_type.setLayoutResolved(ip, size, big_align);
|
||||||
_ = try ty.comptimeOnlySema(pt);
|
_ = try ty.comptimeOnlySema(pt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35260,7 +35268,15 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {
|
|||||||
break :layout .{ size, max_align.max(tag_align), padding };
|
break :layout .{ size, max_align.max(tag_align), padding };
|
||||||
} else .{ max_align.forward(max_size), max_align, 0 };
|
} else .{ max_align.forward(max_size), max_align, 0 };
|
||||||
|
|
||||||
union_type.setHaveLayout(ip, @intCast(size), padding, alignment);
|
const casted_size = std.math.cast(u32, size) orelse {
|
||||||
|
const msg = try sema.errMsg(
|
||||||
|
ty.srcLoc(pt.zcu),
|
||||||
|
"union layout requires size {d}, this compiler implementation supports up to {d}",
|
||||||
|
.{ size, std.math.maxInt(u32) },
|
||||||
|
);
|
||||||
|
return sema.failWithOwnedErrorMsg(null, msg);
|
||||||
|
};
|
||||||
|
union_type.setHaveLayout(ip, casted_size, padding, alignment);
|
||||||
|
|
||||||
if (union_type.flagsUnordered(ip).assumed_runtime_bits and !(try ty.hasRuntimeBitsSema(pt))) {
|
if (union_type.flagsUnordered(ip).assumed_runtime_bits and !(try ty.hasRuntimeBitsSema(pt))) {
|
||||||
const msg = try sema.errMsg(
|
const msg = try sema.errMsg(
|
||||||
|
|||||||
31
test/cases/compile_errors/aggregate_too_large.zig
Normal file
31
test/cases/compile_errors/aggregate_too_large.zig
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
const S = struct {
|
||||||
|
data: [1 << 32]u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const T = struct {
|
||||||
|
d1: [1 << 31]u8,
|
||||||
|
d2: [1 << 31]u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const U = union {
|
||||||
|
a: u32,
|
||||||
|
b: [1 << 32]u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const V = union {
|
||||||
|
a: u32,
|
||||||
|
b: T,
|
||||||
|
};
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
_ = S;
|
||||||
|
_ = T;
|
||||||
|
_ = U;
|
||||||
|
_ = V;
|
||||||
|
}
|
||||||
|
|
||||||
|
// error
|
||||||
|
//
|
||||||
|
// :1:11: error: struct layout requires size 4294967296, this compiler implementation supports up to 4294967295
|
||||||
|
// :5:11: error: struct layout requires size 4294967296, this compiler implementation supports up to 4294967295
|
||||||
|
// :10:11: error: union layout requires size 4294967300, this compiler implementation supports up to 4294967295
|
||||||
Loading…
x
Reference in New Issue
Block a user