ext/libuv/src/win/util.c in libuv-4.0.1 vs ext/libuv/src/win/util.c in libuv-4.0.2
- old
+ new
@@ -35,12 +35,12 @@
#include <iphlpapi.h>
#include <psapi.h>
#include <tlhelp32.h>
#include <windows.h>
#include <userenv.h>
-#include <math.h>
+
/*
* Max title length; the only thing MSDN tells us about the maximum length
* of the console title is that it is smaller than 64K. However in practice
* it is much smaller, and there is no way to figure out what the exact length
* of the title is or can be, at least not on XP. To make it even more
@@ -329,15 +329,10 @@
return (uint64_t)memory_status.ullTotalPhys;
}
-uv_pid_t uv_os_getpid(void) {
- return GetCurrentProcessId();
-}
-
-
uv_pid_t uv_os_getppid(void) {
int parent_pid = -1;
HANDLE handle;
PROCESSENTRY32 pe;
DWORD current_pid = GetCurrentProcessId();
@@ -585,12 +580,12 @@
goto internalError;
} else {
BYTE* address = (BYTE*) object_type + object_type->DefinitionLength +
counter_definition->CounterOffset;
uint64_t value = *((uint64_t*) address);
- *uptime = floor((double) (object_type->PerfTime.QuadPart - value) /
- (double) object_type->PerfFreq.QuadPart);
+ *uptime = (double) (object_type->PerfTime.QuadPart - value) /
+ (double) object_type->PerfFreq.QuadPart;
uv__free(malloced_buffer);
return 0;
}
}
@@ -613,11 +608,11 @@
int uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
uv_cpu_info_t* cpu_infos;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
DWORD sppi_size;
SYSTEM_INFO system_info;
- DWORD cpu_count, i;
+ DWORD cpu_count, r, i;
NTSTATUS status;
ULONG result_size;
int err;
uv_cpu_info_t* cpu_info;
@@ -668,37 +663,38 @@
L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d",
i);
assert(len > 0 && len < ARRAY_SIZE(key_name));
- err = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
- key_name,
- 0,
- KEY_QUERY_VALUE,
- &processor_key);
- if (err != ERROR_SUCCESS) {
+ r = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+ key_name,
+ 0,
+ KEY_QUERY_VALUE,
+ &processor_key);
+ if (r != ERROR_SUCCESS) {
+ err = GetLastError();
goto error;
}
- err = RegQueryValueExW(processor_key,
- L"~MHz",
- NULL,
- NULL,
- (BYTE*)&cpu_speed,
- &cpu_speed_size);
- if (err != ERROR_SUCCESS) {
+ if (RegQueryValueExW(processor_key,
+ L"~MHz",
+ NULL,
+ NULL,
+ (BYTE*) &cpu_speed,
+ &cpu_speed_size) != ERROR_SUCCESS) {
+ err = GetLastError();
RegCloseKey(processor_key);
goto error;
}
- err = RegQueryValueExW(processor_key,
- L"ProcessorNameString",
- NULL,
- NULL,
- (BYTE*)&cpu_brand,
- &cpu_brand_size);
- if (err != ERROR_SUCCESS) {
+ if (RegQueryValueExW(processor_key,
+ L"ProcessorNameString",
+ NULL,
+ NULL,
+ (BYTE*) &cpu_brand,
+ &cpu_brand_size) != ERROR_SUCCESS) {
+ err = GetLastError();
RegCloseKey(processor_key);
goto error;
}
RegCloseKey(processor_key);
@@ -1145,20 +1141,56 @@
}
int uv_os_homedir(char* buffer, size_t* size) {
uv_passwd_t pwd;
+ wchar_t path[MAX_PATH];
+ DWORD bufsize;
size_t len;
int r;
- /* Check if the USERPROFILE environment variable is set first. The task of
- performing input validation on buffer and size is taken care of by
- uv_os_getenv(). */
- r = uv_os_getenv("USERPROFILE", buffer, size);
+ if (buffer == NULL || size == NULL || *size == 0)
+ return UV_EINVAL;
- /* Don't return an error if USERPROFILE was not found. */
- if (r != UV_ENOENT)
- return r;
+ /* Check if the USERPROFILE environment variable is set first */
+ len = GetEnvironmentVariableW(L"USERPROFILE", path, MAX_PATH);
+
+ if (len == 0) {
+ r = GetLastError();
+
+ /* Don't return an error if USERPROFILE was not found */
+ if (r != ERROR_ENVVAR_NOT_FOUND)
+ return uv_translate_sys_error(r);
+ } else if (len > MAX_PATH) {
+ /* This should not be possible */
+ return UV_EIO;
+ } else {
+ /* Check how much space we need */
+ bufsize = WideCharToMultiByte(CP_UTF8, 0, path, -1, NULL, 0, NULL, NULL);
+
+ if (bufsize == 0) {
+ return uv_translate_sys_error(GetLastError());
+ } else if (bufsize > *size) {
+ *size = bufsize;
+ return UV_ENOBUFS;
+ }
+
+ /* Convert to UTF-8 */
+ bufsize = WideCharToMultiByte(CP_UTF8,
+ 0,
+ path,
+ -1,
+ buffer,
+ *size,
+ NULL,
+ NULL);
+
+ if (bufsize == 0)
+ return uv_translate_sys_error(GetLastError());
+
+ *size = bufsize - 1;
+ return 0;
+ }
/* USERPROFILE is not set, so call uv__getpwuid_r() */
r = uv__getpwuid_r(&pwd);
if (r != 0) {