From 39323a2c492ec16f66e9607c962f6edf87f1c843 Mon Sep 17 00:00:00 2001 From: Soumya Koduri Date: Thu, 24 Jun 2021 23:30:45 +0530 Subject: [PATCH] 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 --- src/common/ceph_json.cc | 7 ++++++- src/test/common/test_json_formattable.cc | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/common/ceph_json.cc b/src/common/ceph_json.cc index 979168798403a..b63d73e5a8c3c 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 90327127cc787..62448e8080b58 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; -- 2.39.5