Module: check_mk
Branch: master
Commit: 3dce13b5c7a82ed220370f13fac81ac0177e3b81
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3dce13b5c7a82e…
Author: Sergey Kipnis <sk(a)mathias-kettner.de>
Date: Mon Feb 11 12:13:29 2019 +0100
[CMK-1586] - potential exception is guarded and replaced with Win32 conversion functions
6869 FIX Fixed all crashes in Windows Agent caused by UTF-16 to UTF-8 conversion
Some Windows checks may produce invalid UTF-16 symbols or symbols
which are difficult to convert into UTF-8 using standard library.
As a result the Agent may crash with exception 'range_error' during
'to_bytes' call.
Now in such cases the Agent uses native Win32 API call avoiding the crash.
Provided output from above mentioned Win32 API call conforms to the UTF-8 standard.
Change-Id: I3f5d4a68e1878c9cfd8a2fe6695cea7bcc7e48cf
---
.werks/6869 | 16 ++++++++++++++++
agents/windows/build_version | 2 +-
agents/windows/stringutil.cc | 28 ++++++++++++++++++++++++++++
agents/windows/stringutil.h | 14 +++++++++++++-
4 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/.werks/6869 b/.werks/6869
new file mode 100644
index 0000000..ac22710
--- /dev/null
+++ b/.werks/6869
@@ -0,0 +1,16 @@
+Title: Fixed all crashes in Windows Agent caused by UTF-16 to UTF-8 conversion
+Level: 1
+Component: checks
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1549887562
+
+Some Windows checks may produce invalid UTF-16 symbols or symbols
+which are difficult to convert into UTF-8 using standard library.
+As a result the Agent may crash with exception 'range_error' during
'to_bytes' call.
+
+Now in such cases the Agent uses native Win32 API call avoiding the crash.
+Provided output from above mentioned Win32 API call conforms to the UTF-8 standard.
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 7c85d65..13f14be 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3266
+3268
diff --git a/agents/windows/stringutil.cc b/agents/windows/stringutil.cc
index 8b7f9cf..e509437 100644
--- a/agents/windows/stringutil.cc
+++ b/agents/windows/stringutil.cc
@@ -300,3 +300,31 @@ std::string extractIPAddress(const std::string &inputAddr) {
return inputAddr; // no match, return original input
}
+
+// Windows Native Conversion from Unicode to UTF8.
+// Not standart, but works good for Windows with UTF-16
+// Tested in new windows agent
+// returns empty string in the case of any error
+// no logging support still, this is not so trivial task here
+std::string ConvertToUTF8(const std::wstring &Src) noexcept {
+ using namespace std;
+
+ if (Src.empty()) return {};
+ // Windows only
+ auto in_len = static_cast<int>(Src.length());
+ auto out_len =
+ ::WideCharToMultiByte(CP_UTF8, 0, Src.c_str(), in_len, NULL, 0, 0, 0);
+ if (out_len == 0) return {};
+
+ std::string str;
+ try {
+ str.resize(out_len);
+ } catch (const std::exception &) {
+ return {};
+ }
+
+ // convert
+ ::WideCharToMultiByte(CP_UTF8, 0, Src.c_str(), -1, str.data(), out_len, 0,
+ 0);
+ return str;
+}
diff --git a/agents/windows/stringutil.h b/agents/windows/stringutil.h
index d5de6d5..0fcae84 100644
--- a/agents/windows/stringutil.h
+++ b/agents/windows/stringutil.h
@@ -120,8 +120,20 @@ inline std::vector<std::basic_string<CharT>>
tokenizePossiblyQuoted(
int parse_boolean(const char *value);
+// returns empty string on any error.
+std::string ConvertToUTF8(const std::wstring &Src) noexcept;
+
inline std::string to_utf8(const std::wstring &input) {
- return
std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(input);
+ if (input.empty()) return {};
+
+ try {
+ return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(
+ input);
+ } catch (const std::exception &) {
+ // in the case of error we try to use Windows Conversion
+ // not nice, but effective.
+ return ConvertToUTF8(input);
+ }
}
inline std::wstring to_utf16(const std::string &input) {