Skip to content

Commit

Permalink
[Object] Fix a crash in Archive::child_iterator's default constructor.
Browse files Browse the repository at this point in the history
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.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283387 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
lhames committed Oct 5, 2016
1 parent 8c96de4 commit 1911873
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/llvm/Object/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Archive : public Binary {
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();
}

Expand Down
11 changes: 7 additions & 4 deletions lib/Object/Archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,11 @@ Archive::Child::Child(const Archive *Parent, StringRef Data,
}

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;

Expand Down Expand Up @@ -441,7 +444,7 @@ Expected<Archive::Child> Archive::Child::getNext() const {

// 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()) {
Expand Down Expand Up @@ -768,7 +771,7 @@ Archive::child_iterator Archive::child_begin(Error &Err,
}

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 {
Expand Down

0 comments on commit 1911873

Please sign in to comment.