Skip to content

Commit

Permalink
routing table: fix invalid iterator access
Browse files Browse the repository at this point in the history
When calling findClosestNodes with an key referencing the first bucket,
the itp iterator could point to before-begin() and dereference it.
  • Loading branch information
aberaud committed Jun 29, 2017
1 parent 42fa09a commit 5cb5ab0
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/routing_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ RoutingTable::depth(const RoutingTable::const_iterator& it) const
std::vector<Sp<Node>>
RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) const
{
std::vector<Sp<Node>> nodes {};
std::vector<Sp<Node>> nodes;
nodes.reserve(count);
auto bucket = findBucket(id);

if (bucket == end()) { return nodes; }
Expand All @@ -100,19 +101,15 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count)
};

auto itn = bucket;
auto itp = std::prev(bucket);
auto itp = (bucket == begin()) ? end() : std::prev(bucket);
while (nodes.size() < count && (itn != end() || itp != end())) {
if (itn != end()) {
sortedBucketInsert(*itn);
itn = std::next(itn);
}
if (itp != end()) {
sortedBucketInsert(*itp);
if (itp == begin()) {
itp = end();
continue;
}
itp = std::prev(itp);
itp = (itp == begin()) ? end() : std::prev(itp);
}
}

Expand Down

0 comments on commit 5cb5ab0

Please sign in to comment.