Fix a null pointer dereference when copying a null polymorphic pointer.
authorChandler Carruth <chandlerc@gmail.com>
Wed, 13 Nov 2013 02:48:20 +0000 (02:48 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Wed, 13 Nov 2013 02:48:20 +0000 (02:48 +0000)
This bug only bit the C++98 build bots because all of the actual uses
really do move. ;] But not *quite* ready to do the whole C++11 switch
yet, so clean it up. Also add a unit test that catches this immediately.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194548 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/polymorphic_ptr.h
unittests/ADT/polymorphic_ptr_test.cpp

index a1687474ef41c5c2a7ab636b40ee78ac36d26e32..b8d8d71238e3c26a2a0d0386dc9327bd62739044 100644 (file)
@@ -39,7 +39,7 @@ template <typename T> class polymorphic_ptr {
 
 public:
   polymorphic_ptr(T *ptr = 0) : ptr(ptr) {}
-  polymorphic_ptr(const polymorphic_ptr &arg) : ptr(arg->clone()) {}
+  polymorphic_ptr(const polymorphic_ptr &arg) : ptr(arg ? arg->clone() : 0) {}
 #if LLVM_HAS_RVALUE_REFERENCES
   polymorphic_ptr(polymorphic_ptr &&arg) : ptr(arg.take()) {}
 #endif
index fbe60df523c23368b801b82d50266b490449be78..bd5d83879a83fa5cb419f83808e5bbf01c7523f7 100644 (file)
@@ -82,6 +82,12 @@ TEST(polymorphic_ptr_test, Basic) {
   EXPECT_FALSE(!p3);
   EXPECT_NE(s, &*p3);
   EXPECT_EQ(42, p3->x);
+
+  // Force copies of null without trying to dereference anything.
+  polymorphic_ptr<S> null_copy = dummy_copy(polymorphic_ptr<S>(null));
+  EXPECT_FALSE((bool)null_copy);
+  EXPECT_TRUE(!null_copy);
+  EXPECT_EQ(null, null_copy);
 }
 
 struct Base {