From 48a387938dcc4aea7e3e2ece4494fc68bc8fa4e5 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Mon, 11 Nov 2024 16:26:50 +0100 Subject: [PATCH] [winpr,env] fix use of getcwd do not rely on GNU_SOURCE extensions --- winpr/libwinpr/environment/environment.c | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/winpr/libwinpr/environment/environment.c b/winpr/libwinpr/environment/environment.c index 600b15f20..84743e061 100644 --- a/winpr/libwinpr/environment/environment.c +++ b/winpr/libwinpr/environment/environment.c @@ -24,12 +24,15 @@ #include #include #include +#include #include #include #ifndef _WIN32 +#include + #ifdef WINPR_HAVE_UNISTD_H #include #endif @@ -43,20 +46,35 @@ DWORD GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer) { - char* cwd = NULL; size_t length = 0; + char* cwd = NULL; + char* ccwd = NULL; - cwd = getcwd(NULL, 0); + do + { + length += MAX_PATH; + char* tmp = realloc(cwd, length); + if (!tmp) + { + free(cwd); + return 0; + } + cwd = tmp; - if (!cwd) + ccwd = getcwd(cwd, length); + } while (!ccwd && (errno == ERANGE)); + + if (!ccwd) + { + free(cwd); return 0; + } - length = strlen(cwd); + length = strnlen(cwd, length); if ((nBufferLength == 0) && (lpBuffer == NULL)) { free(cwd); - return (DWORD)length; } else