From 210448b6ed8fe9768bd5e9ad5e02ab0cc058e826 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Thu, 6 Jul 2023 23:43:00 +0200 Subject: [PATCH] LibC: Relax `memmove()` to `memcpy()` in more cases `memcpy()` and `memmove()` are functionally equivalent if the source and destination memory regions do not overlap. If this is the case, we should prefer `memcpy()` as it's implemented in a simpler and faster way. As our `memcpy()` starts copying from the lowest address first, relaxing a `memmove()` call to `memcpy()` is safe if the destination: - starts at a lower address than the source - starts at a higher address than the source's end --- Userland/Libraries/LibC/string.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Userland/Libraries/LibC/string.cpp b/Userland/Libraries/LibC/string.cpp index 24716107b1..88f57bb8f7 100644 --- a/Userland/Libraries/LibC/string.cpp +++ b/Userland/Libraries/LibC/string.cpp @@ -167,6 +167,8 @@ void* memset(void* dest_ptr, int c, size_t n) // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memmove.html void* memmove(void* dest, void const* src, size_t n) { + if (dest < src) + return memcpy(dest, src, n); if (((FlatPtr)dest - (FlatPtr)src) >= n) return memcpy(dest, src, n);