From 71c922bb6021edca6090c68f8469569542b830cc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 14 Jan 2026 15:46:09 -1000 Subject: [PATCH 1/2] [ci] Soft-deprecate str_sprintf/str_snprintf to prevent hidden heap allocations --- script/ci-custom.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/script/ci-custom.py b/script/ci-custom.py index e63e61e096..5a605c4980 100755 --- a/script/ci-custom.py +++ b/script/ci-custom.py @@ -690,6 +690,8 @@ HEAP_ALLOCATING_HELPERS = { "str_truncate": "removal (function is unused)", "str_upper_case": "removal (function is unused)", "str_snake_case": "removal (function is unused)", + "str_sprintf": "snprintf() with a stack buffer", + "str_snprintf": "snprintf() with a stack buffer", } @@ -706,7 +708,9 @@ HEAP_ALLOCATING_HELPERS = { r"get_mac_address(?!_)|" r"str_truncate|" r"str_upper_case|" - r"str_snake_case" + r"str_snake_case|" + r"str_sprintf|" + r"str_snprintf" r")\s*\(" + CPP_RE_EOL, include=cpp_include, exclude=[ From 06c619b2e028a25087e4aaa8bc60a5017321dd57 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 14 Jan 2026 15:48:22 -1000 Subject: [PATCH 2/2] [ci] Soft-deprecate str_sprintf/str_snprintf to prevent hidden heap allocations --- esphome/core/helpers.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 2e9c0e6b13..50e0a271c7 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -563,9 +563,11 @@ inline uint32_t fnv1_hash_object_id(const char *str, size_t len) { } /// snprintf-like function returning std::string of maximum length \p len (excluding null terminator). +/// @warning Allocates heap memory. Use snprintf() with a stack buffer instead. std::string __attribute__((format(printf, 1, 3))) str_snprintf(const char *fmt, size_t len, ...); /// sprintf-like function returning std::string. +/// @warning Allocates heap memory. Use snprintf() with a stack buffer instead. std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, ...); /// Concatenate a name with a separator and suffix using an efficient stack-based approach.