From 04632a8288ebc652ddca9708c3b1382e5173314f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= <rostydela@gmail.com>
Date: Tue, 15 Dec 2015 12:54:13 -0500
Subject: [PATCH] fix findclosestnodes() (was inverted)...

---
 src/dht.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/dht.cpp b/src/dht.cpp
index e366cc81..0b16eafe 100644
--- a/src/dht.cpp
+++ b/src/dht.cpp
@@ -313,7 +313,7 @@ Dht::RoutingTable::findClosestNodes(const InfoHash id, size_t count) const {
         for (auto n : b.nodes) {
             auto here = std::find_if(nodes.begin(), nodes.end(),
                 [&id,&n](std::shared_ptr<Node> &node) {
-                    return id.xorCmp(node->id, n->id) < 0;
+                    return id.xorCmp(n->id, node->id) < 0;
                 }
             );
             nodes.insert(here, n);
@@ -338,7 +338,9 @@ Dht::RoutingTable::findClosestNodes(const InfoHash id, size_t count) const {
     }
 
     // shrink to the count closest nodes.
-    nodes.resize(count);
+    if (nodes.size() > count) {
+        nodes.resize(count);
+    }
     return nodes;
 }
 
@@ -2381,8 +2383,6 @@ Dht::processMessage(const uint8_t *buf, size_t buflen, const sockaddr *from, soc
     uint16_t ttid = 0;
 
     switch (msg.type) {
-    //TODO: handle case where put was made to node claiming to be outside of a
-    // valid range.
     case MessageType::Error:
         if (msg.tid.length != 4) return;
         if (msg.error_code == 401 && msg.id != zeroes && (msg.tid.matches(TransPrefix::ANNOUNCE_VALUES, &ttid) || msg.tid.matches(TransPrefix::LISTEN, &ttid))) {
-- 
GitLab