BIND 10 trac1626, updated. 737c4eb744e7edcd77410a11000b49e17ca3af8b [1626] fix non-printable escaping in json conv

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Mar 30 09:15:13 UTC 2012


The branch, trac1626 has been updated
       via  737c4eb744e7edcd77410a11000b49e17ca3af8b (commit)
      from  9f4292fa5d27c5569384ca57b7878be69cbf5499 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 737c4eb744e7edcd77410a11000b49e17ca3af8b
Author: Jelte Jansen <jelte at isc.org>
Date:   Fri Mar 30 11:14:57 2012 +0200

    [1626] fix non-printable escaping in json conv

-----------------------------------------------------------------------

Summary of changes:
 src/lib/cc/data.cc                 |   61 +++++++++++++++++++++++++++++++-----
 src/lib/cc/tests/data_unittests.cc |   10 +++---
 2 files changed, 58 insertions(+), 13 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/cc/data.cc b/src/lib/cc/data.cc
index 612dc9c..e0bed90 100644
--- a/src/lib/cc/data.cc
+++ b/src/lib/cc/data.cc
@@ -318,13 +318,37 @@ str_from_stringstream(std::istream &in, const std::string& file, const int line,
     while (c != EOF && c != '"') {
         if (c == '\\') {
             // see the spec for allowed escape characters
-            if (strchr("\"\\/\b\f\n\r\t", in.peek()) != NULL) {
-                // drop the escape
-                c = in.get();
-                ++pos;
-            } else {
+            switch (in.peek()) {
+            case '"':
+                c = '"';
+                break;
+            case '/':
+                c = '/';
+                break;
+            case '\\':
+                c = '\\';
+                break;
+            case 'b':
+                c = '\b';
+                break;
+            case 'f':
+                c = '\f';
+                break;
+            case 'n':
+                c = '\n';
+                break;
+            case 'r':
+                c = '\r';
+                break;
+            case 't':
+                c = '\t';
+                break;
+            default:
                 throwJSONError("Bad escape", file, line, pos);
             }
+            // drop the escaped char
+            in.get();
+            ++pos;
         }
         ss << c;
         c = in.get();
@@ -656,10 +680,31 @@ StringElement::toJSON(std::ostream& ss) const {
         // Escape characters as defined in JSON spec
         // Note that we do not escape forward slash; this
         // is allowed, but not mandatory.
-        if (strchr("\"\\\b\f\n\r\t", c) != NULL) {
-            ss << '\\';
+        switch (c) {
+        case '"':
+            ss << '\\' << c;
+            break;
+        case '\\':
+            ss << '\\' << c;
+            break;
+        case '\b':
+            ss << '\\' << 'b';
+            break;
+        case '\f':
+            ss << '\\' << 'f';
+            break;
+        case '\n':
+            ss << '\\' << 'n';
+            break;
+        case '\r':
+            ss << '\\' << 'r';
+            break;
+        case '\t':
+            ss << '\\' << 't';
+            break;
+        default:
+            ss << c;
         }
-        ss << c;
     }
     ss << "\"";
 }
diff --git a/src/lib/cc/tests/data_unittests.cc b/src/lib/cc/tests/data_unittests.cc
index 14e6641..79b53ba 100644
--- a/src/lib/cc/tests/data_unittests.cc
+++ b/src/lib/cc/tests/data_unittests.cc
@@ -321,11 +321,11 @@ TEST(Element, escape) {
     // String elements.
     escapeHelper("foo\"bar", "\"foo\\\"bar\"");
     escapeHelper("foo\\bar", "\"foo\\\\bar\"");
-    escapeHelper("foo\bbar", "\"foo\\\bbar\"");
-    escapeHelper("foo\fbar", "\"foo\\\fbar\"");
-    escapeHelper("foo\nbar", "\"foo\\\nbar\"");
-    escapeHelper("foo\rbar", "\"foo\\\rbar\"");
-    escapeHelper("foo\tbar", "\"foo\\\tbar\"");
+    escapeHelper("foo\bbar", "\"foo\\bbar\"");
+    escapeHelper("foo\fbar", "\"foo\\fbar\"");
+    escapeHelper("foo\nbar", "\"foo\\nbar\"");
+    escapeHelper("foo\rbar", "\"foo\\rbar\"");
+    escapeHelper("foo\tbar", "\"foo\\tbar\"");
     // Bad escapes
     EXPECT_THROW(Element::fromJSON("\\a"), JSONError);
     EXPECT_THROW(Element::fromJSON("\\"), JSONError);



More information about the bind10-changes mailing list