[llvm] r194548 - Fix a null pointer dereference when copying a null polymorphic pointer.

Chandler Carruth chandlerc at gmail.com
Tue Nov 12 18:48:20 PST 2013


Author: chandlerc
Date: Tue Nov 12 20:48:20 2013
New Revision: 194548

URL: http://llvm.org/viewvc/llvm-project?rev=194548&view=rev
Log:
Fix a null pointer dereference when copying a null polymorphic pointer.

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.

Modified:
    llvm/trunk/include/llvm/ADT/polymorphic_ptr.h
    llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp

Modified: llvm/trunk/include/llvm/ADT/polymorphic_ptr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/polymorphic_ptr.h?rev=194548&r1=194547&r2=194548&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/polymorphic_ptr.h (original)
+++ llvm/trunk/include/llvm/ADT/polymorphic_ptr.h Tue Nov 12 20:48:20 2013
@@ -39,7 +39,7 @@ template <typename T> class polymorphic_
 
 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

Modified: llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp?rev=194548&r1=194547&r2=194548&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp (original)
+++ llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp Tue Nov 12 20:48:20 2013
@@ -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 {





More information about the llvm-commits mailing list