[libcxx] r238666 - Don't try to memcpy zero bytes; sometimes the source pointer is NULL, and that's UB. Thanks to Nuno Lopes for the catch.

Marshall Clow mclow.lists at gmail.com
Sat May 30 20:13:31 PDT 2015


Author: marshall
Date: Sat May 30 22:13:31 2015
New Revision: 238666

URL: http://llvm.org/viewvc/llvm-project?rev=238666&view=rev
Log:
Don't try to memcpy zero bytes; sometimes the source pointer is NULL, and that's UB. Thanks to Nuno Lopes for the catch.

Modified:
    libcxx/trunk/include/memory

Modified: libcxx/trunk/include/memory
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/memory?rev=238666&r1=238665&r2=238666&view=diff
==============================================================================
--- libcxx/trunk/include/memory (original)
+++ libcxx/trunk/include/memory Sat May 30 22:13:31 2015
@@ -621,6 +621,8 @@ void* align(size_t alignment, size_t siz
 #pragma GCC system_header
 #endif
 
+extern "C" int printf(const char * __restrict, ...);
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 // addressof moved to <__functional_base>
@@ -1521,7 +1523,8 @@ struct _LIBCPP_TYPE_VIS_ONLY allocator_t
         __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
         {
             ptrdiff_t _Np = __end1 - __begin1;
-            _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
+            if (_Np > 0)
+                _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
             __begin2 += _Np;
         }
 
@@ -1549,7 +1552,8 @@ struct _LIBCPP_TYPE_VIS_ONLY allocator_t
         {
             typedef typename remove_const<_Tp>::type _Vp;
             ptrdiff_t _Np = __end1 - __begin1;
-            _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
+            if (_Np > 0)
+                _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
             __begin2 += _Np;
         }
 
@@ -1580,7 +1584,8 @@ struct _LIBCPP_TYPE_VIS_ONLY allocator_t
         {
             ptrdiff_t _Np = __end1 - __begin1;
             __end2 -= _Np;
-            _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
+            if (_Np > 0)
+                _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
         }
 
 private:





More information about the cfe-commits mailing list