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); }