Merge "Fix search for values in blkId output"

This commit is contained in:
Treehugger Robot 2019-02-06 03:19:32 +00:00 committed by Gerrit Code Review
commit 0fbc5af676
2 changed files with 12 additions and 5 deletions

View file

@ -202,12 +202,15 @@ status_t BindMount(const std::string& source, const std::string& target) {
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;
}
start += qual.length();
auto end = raw.find("\"", start);

View file

@ -27,6 +27,7 @@ TEST_F(UtilsTest, FindValueTest) {
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 @@ TEST_F(UtilsTest, FindValueTest) {
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