HASS set pointer to NULL after free, also add cosnt correct

This commit is contained in:
openshwprojects
2023-01-16 13:56:46 +01:00
parent ddbf8e3ec0
commit 18fae1fe0e
3 changed files with 10 additions and 2 deletions

View File

@ -355,7 +355,11 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel) {
/// @brief Returns the discovery JSON.
/// @param info
/// @return
char* hass_build_discovery_json(HassDeviceInfo* info) {
const char* hass_build_discovery_json(HassDeviceInfo* info) {
if (info == NULL) {
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "ERROR: someone passed NULL pointer to hass_build_discovery_json\r\n");
return "";
}
cJSON_PrintPreallocated(info->root, info->json, HASS_JSON_SIZE, 0);
return info->json;
}

View File

@ -60,5 +60,5 @@ HassDeviceInfo* hass_init_light_device_info(ENTITY_TYPE type);
HassDeviceInfo* hass_init_power_sensor_device_info(int index);
HassDeviceInfo* hass_init_binary_sensor_device_info(int index);
HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel);
char* hass_build_discovery_json(HassDeviceInfo* info);
const char* hass_build_discovery_json(HassDeviceInfo* info);
void hass_free_device_info(HassDeviceInfo* info);

View File

@ -1608,6 +1608,7 @@ void doHomeAssistantDiscovery(const char *topic, http_request_t *request) {
dev_info = hass_init_relay_device_info(i);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
dev_info = NULL;
discoveryQueued = true;
}
}
@ -1619,6 +1620,7 @@ void doHomeAssistantDiscovery(const char *topic, http_request_t *request) {
dev_info = hass_init_binary_sensor_device_info(i);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
dev_info = NULL;
discoveryQueued = true;
}
}
@ -1631,6 +1633,7 @@ void doHomeAssistantDiscovery(const char *topic, http_request_t *request) {
// Enable + RGB control + CW control
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
dev_info = NULL;
discoveryQueued = true;
}
else if (pwmCount > 0) {
@ -1652,6 +1655,7 @@ void doHomeAssistantDiscovery(const char *topic, http_request_t *request) {
if (dev_info != NULL) {
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
dev_info = NULL;
discoveryQueued = true;
}
}