From 708b86b5f9da418ac7d64d44a09ea5d3b389b354 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 2 Jun 2015 13:52:16 +0000 Subject: [PATCH] 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 --- include/algorithm | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/include/algorithm b/include/algorithm index 76f59f240..459071f09 100644 --- a/include/algorithm +++ b/include/algorithm @@ -1763,7 +1763,8 @@ typename enable_if __copy(_Tp* __first, _Tp* __last, _Up* __result) { const size_t __n = static_cast(__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 @@ typename enable_if __copy_backward(_Tp* __first, _Tp* __last, _Up* __result) { const size_t __n = static_cast(__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 @@ typename enable_if __move(_Tp* __first, _Tp* __last, _Up* __result) { const size_t __n = static_cast(__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 @@ typename enable_if __move_backward(_Tp* __first, _Tp* __last, _Up* __result) { const size_t __n = static_cast(__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; }