]> git.apps.os.sepia.ceph.com Git - ceph-ci.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)
committerSage Weil <sage@redhat.com>
Fri, 1 Mar 2019 05:39:44 +0000 (23:39 -0600)
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>
src/common/str_map.cc
src/test/common/test_str_map.cc

index 6e952ab8419d0ec07da0a09880d7f78c81af7105..b1f2b78ee90be376e8900abe3dd314b1ec156340 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 (start <= end && isspace(str[start]) != 0) {
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 &&