[llvm] r283387 - [Object] Fix a crash in Archive::child_iterator's default constructor.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 5 14:20:01 PDT 2016


Author: lhames
Date: Wed Oct  5 16:20:00 2016
New Revision: 283387

URL: http://llvm.org/viewvc/llvm-project?rev=283387&view=rev
Log:
[Object] Fix a crash in Archive::child_iterator's default constructor.

To be default constructible, Archive::child_iterator needs to be able to
construct an Archive::Child with a null parent, however Archive::Child's
constructor always dereferenced its Parent argument to compute the remaining
archive size. This commit fixes Archive::Child's constructor to only do the
size calculation when the parent is non-null.


Modified:
    llvm/trunk/include/llvm/Object/Archive.h
    llvm/trunk/lib/Object/Archive.cpp

Modified: llvm/trunk/include/llvm/Object/Archive.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Archive.h?rev=283387&r1=283386&r2=283387&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Archive.h (original)
+++ llvm/trunk/include/llvm/Object/Archive.h Wed Oct  5 16:20:00 2016
@@ -92,7 +92,7 @@ public:
     Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
 
     bool operator ==(const Child &other) const {
-      assert(Parent == other.Parent);
+      assert(!Parent || !other.Parent || Parent == other.Parent);
       return Data.begin() == other.Data.begin();
     }
 

Modified: llvm/trunk/lib/Object/Archive.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Archive.cpp?rev=283387&r1=283386&r2=283387&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Archive.cpp (original)
+++ llvm/trunk/lib/Object/Archive.cpp Wed Oct  5 16:20:00 2016
@@ -306,8 +306,11 @@ Archive::Child::Child(const Archive *Par
 }
 
 Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
-    : Parent(Parent), Header(Parent, Start, Parent->getData().size() -
-                             (Start - Parent->getData().data()), Err) {
+    : Parent(Parent),
+      Header(Parent, Start,
+             Parent
+               ? Parent->getData().size() - (Start - Parent->getData().data())
+               : 0, Err) {
   if (!Start)
     return;
 
@@ -441,7 +444,7 @@ Expected<Archive::Child> Archive::Child:
 
   // Check to see if this is at the end of the archive.
   if (NextLoc == Parent->Data.getBufferEnd())
-    return Child(Parent, nullptr, nullptr);
+    return Child(nullptr, nullptr, nullptr);
 
   // Check to see if this is past the end of the archive.
   if (NextLoc > Parent->Data.getBufferEnd()) {
@@ -768,7 +771,7 @@ Archive::child_iterator Archive::child_b
 }
 
 Archive::child_iterator Archive::child_end() const {
-  return child_iterator(Child(this, nullptr, nullptr), nullptr);
+  return child_iterator(Child(nullptr, nullptr, nullptr), nullptr);
 }
 
 StringRef Archive::Symbol::getName() const {




More information about the llvm-commits mailing list