summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2013-11-13 02:48:20 +0000
committerChandler Carruth <chandlerc@gmail.com>2013-11-13 02:48:20 +0000
commit3c3f6be0c8b8d6b38e219652580e2edef0f0a757 (patch)
tree90930a40de36fc3d13e339a3b9962d6c3a5383ab
parent29f1788de96cbf88ab87e3da130cf626b2e8e029 (diff)
downloadllvm-3c3f6be0c8b8d6b38e219652580e2edef0f0a757.tar.gz
llvm-3c3f6be0c8b8d6b38e219652580e2edef0f0a757.tar.bz2
llvm-3c3f6be0c8b8d6b38e219652580e2edef0f0a757.tar.xz
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194548 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/ADT/polymorphic_ptr.h2
-rw-r--r--unittests/ADT/polymorphic_ptr_test.cpp6
2 files changed, 7 insertions, 1 deletions
diff --git a/include/llvm/ADT/polymorphic_ptr.h b/include/llvm/ADT/polymorphic_ptr.h
index a1687474ef..b8d8d71238 100644
--- a/include/llvm/ADT/polymorphic_ptr.h
+++ b/include/llvm/ADT/polymorphic_ptr.h
@@ -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
diff --git a/unittests/ADT/polymorphic_ptr_test.cpp b/unittests/ADT/polymorphic_ptr_test.cpp
index fbe60df523..bd5d83879a 100644
--- a/unittests/ADT/polymorphic_ptr_test.cpp
+++ b/unittests/ADT/polymorphic_ptr_test.cpp
@@ -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 {