Skip to content

Commit 0ee2a7b

Browse files
committed
misc/path_utils: fix some bugs
The detection for leading dots was in the wrong place, in case path was not already a basename. Fixed by acting on basename. Also all leading dots are now getting skipped, otherwise the root of `path/to/...ext` could end up as `path/to/..` which in the wrong place/context would mean directory traversal. Additionally, since we act on the basename there is no longer a need to test for trailing path separators. The previous check also did not account for Windows paths with \ separators.
1 parent 7c2cc97 commit 0ee2a7b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

misc/path_utils.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,15 @@ void mp_path_strip_trailing_separator(char *path)
8585
char *mp_splitext(const char *path, bstr *root)
8686
{
8787
mp_assert(path);
88-
int skip = (*path == '.'); // skip leading dot for "hidden" unix files
89-
const char *split = strrchr(path + skip, '.');
90-
if (!split || !split[1] || strchr(split, '/'))
88+
char *bn = mp_basename(path);
89+
90+
// Skip all leading dots, not just for "hidden" unix files, otherwise we
91+
// end up splitting a part of the filename sans leading dot.
92+
while (*bn == '.')
93+
bn++;
94+
95+
const char *split = strrchr(bn, '.');
96+
if (!split || !split[1])
9197
return NULL;
9298
if (root)
9399
*root = (bstr){(char *)path, split - path};

0 commit comments

Comments
 (0)