From 05c53644900093d5bf9110d1d930444a8f3f694b Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 9 Jul 2025 10:13:21 +1200 Subject: [PATCH] [helpers] Fix ``format_hex_pretty`` resize without separator (#9389) Co-authored-by: RubenKelevra --- esphome/core/helpers.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 0c11c5d486..b46077af02 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -263,7 +263,7 @@ std::string format_hex_pretty(const uint8_t *data, size_t length, char separator return ""; std::string ret; uint8_t multiple = separator ? 3 : 2; // 3 if separator is not \0, 2 otherwise - ret.resize(multiple * length - 1); + ret.resize(multiple * length - (separator ? 1 : 0)); for (size_t i = 0; i < length; i++) { ret[multiple * i] = format_hex_pretty_char((data[i] & 0xF0) >> 4); ret[multiple * i + 1] = format_hex_pretty_char(data[i] & 0x0F); @@ -283,7 +283,7 @@ std::string format_hex_pretty(const uint16_t *data, size_t length, char separato return ""; std::string ret; uint8_t multiple = separator ? 5 : 4; // 5 if separator is not \0, 4 otherwise - ret.resize(multiple * length - 1); + ret.resize(multiple * length - (separator ? 1 : 0)); for (size_t i = 0; i < length; i++) { ret[multiple * i] = format_hex_pretty_char((data[i] & 0xF000) >> 12); ret[multiple * i + 1] = format_hex_pretty_char((data[i] & 0x0F00) >> 8); @@ -304,7 +304,7 @@ std::string format_hex_pretty(const std::string &data, char separator, bool show return ""; std::string ret; uint8_t multiple = separator ? 3 : 2; // 3 if separator is not \0, 2 otherwise - ret.resize(multiple * data.length() - 1); + ret.resize(multiple * data.length() - (separator ? 1 : 0)); for (size_t i = 0; i < data.length(); i++) { ret[multiple * i] = format_hex_pretty_char((data[i] & 0xF0) >> 4); ret[multiple * i + 1] = format_hex_pretty_char(data[i] & 0x0F);