diff --git a/esphome/components/syslog/esphome_syslog.cpp b/esphome/components/syslog/esphome_syslog.cpp index 610a1243a3..02f2bbe8c5 100644 --- a/esphome/components/syslog/esphome_syslog.cpp +++ b/esphome/components/syslog/esphome_syslog.cpp @@ -42,31 +42,38 @@ void Syslog::log_(const int level, const char *tag, const char *message, size_t len -= 11; } - // Build syslog packet on stack - 508 is max UDP packet size + // Build syslog packet on stack (508 bytes chosen as practical limit for syslog over UDP) char packet[508]; size_t offset = 0; + size_t remaining = sizeof(packet); // Write PRI - int ret = snprintf(packet, sizeof(packet), "<%d>", pri); - if (ret > 0) + int ret = snprintf(packet, remaining, "<%d>", pri); + if (ret > 0 && static_cast(ret) < remaining) { offset = ret; + remaining -= ret; + } // Write timestamp directly into packet (RFC 5424: use "-" if time not valid) auto now = this->time_->now(); if (now.is_valid()) { - offset += now.strftime(packet + offset, sizeof(packet) - offset, "%b %e %H:%M:%S"); - } else { + size_t written = now.strftime(packet + offset, remaining, "%b %e %H:%M:%S"); + offset += written; + remaining -= written; + } else if (remaining > 0) { packet[offset++] = '-'; + remaining--; } // Write hostname, tag, and message - ret = snprintf(packet + offset, sizeof(packet) - offset, " %s %s: %.*s", App.get_name().c_str(), tag, (int) len, - message); - if (ret > 0) - offset += ret; + ret = snprintf(packet + offset, remaining, " %s %s: %.*s", App.get_name().c_str(), tag, (int) len, message); + if (ret > 0) { + // snprintf returns chars that would be written; clamp to actual buffer space + offset += std::min(static_cast(ret), remaining > 0 ? remaining - 1 : 0); + } if (offset > 0) { - this->parent_->send_packet(reinterpret_cast(packet), std::min(offset, sizeof(packet) - 1)); + this->parent_->send_packet(reinterpret_cast(packet), offset); } }