Merge "memmove: Don't call memcpy if regions overlap"
diff --git a/libc/string/memmove.c b/libc/string/memmove.c
index 072104b..fb1d975 100644
--- a/libc/string/memmove.c
+++ b/libc/string/memmove.c
@@ -32,10 +32,11 @@
{
const char *p = src;
char *q = dst;
- /* We can use the optimized memcpy if the destination is below the
- * source (i.e. q < p), or if it is completely over it (i.e. q >= p+n).
+ /* We can use the optimized memcpy if the source and destination
+ * don't overlap.
*/
- if (__builtin_expect((q < p) || ((size_t)(q - p) >= n), 1)) {
+ if (__builtin_expect(((q < p) && ((size_t)(p - q) >= n))
+ || ((p < q) && ((size_t)(q - p) >= n)), 1)) {
return memcpy(dst, src, n);
} else {
bcopy(src, dst, n);