Recognize broken section headings, mk. 2

This commit is contained in:
TheAssassin
2018-11-14 10:46:48 +01:00
parent dc8f446ca4
commit bf152a509b
2 changed files with 13 additions and 2 deletions

View File

@@ -56,10 +56,13 @@ namespace linuxdeploy {
!((len >= 2 && (line[0] == '/' && line[1] == '/')) || (len >= 1 && line[0] == '#'))) {
if (line[0] == '[') {
// this line apparently introduces a new section
auto closingBracketPos = line.find_last_of(']');
auto closingBracketPos = line.find(']');
auto lastClosingBracketPos = line.find_last_of(']');
if (closingBracketPos == std::string::npos)
throw ParseError("No closing ] bracket in section header");
else if (closingBracketPos != lastClosingBracketPos)
throw ParseError("Two or more closing ] brackets in section header");
size_t length = len - 2;
auto title = line.substr(1, closingBracketPos - 1);

View File

@@ -202,10 +202,18 @@ TEST_F(DesktopFileReaderFixture, testParseLinesWithMultipleSpaces) {
EXPECT_EQ(section["Name"].value(), "What a great name");
}
TEST_F(DesktopFileReaderFixture, testReadBrokenSectionHeader) {
TEST_F(DesktopFileReaderFixture, testReadBrokenSectionHeaderMissingClosingBracket) {
std::stringstream ins;
ins << "[Desktop Entry" << std::endl
<< "test=test" << std::endl;
ASSERT_THROW(DesktopFileReader reader(ins), ParseError);
}
TEST_F(DesktopFileReaderFixture, testReadBrokenSectionHeaderTooManyClosingBrackets) {
std::stringstream ins;
ins << "[Desktop Entry]]" << std::endl
<< "test=test" << std::endl;
ASSERT_THROW(DesktopFileReader reader(ins), ParseError);
}