summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglx22 <glx22@users.noreply.github.com>2020-06-22 14:21:11 +0200
committerGitHub <noreply@github.com>2020-06-22 14:21:11 +0200
commit87a069c887267ff375c35fdbe5d95fbe71ff0579 (patch)
treede405d1bb9c54c6d648e104fdafda322106568bb
parente6f3e15c32b51f4cc430a9bc9d0b1fc708135a77 (diff)
downloadopenttd-87a069c887267ff375c35fdbe5d95fbe71ff0579.tar.xz
Fix #8230: Resolve ".." when opening files in .tar (#8231)
-rw-r--r--src/fileio.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index f17a0ffc0..7340fa172 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -25,6 +25,7 @@
#endif
#include <sys/stat.h>
#include <algorithm>
+#include <sstream>
#ifdef WITH_XDG_BASEDIR
#include <basedir.h>
@@ -481,6 +482,28 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir,
strecpy(resolved_name, filename, lastof(resolved_name));
strtolower(resolved_name);
+ /* Resolve ".." */
+ std::istringstream ss(resolved_name);
+ std::vector<std::string> tokens;
+ std::string token;
+ while (std::getline(ss, token, PATHSEPCHAR)) {
+ if (token == "..") {
+ if (tokens.size() < 2) return nullptr;
+ tokens.pop_back();
+ } else {
+ tokens.push_back(token);
+ }
+ }
+ resolved_name[0] = '\0';
+ bool first = true;
+ for (const std::string &token : tokens) {
+ if (!first) {
+ strecat(resolved_name, PATHSEP, lastof(resolved_name));
+ }
+ strecat(resolved_name, token.c_str(), lastof(resolved_name));
+ first = false;
+ }
+
size_t resolved_len = strlen(resolved_name);
/* Resolve ONE directory link */