[svn] commit: r1315 - in /trunk/src/lib/dns: hex.cc tests/hex_unittest.cc

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Mar 11 00:52:45 UTC 2010


Author: each
Date: Thu Mar 11 00:52:45 2010
New Revision: 1315

Log:
spaces in hex input were not being ignored, causing incorrect values

Modified:
    trunk/src/lib/dns/hex.cc
    trunk/src/lib/dns/tests/hex_unittest.cc

Modified: trunk/src/lib/dns/hex.cc
==============================================================================
--- trunk/src/lib/dns/hex.cc (original)
+++ trunk/src/lib/dns/hex.cc Thu Mar 11 00:52:45 2010
@@ -55,24 +55,45 @@
 void
 decodeHex(const std::string& hex, std::vector<uint8_t>& result)
 {
+    ostringstream comp;
+
+    // compress input by removing whitespace
+    size_t len = hex.length();
+    for (int i = 0; i < len; i++) {
+        char c = hex.at(i);
+        if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
+            continue;
+        }
+        comp << c;
+    }
+
+    istringstream iss(comp.str());
     result.clear();
-    std::istringstream iss(hex);
     char c1, c2;
     uint8_t n;
 
     iss.width(1);
-    if ((hex.size() % 2) == 1) {
+    if ((comp.str().length() % 2) == 1) {
+        c2 = '0';
         iss >> c2;
+
         const char* pos = strchr(hexdigits, toupper(c2));
         if (!pos) {
             isc_throw (BadHexString, "Invalid hex digit");
         }
 
-        n = pos - hexdigits;
-        result.push_back(n);
+        if (!iss.eof() && !iss.bad() && !iss.fail()) {
+            n = pos - hexdigits;
+            result.push_back(n);
+        }
     }
     while (!iss.eof()) {
+        c1 = c2 = '0';
         iss >> c1 >> c2;;
+
+        if (iss.eof() || iss.fail() || iss.bad()) {
+            break;
+        }
 
         const char* pos1 = strchr(hexdigits, toupper(c1));
         const char* pos2 = strchr(hexdigits, toupper(c2));
@@ -82,10 +103,7 @@
 
         n = (pos1 - hexdigits) << 4;
         n |= (pos2 - hexdigits);
-
-        if (!iss.bad() && !iss.fail()) {
-            result.push_back(n);
-        }
+        result.push_back(n);
     }
 }
 

Modified: trunk/src/lib/dns/tests/hex_unittest.cc
==============================================================================
--- trunk/src/lib/dns/tests/hex_unittest.cc (original)
+++ trunk/src/lib/dns/tests/hex_unittest.cc Thu Mar 11 00:52:45 2010
@@ -36,6 +36,7 @@
 };
 
 const std::string hex_txt("DEADBEEFDECADE");
+const std::string hex_txt_space("DEAD BEEF DECADE");
 const std::string hex_txt_lower("deadbeefdecade");
 
 TEST_F(HexTest, encodeHex) {
@@ -74,6 +75,11 @@
     decodeHex(hex_txt_lower, result);
     compareData(result);
 
+    // white space should be ignored
+    result.clear();
+    decodeHex(hex_txt_space, result);
+    compareData(result);
+
     // Bogus input: should fail
     result.clear();
     EXPECT_THROW(decodeHex("1x", result), BadHexString);




More information about the bind10-changes mailing list