[web_server] Replace str_sprintf with stack buffers

This commit is contained in:
J. Nick Koston
2025-12-20 10:39:19 -10:00
parent 6c2d255230
commit de3e72af04
2 changed files with 13 additions and 6 deletions

View File

@@ -1042,7 +1042,9 @@ std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_con
json::JsonBuilder builder;
JsonObject root = builder.root();
std::string value = str_sprintf("%d-%02d-%02d", obj->year, obj->month, obj->day);
// Format: YYYY-MM-DD (max 10 chars + null)
char value[12];
snprintf(value, sizeof(value), "%d-%02d-%02d", obj->year, obj->month, obj->day);
set_json_icon_state_value(root, obj, "date", value, value, start_config);
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
@@ -1098,7 +1100,9 @@ std::string WebServer::time_json(datetime::TimeEntity *obj, JsonDetail start_con
json::JsonBuilder builder;
JsonObject root = builder.root();
std::string value = str_sprintf("%02d:%02d:%02d", obj->hour, obj->minute, obj->second);
// Format: HH:MM:SS (8 chars + null)
char value[12];
snprintf(value, sizeof(value), "%02d:%02d:%02d", obj->hour, obj->minute, obj->second);
set_json_icon_state_value(root, obj, "time", value, value, start_config);
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
@@ -1154,8 +1158,10 @@ std::string WebServer::datetime_json(datetime::DateTimeEntity *obj, JsonDetail s
json::JsonBuilder builder;
JsonObject root = builder.root();
std::string value =
str_sprintf("%d-%02d-%02d %02d:%02d:%02d", obj->year, obj->month, obj->day, obj->hour, obj->minute, obj->second);
// Format: YYYY-MM-DD HH:MM:SS (max 19 chars + null)
char value[24];
snprintf(value, sizeof(value), "%d-%02d-%02d %02d:%02d:%02d", obj->year, obj->month, obj->day, obj->hour, obj->minute,
obj->second);
set_json_icon_state_value(root, obj, "datetime", value, value, start_config);
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);

View File

@@ -343,8 +343,9 @@ bool AsyncWebServerRequest::authenticate(const char *username, const char *passw
void AsyncWebServerRequest::requestAuthentication(const char *realm) const {
httpd_resp_set_hdr(*this, "Connection", "keep-alive");
auto auth_val = str_sprintf("Basic realm=\"%s\"", realm ? realm : "Login Required");
httpd_resp_set_hdr(*this, "WWW-Authenticate", auth_val.c_str());
// Note: realm is never configured in ESPHome, always nullptr -> "Login Required"
(void) realm; // Unused - always use default
httpd_resp_set_hdr(*this, "WWW-Authenticate", "Basic realm=\"Login Required\"");
httpd_resp_send_err(*this, HTTPD_401_UNAUTHORIZED, nullptr);
}
#endif