1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-01 23:51:47 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
J. Nick Koston
1fbb2a12b8 Merge branch 'dev' into voice_get_config 2025-10-19 09:22:55 -10:00
J. Nick Koston
f1fddc058e adjust 2025-10-08 08:06:38 -10:00
J. Nick Koston
d5ee5c7861 adjust 2025-10-08 08:05:44 -10:00
J. Nick Koston
542ca43cf6 [voice_assistant] Fix use-after-free crash with configuration StringRef pointers 2025-10-08 07:54:47 -10:00
2 changed files with 20 additions and 1 deletions

View File

@@ -953,11 +953,21 @@ void VoiceAssistant::on_set_configuration(const std::vector<std::string> &active
}
}
}
// Mark configuration dirty to trigger rebuild on next get_configuration() call.
this->config_needs_rebuild_ = true;
}
#endif
};
const Configuration &VoiceAssistant::get_configuration() {
// Return cached configuration if it hasn't changed. This prevents a use-after-free
// race condition when API message serialization creates StringRef pointers to strings
// in config_.available_wake_words, and avoids wastefully rebuilding on every call.
if (!this->config_needs_rebuild_) {
return this->config_;
}
this->config_.available_wake_words.clear();
this->config_.active_wake_words.clear();
@@ -986,6 +996,8 @@ const Configuration &VoiceAssistant::get_configuration() {
}
#endif
// Mark configuration as clean now that we've rebuilt it
this->config_needs_rebuild_ = false;
return this->config_;
};

View File

@@ -112,7 +112,10 @@ class VoiceAssistant : public Component {
void set_microphone_source(microphone::MicrophoneSource *mic_source) { this->mic_source_ = mic_source; }
#ifdef USE_MICRO_WAKE_WORD
void set_micro_wake_word(micro_wake_word::MicroWakeWord *mww) { this->micro_wake_word_ = mww; }
void set_micro_wake_word(micro_wake_word::MicroWakeWord *mww) {
this->micro_wake_word_ = mww;
this->config_needs_rebuild_ = true;
}
#endif
#ifdef USE_SPEAKER
void set_speaker(speaker::Speaker *speaker) {
@@ -313,7 +316,11 @@ class VoiceAssistant : public Component {
bool udp_socket_running_{false};
bool start_udp_socket_();
// Configuration caching for safety and performance. Only rebuild when config_needs_rebuild_
// is true to prevent use-after-free race condition when StringRef pointers reference
// wake word strings during API message serialization, and to avoid wasteful rebuilding.
Configuration config_{};
bool config_needs_rebuild_{true};
#ifdef USE_MICRO_WAKE_WORD
micro_wake_word::MicroWakeWord *micro_wake_word_{nullptr};