main.c: Improve relative path detection

Otherwise, the following resources would be considered valid:

- /user/../test
- /user/./test
- /user/a/.
- /user/a/./test
This commit is contained in:
Xavier Del Campo Romero 2024-02-20 21:24:17 +01:00
parent afc5cf0dfc
commit 8bcf0bf855
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
1 changed files with 12 additions and 6 deletions

18
main.c
View File

@ -395,16 +395,22 @@ static bool path_isrel(const char *const path)
{
if (!strcmp(path, "..")
|| !strcmp(path, ".")
|| !strcmp(path, "./")
|| !strcmp(path, "../")
|| !strncmp(path, "./", strlen("./"))
|| !strncmp(path, "../", strlen("../"))
|| strstr(path, "/./")
|| strstr(path, "/../"))
return true;
static const char suffix[] = "/..";
const size_t n = strlen(path), sn = strlen(suffix);
static const char *const suffixes[] = {"/.", "/.."};
if (n >= sn && !strcmp(path + n - sn, suffix))
return true;
for (size_t i = 0; i < sizeof suffixes / sizeof *suffixes; i++)
{
const char *const suffix = suffixes[i];
const size_t n = strlen(path), sn = strlen(suffix);
if (n >= sn && !strcmp(path + n - sn, suffix))
return true;
}
return false;
}