Skip to content

Commit 4b7ec72

Browse files
Merge pull request #10844 from Icinga/perfdata-writer-fix-stats-func
Fix potential nullptr-dereference in perfdata writer stats functions
2 parents 75c2eea + 9b93c08 commit 4b7ec72

6 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/perfdata/gelfwriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ void GelfWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perf
4949
for (const GelfWriter::Ptr& gelfwriter : ConfigType::GetObjectsByType<GelfWriter>()) {
5050
size_t workQueueItems = gelfwriter->m_WorkQueue.GetLength();
5151
double workQueueItemRate = gelfwriter->m_WorkQueue.GetTaskCount(60) / 60.0;
52+
auto connection = gelfwriter->m_LockedConnection.load();
5253

5354
nodes.emplace_back(gelfwriter->GetName(), new Dictionary({
5455
{ "work_queue_items", workQueueItems },
5556
{ "work_queue_item_rate", workQueueItemRate },
56-
{ "connected", gelfwriter->m_Connection->IsConnected() },
57+
{ "connected", connection && connection->IsConnected() },
5758
{ "source", gelfwriter->GetSource() }
5859
}));
5960

@@ -91,6 +92,7 @@ void GelfWriter::Resume()
9192
m_WorkQueue.SetExceptionCallback([this](boost::exception_ptr exp) { ExceptionHandler(std::move(exp)); });
9293

9394
m_Connection = new PerfdataWriterConnection{this, GetHost(), GetPort(), m_SslContext, !GetInsecureNoverify()};
95+
m_LockedConnection.store(m_Connection);
9496

9597
/* Register event handlers. */
9698
m_HandleCheckResults = Checkable::OnNewCheckResult.connect([this](const Checkable::Ptr& checkable,

lib/perfdata/gelfwriter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class GelfWriter final : public ObjectImpl<GelfWriter>
3434

3535
private:
3636
PerfdataWriterConnection::Ptr m_Connection;
37+
Locked<PerfdataWriterConnection::Ptr> m_LockedConnection;
3738
WorkQueue m_WorkQueue{10000000, 1};
3839
Shared<boost::asio::ssl::context>::Ptr m_SslContext;
3940

lib/perfdata/graphitewriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ void GraphiteWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&
5858
for (const GraphiteWriter::Ptr& graphitewriter : ConfigType::GetObjectsByType<GraphiteWriter>()) {
5959
size_t workQueueItems = graphitewriter->m_WorkQueue.GetLength();
6060
double workQueueItemRate = graphitewriter->m_WorkQueue.GetTaskCount(60) / 60.0;
61+
auto connection = graphitewriter->m_LockedConnection.load();
6162

6263
nodes.emplace_back(graphitewriter->GetName(), new Dictionary({
6364
{ "work_queue_items", workQueueItems },
6465
{ "work_queue_item_rate", workQueueItemRate },
65-
{ "connected", graphitewriter->m_Connection->IsConnected() }
66+
{ "connected", connection && connection->IsConnected() }
6667
}));
6768

6869
perfdata->Add(new PerfdataValue("graphitewriter_" + graphitewriter->GetName() + "_work_queue_items", workQueueItems));
@@ -86,6 +87,7 @@ void GraphiteWriter::Resume()
8687
m_WorkQueue.SetExceptionCallback([this](boost::exception_ptr exp) { ExceptionHandler(std::move(exp)); });
8788

8889
m_Connection = new PerfdataWriterConnection{this, GetHost(), GetPort()};
90+
m_LockedConnection.store(m_Connection);
8991

9092
/* Register event handlers. */
9193
m_HandleCheckResults = Checkable::OnNewCheckResult.connect([this](const Checkable::Ptr& checkable,

lib/perfdata/graphitewriter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class GraphiteWriter final : public ObjectImpl<GraphiteWriter>
3636

3737
private:
3838
PerfdataWriterConnection::Ptr m_Connection;
39+
Locked<PerfdataWriterConnection::Ptr> m_LockedConnection;
3940
WorkQueue m_WorkQueue{10000000, 1};
4041

4142
boost::signals2::connection m_HandleCheckResults;

lib/perfdata/opentsdbwriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ void OpenTsdbWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&
5555
for (const OpenTsdbWriter::Ptr& opentsdbwriter : ConfigType::GetObjectsByType<OpenTsdbWriter>()) {
5656
size_t workQueueItems = opentsdbwriter->m_WorkQueue.GetLength();
5757
double workQueueItemRate = opentsdbwriter->m_WorkQueue.GetTaskCount(60) / 60.0;
58+
auto connection = opentsdbwriter->m_LockedConnection.load();
5859

5960
nodes.emplace_back(
6061
opentsdbwriter->GetName(),
6162
new Dictionary({
62-
{ "connected", opentsdbwriter->m_Connection->IsConnected() },
63+
{ "connected", connection && connection->IsConnected() },
6364
{"work_queue_items", workQueueItems},
6465
{"work_queue_item_rate", workQueueItemRate}
6566
}
@@ -91,6 +92,7 @@ void OpenTsdbWriter::Resume()
9192
ReadConfigTemplate();
9293

9394
m_Connection = new PerfdataWriterConnection{this, GetHost(), GetPort()};
95+
m_LockedConnection.store(m_Connection);
9496

9597
m_HandleCheckResults = Service::OnNewCheckResult.connect([this](const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr&) {
9698
CheckResultHandler(checkable, cr);

lib/perfdata/opentsdbwriter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class OpenTsdbWriter final : public ObjectImpl<OpenTsdbWriter>
3737
WorkQueue m_WorkQueue{10000000, 1};
3838
std::string m_MsgBuf;
3939
PerfdataWriterConnection::Ptr m_Connection;
40+
Locked<PerfdataWriterConnection::Ptr> m_LockedConnection;
4041

4142
boost::signals2::connection m_HandleCheckResults;
4243

0 commit comments

Comments
 (0)