]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/str_map: fix trim() on empty string
authorSage Weil <sage@redhat.com>
Thu, 28 Feb 2019 16:38:54 +0000 (10:38 -0600)
committerPrashant D <pdhange@redhat.com>
Mon, 11 Mar 2019 23:34:10 +0000 (19:34 -0400)
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 <sage@redhat.com>
(cherry picked from commit 16ffca6f0a23a96284a7d17c890047121d32058e)

src/common/str_map.cc
src/test/common/test_str_map.cc

index c3f6a2b6519e3c5b2597fd0ed7082de8c044d67e..6278473de296b1e07f596a34e7032491a75aba32 100644 (file)
@@ -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 (isspace(str[start]) != 0 && start <= end) {
index e96c792a4780e39e51dcc444f3b61c3a19e03efe..b61739e8f81fa3f82cc4a53f730d6c98642b4dcc 100644 (file)
@@ -66,6 +66,18 @@ TEST(str_map, plaintext) {
   }
 }
 
+TEST(str_map, empty_values) {
+  {
+    map<string,string> 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 &&