From bf9fe449cd51afff8235f3329bc54cb05e381428 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sun, 27 Mar 2016 14:25:46 -0400 Subject: [PATCH] =?UTF-8?q?Correct=20buffer=20management=20with=20Windows?= =?UTF-8?q?=20system=20calls.=20Credit:=20Reiner=20Eitelj=C3=B6rge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tz.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tz.cpp b/tz.cpp index a88b8e7..72f6709 100644 --- a/tz.cpp +++ b/tz.cpp @@ -223,13 +223,13 @@ namespace // Put types in an anonymous name space. value.clear(); wchar_t value_buffer[256]; // in/out parameter. Documentation say that size is a count of bytes not chars. - DWORD size = sizeof(value_buffer); + DWORD size = sizeof(value_buffer) - sizeof(value_buffer[0]); DWORD tzi_type = REG_SZ; if (RegQueryValueExW(handle(), key_name, nullptr, &tzi_type, reinterpret_cast(value_buffer), &size) == ERROR_SUCCESS) { // Function does not guarantee to null terminate. - value_buffer[size] = L'\0'; + value_buffer[size/sizeof(value_buffer[0])] = L'\0'; std::wstring_convert> converter; value = converter.to_bytes(value_buffer); return true; @@ -293,7 +293,7 @@ static void get_windows_timezone_info(std::vector& tz_list) { timezone_info tz; - size = (DWORD) sizeof(zone_key_name); + size = (DWORD) sizeof(zone_key_name)/sizeof(zone_key_name[0]); auto status = RegEnumKeyExW(zones_key.handle(), zone_index, zone_key_name, &size, nullptr, nullptr, nullptr, nullptr); if (status != ERROR_SUCCESS && status != ERROR_NO_MORE_ITEMS) @@ -301,7 +301,6 @@ static void get_windows_timezone_info(std::vector& tz_list) + get_win32_message(status)); if (status == ERROR_NO_MORE_ITEMS) break; - zone_key_name[size] = L'\0'; std::wstring_convert> converter; tz.timezone_id = converter.to_bytes(zone_key_name);