Fix search for values in blkId output
Bug: 122497152
Test: atest tests/Utils_test.cpp
Test: adb shell sm partition disk:7,32 private ; adb logcat -d
Change-Id: Ic7d32bdbc0c55ce1d21f7f9e74c6a6fb3dcf332a
diff --git a/Utils.cpp b/Utils.cpp
index f00d168..a8273d7 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -202,12 +202,15 @@
bool FindValue(const std::string& raw, const std::string& key, std::string* value) {
auto qual = key + "=\"";
- auto start = raw.find(qual);
- if (start > 0 && raw[start - 1] != ' ') {
- start = raw.find(qual, start + 1);
+ size_t start = 0;
+ while (true) {
+ start = raw.find(qual, start);
+ if (start == std::string::npos) return false;
+ if (start == 0 || raw[start - 1] == ' ') {
+ break;
+ }
+ start += 1;
}
-
- if (start == std::string::npos) return false;
start += qual.length();
auto end = raw.find("\"", start);
diff --git a/tests/Utils_test.cpp b/tests/Utils_test.cpp
index e16cbac..d18dc67 100644
--- a/tests/Utils_test.cpp
+++ b/tests/Utils_test.cpp
@@ -27,6 +27,7 @@
std::string tmp;
ASSERT_FALSE(FindValue("", "KEY", &tmp));
+ ASSERT_FALSE(FindValue("NOTMATCH=\"VALUE\"", "KEY", &tmp));
ASSERT_FALSE(FindValue("BADKEY=\"VALUE\"", "KEY", &tmp));
ASSERT_TRUE(FindValue("KEY=\"VALUE\"", "KEY", &tmp));
@@ -37,6 +38,9 @@
ASSERT_TRUE(FindValue("BADKEY=\"VALUE\" KEY=\"BAZ\"", "KEY", &tmp));
ASSERT_EQ("BAZ", tmp);
+
+ ASSERT_TRUE(FindValue("BADKEY=\"VALUE\" NOTKEY=\"OTHER\" KEY=\"QUUX\"", "KEY", &tmp));
+ ASSERT_EQ("QUUX", tmp);
}
} // namespace vold