[libcxx] r238831 - Fix some places where we could call memmove(null, xxx, 0) - which is UB

Marshall Clow mclow.lists at gmail.com
Tue Jun 2 06:52:16 PDT 2015


Author: marshall
Date: Tue Jun  2 08:52:16 2015
New Revision: 238831

URL: http://llvm.org/viewvc/llvm-project?rev=238831&view=rev
Log:
Fix some places where we could call memmove(null,xxx,0) - which is UB

Modified:
    libcxx/trunk/include/algorithm

Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=238831&r1=238830&r2=238831&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Tue Jun  2 08:52:16 2015
@@ -1763,7 +1763,8 @@ typename enable_if
 __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 @@ typename enable_if
 __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 @@ typename enable_if
 __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 @@ typename enable_if
 __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;
 }
 





More information about the cfe-commits mailing list