Skip to content

Commit f06bf56

Browse files
author
Lauris Kaplinski
committed
Check that filenames are valid utf8 strings
Signed-off-by: Lauris Kaplinski <[email protected]>
1 parent d42ad3a commit f06bf56

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

cdoc/CDoc1Writer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ CDoc1Writer::addFile(const std::string& name, size_t size)
292292
{
293293
if(!d)
294294
return WORKFLOW_ERROR;
295+
if (name.empty() || !libcdoc::isValidUtf8(name)) return libcdoc::DATA_FORMAT_ERROR;
295296
d->files.push_back({name, size, {}});
296297
return libcdoc::OK;
297298
}

cdoc/CDoc2Writer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ CDoc2Writer::addFile(const std::string& name, size_t size)
519519
LOG_ERROR("{}", last_error);
520520
return libcdoc::WORKFLOW_ERROR;
521521
}
522+
if (name.empty() || !libcdoc::isValidUtf8(name)) return libcdoc::DATA_FORMAT_ERROR;
522523
if(auto rv = tar->open(name, size); rv < 0) {
523524
setLastError(tar->getLastErrorStr(rv));
524525
LOG_ERROR("{}", last_error);

cdoc/Utils.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ timeToISO(double time)
8282
#endif
8383
}
8484

85+
bool
86+
isValidUtf8 (std::string str)
87+
{
88+
const uint8_t *s = (const uint8_t *) str.data();
89+
const uint8_t *e = s + str.size();
90+
while (s < e) {
91+
size_t s_len = e - s;
92+
if ((s[0] & 0x80) == 0x0) {
93+
s += 1;
94+
} else if (((s[0] & 0xe0) == 0xc0) && (s_len >= 2) && ((s[1] & 0xc0) == 0x80)) {
95+
s += 2;
96+
} else if (((*s & 0xf0) == 0xe0) && (s_len >= 3) && ((s[1] & 0xc0) == 0x80) && ((s[2] & 0xc0) == 0x80)) {
97+
s += 3;
98+
} else if (((*s & 0xf8) == 0xf0) && (s_len >= 4) && ((s[1] & 0xc0) == 0x80) && ((s[2] & 0xc0) == 0x80) && ((s[3] & 0xc0) == 0x80)) {
99+
s += 4;
100+
} else {
101+
return false;
102+
}
103+
}
104+
return true;
105+
}
106+
85107
int
86108
parseURL(const std::string& url, std::string& host, int& port, std::string& path, bool end_with_slash)
87109
{

cdoc/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ double getTime();
8686
double timeFromISO(std::string_view iso);
8787
std::string timeToISO(double time);
8888

89+
bool isValidUtf8 (std::string str);
90+
8991
static std::vector<uint8_t>
9092
readAllBytes(std::istream& ifs)
9193
{

0 commit comments

Comments
 (0)