From 0ea5d7abfff2947c681b00edc98ad57fabe7613c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 14 Jan 2026 15:27:04 -1000 Subject: [PATCH] [statsd] Use direct appends and stack buffer instead of str_sprintf --- esphome/components/statsd/statsd.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/esphome/components/statsd/statsd.cpp b/esphome/components/statsd/statsd.cpp index 7729f36858..c4b49a09a9 100644 --- a/esphome/components/statsd/statsd.cpp +++ b/esphome/components/statsd/statsd.cpp @@ -114,14 +114,23 @@ void StatsdComponent::update() { // This implies you can't explicitly set a gauge to a negative number without first setting it to zero. if (val < 0) { if (this->prefix_) { - out.append(str_sprintf("%s.", this->prefix_)); + out.append(this->prefix_); + out.append("."); } - out.append(str_sprintf("%s:0|g\n", s.name)); + out.append(s.name); + out.append(":0|g\n"); } if (this->prefix_) { - out.append(str_sprintf("%s.", this->prefix_)); + out.append(this->prefix_); + out.append("."); } - out.append(str_sprintf("%s:%f|g\n", s.name, val)); + out.append(s.name); + // Buffer for ":" + value + "|g\n". + // %g uses max 13 chars for value (sign + 6 significant digits + e+xxx) + // Total: 1 + 13 + 4 = 18 chars + null, use 24 for safety + char val_buf[24]; + snprintf(val_buf, sizeof(val_buf), ":%g|g\n", val); + out.append(val_buf); if (out.length() > SEND_THRESHOLD) { this->send_(&out);