From aef3cb16fac4792193a3683bd37e4bd8601b0bc8 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Thu, 3 Mar 2022 07:31:08 +0100 Subject: [PATCH] Fixed GetEnvironmentVariableX for windows --- winpr/libwinpr/environment/environment.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/winpr/libwinpr/environment/environment.c b/winpr/libwinpr/environment/environment.c index b8bdc810c..5e3ab69fd 100644 --- a/winpr/libwinpr/environment/environment.c +++ b/winpr/libwinpr/environment/environment.c @@ -666,17 +666,12 @@ DWORD GetEnvironmentVariableX(const char* lpName, char* lpBuffer, DWORD nSize) char lpBufferMaxA[WINPR_MAX_ENVIRONMENT_LENGTH] = { 0 }; WCHAR lpBufferMaxW[WINPR_MAX_ENVIRONMENT_LENGTH] = { 0 }; - // calling GetEnvironmentVariableX with a NULL buffer should return the expected size - // TODO: dynamically allocate the buffer, or use the theoretical limit of 32,768 characters + nSizeW = ARRAYSIZE(lpBufferMaxW); - lpBufferA = lpBufferMaxA; - lpBufferW = lpBufferMaxW; - nSizeW = sizeof(lpBufferMaxW) / 2; + result = GetEnvironmentVariableW(lpNameW, lpBufferMaxW, nSizeW); - result = GetEnvironmentVariableW(lpNameW, lpBufferW, nSizeW); - - status = ConvertFromUnicode(CP_UTF8, 0, lpBufferW, -1, &lpBufferA, sizeof(lpBufferMaxA), - NULL, NULL); + status = ConvertFromUnicode(CP_UTF8, 0, lpBufferMaxW, _wcsnlen(lpBufferMaxW, nSizeW), + &lpBufferMaxA, sizeof(lpBufferMaxA), NULL, NULL); if (status > 0) result = (DWORD)status;