From fb3ce6c783d36f1b7c034616bc7813e725ecf946 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 26 Sep 2025 09:43:36 -0500 Subject: [PATCH 1/2] bot comments --- esphome/components/esp32_ble_server/ble_characteristic.cpp | 6 ++---- .../components/esp32_ble_server/ble_server_automations.cpp | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/esphome/components/esp32_ble_server/ble_characteristic.cpp b/esphome/components/esp32_ble_server/ble_characteristic.cpp index 4d0ada0ac2..fabcc75321 100644 --- a/esphome/components/esp32_ble_server/ble_characteristic.cpp +++ b/esphome/components/esp32_ble_server/ble_characteristic.cpp @@ -313,10 +313,8 @@ void BLECharacteristic::remove_client_from_notify_list_(uint16_t conn_id) { // for the common case by swapping with the last element and popping for (size_t i = 0; i < this->clients_to_notify_.size(); i++) { if (this->clients_to_notify_[i].conn_id == conn_id) { - // Swap with last element and pop - if (i != this->clients_to_notify_.size() - 1) { - this->clients_to_notify_[i] = this->clients_to_notify_.back(); - } + // Swap with last element and pop (safe even when i is the last element) + this->clients_to_notify_[i] = this->clients_to_notify_.back(); this->clients_to_notify_.pop_back(); return; } diff --git a/esphome/components/esp32_ble_server/ble_server_automations.cpp b/esphome/components/esp32_ble_server/ble_server_automations.cpp index ea6a074daa..b140e08b46 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.cpp +++ b/esphome/components/esp32_ble_server/ble_server_automations.cpp @@ -83,10 +83,8 @@ void BLECharacteristicSetValueActionManager::remove_listener_(BLECharacteristic // Since we typically have very few listeners, optimize by swapping with back and popping for (size_t i = 0; i < this->listeners_.size(); i++) { if (this->listeners_[i].characteristic == characteristic) { - // Swap with last element and pop - if (i != this->listeners_.size() - 1) { - this->listeners_[i] = this->listeners_.back(); - } + // Swap with last element and pop (safe even when i is the last element) + this->listeners_[i] = this->listeners_.back(); this->listeners_.pop_back(); return; } From 70685f2939532aa5c7b834d7335f66da0638965a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 26 Sep 2025 09:46:38 -0500 Subject: [PATCH 2/2] bot comments --- esphome/components/esp32_ble_server/ble_server_automations.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/esphome/components/esp32_ble_server/ble_server_automations.h b/esphome/components/esp32_ble_server/ble_server_automations.h index 54bc0f2632..910335826c 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.h +++ b/esphome/components/esp32_ble_server/ble_server_automations.h @@ -20,6 +20,9 @@ namespace esp32_ble_server_automations { using namespace esp32_ble; using namespace event_emitter; +// Invalid listener ID constant - 0 is used as sentinel value in EventEmitter +static constexpr EventEmitterListenerID INVALID_LISTENER_ID = 0; + class BLETriggers { public: static Trigger, uint16_t> *create_characteristic_on_write_trigger( @@ -50,7 +53,7 @@ class BLECharacteristicSetValueActionManager return entry.listener_id; } } - return 0; + return INVALID_LISTENER_ID; } void emit_pre_notify(BLECharacteristic *characteristic) { this->emit_(BLECharacteristicSetValueActionEvt::PRE_NOTIFY, characteristic);