win: fix registry API error handling

The Reg* APIs on Windows don't use GetLastError() to report
failures. The errors are returned directly from the call.

For systems which don't have one of the values GetLastError() can
end up returning 0 to the caller, indicating success. The caller
then assumes that the data is valid and can attempt to execute on
garbage data. This change fixes the flow to correctly return the
error to the caller.

PR-URL: https://github.com/libuv/libuv/pull/1811
Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Kyle Farnung 2018-04-20 10:03:26 -07:00 committed by cjihrig
parent 1e4823ca99
commit 0aa6de6deb
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -615,7 +615,7 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
DWORD sppi_size;
SYSTEM_INFO system_info;
DWORD cpu_count, r, i;
DWORD cpu_count, i;
NTSTATUS status;
ULONG result_size;
int err;
@ -670,34 +670,33 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
assert(len > 0 && len < ARRAY_SIZE(key_name));
r = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
key_name,
0,
KEY_QUERY_VALUE,
&processor_key);
if (r != ERROR_SUCCESS) {
err = GetLastError();
err = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
key_name,
0,
KEY_QUERY_VALUE,
&processor_key);
if (err != ERROR_SUCCESS) {
goto error;
}
if (RegQueryValueExW(processor_key,
L"~MHz",
NULL,
NULL,
(BYTE*) &cpu_speed,
&cpu_speed_size) != ERROR_SUCCESS) {
err = GetLastError();
err = RegQueryValueExW(processor_key,
L"~MHz",
NULL,
NULL,
(BYTE*)&cpu_speed,
&cpu_speed_size);
if (err != ERROR_SUCCESS) {
RegCloseKey(processor_key);
goto error;
}
if (RegQueryValueExW(processor_key,
L"ProcessorNameString",
NULL,
NULL,
(BYTE*) &cpu_brand,
&cpu_brand_size) != ERROR_SUCCESS) {
err = GetLastError();
err = RegQueryValueExW(processor_key,
L"ProcessorNameString",
NULL,
NULL,
(BYTE*)&cpu_brand,
&cpu_brand_size);
if (err != ERROR_SUCCESS) {
RegCloseKey(processor_key);
goto error;
}