From 261d2c5ae4517612b434a0d023ba5678bd074f01 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 10 Jul 2019 02:25:11 +0300 Subject: [PATCH] Protected from size_t to int overflow in systemd sink --- include/spdlog/sinks/systemd_sink.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index eeb56bae..4f626afe 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -44,16 +44,23 @@ protected: { int err; + size_t length = msg.payload.size(); + // limit to max int + if(length > std::numeric_limits::max()) + { + length = std::numeric_limits::max(); + } + // Do not send source location if not available if (msg.source.empty()) { // Note: function call inside '()' to avoid macro expansion err = (sd_journal_send)( - "MESSAGE=%.*s", static_cast(msg.payload.size()), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), nullptr); + "MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), nullptr); } else { - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(msg.payload.size()), msg.payload.data(), "PRIORITY=%d", + err = (sd_journal_send)("MESSAGE=%.*s", static_cast(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", msg.source.funcname, nullptr); }