Skip to content

Commit 1e28ee3

Browse files
ids1024TheAssassin
authored andcommitted
Rework and improve locale validation
1 parent 300792a commit 1e28ee3

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-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];

0 commit comments

Comments
 (0)