[codec,xcrush] fix possible div by zero
If source and destination buffers are equal the calculation of
'rest = num % div' is a division by zero. Avoid that by checking
explicitly for that condition.
@@ -754,26 +754,21 @@ 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);
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)
- {
+ const size_t diff = (dst > src) ? dst - src : src - dst;
+ const size_t rest = (diff > 0) ? num % diff : 0;
+ const size_t end = num - rest;
+
+ for (size_t a = 0; a < end; a += diff)
memcpy(&dst[a], &src[a], diff);
- }
if (rest != 0)
memcpy(&dst[end], &src[end], rest);
