authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-03-05 22:08:34+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-07 14:58:45+02:00
loge47b754b28d658f05b72811207b9f953c1ab83e9
tree2f435ce3ea43c4d2f55a39cb660efef42807a28c
parent6d69a29d753de8680ff1220b8d9127e890e7d965

std: Prevent null pointer deref in mem.len{,Z}

Closes #8140

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

lib/std/mem.zig+8-2
...@@ -647,7 +647,10 @@ pub fn len(value: anytype) usize {...@@ -647,7 +647,10 @@ pub fn len(value: anytype) usize {
647 indexOfSentinel(info.child, sentinel, value)647 indexOfSentinel(info.child, sentinel, value)
648 else648 else
649 @compileError("length of pointer with no sentinel"),649 @compileError("length of pointer with no sentinel"),
650 .C => indexOfSentinel(info.child, 0, value),650 .C => {
651 assert(value != null);
652 return indexOfSentinel(info.child, 0, value);
653 },
651 .Slice => value.len,654 .Slice => value.len,
652 },655 },
653 .Struct => |info| if (info.is_tuple) {656 .Struct => |info| if (info.is_tuple) {
...@@ -708,7 +711,10 @@ pub fn lenZ(ptr: anytype) usize {...@@ -708,7 +711,10 @@ pub fn lenZ(ptr: anytype) usize {
708 indexOfSentinel(info.child, sentinel, ptr)711 indexOfSentinel(info.child, sentinel, ptr)
709 else712 else
710 @compileError("length of pointer with no sentinel"),713 @compileError("length of pointer with no sentinel"),
711 .C => indexOfSentinel(info.child, 0, ptr),714 .C => {
715 assert(ptr != null);
716 return indexOfSentinel(info.child, 0, ptr);
717 },
712 .Slice => if (info.sentinel) |sentinel|718 .Slice => if (info.sentinel) |sentinel|
713 indexOfSentinel(info.child, sentinel, ptr.ptr)719 indexOfSentinel(info.child, sentinel, ptr.ptr)
714 else720 else