Module: check_mk
Branch: master
Commit: 2555cc8a3223e3fe98610a36f64ddbf56f246cd9
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2555cc8a3223e3…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Fri Oct 27 12:38:54 2017 +0200
Fix Win-agent crash when failing to read registry
Windows agent crashed when failing to read perf counters from registry.
Throw and catch exceptions when storing configurables instead of
returning and storing nullptr's that get never checked afterwards.
Broken by the huge refactoring c5ee818358e695b01c2f2dbc05ad0713723a37f8
Change-Id: I897fc158acaf608e8cc96bd2ca403afa95044b57
---
agents/windows/Configurable.h | 14 +++++++++++---
agents/windows/build_version | 2 +-
agents/windows/types.cc | 24 +++++++++---------------
agents/windows/types.h | 6 ++++++
4 files changed, 27 insertions(+), 19 deletions(-)
diff --git a/agents/windows/Configurable.h b/agents/windows/Configurable.h
index 3ffd712..043879d 100644
--- a/agents/windows/Configurable.h
+++ b/agents/windows/Configurable.h
@@ -89,7 +89,11 @@ public:
virtual void startBlock() { _block_mode.startBlock(_values); }
virtual void feed(const std::string &, const std::string &value) override {
- this->add(from_string<DataT>(_winapi, value));
+ try {
+ this->add(from_string<DataT>(_winapi, value));
+ } catch (const StringConversionError &e) {
+ std::cerr << e.what() << std::endl;
+ }
}
virtual void output(const std::string &key,
@@ -158,8 +162,12 @@ public:
key = std::string(var.begin() + pos + 1, var.end());
}
startBlock();
- _add_mode.add(_values,
- std::make_pair(key, from_string<DataT>(_winapi, value)));
+ try {
+ _add_mode.add(_values, std::make_pair(key, from_string<DataT>(
+ _winapi, value)));
+ } catch (const StringConversionError &e) {
+ std::cerr << e.what() << std::endl;
+ }
}
virtual void output(const std::string &key,
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 1648816..ce89b3e 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-2990
+2992
diff --git a/agents/windows/types.cc b/agents/windows/types.cc
index 806c3e2..4fe4e5d 100644
--- a/agents/windows/types.cc
+++ b/agents/windows/types.cc
@@ -88,22 +88,18 @@ mrpe_entry *from_string<mrpe_entry *>(const WinApiAdaptor
&winapi,
}
if (command_line.empty()) {
- fprintf(stderr,
- "Invalid command specification for mrpe:\r\n"
- "Format: SERVICEDESC COMMANDLINE\r\n");
- return nullptr;
+ throw StringConversionError(
+ "Invalid command specification for mrpe:\r\n"
+ "Format: SERVICEDESC COMMANDLINE");
}
if (winapi.PathIsRelative(command_line.c_str())) {
Environment *env = Environment::instance();
- if (env != nullptr) {
- snprintf(result->command_line, sizeof(result->command_line),
- "%s\\%s", env->agentDirectory().c_str(),
- lstrip(command_line.c_str()));
- } else {
- fprintf(stderr, "No environment\n");
- return nullptr;
+ if (env == nullptr) {
+ throw StringConversionError("No environment");
}
+ snprintf(result->command_line, sizeof(result->command_line),
"%s\\%s",
+ env->agentDirectory().c_str(), lstrip(command_line.c_str()));
} else {
strncpy(result->command_line, command_line.c_str(),
sizeof(result->command_line));
@@ -150,10 +146,8 @@ winperf_counter *from_string<winperf_counter *>(const
WinApiAdaptor &winapi,
result->id =
PerfCounterObject::resolve_counter_name(winapi, base_id.c_str());
if (result->id == -1) {
- fprintf(stderr,
- "No matching performance counter id found for %s.\n",
- value.c_str());
- return nullptr;
+ throw StringConversionError(
+ "No matching performance counter id found for " + value);
}
}
diff --git a/agents/windows/types.h b/agents/windows/types.h
index 61e3c2f..c25d614 100644
--- a/agents/windows/types.h
+++ b/agents/windows/types.h
@@ -82,6 +82,12 @@ static const unsigned int SECTION_WINPERF =
SECTION_WINPERF_IF | SECTION_WINPERF_CPU | SECTION_WINPERF_PHYDISK |
SECTION_WINPERF_CONFIG;
+class StringConversionError : public std::invalid_argument {
+public:
+ explicit StringConversionError(const std::string &what)
+ : std::invalid_argument(what) {}
+};
+
template <typename T>
T from_string(const WinApiAdaptor &winapi, const std::string &input);