summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYifan Hong2017-07-11 19:46:46 -0500
committerandroid-build-merger2017-07-11 19:46:46 -0500
commit93368447cae82ea0d4788706f31cb4b14a526f6b (patch)
tree622752185ad45f9aac0f439cc45c74bd558b0a7c
parent84fc76ec23e2bfa6c9ed97860ef35f996093b6bb (diff)
parent02e94004a6d35d20af7df1ea9c66c5b65e4bd9c6 (diff)
downloadplatform-system-libvintf-93368447cae82ea0d4788706f31cb4b14a526f6b.tar.gz
platform-system-libvintf-93368447cae82ea0d4788706f31cb4b14a526f6b.tar.xz
platform-system-libvintf-93368447cae82ea0d4788706f31cb4b14a526f6b.zip
Do not use regex to parse /proc/config.gz.
am: 02e94004a6 Change-Id: I537f1c82c7813180e17ee92c9776e835a0a2da6d
-rw-r--r--KernelConfigParser.cpp41
-rw-r--r--assemble_vintf.cpp2
-rw-r--r--include/vintf/KernelConfigParser.h3
-rw-r--r--test/main.cpp15
4 files changed, 45 insertions, 16 deletions
diff --git a/KernelConfigParser.cpp b/KernelConfigParser.cpp
index 44cbf07..a0955e2 100644
--- a/KernelConfigParser.cpp
+++ b/KernelConfigParser.cpp
@@ -28,7 +28,8 @@ static const std::regex sCommentPattern("^\\s*" COMMENT "$");
28namespace android { 28namespace android {
29namespace vintf { 29namespace vintf {
30 30
31KernelConfigParser::KernelConfigParser(bool processComments) : mProcessComments(processComments) {} 31KernelConfigParser::KernelConfigParser(bool processComments, bool relaxedFormat)
32 : mProcessComments(processComments), mRelaxedFormat(relaxedFormat) {}
32 33
33status_t KernelConfigParser::finish() { 34status_t KernelConfigParser::finish() {
34 return process("\n", 1 /* sizeof "\n" */); 35 return process("\n", 1 /* sizeof "\n" */);
@@ -61,12 +62,28 @@ status_t KernelConfigParser::processRemaining() {
61 } 62 }
62 63
63 std::smatch match; 64 std::smatch match;
64 if (std::regex_match(mRemaining, match, sKeyValuePattern)) { 65
65 if (mConfigs.emplace(match[1], trimTrailingSpaces(match[2])).second) { 66 if (mRelaxedFormat) {
66 return OK; 67 // Allow free format like " CONFIG_FOO = bar #trailing comments"
68 if (std::regex_match(mRemaining, match, sKeyValuePattern)) {
69 if (mConfigs.emplace(match[1], trimTrailingSpaces(match[2])).second) {
70 return OK;
71 }
72 mError << "Duplicated key in configs: " << match[1] << "\n";
73 return UNKNOWN_ERROR;
74 }
75 } else {
76 // No spaces. Strictly like "CONFIG_FOO=bar"
77 size_t equalPos = mRemaining.find('=');
78 if (equalPos != std::string::npos) {
79 std::string key = mRemaining.substr(0, equalPos);
80 std::string value = mRemaining.substr(equalPos + 1);
81 if (mConfigs.emplace(std::move(key), std::move(value)).second) {
82 return OK;
83 }
84 mError << "Duplicated key in configs: " << mRemaining.substr(0, equalPos) << "\n";
85 return UNKNOWN_ERROR;
67 } 86 }
68 mError << "Duplicated key in configs: " << match[1] << "\n";
69 return UNKNOWN_ERROR;
70 } 87 }
71 88
72 if (mProcessComments && std::regex_match(mRemaining, match, sNotSetPattern)) { 89 if (mProcessComments && std::regex_match(mRemaining, match, sNotSetPattern)) {
@@ -78,8 +95,16 @@ status_t KernelConfigParser::processRemaining() {
78 return UNKNOWN_ERROR; 95 return UNKNOWN_ERROR;
79 } 96 }
80 97
81 if (std::regex_match(mRemaining, match, sCommentPattern)) { 98 if (mRelaxedFormat) {
82 return OK; 99 // Allow free format like " #comments here"
100 if (std::regex_match(mRemaining, match, sCommentPattern)) {
101 return OK;
102 }
103 } else {
104 // No leading spaces before the comment
105 if (mRemaining.at(0) == '#') {
106 return OK;
107 }
83 } 108 }
84 109
85 mError << "Unrecognized line in configs: " << mRemaining << "\n"; 110 mError << "Unrecognized line in configs: " << mRemaining << "\n";
diff --git a/assemble_vintf.cpp b/assemble_vintf.cpp
index 7683472..2752078 100644
--- a/assemble_vintf.cpp
+++ b/assemble_vintf.cpp
@@ -65,7 +65,7 @@ public:
65 std::cerr << "File '" << path << "' does not exist or cannot be read." << std::endl; 65 std::cerr << "File '" << path << "' does not exist or cannot be read." << std::endl;
66 return false; 66 return false;
67 } 67 }
68 KernelConfigParser parser(true /* processComments */); 68 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
69 std::string content = read(ifs); 69 std::string content = read(ifs);
70 status_t err = parser.process(content.c_str(), content.size()); 70 status_t err = parser.process(content.c_str(), content.size());
71 if (err != OK) { 71 if (err != OK) {
diff --git a/include/vintf/KernelConfigParser.h b/include/vintf/KernelConfigParser.h
index bad66a5..06a034d 100644
--- a/include/vintf/KernelConfigParser.h
+++ b/include/vintf/KernelConfigParser.h
@@ -29,7 +29,7 @@ namespace vintf {
29 29
30class KernelConfigParser { 30class KernelConfigParser {
31 public: 31 public:
32 KernelConfigParser(bool processComments = false); 32 KernelConfigParser(bool processComments = false, bool relaxedFormat = false);
33 33
34 status_t process(const char* buf, size_t len); 34 status_t process(const char* buf, size_t len);
35 status_t finish(); 35 status_t finish();
@@ -43,6 +43,7 @@ class KernelConfigParser {
43 std::stringstream mError; 43 std::stringstream mError;
44 std::string mRemaining; 44 std::string mRemaining;
45 bool mProcessComments; 45 bool mProcessComments;
46 bool mRelaxedFormat;
46}; 47};
47 48
48} // namespace vintf 49} // namespace vintf
diff --git a/test/main.cpp b/test/main.cpp
index b3b8a59..8dc5ddb 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -1310,8 +1310,9 @@ TEST_F(LibVintfTest, MatrixXmlFilePathMissing) {
1310 EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}), ""); 1310 EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}), "");
1311} 1311}
1312 1312
1313std::pair<KernelConfigParser, status_t> processData(const std::string& data, bool processComments) { 1313std::pair<KernelConfigParser, status_t> processData(const std::string& data, bool processComments,
1314 KernelConfigParser parser(processComments); 1314 bool relaxedFormat = false) {
1315 KernelConfigParser parser(processComments, relaxedFormat);
1315 const char* p = data.c_str(); 1316 const char* p = data.c_str();
1316 size_t n = 0; 1317 size_t n = 0;
1317 size_t chunkSize; 1318 size_t chunkSize;
@@ -1378,7 +1379,7 @@ TEST_F(LibVintfTest, KernelConfigParserSpace) {
1378 "CONFIG_WORLD=hello world! \n" 1379 "CONFIG_WORLD=hello world! \n"
1379 "CONFIG_GOOD = good morning! #comments here\n" 1380 "CONFIG_GOOD = good morning! #comments here\n"
1380 " CONFIG_MORNING = good morning! \n"; 1381 " CONFIG_MORNING = good morning! \n";
1381 auto pair = processData(data, true /* processComments */); 1382 auto pair = processData(data, true /* processComments */, true /* relaxedFormat */);
1382 ASSERT_EQ(OK, pair.second) << pair.first.error(); 1383 ASSERT_EQ(OK, pair.second) << pair.first.error();
1383 const auto& configs = pair.first.configs(); 1384 const auto& configs = pair.first.configs();
1384 1385
@@ -1403,7 +1404,7 @@ class KernelConfigParserInvalidTest : public ::testing::TestWithParam<bool> {};
1403 1404
1404TEST_P(KernelConfigParserInvalidTest, NonSet1) { 1405TEST_P(KernelConfigParserInvalidTest, NonSet1) {
1405 const std::string data = "# CONFIG_NOT_EXIST is not sat\n"; 1406 const std::string data = "# CONFIG_NOT_EXIST is not sat\n";
1406 auto pair = processData(data, GetParam() /* processComments */); 1407 auto pair = processData(data, GetParam() /* processComments */, true /* relaxedFormat */);
1407 ASSERT_EQ(OK, pair.second) << pair.first.error(); 1408 ASSERT_EQ(OK, pair.second) << pair.first.error();
1408 const auto& configs = pair.first.configs(); 1409 const auto& configs = pair.first.configs();
1409 EXPECT_EQ(configs.find("CONFIG_NOT_EXIST"), configs.end()) 1410 EXPECT_EQ(configs.find("CONFIG_NOT_EXIST"), configs.end())
@@ -1412,12 +1413,14 @@ TEST_P(KernelConfigParserInvalidTest, NonSet1) {
1412 1413
1413TEST_P(KernelConfigParserInvalidTest, InvalidLine1) { 1414TEST_P(KernelConfigParserInvalidTest, InvalidLine1) {
1414 const std::string data = "FOO_CONFIG=foo\n"; 1415 const std::string data = "FOO_CONFIG=foo\n";
1415 ASSERT_NE(OK, processData(data, GetParam() /* processComments */).second); 1416 ASSERT_NE(OK,
1417 processData(data, GetParam() /* processComments */, true /* relaxedFormat */).second);
1416} 1418}
1417 1419
1418TEST_P(KernelConfigParserInvalidTest, InvalidLine2) { 1420TEST_P(KernelConfigParserInvalidTest, InvalidLine2) {
1419 const std::string data = "CONFIG_BAR-BAZ=foo\n"; 1421 const std::string data = "CONFIG_BAR-BAZ=foo\n";
1420 ASSERT_NE(OK, processData(data, GetParam() /* processComments */).second); 1422 ASSERT_NE(OK,
1423 processData(data, GetParam() /* processComments */, true /* relaxedFormat */).second);
1421} 1424}
1422 1425
1423INSTANTIATE_TEST_CASE_P(KernelConfigParser, KernelConfigParserInvalidTest, ::testing::Bool()); 1426INSTANTIATE_TEST_CASE_P(KernelConfigParser, KernelConfigParserInvalidTest, ::testing::Bool());