From c7946415abcd9c7a86ecde867d44cb50a249c349 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 10 Sep 2013 08:34:29 +0800 Subject: [PATCH] Usermode/libc - Minor fix to memmove --- Usermode/Libraries/libc.so_src/heap.c | 2 +- Usermode/Libraries/libc.so_src/string.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Usermode/Libraries/libc.so_src/heap.c b/Usermode/Libraries/libc.so_src/heap.c index 3cbd0b53..9b6799b0 100644 --- a/Usermode/Libraries/libc.so_src/heap.c +++ b/Usermode/Libraries/libc.so_src/heap.c @@ -177,7 +177,7 @@ EXPORT void *calloc(size_t __nmemb, size_t __size) EXPORT void free(void *mem) { heap_head *head = (void*)((intptr_t)mem-sizeof(heap_head)); - + // Sanity please! if(!mem) return; diff --git a/Usermode/Libraries/libc.so_src/string.c b/Usermode/Libraries/libc.so_src/string.c index 2ab271f5..6ee6d6ca 100644 --- a/Usermode/Libraries/libc.so_src/string.c +++ b/Usermode/Libraries/libc.so_src/string.c @@ -282,14 +282,23 @@ EXPORT void *memcpy(void *__dest, const void *__src, size_t count) */ EXPORT void *memmove(void *dest, const void *src, size_t count) { - char *sp = (char *)src; + const char *sp = (const char *)src; char *dp = (char *)dest; // Check if the areas overlap - if( (uintptr_t)src < (uintptr_t)dest && (uintptr_t)dest < (uintptr_t)src+count ) - for(;count--;) - dp[count] = sp[count]; - else + if( sp >= dp+count ) + memcpy(dest, src, count); + else if( dp >= sp+count ) memcpy(dest, src, count); + else { + if( sp < dp ) { + while(count--) + dp[count] = sp[count]; + } + else { + while(count--) + *dp++ = *sp++; + } + } return dest; } -- 2.20.1