You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by me...@apache.org on 2015/10/21 10:18:12 UTC

mesos git commit: Check for trailing characters in JSON::parse().

Repository: mesos
Updated Branches:
  refs/heads/master b8e889a53 -> edffeeee6


Check for trailing characters in JSON::parse().

Review: https://reviews.apache.org/r/39211


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/edffeeee
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/edffeeee
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/edffeeee

Branch: refs/heads/master
Commit: edffeeee663c7c8f11a65210b26fe0d2a43ea55d
Parents: b8e889a
Author: Greg Mann <gr...@mesosphere.io>
Authored: Wed Oct 21 00:21:25 2015 -0700
Committer: Adam B <ad...@mesosphere.io>
Committed: Wed Oct 21 00:22:39 2015 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/json.hpp       | 16 ++++++-
 .../3rdparty/stout/include/stout/strings.hpp    |  4 +-
 .../3rdparty/stout/tests/json_tests.cpp         | 46 ++++++++++++++++++++
 3 files changed, 64 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/edffeeee/3rdparty/libprocess/3rdparty/stout/include/stout/json.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/json.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/json.hpp
index 0870300..9625894 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/json.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/json.hpp
@@ -734,13 +734,27 @@ inline Value convert(const picojson::value& value)
 
 inline Try<Value> parse(const std::string& s)
 {
+  const char* parseBegin = s.c_str();
   picojson::value value;
   std::string error;
 
-  picojson::parse(value, s.c_str(), s.c_str() + s.size(), &error);
+  // Because PicoJson supports repeated parsing of multiple objects/arrays in a
+  // stream, it will quietly ignore trailing non-whitespace characters. We would
+  // rather throw an error, however, so use `last_char` to check for this.
+  const char* lastVisibleChar =
+    parseBegin + s.find_last_not_of(strings::WHITESPACE);
+
+  // Parse the string, returning a pointer to the character
+  // immediately following the last one parsed.
+  const char* parseEnd =
+    picojson::parse(value, parseBegin, parseBegin + s.size(), &error);
 
   if (!error.empty()) {
     return Error(error);
+  } else if (parseEnd != lastVisibleChar + 1) {
+    return Error(
+        "Parsed JSON included non-whitespace trailing characters: "
+        + s.substr(parseEnd - parseBegin, lastVisibleChar + 1 - parseEnd));
   }
 
   return internal::convert(value);

http://git-wip-us.apache.org/repos/asf/mesos/blob/edffeeee/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
index b95667f..ef31099 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
@@ -27,6 +27,8 @@
 
 namespace strings {
 
+const std::string WHITESPACE = " \t\n\r";
+
 // Flags indicating how 'remove' or 'trim' should operate.
 enum Mode
 {
@@ -65,7 +67,7 @@ inline std::string remove(
 inline std::string trim(
     const std::string& from,
     Mode mode = ANY,
-    const std::string& chars = " \t\n\r")
+    const std::string& chars = WHITESPACE)
 {
   size_t start = 0;
   Option<size_t> end = None();

http://git-wip-us.apache.org/repos/asf/mesos/blob/edffeeee/3rdparty/libprocess/3rdparty/stout/tests/json_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/json_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/json_tests.cpp
index 131d39b..40b7879 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/json_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/json_tests.cpp
@@ -226,6 +226,52 @@ TEST(JsonTest, Parse)
   object.values["array"] = array;
 
   EXPECT_SOME_EQ(object, JSON::parse(stringify(object)));
+
+  // Test parsing with whitespace before and after a JSON object.
+  object = JSON::Object();
+  object.values["some"] = "stuff";
+  object.values["oh"] = "no";
+
+  string jsonString =
+    "      "
+    "{\n"
+    "  \"some\": \"stuff\","
+    "  \"oh\": \"no\""
+    "}\t   ";
+
+  EXPECT_SOME_EQ(object, JSON::parse<JSON::Object>(jsonString));
+}
+
+
+TEST(JsonTest, ParseError)
+{
+  string jsonString =
+    "{"
+    "  \"key1\": \"value1\","
+    "  \"key2\": \"value2\""
+    "}trailingcharacters";
+
+  EXPECT_ERROR(JSON::parse<JSON::Object>(jsonString));
+
+  jsonString =
+    "{"
+    "  \"key1\": \"value1\","
+    "  \"key2\": \"value2\""
+    "},"
+    "{"
+    "  \"key3\": \"value3\","
+    "  \"key4\": \"value4\""
+    "}";
+
+  EXPECT_ERROR(JSON::parse<JSON::Object>(jsonString));
+
+  jsonString =
+    "{"
+    "  \"key1\": \"value1\","
+    "  \"key2\": \"value2\""
+    " ";
+
+  EXPECT_ERROR(JSON::parse<JSON::Object>(jsonString));
 }