From adb3b226091f23aff794f08e861b5a0fd1bbde1c Mon Sep 17 00:00:00 2001 From: akallabeth Date: Tue, 13 Dec 2022 13:16:59 +0100 Subject: [PATCH] Revert "fix xcrush-divideByZero (when src ==dst)" This reverts commit 85e830d16f24ae6aa0af6291cca951bcd969b7bd. --- libfreerdp/codec/xcrush.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/libfreerdp/codec/xcrush.c b/libfreerdp/codec/xcrush.c index e92472178..684cfca6a 100644 --- a/libfreerdp/codec/xcrush.c +++ b/libfreerdp/codec/xcrush.c @@ -754,10 +754,31 @@ static int xcrush_generate_output(XCRUSH_CONTEXT* xcrush, BYTE* OutputBuffer, UI static INLINE size_t xcrush_copy_bytes(BYTE* dst, const BYTE* src, size_t num) { + size_t diff, rest, end, a; + WINPR_ASSERT(dst); WINPR_ASSERT(src); - memmove(dst, src, num); + if (src + num < dst || src > dst + num) + { + memcpy(dst, src, num); + } + else + { + // src and dst overlaps + // we should copy the area that doesn't overlap repeatly + diff = (dst > src) ? dst - src : src - dst; + rest = num % diff; + end = num - rest; + for (a = 0; a < end; a += diff) + { + memcpy(&dst[a], &src[a], diff); + } + + if (rest != 0) + memcpy(&dst[end], &src[end], rest); + } + return num; }