Fix some places where we could call memmove(null,xxx,0) - which is UB
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@238831 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/algorithm b/include/algorithm
index 76f59f2..459071f 100644
--- a/include/algorithm
+++ b/include/algorithm
@@ -1763,7 +1763,8 @@
__copy(_Tp* __first, _Tp* __last, _Up* __result)
{
const size_t __n = static_cast<size_t>(__last - __first);
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
+ if (__n > 0)
+ _VSTD::memmove(__result, __first, __n * sizeof(_Up));
return __result + __n;
}
@@ -1798,8 +1799,11 @@
__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
{
const size_t __n = static_cast<size_t>(__last - __first);
- __result -= __n;
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
+ if (__n > 0)
+ {
+ __result -= __n;
+ _VSTD::memmove(__result, __first, __n * sizeof(_Up));
+ }
return __result;
}
@@ -1896,7 +1900,8 @@
__move(_Tp* __first, _Tp* __last, _Up* __result)
{
const size_t __n = static_cast<size_t>(__last - __first);
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
+ if (__n > 0)
+ _VSTD::memmove(__result, __first, __n * sizeof(_Up));
return __result + __n;
}
@@ -1931,8 +1936,11 @@
__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
{
const size_t __n = static_cast<size_t>(__last - __first);
- __result -= __n;
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
+ if (__n > 0)
+ {
+ __result -= __n;
+ _VSTD::memmove(__result, __first, __n * sizeof(_Up));
+ }
return __result;
}