mirror of
https://github.com/esphome/esphome.git
synced 2026-09-14 08:38:39 +00:00
Fix dangling reference in save_state_, restore deleted string overloads
1. save_state_() bound a reference to supported_preset_modes() from a temporary FanTraits returned by get_traits(). When the compat path fired, the temporary was destroyed and the reference dangled. Fix: keep the FanTraits alive for the loop duration. 2. Restore deleted std::vector<std::string> / initializer_list<std::string> overloads to preserve clear compile-time diagnostics.
This commit is contained in:
@@ -297,9 +297,11 @@ void Fan::save_state_() {
|
||||
state.preset_mode = FanRestoreState::NO_PRESET;
|
||||
|
||||
if (this->has_preset_mode()) {
|
||||
// Use Fan-owned vector, or fall back to traits for deprecated path
|
||||
// Use Fan-owned vector, or fall back to traits for deprecated path.
|
||||
// Keep traits alive so the reference to compat_preset_modes_ doesn't dangle.
|
||||
auto traits = this->supported_preset_modes_ ? FanTraits() : this->get_traits();
|
||||
const auto &preset_modes =
|
||||
this->supported_preset_modes_ ? *this->supported_preset_modes_ : this->get_traits().supported_preset_modes();
|
||||
this->supported_preset_modes_ ? *this->supported_preset_modes_ : traits.supported_preset_modes();
|
||||
for (size_t i = 0; i < preset_modes.size(); i++) {
|
||||
if (preset_modes[i] == this->preset_mode_) {
|
||||
state.preset_mode = i;
|
||||
|
||||
@@ -48,6 +48,10 @@ class FanTraits {
|
||||
this->compat_preset_modes_ = preset_modes;
|
||||
}
|
||||
|
||||
// Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
|
||||
void set_supported_preset_modes(const std::vector<std::string> &preset_modes) = delete;
|
||||
void set_supported_preset_modes(std::initializer_list<std::string> preset_modes) = delete;
|
||||
|
||||
/// Return if preset modes are supported
|
||||
bool supports_preset_modes() const {
|
||||
return (this->preset_modes_ && !this->preset_modes_->empty()) || !this->compat_preset_modes_.empty();
|
||||
|
||||
Reference in New Issue
Block a user