Skip to content

Commit 7075b80

Browse files
authored
Merge pull request #6 from ids1024/locale
Fix `entryLocale` validation code
2 parents 5e7a8b1 + 1e28ee3 commit 7075b80

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/desktopfilereader.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,50 @@ namespace linuxdeploy {
136136
throw ParseError(errorPrefix + "invalid ] position");
137137
}
138138

139-
// locale may only contain A-Za-z- characters according to specification
140-
for (const char c : entryName) {
139+
// splitting the locale into the three parts (of which only one is mandatory) is easier
140+
// if we strip the surrounding brackets first
141+
const auto innerPart = entryLocale.substr(1, entryLocale.size() - 2);
142+
143+
// will be std::string::npos if necessary
144+
const auto stopPosition = innerPart.find('.');
145+
const auto atPosition = innerPart.find('@');
146+
147+
auto langCountry = innerPart.substr(0, std::min(stopPosition, atPosition));
148+
std::string encoding, modifier;
149+
150+
if (stopPosition != std::string::npos) {
151+
encoding = innerPart.substr(stopPosition + 1, atPosition);
152+
}
153+
if (atPosition != std::string::npos) {
154+
modifier = innerPart.substr(atPosition + 1);
155+
}
156+
157+
// lang_COUNTRY may only contain A-Za-z- characters according to specification
158+
for (const char c : langCountry) {
159+
if (!(
160+
(c >= 'A' && c <= 'Z') ||
161+
(c >= 'a' && c <= 'z') ||
162+
(c == '_')
163+
)) {
164+
throw ParseError("Locale " + key + " lang_COUNTRY contains invalid character " + std::string{c});
165+
}
166+
}
167+
168+
// encoding may only contain A-Za-z- characters according to specification
169+
// if the encoding is empty, this loop is a no-op
170+
for (const char c : encoding) {
141171
if (!(
142172
(c >= 'A' && c <= 'Z') ||
143173
(c >= 'a' && c <= 'z') ||
144-
(c == '_') || (c == '@')
174+
(c >= '0' && c <= '9') ||
175+
(c == '-')
145176
)) {
146-
throw ParseError("Key " + key + " contains invalid character " + std::string{c});
177+
throw ParseError("Locale " + key + " encoding contains invalid character " + std::string{c});
147178
}
148179
}
180+
181+
// TODO: test modifier properly
182+
(void) modifier;
149183
}
150184

151185
auto& section = sections[currentSectionName];

tests/test_desktopfilereader.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,11 @@ TEST_F(DesktopFileReaderTest, testBrokenLocalizedKeys) {
429429
EXPECT_THROW(DesktopFileReader reader(ins), ParseError) << "key: " << key;
430430
}
431431
}
432+
433+
TEST_F(DesktopFileReaderTest, testKeyWithDashesAndLocaleWithUnderscore) {
434+
std::stringstream ins;
435+
ins << "[Desktop Entry]" << std::endl
436+
<< "X-GNOME-FullName[fr_FR]=test" << std::endl;
437+
438+
EXPECT_NO_THROW(DesktopFileReader reader(ins));
439+
}

0 commit comments

Comments
 (0)