From 5caa9b8140cb015ebbe8a960bd58ed33733b64ed Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 31 Dec 2025 23:03:55 -1000 Subject: [PATCH] snprintf --- .../remote_base/abbwelcome_protocol.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/esphome/components/remote_base/abbwelcome_protocol.h b/esphome/components/remote_base/abbwelcome_protocol.h index 2409870eb2..307fa7bc1a 100644 --- a/esphome/components/remote_base/abbwelcome_protocol.h +++ b/esphome/components/remote_base/abbwelcome_protocol.h @@ -158,31 +158,32 @@ class ABBWelcomeData { std::array data_; // Calculate checksum uint8_t calc_cs_() const; - // Internal format implementation + // Internal format implementation - buffer guaranteed >= FORMAT_BUFFER_SIZE by caller char *format_to_internal_(char *buffer, uint8_t max_print_bytes) const { char *ptr = buffer; + char *end = buffer + FORMAT_BUFFER_SIZE; uint8_t print_bytes = std::min(this->size(), max_print_bytes); if (print_bytes) { char raw_hex[format_hex_pretty_size(12 + MAX_DATA_LENGTH)]; format_hex_pretty_to(raw_hex, this->data_.data(), print_bytes, '.'); - ptr += sprintf(ptr, "%s ", raw_hex); + ptr += snprintf(ptr, end - ptr, "%s ", raw_hex); } if (this->is_valid()) { - ptr += sprintf(ptr, - this->get_three_byte_address() ? "[%06" PRIX32 " %s %06" PRIX32 "] Type: %02X" - : "[%04" PRIX32 " %s %04" PRIX32 "] Type: %02X", - this->get_source_address(), this->get_retransmission() ? "»" : ">", - this->get_destination_address(), this->get_message_type()); + ptr += snprintf(ptr, end - ptr, + this->get_three_byte_address() ? "[%06" PRIX32 " %s %06" PRIX32 "] Type: %02X" + : "[%04" PRIX32 " %s %04" PRIX32 "] Type: %02X", + this->get_source_address(), this->get_retransmission() ? "»" : ">", + this->get_destination_address(), this->get_message_type()); if (this->get_data_size()) { char data_hex[format_hex_pretty_size(MAX_DATA_LENGTH)]; format_hex_pretty_to(data_hex, this->data_.data() + 5 + 2 * this->get_address_length(), this->get_data_size(), '.'); - sprintf(ptr, ", Data: %s", data_hex); + snprintf(ptr, end - ptr, ", Data: %s", data_hex); } } else { - sprintf(ptr, "[Invalid]"); + snprintf(ptr, end - ptr, "[Invalid]"); } return buffer;