From 296819176c9a9534e0b96ec80a7bd0638d64f721 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Fri, 4 Aug 2017 18:32:18 -0400 Subject: [PATCH 1/4] process: EnvGetter to use libuv --- src/node.cc | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/node.cc b/src/node.cc index 81ea92b7e20b..9dc64e207e9d 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2752,28 +2752,12 @@ static void EnvGetter(Local property, if (property->IsSymbol()) { return info.GetReturnValue().SetUndefined(); } -#ifdef __POSIX__ node::Utf8Value key(isolate, property); - const char* val = getenv(*key); - if (val) { - return info.GetReturnValue().Set(String::NewFromUtf8(isolate, val)); - } -#else // _WIN32 - node::TwoByteValue key(isolate, property); - WCHAR buffer[32767]; // The maximum size allowed for environment variables. - DWORD result = GetEnvironmentVariableW(reinterpret_cast(*key), - buffer, - arraysize(buffer)); - // If result >= sizeof buffer the buffer was too small. That should never - // happen. If result == 0 and result != ERROR_SUCCESS the variable was not - // not found. - if ((result > 0 || GetLastError() == ERROR_SUCCESS) && - result < arraysize(buffer)) { - const uint16_t* two_byte_buffer = reinterpret_cast(buffer); - Local rc = String::NewFromTwoByte(isolate, two_byte_buffer); - return info.GetReturnValue().Set(rc); + char buffer[32767]; + int ret = uv_os_getenv(*key, buffer, arraysize(buffer)); + if (!ret) { + return info.GetReturnValue().Set(String::NewFromUtf8(isolate, buffer)); } -#endif } @@ -2809,8 +2793,8 @@ static void EnvQuery(Local property, #else // _WIN32 node::TwoByteValue key(info.GetIsolate(), property); WCHAR* key_ptr = reinterpret_cast(*key); - if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 || - GetLastError() == ERROR_SUCCESS) { + GetEnvironmentVariableW(key_ptr, nullptr, 0); + if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) { rc = 0; if (key_ptr[0] == L'=') { // Environment variables that start with '=' are hidden and read-only. From a3464e6559f370ad701d1c17848b7bdc0a2f27f2 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Tue, 21 Nov 2017 10:57:00 -0500 Subject: [PATCH 2/4] [sqwsh] correct usage of uv_os_getenv & implement in EnvQuery --- src/node.cc | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/node.cc b/src/node.cc index 9dc64e207e9d..5da61d554f2f 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2753,8 +2753,11 @@ static void EnvGetter(Local property, return info.GetReturnValue().SetUndefined(); } node::Utf8Value key(isolate, property); - char buffer[32767]; - int ret = uv_os_getenv(*key, buffer, arraysize(buffer)); + // On POSIX there is not explicitly defined size limit, but on Windows + // environment variables have a maximum size limit of 2**15 - 1. + char buffer[32768]; + size_t buf_size = arraysize(buffer); + int ret = uv_os_getenv(*key, buffer, &buf_size); if (!ret) { return info.GetReturnValue().Set(String::NewFromUtf8(isolate, buffer)); } @@ -2786,24 +2789,22 @@ static void EnvQuery(Local property, const PropertyCallbackInfo& info) { int32_t rc = -1; // Not found unless proven otherwise. if (property->IsString()) { -#ifdef __POSIX__ node::Utf8Value key(info.GetIsolate(), property); - if (getenv(*key)) - rc = 0; -#else // _WIN32 - node::TwoByteValue key(info.GetIsolate(), property); - WCHAR* key_ptr = reinterpret_cast(*key); - GetEnvironmentVariableW(key_ptr, nullptr, 0); - if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) { + // We are only interested in exsistance, so we can keep the buffer small. + char buffer[256]; + size_t = sizeof(buffer); + int ret = uv_os_getenv(*key, buffer, &buf_size); + if (ret != UV_ENOENT) { rc = 0; - if (key_ptr[0] == L'=') { +#ifdef _WIN32 + if (*key[0] == L'=') { // Environment variables that start with '=' are hidden and read-only. rc = static_cast(v8::ReadOnly) | static_cast(v8::DontDelete) | static_cast(v8::DontEnum); } - } #endif + } } if (rc != -1) info.GetReturnValue().Set(rc); From ead9c39e59ea7277bd1d077fd85b26642580f53c Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Tue, 21 Nov 2017 11:04:41 -0500 Subject: [PATCH 3/4] [sqwsh] typos & syntax errors --- src/node.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/node.cc b/src/node.cc index 5da61d554f2f..4e561b280d38 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2756,7 +2756,7 @@ static void EnvGetter(Local property, // On POSIX there is not explicitly defined size limit, but on Windows // environment variables have a maximum size limit of 2**15 - 1. char buffer[32768]; - size_t buf_size = arraysize(buffer); + size_t buf_size = sizeof(buffer); int ret = uv_os_getenv(*key, buffer, &buf_size); if (!ret) { return info.GetReturnValue().Set(String::NewFromUtf8(isolate, buffer)); @@ -2790,9 +2790,9 @@ static void EnvQuery(Local property, int32_t rc = -1; // Not found unless proven otherwise. if (property->IsString()) { node::Utf8Value key(info.GetIsolate(), property); - // We are only interested in exsistance, so we can keep the buffer small. + // We are only interested in existence, so we can keep the buffer small. char buffer[256]; - size_t = sizeof(buffer); + size_t buf_size = sizeof(buffer); int ret = uv_os_getenv(*key, buffer, &buf_size); if (ret != UV_ENOENT) { rc = 0; From a23bdb543ba84122875666199e71b3ea59786c5d Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Tue, 21 Nov 2017 12:36:36 -0500 Subject: [PATCH 4/4] [sqwsh] on boy --- src/node.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 4e561b280d38..63e415c9d82f 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2797,7 +2797,7 @@ static void EnvQuery(Local property, if (ret != UV_ENOENT) { rc = 0; #ifdef _WIN32 - if (*key[0] == L'=') { + if (key[0] == L'=') { // Environment variables that start with '=' are hidden and read-only. rc = static_cast(v8::ReadOnly) | static_cast(v8::DontDelete) |