Skip to content

Commit fdfa11b

Browse files
authored
Merge pull request #10244 from Icinga/IcingaDB-int-range213
IcingaDB: limit several numbers not to crash Go daemon
2 parents 88856ba + dd51b81 commit fdfa11b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/icingadb/icingadb-objects.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,8 +2644,8 @@ Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable)
26442644

26452645
if (!cr->GetCommand().IsEmpty())
26462646
attrs->Set("check_commandline", FormatCommandLine(cr->GetCommand()));
2647-
attrs->Set("execution_time", TimestampToMilliseconds(fmax(0.0, cr->CalculateExecutionTime())));
2648-
attrs->Set("latency", TimestampToMilliseconds(cr->CalculateLatency()));
2647+
attrs->Set("execution_time", std::min((long long)UINT32_MAX, TimestampToMilliseconds(fmax(0.0, cr->CalculateExecutionTime()))));
2648+
attrs->Set("latency", std::min((long long)UINT32_MAX, TimestampToMilliseconds(cr->CalculateLatency())));
26492649
attrs->Set("check_source", cr->GetCheckSource());
26502650
attrs->Set("scheduling_source", cr->GetSchedulingSource());
26512651
}

lib/icingadb/icingadb-utility.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "icinga/eventcommand.hpp"
1919
#include "icinga/host.hpp"
2020
#include <boost/algorithm/string.hpp>
21+
#include <cmath>
2122
#include <map>
2223
#include <utility>
2324
#include <vector>
@@ -239,7 +240,18 @@ String IcingaDB::GetLowerCaseTypeNameDB(const ConfigObject::Ptr& obj)
239240
}
240241

241242
long long IcingaDB::TimestampToMilliseconds(double timestamp) {
242-
return static_cast<long long>(timestamp * 1000);
243+
// In addition to the limits of the Icinga DB MySQL (0 - 2^64) and PostgreSQL (0 - 2^63) schemata,
244+
// years not fitting in YYYY may cause problems, see e.g. https://github.com/golang/go/issues/4556.
245+
// RFC 3339: "All dates and times are assumed to be (...) somewhere between 0000AD and 9999AD."
246+
//
247+
// The below upper limit includes a safety buffer to make sure the timestamp is within 9999AD in all time zones:
248+
// $ date -ud @253402214400
249+
// Fri Dec 31 00:00:00 UTC 9999
250+
// $ TZ=Asia/Vladivostok date -d @253402214400
251+
// Fri Dec 31 10:00:00 +10 9999
252+
// $ TZ=America/Juneau date -d @253402214400
253+
// Thu Dec 30 15:00:00 AKST 9999
254+
return std::fmin(std::fmax(timestamp, 0.0), 253402214400.0) * 1000.0;
243255
}
244256

245257
String IcingaDB::IcingaToStreamValue(const Value& value)

0 commit comments

Comments
 (0)