From aaec4b7bd393c22268c88f77da202b31ee40723b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 6 Jul 2025 22:13:35 -0500 Subject: [PATCH] validation consistent --- esphome/core/scheduler.cpp | 6 +++--- esphome/core/scheduler.h | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index aa981d0b05..d3da003a88 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -66,7 +66,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type if (delay == SCHEDULER_DONT_RUN) { // Still need to cancel existing timer if name is not empty - if (name_cstr != nullptr && name_cstr[0] != '\0') { + if (this->is_name_valid_(name_cstr)) { LockGuard guard{this->lock_}; this->cancel_item_locked_(component, name_cstr, type); } @@ -125,7 +125,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type LockGuard guard{this->lock_}; // If name is provided, do atomic cancel-and-add - if (name_cstr != nullptr && name_cstr[0] != '\0') { + if (this->is_name_valid_(name_cstr)) { // Cancel existing items this->cancel_item_locked_(component, name_cstr, type); } @@ -443,7 +443,7 @@ bool HOT Scheduler::cancel_item_(Component *component, bool is_static_string, co const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr); // Handle null or empty names - if (name_cstr == nullptr) + if (!this->is_name_valid_(name_cstr)) return false; // obtain lock because this function iterates and can be called from non-loop task context diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index 39cee5a876..084ff699c5 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -150,6 +150,9 @@ class Scheduler { return is_static_string ? static_cast(name_ptr) : static_cast(name_ptr)->c_str(); } + // Helper to check if a name is valid (not null and not empty) + inline bool is_name_valid_(const char *name) { return name != nullptr && name[0] != '\0'; } + // Common implementation for cancel operations bool cancel_item_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type);