From 280d46002535de0ae2b88678ef6e804aa7959cb1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 19 Jan 2026 17:40:20 -1000 Subject: [PATCH] [statsd] Use direct appends and stack buffer instead of str_sprintf (#13223) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- esphome/components/statsd/statsd.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/esphome/components/statsd/statsd.cpp b/esphome/components/statsd/statsd.cpp index 7729f36858..7d773bc56e 100644 --- a/esphome/components/statsd/statsd.cpp +++ b/esphome/components/statsd/statsd.cpp @@ -114,14 +114,22 @@ 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". + // %f with -DBL_MAX can produce up to 321 chars, plus ":" and "|g\n" (4) + null = 326 + char val_buf[330]; + buf_append_printf(val_buf, sizeof(val_buf), 0, ":%f|g\n", val); + out.append(val_buf); if (out.length() > SEND_THRESHOLD) { this->send_(&out);