mirror of
https://github.com/minetest/minetest.git
synced 2025-01-11 15:57:29 +01:00
Fix RemoveRelatvePathComponents
This used to return "/foo" for "../foo" when it should return the enpty string (i.e., error removing all relative components).
This commit is contained in:
parent
59f84ca0a0
commit
f522e7351a
@ -631,14 +631,12 @@ std::string RemoveRelativePathComponents(std::string path)
|
|||||||
std::string component = path.substr(component_start,
|
std::string component = path.substr(component_start,
|
||||||
component_end - component_start);
|
component_end - component_start);
|
||||||
bool remove_this_component = false;
|
bool remove_this_component = false;
|
||||||
if(component == "." && component_start != 0){
|
if (component == ".") {
|
||||||
remove_this_component = true;
|
remove_this_component = true;
|
||||||
}
|
} else if (component == "..") {
|
||||||
else if(component == ".."){
|
|
||||||
remove_this_component = true;
|
remove_this_component = true;
|
||||||
dotdot_count += 1;
|
dotdot_count += 1;
|
||||||
}
|
} else if (dotdot_count != 0) {
|
||||||
else if(dotdot_count != 0){
|
|
||||||
remove_this_component = true;
|
remove_this_component = true;
|
||||||
dotdot_count -= 1;
|
dotdot_count -= 1;
|
||||||
}
|
}
|
||||||
@ -646,9 +644,14 @@ std::string RemoveRelativePathComponents(std::string path)
|
|||||||
if (remove_this_component) {
|
if (remove_this_component) {
|
||||||
while (pos != 0 && IsDirDelimiter(path[pos-1]))
|
while (pos != 0 && IsDirDelimiter(path[pos-1]))
|
||||||
pos--;
|
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 = path.substr(0, pos) + DIR_DELIM +
|
||||||
path.substr(component_with_delim_end,
|
path.substr(component_with_delim_end, std::string::npos);
|
||||||
std::string::npos);
|
}
|
||||||
|
if (pos > 0)
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,7 +251,10 @@ void TestFilePath::testRemoveRelativePathComponent()
|
|||||||
UASSERT(result == p("/home/user/minetest/worlds/world1"));
|
UASSERT(result == p("/home/user/minetest/worlds/world1"));
|
||||||
path = p(".");
|
path = p(".");
|
||||||
result = fs::RemoveRelativePathComponents(path);
|
result = fs::RemoveRelativePathComponents(path);
|
||||||
UASSERT(result == ".");
|
UASSERT(result == "");
|
||||||
|
path = p("../a");
|
||||||
|
result = fs::RemoveRelativePathComponents(path);
|
||||||
|
UASSERT(result == "");
|
||||||
path = p("./subdir/../..");
|
path = p("./subdir/../..");
|
||||||
result = fs::RemoveRelativePathComponents(path);
|
result = fs::RemoveRelativePathComponents(path);
|
||||||
UASSERT(result == "");
|
UASSERT(result == "");
|
||||||
|
Loading…
Reference in New Issue
Block a user