[haier] Fix bugprone-unchecked-optional-access; switch HardwareInfo to char[9] (#16124)

This commit is contained in:
Jonathan Swoboda
2026-04-29 10:26:53 -04:00
committed by GitHub
parent 42b8597719
commit 2157d11913
3 changed files with 28 additions and 35 deletions
+20 -28
View File
@@ -85,7 +85,7 @@ void HonClimate::set_horizontal_airflow(hon_protocol::HorizontalSwingMode direct
this->force_send_control_ = true; this->force_send_control_ = true;
} }
std::string HonClimate::get_cleaning_status_text() const { const char *HonClimate::get_cleaning_status_text() const {
switch (this->cleaning_status_) { switch (this->cleaning_status_) {
case CleaningState::SELF_CLEAN: case CleaningState::SELF_CLEAN:
return "Self clean"; return "Self clean";
@@ -134,29 +134,22 @@ haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haie
} }
// All OK // All OK
hon_protocol::DeviceVersionAnswer *answr = (hon_protocol::DeviceVersionAnswer *) data; hon_protocol::DeviceVersionAnswer *answr = (hon_protocol::DeviceVersionAnswer *) data;
char tmp[9]; HardwareInfo info{}; // zero-init guarantees null-termination
tmp[8] = 0; strncpy(info.protocol_version_, answr->protocol_version, HARDWARE_INFO_STR_SIZE - 1);
strncpy(tmp, answr->protocol_version, 8); strncpy(info.software_version_, answr->software_version, HARDWARE_INFO_STR_SIZE - 1);
this->hvac_hardware_info_ = HardwareInfo(); strncpy(info.hardware_version_, answr->hardware_version, HARDWARE_INFO_STR_SIZE - 1);
this->hvac_hardware_info_.value().protocol_version_ = std::string(tmp); strncpy(info.device_name_, answr->device_name, HARDWARE_INFO_STR_SIZE - 1);
strncpy(tmp, answr->software_version, 8); info.functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support
this->hvac_hardware_info_.value().software_version_ = std::string(tmp); info.functions_[1] = (answr->functions[1] & 0x02) != 0; // controller-device mode support
strncpy(tmp, answr->hardware_version, 8); info.functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
this->hvac_hardware_info_.value().hardware_version_ = std::string(tmp); info.functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
strncpy(tmp, answr->device_name, 8); info.functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
this->hvac_hardware_info_.value().device_name_ = std::string(tmp); this->use_crc_ = info.functions_[2];
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
this->update_sub_text_sensor_(SubTextSensorType::APPLIANCE_NAME, this->hvac_hardware_info_.value().device_name_); this->update_sub_text_sensor_(SubTextSensorType::APPLIANCE_NAME, info.device_name_);
this->update_sub_text_sensor_(SubTextSensorType::PROTOCOL_VERSION, this->update_sub_text_sensor_(SubTextSensorType::PROTOCOL_VERSION, info.protocol_version_);
this->hvac_hardware_info_.value().protocol_version_);
#endif #endif
this->hvac_hardware_info_.value().functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support this->hvac_hardware_info_ = info;
this->hvac_hardware_info_.value().functions_[1] =
(answr->functions[1] & 0x02) != 0; // controller-device mode support
this->hvac_hardware_info_.value().functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
this->hvac_hardware_info_.value().functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
this->hvac_hardware_info_.value().functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
this->use_crc_ = this->hvac_hardware_info_.value().functions_[2];
this->set_phase(ProtocolPhases::SENDING_INIT_2); this->set_phase(ProtocolPhases::SENDING_INIT_2);
return result; return result;
} else { } else {
@@ -347,10 +340,9 @@ void HonClimate::dump_config() {
" Device software version: %s\n" " Device software version: %s\n"
" Device hardware version: %s\n" " Device hardware version: %s\n"
" Device name: %s", " Device name: %s",
this->hvac_hardware_info_.value().protocol_version_.c_str(), this->hvac_hardware_info_.value().protocol_version_,
this->hvac_hardware_info_.value().software_version_.c_str(), this->hvac_hardware_info_.value().software_version_,
this->hvac_hardware_info_.value().hardware_version_.c_str(), this->hvac_hardware_info_.value().hardware_version_, this->hvac_hardware_info_.value().device_name_);
this->hvac_hardware_info_.value().device_name_.c_str());
ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s", ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s",
(this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""), (this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""),
(this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""), (this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""),
@@ -460,7 +452,7 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
if (this->action_request_.has_value()) { if (this->action_request_.has_value()) {
if (this->action_request_.value().message.has_value()) { if (this->action_request_.value().message.has_value()) {
this->send_message_(this->action_request_.value().message.value(), this->use_crc_); this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
this->action_request_.value().message.reset(); this->action_request_.value().message.reset(); // NOLINT(bugprone-unchecked-optional-access)
} else { } else {
// Message already sent, reseting request and return to idle // Message already sent, reseting request and return to idle
this->action_request_.reset(); this->action_request_.reset();
@@ -796,7 +788,7 @@ void HonClimate::set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSe
} }
} }
void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const std::string &value) { void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const char *value) {
size_t index = (size_t) type; size_t index = (size_t) type;
if (this->sub_text_sensors_[index] != nullptr) if (this->sub_text_sensors_[index] != nullptr)
this->sub_text_sensors_[index]->publish_state(value); this->sub_text_sensors_[index]->publish_state(value);
+7 -6
View File
@@ -90,7 +90,7 @@ class HonClimate : public HaierClimateBase {
void set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSensor *sens); void set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSensor *sens);
protected: protected:
void update_sub_text_sensor_(SubTextSensorType type, const std::string &value); void update_sub_text_sensor_(SubTextSensorType type, const char *value);
text_sensor::TextSensor *sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]{nullptr}; text_sensor::TextSensor *sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]{nullptr};
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
@@ -116,7 +116,7 @@ class HonClimate : public HaierClimateBase {
void set_vertical_airflow(hon_protocol::VerticalSwingMode direction); void set_vertical_airflow(hon_protocol::VerticalSwingMode direction);
esphome::optional<hon_protocol::HorizontalSwingMode> get_horizontal_airflow() const; esphome::optional<hon_protocol::HorizontalSwingMode> get_horizontal_airflow() const;
void set_horizontal_airflow(hon_protocol::HorizontalSwingMode direction); void set_horizontal_airflow(hon_protocol::HorizontalSwingMode direction);
std::string get_cleaning_status_text() const; const char *get_cleaning_status_text() const;
CleaningState get_cleaning_status() const; CleaningState get_cleaning_status() const;
void start_self_cleaning(); void start_self_cleaning();
void start_steri_cleaning(); void start_steri_cleaning();
@@ -166,11 +166,12 @@ class HonClimate : public HaierClimateBase {
void fill_control_messages_queue_(); void fill_control_messages_queue_();
void clear_control_messages_queue_(); void clear_control_messages_queue_();
static constexpr size_t HARDWARE_INFO_STR_SIZE = 9;
struct HardwareInfo { struct HardwareInfo {
std::string protocol_version_; char protocol_version_[HARDWARE_INFO_STR_SIZE];
std::string software_version_; char software_version_[HARDWARE_INFO_STR_SIZE];
std::string hardware_version_; char hardware_version_[HARDWARE_INFO_STR_SIZE];
std::string device_name_; char device_name_[HARDWARE_INFO_STR_SIZE];
bool functions_[5]; bool functions_[5];
}; };
@@ -191,7 +191,7 @@ void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now)
if (this->action_request_.has_value()) { if (this->action_request_.has_value()) {
if (this->action_request_.value().message.has_value()) { if (this->action_request_.value().message.has_value()) {
this->send_message_(this->action_request_.value().message.value(), this->use_crc_); this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
this->action_request_.value().message.reset(); this->action_request_.value().message.reset(); // NOLINT(bugprone-unchecked-optional-access)
} else { } else {
// Message already sent, reseting request and return to idle // Message already sent, reseting request and return to idle
this->action_request_.reset(); this->action_request_.reset();