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...@@ -26193,6 +26193,8 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
26193 }26193 }
2619426194
26195 try sema.requireRuntimeBlock(block, src, runtime_src);26195 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
26197 // Aliasing safety check.26199 // Aliasing safety check.
26198 if (block.wantSafety()) {26200 if (block.wantSafety()) {
...@@ -26321,6 +26323,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -26321,6 +26323,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
26321 };26323 };
2632226324
26323 try sema.requireRuntimeBlock(block, src, runtime_src);26325 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
26324 _ = try block.addInst(.{26329 _ = try block.addInst(.{
26325 .tag = if (block.wantSafety()) .memset_safe else .memset,26330 .tag = if (block.wantSafety()) .memset_safe else .memset,
26326 .data = .{ .bin_op = .{26331 .data = .{ .bin_op = .{
test/cases/compile_errors/comptime_var_referenced_at_runtime.zig+20
...@@ -47,6 +47,22 @@ export fn qar() void {...@@ -47,6 +47,22 @@ export fn qar() void {
47 _ = y;47 _ = y;
48}48}
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
50// error66// error
51//67//
52// :5:19: error: runtime value contains reference to comptime var68// :5:19: error: runtime value contains reference to comptime var
...@@ -63,3 +79,7 @@ export fn qar() void {...@@ -63,3 +79,7 @@ export fn qar() void {
63// :41:12: note: comptime var pointers are not available at runtime79// :41:12: note: comptime var pointers are not available at runtime
64// :46:39: error: runtime value contains reference to comptime var80// :46:39: error: runtime value contains reference to comptime var
65// :46:39: note: comptime var pointers are not available at runtime81// :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