authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-10-07 07:27:50+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-10-07 07:27:50+01:00
log36243567e610808834147218fa791cbaab535941
tree7c7eca7acc54ac54ed5e3326118f0ce8cb18490d
parent7a2fde973d27c7c47dc6eba174c71ae93af29d5f
signaturelock-open Commit is signed but in an unrecognized format.

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


2 files changed, 25 insertions(+), 0 deletions(-)

src/Sema.zig+5
......@@ -26193,6 +26193,8 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2619326193 }
2619426194
2619526195 try sema.requireRuntimeBlock(block, src, runtime_src);
26196 try sema.validateRuntimeValue(block, dest_src, dest_ptr);
26197 try sema.validateRuntimeValue(block, src_src, src_ptr);
2619626198
2619726199 // Aliasing safety check.
2619826200 if (block.wantSafety()) {
......@@ -26321,6 +26323,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2632126323 };
2632226324
2632326325 try sema.requireRuntimeBlock(block, src, runtime_src);
26326 try sema.validateRuntimeValue(block, dest_src, dest_ptr);
26327 try sema.validateRuntimeValue(block, value_src, elem);
26328
2632426329 _ = try block.addInst(.{
2632526330 .tag = if (block.wantSafety()) .memset_safe else .memset,
2632626331 .data = .{ .bin_op = .{
test/cases/compile_errors/comptime_var_referenced_at_runtime.zig+20
......@@ -47,6 +47,22 @@ export fn qar() void {
4747 _ = y;
4848}
4949
50export fn bux() void {
51 comptime var x: [2]u32 = undefined;
52 x = .{ 1, 2 };
53
54 var rt: [2]u32 = undefined;
55 @memcpy(&rt, &x);
56}
57
58export fn far() void {
59 comptime var x: u32 = 123;
60
61 var rt: [2]*u32 = undefined;
62 const elem: *u32 = &x;
63 @memset(&rt, elem);
64}
65
5066// error
5167//
5268// :5:19: error: runtime value contains reference to comptime var
......@@ -63,3 +79,7 @@ export fn qar() void {
6379// :41:12: note: comptime var pointers are not available at runtime
6480// :46:39: error: runtime value contains reference to comptime var
6581// :46:39: note: comptime var pointers are not available at runtime
82// :55:18: error: runtime value contains reference to comptime var
83// :55:18: note: comptime var pointers are not available at runtime
84// :63:18: error: runtime value contains reference to comptime var
85// :63:18: note: comptime var pointers are not available at runtime