[UB] Fix a nasty place where we would pass null pointers to memcpy.
[oota-llvm.git] / include / llvm / ADT / SmallVector.h
index 5b208b76a21fa8c465b2b7645c16b649c8e4000e..b9384702c3ba0588d3a402e677a9805d4c471729 100644 (file)
@@ -315,8 +315,10 @@ protected:
                                            T2>::value>::type * = nullptr) {
     // Use memcpy for PODs iterated by pointers (which includes SmallVector
     // iterators): std::uninitialized_copy optimizes to memmove, but we can
-    // use memcpy here.
-    memcpy(Dest, I, (E-I)*sizeof(T));
+    // use memcpy here. Note that I and E are iterators and thus might be
+    // invalid for memcpy if they are equal.
+    if (I != E)
+      memcpy(Dest, I, (E - I) * sizeof(T));
   }
 
   /// Double the size of the allocated memory, guaranteeing space for at