mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 07:03:11 +00:00
stage2: safety checks for slicing a null C pointer
This commit is contained in:
parent
d15bbebe2e
commit
01698528d1
14
src/Sema.zig
14
src/Sema.zig
@ -19964,6 +19964,14 @@ fn analyzeSlice(
|
||||
slice_ty = ptr_ptr_child_ty;
|
||||
array_ty = ptr_ptr_child_ty;
|
||||
elem_ty = ptr_ptr_child_ty.childType();
|
||||
|
||||
if (ptr_ptr_child_ty.ptrSize() == .C) {
|
||||
if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| {
|
||||
if (ptr_val.isNull()) {
|
||||
return sema.fail(block, ptr_src, "slice of null pointer", .{});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
.Slice => {
|
||||
ptr_sentinel = ptr_ptr_child_ty.sentinel();
|
||||
@ -20162,6 +20170,12 @@ fn analyzeSlice(
|
||||
|
||||
try sema.requireRuntimeBlock(block, src);
|
||||
if (block.wantSafety()) {
|
||||
// requirement: slicing C ptr is non-null
|
||||
if (ptr_ptr_child_ty.isCPtr()) {
|
||||
const is_non_null = try sema.analyzeIsNull(block, ptr_src, ptr, true);
|
||||
try sema.addSafetyCheck(block, is_non_null, .unwrap_null);
|
||||
}
|
||||
|
||||
// requirement: end <= len
|
||||
const opt_len_inst = if (array_ty.zigTypeTag() == .Array)
|
||||
try sema.addIntUnsigned(Type.usize, array_ty.arrayLenIncludingSentinel())
|
||||
|
||||
@ -233,6 +233,7 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
|
||||
|
||||
test "C pointer" {
|
||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||
|
||||
var buf: [*c]const u8 = "kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf";
|
||||
var len: u32 = 10;
|
||||
|
||||
10
test/compile_errors/stage2/slice_of_null_pointer.zig
Normal file
10
test/compile_errors/stage2/slice_of_null_pointer.zig
Normal file
@ -0,0 +1,10 @@
|
||||
comptime {
|
||||
var x: [*c]u8 = null;
|
||||
var runtime_len: usize = 0;
|
||||
var y = x[0..runtime_len];
|
||||
_ = y;
|
||||
}
|
||||
|
||||
// slice of null C pointer
|
||||
//
|
||||
// :4:14: error: slice of null pointer
|
||||
Loading…
x
Reference in New Issue
Block a user