]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
JSONFormattable incorrectly sets a string starting with digit as int 35870/head
authorSoumya Koduri <skoduri@redhat.com>
Thu, 24 Jun 2021 18:00:45 +0000 (23:30 +0530)
committerSoumya Koduri <skoduri@redhat.com>
Thu, 21 Jul 2022 03:44:44 +0000 (09:14 +0530)
JSONFormattable::set calls JSONParser::parse() to check if the given string
is a valid JSON object, which internally uses json_spirit::read to determine
the string type and copy it into data.
This routine incorrectly sets data type to integer if the string starts with
digit and copies only the first set of numeric values of the string.

To work-around this issue, verify if the entire string is parsed to
determine if its valid json data type.

Signed-off-by: Soumya Koduri <skoduri@redhat.com>
src/common/ceph_json.cc
src/test/common/test_json_formattable.cc

index 979168798403a6aa5dd701b4b3f6a777cfbdecd4..b63d73e5a8c3c9c50ad86d3a5a75226cbc52e502 100644 (file)
@@ -249,7 +249,12 @@ bool JSONParser::parse(const char *buf_, int len)
       if (data.type() == str_type) {
         val.set(data.get_str(), true);
       } else {
-        val.set(json_spirit::write_string(data), false);
+        const std::string& s = json_spirit::write_string(data);
+        if (s.size() == (uint64_t)len) { /* Check if entire string is read */
+          val.set(s, false);
+        } else {
+          set_failure();
+        }
       }
     }
   } else {
index 90327127cc78746bb953c4b7bb5d9c1e4756ab11..62448e8080b586ba7dbc5b7ed96d35577b652a9a 100644 (file)
@@ -61,6 +61,12 @@ TEST(formatable, str2) {
 
 }
 
+TEST(formatable, str3) {
+  JSONFormattable f;
+  get_jf("{ \"foo\": \"1234bar56\" }", &f);
+  ASSERT_EQ((string)f["foo"], "1234bar56");
+}
+
 TEST(formatable, int) {
   JSONFormattable f;
   get_jf("{ \"foo\": 1 }", &f);
@@ -193,6 +199,12 @@ TEST(formatable, set) {
   ASSERT_EQ((int)f["obj"]["c"], 30);
 }
 
+TEST(formatable, set2) {
+  JSONFormattable f;
+  f.set("foo", "1234bar56");
+  ASSERT_EQ((string)f["foo"], "1234bar56");
+}
+
 TEST(formatable, erase) {
   JSONFormattable f, f2;