Skip to content

Commit

Permalink
Merge pull request #10298 from Icinga/timestamp-serialization-issues
Browse files Browse the repository at this point in the history
IcingaDB: limit several numbers not to crash Go daemon
  • Loading branch information
yhabteab authored Jan 13, 2025
2 parents f53e534 + cf895e7 commit ee98a9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/icingadb/icingadb-objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2651,8 +2651,8 @@ Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable)

if (!cr->GetCommand().IsEmpty())
attrs->Set("check_commandline", FormatCommandLine(cr->GetCommand()));
attrs->Set("execution_time", TimestampToMilliseconds(fmax(0.0, cr->CalculateExecutionTime())));
attrs->Set("latency", TimestampToMilliseconds(cr->CalculateLatency()));
attrs->Set("execution_time", std::min((long long)UINT32_MAX, TimestampToMilliseconds(fmax(0.0, cr->CalculateExecutionTime()))));
attrs->Set("latency", std::min((long long)UINT32_MAX, TimestampToMilliseconds(cr->CalculateLatency())));
attrs->Set("check_source", cr->GetCheckSource());
attrs->Set("scheduling_source", cr->GetSchedulingSource());
}
Expand Down
14 changes: 13 additions & 1 deletion lib/icingadb/icingadb-utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "icinga/eventcommand.hpp"
#include "icinga/host.hpp"
#include <boost/algorithm/string.hpp>
#include <cmath>
#include <map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -245,7 +246,18 @@ String IcingaDB::GetLowerCaseTypeNameDB(const ConfigObject::Ptr& obj)
}

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

String IcingaDB::IcingaToStreamValue(const Value& value)
Expand Down

0 comments on commit ee98a9e

Please sign in to comment.