path: change win32 semantics for joining drive-relative paths

win32 is a cursed abomination which has "drive letters" at the root of
the filesystem namespace for no reason. This requires special handling
beyond tolerating the idiotic "\" path separator.

Even more cursed is the fact that a path starting with a drive letter
can be a relative path. For example, "c:billsucks" is actually a
relative path to the current working directory of the C drive. So for
example if the current working directory is "c:/windowsphone", then
"c:billsucks" would reference "c:/windowsphone/billsucks".

You should realize that win32 is a ridiculous satanic trash fire by the
point you realize that win32 has at least 26 current working
directories, one for each drive letter.

Anyway, the actual problem is that mpv's mp_path_join() function would
return a relative path if an absolute relative path is joined with a
drive-relative path. This should never happen; I bet it breaks a lot of
assumptions (maybe even some security or safety relevant ones, but
probably not).

Since relative drive paths are such a fucked up shit idea, don't try to
support them "properly", and just solve the problem at hand. The
solution produces a path that should be invalid on win32.

Joining two relative paths still behaves the same; this is probably OK
(maybe).

The change isn't very minimal due to me rewriting parts of it without
strict need, but I don't care.

Note that the Python os.path.join() function (after which the mpv
function was apparently modeled) has the same problem.
This commit is contained in:
wm4 2020-02-06 14:06:53 +01:00
parent 1293c40623
commit 31acec5438
2 changed files with 14 additions and 14 deletions

View File

@ -256,28 +256,28 @@ char *mp_splitext(const char *path, bstr *root)
char *mp_path_join_bstr(void *talloc_ctx, struct bstr p1, struct bstr p2) char *mp_path_join_bstr(void *talloc_ctx, struct bstr p1, struct bstr p2)
{ {
bool test;
if (p1.len == 0) if (p1.len == 0)
return bstrdup0(talloc_ctx, p2); return bstrdup0(talloc_ctx, p2);
if (p2.len == 0) if (p2.len == 0)
return bstrdup0(talloc_ctx, p1); return bstrdup0(talloc_ctx, p1);
bool is_absolute = strchr(mp_path_separators, p2.start[0]);
#if HAVE_DOS_PATHS #if HAVE_DOS_PATHS
test = (p2.len >= 2 && p2.start[1] == ':') // Note: "X:filename" is a path relative to the current working directory
|| p2.start[0] == '\\' || p2.start[0] == '/'; // of drive X, and thus is not an absolute path. It needs to be
#else // followed by \ or /.
test = p2.start[0] == '/'; if (p2.len >= 3 && p2.start[1] == ':' &&
strchr(mp_path_separators, p2.start[2]))
is_absolute = true;
#endif #endif
if (test) if (is_absolute)
return bstrdup0(talloc_ctx, p2); // absolute path return bstrdup0(talloc_ctx, p2);
bool have_separator; bool have_separator = strchr(mp_path_separators, p1.start[p1.len - 1]);
int endchar1 = p1.start[p1.len - 1];
#if HAVE_DOS_PATHS #if HAVE_DOS_PATHS
have_separator = endchar1 == '/' || endchar1 == '\\' // "X:" only => path relative to "X:" current working directory.
|| (p1.len == 2 && endchar1 == ':'); // "X:" only if (p1.len == 2 && p1.start[1] == ':')
#else have_separator = true;
have_separator = endchar1 == '/';
#endif #endif
return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1), return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),

View File

@ -36,7 +36,7 @@ static void run(struct test_ctx *ctx)
TEST_JOIN("c:\\a", "c:\\b", "c:\\b"); TEST_JOIN("c:\\a", "c:\\b", "c:\\b");
TEST_JOIN("c:/a", "c:/b", "c:/b"); TEST_JOIN("c:/a", "c:/b", "c:/b");
// Note: drive-relative paths are not always supported "properly" // Note: drive-relative paths are not always supported "properly"
TEST_JOIN("c:/a", "d:b", "d:b"); TEST_JOIN("c:/a", "d:b", "c:/a/d:b");
TEST_JOIN("c:a", "b", "c:a/b"); TEST_JOIN("c:a", "b", "c:a/b");
TEST_JOIN("c:", "b", "c:b"); TEST_JOIN("c:", "b", "c:b");
#endif #endif