diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 1508fce89d..f1f1dbd6fb 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -288,7 +288,7 @@ std::string WebServer::get_config_json() { root[ESPHOME_F("title")] = App.get_friendly_name().empty() ? App.get_name() : App.get_friendly_name(); char comment_buffer[ESPHOME_COMMENT_SIZE]; - App.get_comment_string(comment_buffer, sizeof(comment_buffer)); + App.get_comment_string(comment_buffer); root[ESPHOME_F("comment")] = comment_buffer; #if defined(USE_WEBSERVER_OTA_DISABLED) || !defined(USE_WEBSERVER_OTA) root[ESPHOME_F("ota")] = false; // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal diff --git a/esphome/core/application.h b/esphome/core/application.h index 712748be9d..d2addd01d6 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -257,10 +257,10 @@ class Application { } /// Copy the comment string into the provided buffer - /// Buffer must be ESPHOME_COMMENT_SIZE bytes - void get_comment_string(char *buffer, size_t size) { - ESPHOME_strncpy_P(buffer, ESPHOME_COMMENT_STR, size); - buffer[size - 1] = '\0'; + /// Buffer must be ESPHOME_COMMENT_SIZE bytes (compile-time enforced) + void get_comment_string(std::span buffer) { + ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, buffer.size()); + buffer[buffer.size() - 1] = '\0'; } /// Get the comment of this Application (deprecated, use get_comment_string() instead) @@ -268,7 +268,7 @@ class Application { ESPDEPRECATED("Use get_comment_string() instead. Removed in 2026.7.0", "2026.1.0") std::string get_comment() { char buffer[ESPHOME_COMMENT_SIZE]; - this->get_comment_string(buffer, sizeof(buffer)); + this->get_comment_string(buffer); return std::string(buffer); }