Skip to content

Commit 9116e26

Browse files
committed
Sema: add null check for implicit casts
1 parent 5605f6e commit 9116e26

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/Sema.zig

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,8 +1577,7 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize)
15771577

15781578
// st.index = 0;
15791579
const index_field_ptr = try sema.fieldPtr(&err_trace_block, src, st_ptr, "index", src, true);
1580-
const zero = try sema.addConstant(Type.usize, Value.zero);
1581-
try sema.storePtr2(&err_trace_block, src, index_field_ptr, src, zero, src, .store);
1580+
try sema.storePtr2(&err_trace_block, src, index_field_ptr, src, .zero_usize, src, .store);
15821581

15831582
// @errorReturnTrace() = &st;
15841583
_ = try err_trace_block.addUnOp(.set_err_return_trace, st_ptr);
@@ -17134,7 +17133,7 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1713417133
const is_aligned = try block.addBinOp(.cmp_eq, remainder, .zero_usize);
1713517134
const ok = if (ptr_ty.isSlice()) ok: {
1713617135
const len = try sema.analyzeSliceLen(block, ptr_src, ptr);
17137-
const len_zero = try block.addBinOp(.cmp_eq, len, try sema.addConstant(Type.usize, Value.zero));
17136+
const len_zero = try block.addBinOp(.cmp_eq, len, .zero_usize);
1713817137
break :ok try block.addBinOp(.bit_or, len_zero, is_aligned);
1713917138
} else is_aligned;
1714017139
try sema.addSafetyCheck(block, ok, .incorrect_alignment);
@@ -21957,7 +21956,6 @@ fn coerceExtra(
2195721956
.ok => {},
2195821957
else => break :src_c_ptr,
2195921958
}
21960-
// TODO add safety check for null pointer
2196121959
return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
2196221960
}
2196321961

@@ -24430,6 +24428,22 @@ fn coerceCompatiblePtrs(
2443024428
return sema.addConstant(dest_ty, val);
2443124429
}
2443224430
try sema.requireRuntimeBlock(block, inst_src, null);
24431+
const inst_ty = sema.typeOf(inst);
24432+
const inst_allows_zero = (inst_ty.zigTypeTag() == .Pointer and inst_ty.ptrAllowsZero()) or true;
24433+
if (block.wantSafety() and inst_allows_zero and !dest_ty.ptrAllowsZero()) {
24434+
const actual_ptr = if (inst_ty.isSlice())
24435+
try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty)
24436+
else
24437+
inst;
24438+
const ptr_int = try block.addUnOp(.ptrtoint, actual_ptr);
24439+
const is_non_zero = try block.addBinOp(.cmp_neq, ptr_int, .zero_usize);
24440+
const ok = if (inst_ty.isSlice()) ok: {
24441+
const len = try sema.analyzeSliceLen(block, inst_src, inst);
24442+
const len_zero = try block.addBinOp(.cmp_eq, len, .zero_usize);
24443+
break :ok try block.addBinOp(.bit_or, len_zero, is_non_zero);
24444+
} else is_non_zero;
24445+
try sema.addSafetyCheck(block, ok, .cast_to_null);
24446+
}
2443324447
return sema.bitCast(block, dest_ty, inst, inst_src);
2443424448
}
2443524449

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
const std = @import("std");
22

33
pub 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, "cast causes pointer to be null")) {
6+
std.process.exit(0);
7+
}
8+
std.process.exit(1);
79
}
10+
811
pub fn main() !void {
912
var c_ptr: [*c]u8 = 0;
1013
var zig_ptr: *u8 = c_ptr;
1114
_ = zig_ptr;
1215
return error.TestFailed;
1316
}
17+
1418
// run
15-
// backend=stage1
19+
// backend=llvm
1620
// target=native

test/cases/safety/slicing null C pointer runtime len.zig renamed to test/cases/safety/slicing null C pointer - runtime len.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ pub fn main() !void {
1717
}
1818
// run
1919
// backend=llvm
20-
// target=native
20+
// target=native

test/cases/safety/slicing null C pointer.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ pub fn main() !void {
1616
}
1717
// run
1818
// backend=llvm
19-
// target=native
19+
// target=native

0 commit comments

Comments
 (0)