From: Peter van Dijk Date: Fri, 21 Sep 2012 13:31:05 +0000 (+0000) Subject: make extract_bits more robust, limit to 8 bits (we only use it for 5 at a time anyway... X-Git-Tag: auth-3.2-rc1~167 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c8722b89bb3e9476d4424a21b04392a5313aae1c;p=thirdparty%2Fpdns.git make extract_bits more robust, limit to 8 bits (we only use it for 5 at a time anyway). bonus: this makes valgrind happy. git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2715 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/base32.cc b/pdns/base32.cc index 3655442e20..d20926d698 100644 --- a/pdns/base32.cc +++ b/pdns/base32.cc @@ -8,16 +8,20 @@ #include "namespaces.hh" /* based on freebsd:src/contrib/opie/libopie/btoe.c extract: get bit ranges from a char* */ -uint32_t extract_bits(const char *s, int start, int length) +unsigned char extract_bits(const char *s, int start, int length) { - uint32_t x; - unsigned char cl, cc, cr; + uint16_t x; + unsigned char cl, cc; + + if(!length) + return 0; cl = s[start / 8]; - cc = s[start / 8 + 1]; - cr = s[start / 8 + 2]; - x = ((uint32_t) (cl << 8 | cc) << 8 | cr); - x = x >> (24 - (length + (start % 8))); + if(start / 8 < (start + length-1)/8) + cc = s[start / 8 + 1]; + + x = (uint16_t) (cl << 8 | cc); + x = x >> (16 - (length + (start % 8))); x = (x & (0xffff >> (16 - length))); return (x); }