Protected from size_t to int overflow in systemd sink

This commit is contained in:
gabime 2019-07-10 02:25:11 +03:00
parent 847f7de003
commit 261d2c5ae4

View File

@ -44,16 +44,23 @@ protected:
{ {
int err; int err;
size_t length = msg.payload.size();
// limit to max int
if(length > std::numeric_limits<int>::max())
{
length = std::numeric_limits<int>::max();
}
// Do not send source location if not available // Do not send source location if not available
if (msg.source.empty()) if (msg.source.empty())
{ {
// Note: function call inside '()' to avoid macro expansion // Note: function call inside '()' to avoid macro expansion
err = (sd_journal_send)( err = (sd_journal_send)(
"MESSAGE=%.*s", static_cast<int>(msg.payload.size()), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), nullptr); "MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), nullptr);
} }
else else
{ {
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(msg.payload.size()), msg.payload.data(), "PRIORITY=%d", err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d",
syslog_level(msg.level), "SOURCE_FILE=%s", msg.source.filename, "SOURCE_LINE=%d", msg.source.line, "SOURCE_FUNC=%s", syslog_level(msg.level), "SOURCE_FILE=%s", msg.source.filename, "SOURCE_LINE=%d", msg.source.line, "SOURCE_FUNC=%s",
msg.source.funcname, nullptr); msg.source.funcname, nullptr);
} }