Sema: add missing runtime value validation to @memcpy and @memset

This commit is contained in:
mlugg
2024-10-07 07:27:50 +01:00
parent 7a2fde973d
commit 36243567e6
2 changed files with 25 additions and 0 deletions

View File

@@ -26193,6 +26193,8 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
}
try sema.requireRuntimeBlock(block, src, runtime_src);
try sema.validateRuntimeValue(block, dest_src, dest_ptr);
try sema.validateRuntimeValue(block, src_src, src_ptr);
// Aliasing safety check.
if (block.wantSafety()) {
@@ -26321,6 +26323,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
};
try sema.requireRuntimeBlock(block, src, runtime_src);
try sema.validateRuntimeValue(block, dest_src, dest_ptr);
try sema.validateRuntimeValue(block, value_src, elem);
_ = try block.addInst(.{
.tag = if (block.wantSafety()) .memset_safe else .memset,
.data = .{ .bin_op = .{

View File

@@ -47,6 +47,22 @@ export fn qar() void {
_ = y;
}
export fn bux() void {
comptime var x: [2]u32 = undefined;
x = .{ 1, 2 };
var rt: [2]u32 = undefined;
@memcpy(&rt, &x);
}
export fn far() void {
comptime var x: u32 = 123;
var rt: [2]*u32 = undefined;
const elem: *u32 = &x;
@memset(&rt, elem);
}
// error
//
// :5:19: error: runtime value contains reference to comptime var
@@ -63,3 +79,7 @@ export fn qar() void {
// :41:12: note: comptime var pointers are not available at runtime
// :46:39: error: runtime value contains reference to comptime var
// :46:39: note: comptime var pointers are not available at runtime
// :55:18: error: runtime value contains reference to comptime var
// :55:18: note: comptime var pointers are not available at runtime
// :63:18: error: runtime value contains reference to comptime var
// :63:18: note: comptime var pointers are not available at runtime