[core] Cut switch_cut_path() at the last separator of either kind (#3298)

`switch_cut_path()` scanned for `/`, then restarted from the start of
the string for `\`, so whenever the last `\` came before the last `/`
the result still held `/`-separated components. Search for the last
`/` first, then for the last `\` in what follows it, so the result is
always what comes after the final separator, as documented.

Adds mixed-separator cases to
`test_switch_cut_path_strips_directory_components`.
This commit is contained in:
Dmitry Verenitsin authored and GitHub committed 2026-11-18 21:42:36 +03:00
1 parent bb26953644
commit b5336ca3f9
2 files changed
+20 -11

No files matched your search

+12 -11
View File
@@ -2895,20 +2895,21 @@ SWITCH_DECLARE(unsigned int) switch_separate_string(char *buf, char delim, char
SWITCH_DECLARE(const char *) switch_cut_path(const char *in)
{
const char *p, *ret = in;
const char delims[] = "/\\";
const char *i;
if (in) {
for (i = delims; *i; i++) {
p = in;
while ((p = strchr(p, *i)) != 0) {
ret = ++p;
}
}
return ret;
} else {
if (!in) {
return NULL;
}
/* The second search only sees what follows the last '/', so the last separator of either kind wins. */
if ((p = strrchr(ret, '/'))) {
ret = p + 1;
}
if ((p = strrchr(ret, '\\'))) {
ret = p + 1;
}
return ret;
}
SWITCH_DECLARE(switch_status_t) switch_string_match(const char *string, size_t string_len, const char *search, size_t search_len)
+8
View File
@@ -500,10 +500,18 @@ FST_TEST_BEGIN(test_switch_cut_path_strips_directory_components)
{ "/etc/passwd", "passwd" }, /* leading absolute path dropped */
{ "a/../../x", "x" }, /* embedded .. dropped with the rest of the path */
{ "dir\\sub\\f.txt", "f.txt" }, /* backslash is also a separator */
{ "\\../../x", "x" }, /* mixed: a backslash before slash-separated components */
{ "a/b\\c/d", "d" }, /* mixed: the last separator wins whichever kind it is */
{ "a\\b/c\\d", "d" }, /* mixed: backslash last */
{ "trailing/", "" }, /* nothing follows the final separator */
{ "trailing\\", "" },
{ "/", "" }, /* separator only */
{ "\\", "" },
{ "..", ".." }, /* no separator: caller must reject this residue */
};
fst_xcheck(switch_cut_path(NULL) == NULL, "a NULL path yields NULL");
for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
fst_check_string_equals(switch_cut_path(cases[i].in), cases[i].want);
}