authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-05 16:50:27+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-05 22:13:57+03:00
log6aa438f0654569c1713a5c0c49b0bdda20d87c0f
tree4a903310bc1153af77490332b79c425d38b1fa30
parenteec2978fac240f6852873bdd9a3ae03b77df6199

Sema: add null pointer slice safety check when len is comptime known


7 files changed, 42 insertions(+), 15 deletions(-)

src/Sema.zig+5
......@@ -25569,6 +25569,11 @@ fn analyzeSlice(
2556925569 const new_ptr_val = opt_new_ptr_val orelse {
2557025570 const result = try block.addBitCast(return_ty, new_ptr);
2557125571 if (block.wantSafety()) {
25572 // requirement: slicing C ptr is non-null
25573 if (ptr_ptr_child_ty.isCPtr()) {
25574 const is_non_null = try sema.analyzeIsNull(block, ptr_src, ptr, true);
25575 try sema.addSafetyCheck(block, is_non_null, .unwrap_null);
25576 }
2557225577 // requirement: result[new_len] == slice_sentinel
2557325578 try sema.panicSentinelMismatch(block, src, slice_sentinel, elem_ty, result, new_len);
2557425579 }
test/cases/safety/pointer slice sentinel mismatch.zig +3-3
......@@ -2,14 +2,14 @@ const std = @import("std");
22
33pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
44 _ = stack_trace;
5 if (std.mem.eql(u8, message, "sentinel mismatch")) {
5 if (std.mem.eql(u8, message, "sentinel mismatch: expected 0, found 4")) {
66 std.process.exit(0);
77 }
88 std.process.exit(1);
99}
1010
1111pub fn main() !void {
12 var buf: [4]u8 = undefined;
12 var buf: [4]u8 = .{ 1, 2, 3, 4 };
1313 const ptr: [*]u8 = &buf;
1414 const slice = ptr[0..3 :0];
1515 _ = slice;
......@@ -17,5 +17,5 @@ pub fn main() !void {
1717}
1818
1919// run
20// backend=stage1
20// backend=llvm
2121// target=native
test/cases/safety/slice sentinel mismatch - floats.zig +3-3
......@@ -2,19 +2,19 @@ const std = @import("std");
22
33pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
44 _ = stack_trace;
5 if (std.mem.eql(u8, message, "sentinel mismatch")) {
5 if (std.mem.eql(u8, message, "sentinel mismatch: expected 1.20000004e+00, found 4.0e+00")) {
66 std.process.exit(0);
77 }
88 std.process.exit(1);
99}
1010
1111pub fn main() !void {
12 var buf: [4]f32 = undefined;
12 var buf: [4]f32 = .{ 1, 2, 3, 4 };
1313 const slice = buf[0..3 :1.2];
1414 _ = slice;
1515 return error.TestFailed;
1616}
1717
1818// run
19// backend=stage1
19// backend=llvm
2020// target=native
test/cases/safety/slice sentinel mismatch - optional pointers.zig +3-3
......@@ -2,19 +2,19 @@ const std = @import("std");
22
33pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
44 _ = stack_trace;
5 if (std.mem.eql(u8, message, "sentinel mismatch")) {
5 if (std.mem.eql(u8, message, "sentinel mismatch: expected null, found i32@10")) {
66 std.process.exit(0);
77 }
88 std.process.exit(1);
99}
1010
1111pub fn main() !void {
12 var buf: [4]?*i32 = undefined;
12 var buf: [4]?*i32 = .{ @intToPtr(*i32, 4), @intToPtr(*i32, 8), @intToPtr(*i32, 12), @intToPtr(*i32, 16) };
1313 const slice = buf[0..3 :null];
1414 _ = slice;
1515 return error.TestFailed;
1616}
1717
1818// run
19// backend=stage1
19// backend=llvm
2020// target=native
test/cases/safety/slice slice sentinel mismatch.zig +3-3
......@@ -2,18 +2,18 @@ const std = @import("std");
22
33pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
44 _ = stack_trace;
5 if (std.mem.eql(u8, message, "sentinel mismatch")) {
5 if (std.mem.eql(u8, message, "sentinel mismatch: expected 0, found 4")) {
66 std.process.exit(0);
77 }
88 std.process.exit(1);
99}
1010pub fn main() !void {
11 var buf: [4]u8 = undefined;
11 var buf: [4]u8 = .{ 1, 2, 3, 4 };
1212 const slice = buf[0..];
1313 const slice2 = slice[0..3 :0];
1414 _ = slice2;
1515 return error.TestFailed;
1616}
1717// run
18// backend=stage1
18// backend=llvm
1919// target=native
test/cases/safety/slicing null C pointer runtime len.zig created+20
......@@ -0,0 +1,20 @@
1const std = @import("std");
2
3pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
4 _ = stack_trace;
5 if (std.mem.eql(u8, message, "attempt to use null value")) {
6 std.process.exit(0);
7 }
8 std.process.exit(1);
9}
10
11pub fn main() !void {
12 var ptr: [*c]const u32 = null;
13 var len: usize = 3;
14 var slice = ptr[0..len];
15 _ = slice;
16 return error.TestFailed;
17}
18// run
19// backend=llvm
20// target=native
\ No newline at end of file
test/cases/safety/slicing null C pointer.zig +5-3
......@@ -1,9 +1,11 @@
11const std = @import("std");
22
33pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
4 _ = message;
54 _ = stack_trace;
6 std.process.exit(0);
5 if (std.mem.eql(u8, message, "attempt to use null value")) {
6 std.process.exit(0);
7 }
8 std.process.exit(1);
79}
810
911pub fn main() !void {
......@@ -13,5 +15,5 @@ pub fn main() !void {
1315 return error.TestFailed;
1416}
1517// run
16// backend=stage1
18// backend=llvm
1719// target=native
\ No newline at end of file