From 5b92d0b89e1e6a6b4cb87c8bdeaeb5a1adb4bdc8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Jan 2026 18:37:14 -1000 Subject: [PATCH] [wiegand] Replace heap-allocating to_string with stack buffers (#13294) --- esphome/components/wiegand/wiegand.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/esphome/components/wiegand/wiegand.cpp b/esphome/components/wiegand/wiegand.cpp index dd1443d10c..f3f578794a 100644 --- a/esphome/components/wiegand/wiegand.cpp +++ b/esphome/components/wiegand/wiegand.cpp @@ -1,4 +1,5 @@ #include "wiegand.h" +#include #include "esphome/core/helpers.h" #include "esphome/core/log.h" @@ -69,32 +70,35 @@ void Wiegand::loop() { for (auto *trigger : this->raw_triggers_) trigger->trigger(count, value); if (count == 26) { - std::string tag = to_string((value >> 1) & 0xffffff); - ESP_LOGD(TAG, "received 26-bit tag: %s", tag.c_str()); + char tag_buf[12]; // max 8 digits for 24-bit value + null + buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu32, static_cast((value >> 1) & 0xffffff)); + ESP_LOGD(TAG, "received 26-bit tag: %s", tag_buf); if (!check_eparity(value, 13, 13) || !check_oparity(value, 0, 13)) { ESP_LOGW(TAG, "invalid parity"); return; } for (auto *trigger : this->tag_triggers_) - trigger->trigger(tag); + trigger->trigger(tag_buf); } else if (count == 34) { - std::string tag = to_string((value >> 1) & 0xffffffff); - ESP_LOGD(TAG, "received 34-bit tag: %s", tag.c_str()); + char tag_buf[12]; // max 10 digits for 32-bit value + null + buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu32, static_cast((value >> 1) & 0xffffffff)); + ESP_LOGD(TAG, "received 34-bit tag: %s", tag_buf); if (!check_eparity(value, 17, 17) || !check_oparity(value, 0, 17)) { ESP_LOGW(TAG, "invalid parity"); return; } for (auto *trigger : this->tag_triggers_) - trigger->trigger(tag); + trigger->trigger(tag_buf); } else if (count == 37) { - std::string tag = to_string((value >> 1) & 0x7ffffffff); - ESP_LOGD(TAG, "received 37-bit tag: %s", tag.c_str()); + char tag_buf[12]; // max 11 digits for 35-bit value + null + buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu64, static_cast((value >> 1) & 0x7ffffffff)); + ESP_LOGD(TAG, "received 37-bit tag: %s", tag_buf); if (!check_eparity(value, 18, 19) || !check_oparity(value, 0, 19)) { ESP_LOGW(TAG, "invalid parity"); return; } for (auto *trigger : this->tag_triggers_) - trigger->trigger(tag); + trigger->trigger(tag_buf); } else if (count == 4) { for (auto *trigger : this->key_triggers_) trigger->trigger(value);