various: use correct PATH_MAX for win32

In commit c09245cdf2
long-path support was enabled for mpv without actually
making sure that there was no code left that used the
old limit (260 Unicode chars) for buffer sizes.
This commit fixes all but one case.
This commit is contained in:
sfan5 2023-12-23 18:46:54 +01:00
parent f57ec94d9f
commit 9565675488
6 changed files with 35 additions and 15 deletions

View File

@ -576,9 +576,10 @@ static void init_session_display(struct wasapi_state *state, const char *name) {
(void **)&state->pSessionControl); (void **)&state->pSessionControl);
EXIT_ON_ERROR(hr); EXIT_ON_ERROR(hr);
wchar_t path[MAX_PATH] = {0}; wchar_t *path = talloc_array(NULL, wchar_t, MP_PATH_MAX);
GetModuleFileNameW(NULL, path, MAX_PATH); GetModuleFileNameW(NULL, path, MP_PATH_MAX);
hr = IAudioSessionControl_SetIconPath(state->pSessionControl, path, NULL); hr = IAudioSessionControl_SetIconPath(state->pSessionControl, path, NULL);
talloc_free(path);
if (FAILED(hr)) { if (FAILED(hr)) {
// don't goto exit_label here since SetDisplayName might still work // don't goto exit_label here since SetDisplayName might still work
MP_WARN(state, "Error setting audio session icon: %s\n", MP_WARN(state, "Error setting audio session icon: %s\n",

View File

@ -80,9 +80,19 @@ int mp_make_wakeup_pipe(int pipes[2]);
void mp_flush_wakeup_pipe(int pipe_end); void mp_flush_wakeup_pipe(int pipe_end);
#ifdef _WIN32 #ifdef _WIN32
#include <wchar.h> #include <wchar.h>
wchar_t *mp_from_utf8(void *talloc_ctx, const char *s); wchar_t *mp_from_utf8(void *talloc_ctx, const char *s);
char *mp_to_utf8(void *talloc_ctx, const wchar_t *s); char *mp_to_utf8(void *talloc_ctx, const wchar_t *s);
// Use this in win32-specific code rather than PATH_MAX or MAX_PATH.
// This is necessary because we declare long-path aware support which raises
// the effective limit without affecting any defines.
// The actual limit is 32767 but there's a few edge cases that reduce
// it. So pick this nice round number.
// Note that this is wchars, not chars.
#define MP_PATH_MAX (32000)
#endif #endif
#ifdef __CYGWIN__ #ifdef __CYGWIN__

View File

@ -27,9 +27,13 @@ WINBASEAPI DWORD WINAPI GetCurrentDirectoryW(DWORD nBufferLength, LPWSTR lpBuffe
const char *mp_get_platform_path_uwp(void *talloc_ctx, const char *type) const char *mp_get_platform_path_uwp(void *talloc_ctx, const char *type)
{ {
if (strcmp(type, "home") == 0) { if (strcmp(type, "home") == 0) {
wchar_t homeDir[_MAX_PATH]; DWORD count = GetCurrentDirectoryW(0, NULL);
if (GetCurrentDirectoryW(_MAX_PATH, homeDir) != 0) wchar_t *home_dir = talloc_array(NULL, wchar_t, count);
return mp_to_utf8(talloc_ctx, homeDir); if (GetCurrentDirectoryW(count, home_dir) != 0) {
char *ret = mp_to_utf8(talloc_ctx, home_dir);
talloc_free(home_dir);
return ret;
}
} }
return NULL; return NULL;
} }

View File

@ -24,17 +24,15 @@
#include "osdep/path.h" #include "osdep/path.h"
#include "osdep/threads.h" #include "osdep/threads.h"
// Warning: do not use PATH_MAX. Cygwin messed it up.
static mp_once path_init_once = MP_STATIC_ONCE_INITIALIZER; static mp_once path_init_once = MP_STATIC_ONCE_INITIALIZER;
static char *portable_path; static char *portable_path;
static char *mp_get_win_exe_dir(void *talloc_ctx) static char *mp_get_win_exe_dir(void *talloc_ctx)
{ {
wchar_t w_exedir[MAX_PATH + 1] = {0}; wchar_t *w_exedir = talloc_array(NULL, wchar_t, MP_PATH_MAX);
int len = (int)GetModuleFileNameW(NULL, w_exedir, MAX_PATH); int len = (int)GetModuleFileNameW(NULL, w_exedir, MP_PATH_MAX);
int imax = 0; int imax = 0;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if (w_exedir[i] == '\\') { if (w_exedir[i] == '\\') {
@ -42,10 +40,11 @@ static char *mp_get_win_exe_dir(void *talloc_ctx)
imax = i; imax = i;
} }
} }
w_exedir[imax] = '\0'; w_exedir[imax] = '\0';
return mp_to_utf8(talloc_ctx, w_exedir); char *ret = mp_to_utf8(talloc_ctx, w_exedir);
talloc_free(w_exedir);
return ret;
} }
static char *mp_get_win_exe_subdir(void *ta_ctx, const char *name) static char *mp_get_win_exe_subdir(void *ta_ctx, const char *name)

View File

@ -19,6 +19,9 @@
#include <stdio.h> #include <stdio.h>
#include <windows.h> #include <windows.h>
// copied from osdep/io.h since this file is standalone
#define MP_PATH_MAX (32000)
int wmain(int argc, wchar_t **argv, wchar_t **envp); int wmain(int argc, wchar_t **argv, wchar_t **envp);
static void cr_perror(const wchar_t *prefix) static void cr_perror(const wchar_t *prefix)
@ -75,10 +78,11 @@ static int cr_runproc(wchar_t *name, wchar_t *cmdline)
int wmain(int argc, wchar_t **argv, wchar_t **envp) int wmain(int argc, wchar_t **argv, wchar_t **envp)
{ {
wchar_t *cmd; wchar_t *cmd;
wchar_t exe[MAX_PATH]; wchar_t *exe;
cmd = GetCommandLineW(); cmd = GetCommandLineW();
GetModuleFileNameW(NULL, exe, MAX_PATH); exe = LocalAlloc(LPTR, MP_PATH_MAX * sizeof(wchar_t));
GetModuleFileNameW(NULL, exe, MP_PATH_MAX);
wcscpy(wcsrchr(exe, '.') + 1, L"exe"); wcscpy(wcsrchr(exe, '.') + 1, L"exe");
// Set an environment variable so the child process can tell whether it // Set an environment variable so the child process can tell whether it

View File

@ -578,18 +578,20 @@ static double get_refresh_rate_from_gdi(const wchar_t *device)
static char *get_color_profile(void *ctx, const wchar_t *device) static char *get_color_profile(void *ctx, const wchar_t *device)
{ {
char *name = NULL; char *name = NULL;
wchar_t *wname = NULL;
HDC ic = CreateICW(device, NULL, NULL, NULL); HDC ic = CreateICW(device, NULL, NULL, NULL);
if (!ic) if (!ic)
goto done; goto done;
wchar_t wname[MAX_PATH + 1]; wname = talloc_array(NULL, wchar_t, MP_PATH_MAX);
if (!GetICMProfileW(ic, &(DWORD){ MAX_PATH }, wname)) if (!GetICMProfileW(ic, &(DWORD){ MP_PATH_MAX - 1 }, wname))
goto done; goto done;
name = mp_to_utf8(ctx, wname); name = mp_to_utf8(ctx, wname);
done: done:
if (ic) if (ic)
DeleteDC(ic); DeleteDC(ic);
talloc_free(wname);
return name; return name;
} }