diff options
author | ShadowNinja <shadowninja@minetest.net> | 2016-12-16 17:35:58 -0500 |
---|---|---|
committer | Craig Robbins <kde.psych@gmail.com> | 2016-12-20 17:17:38 +1000 |
commit | f522e7351a1eaffcd4b0f1f06fab65a44281f972 (patch) | |
tree | 0f15074f71d5a57f19dc2a534d9d12fe2b57538a /src | |
parent | 59f84ca0a07e50dd5ce050d38ae1aeb529bd25ac (diff) | |
download | minetest-f522e7351a1eaffcd4b0f1f06fab65a44281f972.tar.gz minetest-f522e7351a1eaffcd4b0f1f06fab65a44281f972.tar.bz2 minetest-f522e7351a1eaffcd4b0f1f06fab65a44281f972.zip |
Fix RemoveRelatvePathComponents
This used to return "/foo" for "../foo" when it should return the enpty
string (i.e., error removing all relative components).
Diffstat (limited to 'src')
-rw-r--r-- | src/filesys.cpp | 35 | ||||
-rw-r--r-- | src/unittest/test_filepath.cpp | 5 |
2 files changed, 23 insertions, 17 deletions
diff --git a/src/filesys.cpp b/src/filesys.cpp index c718a9689..bd8b94aff 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -617,48 +617,51 @@ std::string RemoveRelativePathComponents(std::string path) { size_t pos = path.size(); size_t dotdot_count = 0; - while(pos != 0){ + while (pos != 0) { size_t component_with_delim_end = pos; // skip a dir delimiter - while(pos != 0 && IsDirDelimiter(path[pos-1])) + while (pos != 0 && IsDirDelimiter(path[pos-1])) pos--; // strip a path component size_t component_end = pos; - while(pos != 0 && !IsDirDelimiter(path[pos-1])) + while (pos != 0 && !IsDirDelimiter(path[pos-1])) pos--; size_t component_start = pos; std::string component = path.substr(component_start, component_end - component_start); bool remove_this_component = false; - if(component == "." && component_start != 0){ + if (component == ".") { remove_this_component = true; - } - else if(component == ".."){ + } else if (component == "..") { remove_this_component = true; dotdot_count += 1; - } - else if(dotdot_count != 0){ + } else if (dotdot_count != 0) { remove_this_component = true; dotdot_count -= 1; } - if(remove_this_component){ - while(pos != 0 && IsDirDelimiter(path[pos-1])) + if (remove_this_component) { + while (pos != 0 && IsDirDelimiter(path[pos-1])) pos--; - path = path.substr(0, pos) + DIR_DELIM + - path.substr(component_with_delim_end, - std::string::npos); - pos++; + if (component_start == 0) { + // We need to remove the delemiter too + path = path.substr(component_with_delim_end, std::string::npos); + } else { + path = path.substr(0, pos) + DIR_DELIM + + path.substr(component_with_delim_end, std::string::npos); + } + if (pos > 0) + pos++; } } - if(dotdot_count > 0) + if (dotdot_count > 0) return ""; // remove trailing dir delimiters pos = path.size(); - while(pos != 0 && IsDirDelimiter(path[pos-1])) + while (pos != 0 && IsDirDelimiter(path[pos-1])) pos--; return path.substr(0, pos); } diff --git a/src/unittest/test_filepath.cpp b/src/unittest/test_filepath.cpp index 5e3cdcc08..ac2d69b5a 100644 --- a/src/unittest/test_filepath.cpp +++ b/src/unittest/test_filepath.cpp @@ -251,7 +251,10 @@ void TestFilePath::testRemoveRelativePathComponent() UASSERT(result == p("/home/user/minetest/worlds/world1")); path = p("."); result = fs::RemoveRelativePathComponents(path); - UASSERT(result == "."); + UASSERT(result == ""); + path = p("../a"); + result = fs::RemoveRelativePathComponents(path); + UASSERT(result == ""); path = p("./subdir/../.."); result = fs::RemoveRelativePathComponents(path); UASSERT(result == ""); |