From 912d65cba97db3d27a747decb1c1635aece8bb90 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 9 Mar 2024 06:24:42 +0000 Subject: win32: improvements to realpath(3) The previous commit unnecessarily duplicated the path returned from resolve_symlinks(), resulting in a memory leak. Thanks to avih for spotting this. Even if we can't canonicalise the path in resolve_symlinks() we can at least return the result of resolving the trailing symlink. Moreover, this can be done both when the necessary API is missing and when the filesystem doesn't support it. Not perfect, but better than nothing, and there's no cost. This change gets rid of a handful of realpath-related test failures on ReactOS and Windows XP. Some background: Commit 8ebe81483 returned the raw path when the required API was missing. This was reverted in commit f902184fa because it caused an infinite loop (GitHub issue #204). Commit 31467ddfc fixed the cause of the loop, which is why it's now safe to reintroduce a fallback return. (GitHub commit #389) --- win32/mingw.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/win32/mingw.c b/win32/mingw.c index eef3ab5d3..a8170bb9c 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1551,7 +1551,9 @@ static char *resolve_symlinks(char *path) FILE_FLAG_BACKUP_SEMANTICS, NULL); if (h != INVALID_HANDLE_VALUE) { if (!INIT_PROC_ADDR(kernel32.dll, GetFinalPathNameByHandleA)) { - errno = ENOSYS; + if (resolve) + strcpy(path, resolve); + ptr = path; goto end; } @@ -1562,7 +1564,9 @@ static char *resolve_symlinks(char *path) ptr = normalize_ntpathA(path); goto end; } else if (err_win_to_posix() == ENOSYS) { - ptr = xstrdup(path); + if (resolve) + strcpy(path, resolve); + ptr = path; goto end; } } -- cgit v1.2.3-55-g6feb