Commit b5336ca3f9 for freeswitch.com
commit b5336ca3f9d13ec0c41ca2ae0c328bf5c922b425
Author: Dmitry Verenitsin <morbit85@gmail.com>
Date: Wed Nov 18 23:42:36 2026 +0500
[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`.
diff --git a/src/switch_utils.c b/src/switch_utils.c
index f3c6521e99..7e94105422 100644
--- a/src/switch_utils.c
+++ b/src/switch_utils.c
@@ -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)
diff --git a/tests/unit/switch_utils.c b/tests/unit/switch_utils.c
index c4ff2e3818..b566e70c59 100644
--- a/tests/unit/switch_utils.c
+++ b/tests/unit/switch_utils.c
@@ -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);
}