make extract_bits slightly more robust; document limitations (thanks Maik)
authorPeter van Dijk <peter.van.dijk@netherlabs.nl>
Fri, 21 Sep 2012 13:44:20 +0000 (13:44 +0000)
committerPeter van Dijk <peter.van.dijk@netherlabs.nl>
Fri, 21 Sep 2012 13:44:20 +0000 (13:44 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2716 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/base32.cc

index d20926d69820cf71ce9321f2b2278d866ac2b73e..5c96cc2ff154fbbc35db80dcbf9ebaf6de4ac262 100644 (file)
@@ -8,6 +8,7 @@
 #include "namespaces.hh"
 
 /* based on freebsd:src/contrib/opie/libopie/btoe.c extract: get bit ranges from a char* */
+/* NOTE: length should not exceed 8; all callers inside PowerDNS only pass length=5 though */
 unsigned char extract_bits(const char *s, int start, int length)
 {
   uint16_t x;
@@ -19,6 +20,8 @@ unsigned char extract_bits(const char *s, int start, int length)
   cl = s[start / 8];
   if(start / 8 < (start + length-1)/8)
     cc = s[start / 8 + 1];
+  else
+    cc = 0;
 
   x = (uint16_t) (cl << 8 | cc);
   x = x >> (16 - (length + (start % 8)));