From 16ffca6f0a23a96284a7d17c890047121d32058e Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 28 Feb 2019 10:38:54 -0600 Subject: [PATCH] common/str_map: fix trim() on empty string If was pass an empty string (e.g., with something like get_str_map("M= T= P=")) we end up with a (size_t)-1 for end. Fixes: http://tracker.ceph.com/issues/38329 Signed-off-by: Sage Weil --- src/common/str_map.cc | 3 +++ src/test/common/test_str_map.cc | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/common/str_map.cc b/src/common/str_map.cc index 6e952ab8419d0..b1f2b78ee90be 100644 --- a/src/common/str_map.cc +++ b/src/common/str_map.cc @@ -57,6 +57,9 @@ int get_json_str_map( return 0; } string trim(const string& str) { + if (str.empty()) { + return str; + } size_t start = 0; size_t end = str.size() - 1; while (start <= end && isspace(str[start]) != 0) { diff --git a/src/test/common/test_str_map.cc b/src/test/common/test_str_map.cc index e96c792a4780e..b61739e8f81fa 100644 --- a/src/test/common/test_str_map.cc +++ b/src/test/common/test_str_map.cc @@ -66,6 +66,18 @@ TEST(str_map, plaintext) { } } +TEST(str_map, empty_values) { + { + map str_map; + ASSERT_EQ(0, get_str_map("M= P= L=", + &str_map)); + ASSERT_EQ(3u, str_map.size()); + ASSERT_EQ("", str_map["M"]); + ASSERT_EQ("", str_map["P"]); + ASSERT_EQ("", str_map["L"]); + } +} + /* * Local Variables: * compile-command: "cd ../.. ; make -j4 && -- 2.39.5