From e47b754b28d658f05b72811207b9f953c1ab83e9 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 5 Mar 2021 22:08:34 +0100 Subject: [PATCH] std: Prevent null pointer deref in mem.len{,Z} Closes #8140 --- lib/std/mem.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 581fb16e6c..2bd5fdac7b 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -647,7 +647,10 @@ pub fn len(value: anytype) usize { indexOfSentinel(info.child, sentinel, value) else @compileError("length of pointer with no sentinel"), - .C => indexOfSentinel(info.child, 0, value), + .C => { + assert(value != null); + return indexOfSentinel(info.child, 0, value); + }, .Slice => value.len, }, .Struct => |info| if (info.is_tuple) { @@ -708,7 +711,10 @@ pub fn lenZ(ptr: anytype) usize { indexOfSentinel(info.child, sentinel, ptr) else @compileError("length of pointer with no sentinel"), - .C => indexOfSentinel(info.child, 0, ptr), + .C => { + assert(ptr != null); + return indexOfSentinel(info.child, 0, ptr); + }, .Slice => if (info.sentinel) |sentinel| indexOfSentinel(info.child, sentinel, ptr.ptr) else