Skip to content

Commit

Permalink
Sanitize FileEntries iterator behaviour
Browse files Browse the repository at this point in the history
Only fetch data from iterator when using operator *, or when operator++
if we have not already fetched.
  • Loading branch information
peadar committed May 8, 2024
1 parent e804f26 commit 914c6e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 5 additions & 3 deletions dead.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ CoreProcess::getPID() const
FileEntries::iterator::iterator(const FileEntries &entries, ReaderArray<FileEntry>::iterator pos)
: entries(entries)
, entriesIterator(pos) {
fetch();
}

struct NamedEntry {
Expand All @@ -205,14 +204,17 @@ struct NamedEntry {

void
FileEntries::iterator::fetch() {
if (entries.names)
if (!fetched && entries.names) {
cur = std::make_pair(entries.names->readString(nameoff), *entriesIterator);
fetched = true;
}
}

FileEntries::iterator &FileEntries::iterator::operator++() {
fetch();
++entriesIterator;
nameoff += cur.first.size() + 1;
fetch();
fetched = false;
return *this;
}

Expand Down
12 changes: 9 additions & 3 deletions libpstack/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,17 @@ class FileEntries {

public:
class iterator {
friend class FileEntries;
const FileEntries &entries;
void fetch();
bool fetched = false;
size_t nameoff = 0;
std::pair<std::string, FileEntry> cur;
ReaderArray<FileEntry>::iterator entriesIterator;
public:
iterator(const FileEntries &entries, ReaderArray<FileEntry>::iterator start);
iterator &operator++();
std::pair<std::string, FileEntry> operator *() { return cur; }
std::pair<std::string, FileEntry> operator *() { fetch(); return cur; }
bool operator != (const iterator &rhs) const { return entriesIterator != rhs.entriesIterator; }
};
FileEntries(const Elf::Object &obj) {
Expand All @@ -461,8 +463,12 @@ class FileEntries {
entries = std::make_shared<NullReader>();
entriesArray = std::make_unique<ReaderArray<FileEntry>>(*entries);
}
iterator begin() const { return iterator(*this, entriesArray->begin()); }
iterator end() const { return iterator(*this, entriesArray->end()); }
iterator begin() const {
return iterator(*this, entriesArray->begin());
}
iterator end() const {
return iterator(*this, entriesArray->end());
}
};


Expand Down

0 comments on commit 914c6e7

Please sign in to comment.