From: Soumya Koduri Date: Thu, 24 Jun 2021 18:00:45 +0000 (+0530) Subject: JSONFormattable incorrectly sets a string starting with digit as int X-Git-Tag: v18.0.0~433^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=39323a2c492ec16f66e9607c962f6edf87f1c843;p=ceph.git JSONFormattable incorrectly sets a string starting with digit as int 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 --- diff --git a/src/common/ceph_json.cc b/src/common/ceph_json.cc index 979168798403..b63d73e5a8c3 100644 --- a/src/common/ceph_json.cc +++ b/src/common/ceph_json.cc @@ -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 { diff --git a/src/test/common/test_json_formattable.cc b/src/test/common/test_json_formattable.cc index 90327127cc78..62448e8080b5 100644 --- a/src/test/common/test_json_formattable.cc +++ b/src/test/common/test_json_formattable.cc @@ -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;