Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions src/desktopfilereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,50 @@ namespace linuxdeploy {
throw ParseError(errorPrefix + "invalid ] position");
}

// locale may only contain A-Za-z- characters according to specification
for (const char c : entryName) {
// splitting the locale into the three parts (of which only one is mandatory) is easier
// if we strip the surrounding brackets first
const auto innerPart = entryLocale.substr(1, entryLocale.size() - 2);

// will be std::string::npos if necessary
const auto stopPosition = innerPart.find('.');
const auto atPosition = innerPart.find('@');

auto langCountry = innerPart.substr(0, std::min(stopPosition, atPosition));
std::string encoding, modifier;

if (stopPosition != std::string::npos) {
encoding = innerPart.substr(stopPosition + 1, atPosition);
}
if (atPosition != std::string::npos) {
modifier = innerPart.substr(atPosition + 1);
}

// lang_COUNTRY may only contain A-Za-z- characters according to specification
for (const char c : langCountry) {
if (!(
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c == '_')
)) {
throw ParseError("Locale " + key + " lang_COUNTRY contains invalid character " + std::string{c});
}
}

// encoding may only contain A-Za-z- characters according to specification
// if the encoding is empty, this loop is a no-op
for (const char c : encoding) {
if (!(
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c == '_') || (c == '@')
(c >= '0' && c <= '9') ||
(c == '-')
)) {
throw ParseError("Key " + key + " contains invalid character " + std::string{c});
throw ParseError("Locale " + key + " encoding contains invalid character " + std::string{c});
}
}

// TODO: test modifier properly
(void) modifier;
}

auto& section = sections[currentSectionName];
Expand Down
8 changes: 8 additions & 0 deletions tests/test_desktopfilereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,11 @@ TEST_F(DesktopFileReaderTest, testBrokenLocalizedKeys) {
EXPECT_THROW(DesktopFileReader reader(ins), ParseError) << "key: " << key;
}
}

TEST_F(DesktopFileReaderTest, testKeyWithDashesAndLocaleWithUnderscore) {
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< "X-GNOME-FullName[fr_FR]=test" << std::endl;

EXPECT_NO_THROW(DesktopFileReader reader(ins));
}