From 46c0e1769bf24656be7add69562ea8617ab75265 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 31 Mar 2024 17:25:13 +0200 Subject: [PATCH] win: robustify uv_os_getenv() error checking (#4339) Make it less likely for the thread-local error value to get clobbered between performing the operation and checking the result. Refs: https://github.com/libuv/libuv/issues/4338 --- src/win/util.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/win/util.c b/src/win/util.c index a96cb915..c4dd8bf7 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -1259,6 +1259,9 @@ int uv_os_getenv(const char* name, char* buffer, size_t* size) { SetLastError(ERROR_SUCCESS); len = GetEnvironmentVariableW(name_w, var, varlen); + if (len == 0) + r = uv_translate_sys_error(GetLastError()); + if (len < varlen) break; @@ -1280,15 +1283,8 @@ int uv_os_getenv(const char* name, char* buffer, size_t* size) { uv__free(name_w); name_w = NULL; - if (len == 0) { - r = GetLastError(); - if (r != ERROR_SUCCESS) { - r = uv_translate_sys_error(r); - goto fail; - } - } - - r = uv__copy_utf16_to_utf8(var, len, buffer, size); + if (r == 0) + r = uv__copy_utf16_to_utf8(var, len, buffer, size); fail: