Skip to content
Snippets Groups Projects
Commit 5cb5ab01 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

routing table: fix invalid iterator access

When calling findClosestNodes with an key referencing the first bucket,
the itp iterator could point to before-begin() and dereference it.
parent 42fa09a6
No related branches found
No related tags found
No related merge requests found
...@@ -80,7 +80,8 @@ RoutingTable::depth(const RoutingTable::const_iterator& it) const ...@@ -80,7 +80,8 @@ RoutingTable::depth(const RoutingTable::const_iterator& it) const
std::vector<Sp<Node>> std::vector<Sp<Node>>
RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) const 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); auto bucket = findBucket(id);
if (bucket == end()) { return nodes; } if (bucket == end()) { return nodes; }
...@@ -100,7 +101,7 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) ...@@ -100,7 +101,7 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count)
}; };
auto itn = bucket; auto itn = bucket;
auto itp = std::prev(bucket); auto itp = (bucket == begin()) ? end() : std::prev(bucket);
while (nodes.size() < count && (itn != end() || itp != end())) { while (nodes.size() < count && (itn != end() || itp != end())) {
if (itn != end()) { if (itn != end()) {
sortedBucketInsert(*itn); sortedBucketInsert(*itn);
...@@ -108,11 +109,7 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) ...@@ -108,11 +109,7 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count)
} }
if (itp != end()) { if (itp != end()) {
sortedBucketInsert(*itp); sortedBucketInsert(*itp);
if (itp == begin()) { itp = (itp == begin()) ? end() : std::prev(itp);
itp = end();
continue;
}
itp = std::prev(itp);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment