Permalink
Browse files

InfoHash: throw in case of invalid input string

  • Loading branch information...
aberaud committed Nov 3, 2017
1 parent bf4e838 commit 92320cef59068bd1f64f485f73932f370b9baaac
Showing with 10 additions and 8 deletions.
  1. +9 −7 include/opendht/infohash.h
  2. +1 −1 python/opendht.pyx
View
@@ -288,20 +288,22 @@ std::istream& operator>> (std::istream& s, Hash<N>& h)
template <size_t N>
Hash<N>::Hash(const std::string& hex) {
if (hex.size() < 2*N) {
if (hex.empty())
data_.fill(0);
return;
}
fromString(hex.c_str());
else if (hex.size() < 2*N)
throw std::invalid_argument("string not long enough");
else
fromString(hex.c_str());
}
template <size_t N>
void
Hash<N>::fromString(const char* in) {
auto hex2bin = [](char c) -> uint8_t {
if (c >= 'a') return 10 + c - 'a';
else if (c >= 'A') return 10 + c - 'A';
else return c - '0';
if (c >= 'a' and c <= 'f') return 10 + c - 'a';
else if (c >= 'A' and c <= 'F') return 10 + c - 'A';
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]);
View
@@ -92,7 +92,7 @@ cdef class _WithID(object):
cdef class InfoHash(_WithID):
cdef cpp.InfoHash _infohash
def __cinit__(self, bytes str=b''):
self._infohash = cpp.InfoHash(str)
self._infohash = cpp.InfoHash(str) if str else cpp.InfoHash()
def __bool__(InfoHash self):
return <bool>self._infohash
def __richcmp__(InfoHash self, InfoHash other, int op):

0 comments on commit 92320ce

Please sign in to comment.