From 531af6a277929c77f1615e6c18ef10c9db2add2c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 23 Nov 2025 20:50:31 -0600 Subject: [PATCH] [esp32_ble] Store device name in flash to reduce RAM usage --- esphome/components/esp32_ble/ble.cpp | 2 +- esphome/core/helpers.cpp | 9 ++++++--- esphome/core/helpers.h | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index 86c750e8f86..226ff1952ed 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -264,7 +264,7 @@ bool ESP32BLE::ble_setup_() { char mac_addr[13]; get_mac_address_into_buffer(mac_addr); const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len; - name = make_name_with_suffix(this->name_, '-', mac_suffix_ptr, mac_address_suffix_len); + name = make_name_with_suffix(this->name_, strlen(this->name_), '-', mac_suffix_ptr, mac_address_suffix_len); } else { name = this->name_; } diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 6d8f2f02eca..1f675563c70 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -238,13 +238,16 @@ std::string str_sprintf(const char *fmt, ...) { // Maximum size for name with suffix: 120 (max friendly name) + 1 (separator) + 6 (MAC suffix) + 1 (null term) static constexpr size_t MAX_NAME_WITH_SUFFIX_SIZE = 128; -std::string make_name_with_suffix(const char *name, char sep, const char *suffix_ptr, size_t suffix_len) { +std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr, + size_t suffix_len) { char buffer[MAX_NAME_WITH_SUFFIX_SIZE]; - size_t name_len = strlen(name); size_t total_len = name_len + 1 + suffix_len; // Silently truncate if needed: prioritize keeping the full suffix if (total_len >= MAX_NAME_WITH_SUFFIX_SIZE) { + // NOTE: This calculation could underflow if suffix_len >= MAX_NAME_WITH_SUFFIX_SIZE - 2, + // but this is safe because this helper is only called with small suffixes: + // MAC suffixes (6-12 bytes), ".local" (5 bytes), etc. name_len = MAX_NAME_WITH_SUFFIX_SIZE - suffix_len - 2; // -2 for separator and null terminator total_len = name_len + 1 + suffix_len; } @@ -257,7 +260,7 @@ std::string make_name_with_suffix(const char *name, char sep, const char *suffix } std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len) { - return make_name_with_suffix(name.c_str(), sep, suffix_ptr, suffix_len); + return make_name_with_suffix(name.c_str(), name.size(), sep, suffix_ptr, suffix_len); } // Parsing & formatting diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 810b4860112..a43c55e06b5 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -514,12 +514,14 @@ std::string make_name_with_suffix(const std::string &name, char sep, const char /// Optimized string concatenation: name + separator + suffix (const char* overload) /// Uses a fixed stack buffer to avoid heap allocations. -/// @param name The base name (null-terminated string) +/// @param name The base name string +/// @param name_len Length of the name /// @param sep Single character separator /// @param suffix_ptr Pointer to the suffix characters /// @param suffix_len Length of the suffix /// @return The concatenated string: name + sep + suffix -std::string make_name_with_suffix(const char *name, char sep, const char *suffix_ptr, size_t suffix_len); +std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr, + size_t suffix_len); ///@}