From 0dccae68e62550009268c64d344aa0e3b1c12a86 Mon Sep 17 00:00:00 2001 From: Adrien Beraud <adrien.beraud@savoirfairelinux.com> Date: Fri, 3 Nov 2017 16:03:11 -0400 Subject: [PATCH] InfoHash: make false in case of parsing error --- include/opendht/infohash.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index 5acc96f8..e62193f0 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -288,10 +288,8 @@ std::istream& operator>> (std::istream& s, Hash<N>& h) template <size_t N> Hash<N>::Hash(const std::string& hex) { - if (hex.empty()) + if (hex.size() < 2*N) data_.fill(0); - else if (hex.size() < 2*N) - throw std::invalid_argument("string not long enough"); else fromString(hex.c_str()); } @@ -305,8 +303,11 @@ Hash<N>::fromString(const char* in) { else if (c >= '0' and c <= '9') return c - '0'; else throw std::domain_error("not an hex character"); }; - for (size_t i=0; i<N; i++) { - data_[i] = (hex2bin(in[2*i]) << 4) | hex2bin(in[2*i+1]); + try { + for (size_t i=0; i<N; i++) + data_[i] = (hex2bin(in[2*i]) << 4) | hex2bin(in[2*i+1]); + } catch (const std::domain_error&) { + data_.fill(0); } } -- GitLab