From 8e7ad28a33d02c0dcb782c0312a8c80ca5d46d41 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 5 Feb 2021 19:46:31 +0100 Subject: [PATCH] AK: Avoid UB in TypedTransfer Don't be calling __builtin_memfoo() with null pointer arguments. Found by KUBSAN :^) --- AK/TypedTransfer.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/AK/TypedTransfer.h b/AK/TypedTransfer.h index 0e0de78e63..614b42afa9 100644 --- a/AK/TypedTransfer.h +++ b/AK/TypedTransfer.h @@ -35,6 +35,9 @@ class TypedTransfer { public: static size_t move(T* destination, T* source, size_t count) { + if (!count) + return 0; + if constexpr (Traits::is_trivial()) { __builtin_memmove(destination, source, count * sizeof(T)); return count; @@ -52,6 +55,9 @@ public: static size_t copy(T* destination, const T* source, size_t count) { + if (!count) + return 0; + if constexpr (Traits::is_trivial()) { __builtin_memmove(destination, source, count * sizeof(T)); return count; @@ -69,6 +75,9 @@ public: static bool compare(const T* a, const T* b, size_t count) { + if (!count) + return true; + if constexpr (Traits::is_trivial()) return !__builtin_memcmp(a, b, count * sizeof(T));