This commit is contained in:
J. Nick Koston
2026-02-21 21:34:07 -06:00
parent 1003247a41
commit b6e8e92416
2 changed files with 7 additions and 5 deletions
+6 -2
View File
@@ -1705,8 +1705,10 @@ void APIConnection::on_home_assistant_state_response(const HomeAssistantStateRes
// Null-terminate state in-place for safe c_str() usage (e.g., parse_number in callbacks).
// Safe: decode is complete, byte after string data was already consumed during parse,
// and frame helpers reserve RX_BUF_NULL_TERMINATOR extra byte in rx_buf_.
// const_cast is safe: msg references rx_buf_ data which is mutable; the const& handler
// signature is a generated protobuf pattern, not a true immutability contract.
if (!msg.state.empty()) {
msg.state.null_terminate_in_place();
const_cast<StringRef &>(msg.state).null_terminate_in_place();
}
for (auto &it : this->parent_->get_state_subs()) {
@@ -1729,7 +1731,9 @@ void APIConnection::on_execute_service_request(const ExecuteServiceRequest &msg)
// Null-terminate string args in-place for safe c_str() usage in YAML service triggers.
// Safe: full ExecuteServiceRequest decode is complete, all bytes in rx_buf_ consumed,
// and frame helpers reserve RX_BUF_NULL_TERMINATOR extra byte for the last field.
for (auto &arg : msg.args) {
// const_cast is safe: msg references rx_buf_ data which is mutable; the const& handler
// signature is a generated protobuf pattern, not a true immutability contract.
for (auto &arg : const_cast<ExecuteServiceRequest &>(msg).args) {
if (!arg.string_.empty()) {
arg.string_.null_terminate_in_place();
}
+1 -3
View File
@@ -84,9 +84,7 @@ class StringRef {
/// Write a null terminator at base_[len_] in-place.
/// Caller must guarantee that the byte at base_[len_] is writable memory
/// (e.g., the RX_BUF_NULL_TERMINATOR byte reserved by frame helpers after decode).
/// Marked const because StringRef itself is not modified; the underlying buffer
/// (owned by frame helper rx_buf_) is mutated via const_cast.
void null_terminate_in_place() const { const_cast<char *>(base_)[len_] = '\0'; }
void null_terminate_in_place() { const_cast<char *>(base_)[len_] = '\0'; }
/// Find first occurrence of substring, returns std::string::npos if not found.
/// Note: Requires the underlying string to be null-terminated.