From c8fcffbe6b492ed86a1ff2a9e7ba49deaf3a08f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= <rostydela@gmail.com>
Date: Tue, 12 Jan 2016 16:12:49 -0500
Subject: [PATCH] fix possible out of range error in Prefix ctor

---
 include/opendht/indexation/pht.h | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h
index aed1921c..ad405a3c 100644
--- a/include/opendht/indexation/pht.h
+++ b/include/opendht/indexation/pht.h
@@ -28,10 +28,13 @@ struct Prefix {
     Prefix() {}
     Prefix(InfoHash h) : size_(h.size() * 8), content_(h.begin(), h.end()) {}
     Prefix(const Blob& d) : size_(d.size()*8), content_(d) {}
-    Prefix(const Prefix& p, size_t first) : size_(first), content_(p.content_.begin(), p.content_.begin()+first/8) {
-       auto rem = first % 8;
-       if (rem)
-           content_.push_back(p.content_[first/8] & (0xFF << (7 - rem)));
+    Prefix(const Prefix& p, size_t first) :
+        size_(std::min(first, p.content_.size()*8)),
+        content_(Blob(p.content_.begin(), p.content_.begin()+size_/8))
+    {
+        auto rem = size_ % 8;
+        if (rem)
+            content_.push_back(p.content_[size_/8] & (0xFF << (7 - rem)));
     }
 
     Prefix getPrefix(ssize_t len) const {
@@ -50,12 +53,12 @@ struct Prefix {
      * @return The prefix of this sibling.
      */
     Prefix getSibling() const {
-       Prefix copy = *this;
-       if (size_) {
-          size_t last_bit =  (8 - size_) % 8;
-          copy.content_.back() ^= (1 << last_bit);
-       }
-       return copy;
+        Prefix copy = *this;
+        if (size_) {
+            size_t last_bit =  (8 - size_) % 8;
+            copy.content_.back() ^= (1 << last_bit);
+        }
+        return copy;
     }
 
     InfoHash hash() const {
-- 
GitLab