Fix computeAbsolutePath.

Correctly delete the duplicated string.
Use strtok_r to be thread safe.
This commit is contained in:
Matthieu Gautier 2019-08-11 15:25:00 +02:00
parent 44bec86f31
commit 52299ef767
1 changed files with 6 additions and 4 deletions

View File

@ -121,22 +121,24 @@ std::string computeAbsolutePath(const std::string& path, const std::string& rela
#else
char* cRelativePath = strdup(relativePath.c_str());
#endif
char* token = strtok(cRelativePath, "/");
char* saveptr = nullptr;
char* token = strtok_r(cRelativePath, "/", &saveptr);
while (token != NULL) {
if (std::string(token) == "..") {
absolutePath = removeLastPathElement(absolutePath, true, false);
token = strtok(NULL, "/");
token = strtok_r(NULL, "/", &saveptr);
} else if (strcmp(token, ".") && strcmp(token, "")) {
absolutePath += std::string(token);
token = strtok(NULL, "/");
token = strtok_r(NULL, "/", &saveptr);
if (token != NULL) {
absolutePath += SEPARATOR;
}
} else {
token = strtok(NULL, "/");
token = strtok_r(NULL, "/", &saveptr);
}
}
free(cRelativePath);
return absolutePath;
}