diff --git a/misc/path_utils.c b/misc/path_utils.c index 766626e82dd25..2d12149b5d504 100644 --- a/misc/path_utils.c +++ b/misc/path_utils.c @@ -43,6 +43,9 @@ char *mp_basename(const char *path) { char *s; + /* otherwise the check below finds the wrong index */ + mp_path_strip_trailing_separator((char *)path); + #if HAVE_DOS_PATHS if (!mp_is_url(bstr0(path))) { s = strrchr(path, '\\'); @@ -54,7 +57,7 @@ char *mp_basename(const char *path) } #endif s = strrchr(path, '/'); - return s ? s + 1 : (char *)path; + return s && strlen(s) > 1 ? s + 1 : (char *)path; } struct bstr mp_dirname(const char *path) @@ -77,8 +80,8 @@ static const char mp_path_separators[] = "/"; // Mutates path and removes a trailing '/' (or '\' on Windows) void mp_path_strip_trailing_separator(char *path) { - size_t len = strlen(path); - if (len > 0 && strchr(mp_path_separators, path[len - 1])) + size_t len; + while ((len=strlen(path)) > 0 && strchr(mp_path_separators, path[len - 1])) path[len - 1] = '\0'; }