os: 'Path_base::strip_double_dot_dirs()' improvements

Issue #5106
This commit is contained in:
Christian Prochaska 2024-02-07 13:58:39 +01:00 committed by Christian Helmuth
parent 7651c94bf5
commit fb2e0b50c8

View File

@ -130,6 +130,12 @@ class Genode::Path_base
static void strip_double_dot_dirs(char *path)
{
bool path_was_absolute = absolute(path);
/*
* Strip any occurrence of "/.." and the previous path
* element (including leading slash) if one exists.
*/
unsigned i;
while ((i = find_double_dot_dir(path))) {
@ -141,12 +147,27 @@ class Genode::Path_base
while (cut_start > 0 && path[cut_start - 1] != '/')
cut_start--;
/* skip slash in front of the pair of dots */
/* skip slash in front of the previous path element */
if (cut_start > 0)
cut_start--;
strip(path + cut_start, cut_end - cut_start);
}
if (path_was_absolute) {
/* restore the leading slash if it got stripped */
if (empty(path))
copy_cstring(path, "/", 2);
} else {
/* remove leading ".." if present */
if (path[0] == '.' && path[1] == '.'
&& (path[2] == 0 || path[2] == '/'))
strip(path, 2);
/* remove leading slash if one is present now */
if (path[0] == '/')
strip(path, 1);
}
}
private: