authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-20 18:19:01-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-20 18:28:59-05:00
log8d73703d524e06e17320e025ff970c86ebc01d22
tree1c9eae2163aa25df4380c6b6690933f5cec62ca1
parent8918cb06fca10309dc67ac881894528eac33a8fc
signaturelock-open Commit is signed but in an unrecognized format.

fix safety for sentinel-slicing floats


2 files changed, 34 insertions(+), 1 deletions(-)

src/codegen.cpp+6-1
...@@ -1425,7 +1425,12 @@ static void add_sentinel_check(CodeGen *g, LLVMValueRef sentinel_elem_ptr, ZigVa...@@ -1425,7 +1425,12 @@ static void add_sentinel_check(CodeGen *g, LLVMValueRef sentinel_elem_ptr, ZigVa
1425 LLVMValueRef expected_sentinel = gen_const_val(g, sentinel, "");1425 LLVMValueRef expected_sentinel = gen_const_val(g, sentinel, "");
14261426
1427 LLVMValueRef actual_sentinel = gen_load_untyped(g, sentinel_elem_ptr, 0, false, "");1427 LLVMValueRef actual_sentinel = gen_load_untyped(g, sentinel_elem_ptr, 0, false, "");
1428 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, actual_sentinel, expected_sentinel, "");1428 LLVMValueRef ok_bit;
1429 if (sentinel->type->id == ZigTypeIdFloat) {
1430 ok_bit = LLVMBuildFCmp(g->builder, LLVMRealOEQ, actual_sentinel, expected_sentinel, "");
1431 } else {
1432 ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, actual_sentinel, expected_sentinel, "");
1433 }
14291434
1430 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SentinelFail");1435 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SentinelFail");
1431 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SentinelOk");1436 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SentinelOk");
test/runtime_safety.zig+28
...@@ -1,6 +1,34 @@...@@ -1,6 +1,34 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompareOutputContext) void {3pub fn addCases(cases: *tests.CompareOutputContext) void {
4 cases.addRuntimeSafety("slice sentinel mismatch - optional pointers",
5 \\const std = @import("std");
6 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
7 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
8 \\ std.process.exit(126); // good
9 \\ }
10 \\ std.process.exit(0); // test failed
11 \\}
12 \\pub fn main() void {
13 \\ var buf: [4]?*i32 = undefined;
14 \\ const slice = buf[0..3 :null];
15 \\}
16 );
17
18 cases.addRuntimeSafety("slice sentinel mismatch - floats",
19 \\const std = @import("std");
20 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
21 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
22 \\ std.process.exit(126); // good
23 \\ }
24 \\ std.process.exit(0); // test failed
25 \\}
26 \\pub fn main() void {
27 \\ var buf: [4]f32 = undefined;
28 \\ const slice = buf[0..3 :1.2];
29 \\}
30 );
31
4 cases.addRuntimeSafety("pointer slice sentinel mismatch",32 cases.addRuntimeSafety("pointer slice sentinel mismatch",
5 \\const std = @import("std");33 \\const std = @import("std");
6 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {34 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {