From d3a44fcb1101437caacbda85f071903501a58209 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 28 Mar 2025 14:47:00 -0300 Subject: [PATCH 01/14] fix(zigbeeEP): review of names and memory allocation --- libraries/Zigbee/src/ZigbeeEP.cpp | 61 ++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index e7d507dc441..a34bed46165 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -19,6 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _ep_config.endpoint = 0; _cluster_list = nullptr; _on_identify = nullptr; + _read_model = nullptr; + _read_manufacturer = nullptr; _time_status = 0; if (!lock) { lock = xSemaphoreCreateBinary(); @@ -40,9 +42,22 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { log_e("Manufacturer or model name is too long"); return false; } + // Get and check the basic cluster + esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + if (basic_cluster == nullptr) { + log_e("Failed to get basic cluster"); + return false; + } // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator) - char *zb_name = new char[name_length + 2]; - char *zb_model = new char[model_length + 2]; + char *zb_name = (char *) malloc(name_length + 2); + char *zb_model = (char *) malloc(model_length + 2); + if (zb_name == nullptr || zb_model == nullptr) { + log_e("Failed to allocate memory for name and model data"); + // make sure any allocated memory is returned to heap + free(zb_name); + free(zb_model); + return false; + } // Store the length as the first element zb_name[0] = static_cast(name_length); // Cast size_t to char zb_model[0] = static_cast(model_length); @@ -52,9 +67,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { // Null-terminate the array zb_name[name_length + 1] = '\0'; zb_model[model_length + 1] = '\0'; - - // Get the basic cluster and update the manufacturer and model attributes - esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + // Update the manufacturer and model attributes esp_err_t ret_name = esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)zb_name); if (ret_name != ESP_OK) { log_e("Failed to set manufacturer: 0x%x: %s", ret_name, esp_err_to_name(ret_name)); @@ -63,8 +76,8 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { if (ret_model != ESP_OK) { log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model)); } - delete[] zb_name; - delete[] zb_model; + free(zb_model); + free(zb_name); return ret_name == ESP_OK && ret_model == ESP_OK; } @@ -163,9 +176,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - if (_read_manufacturer != nullptr) { - free(_read_manufacturer); - } + free(_read_manufacturer); _read_manufacturer = nullptr; esp_zb_lock_acquire(portMAX_DELAY); @@ -201,9 +212,7 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - if (_read_model != nullptr) { - free(_read_model); - } + free(_read_model); _read_model = nullptr; esp_zb_lock_acquire(portMAX_DELAY); @@ -245,20 +254,30 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { /* Basic cluster attributes */ if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *string = (char *)malloc(zbstr->len + 1); - memcpy(string, zbstr->data, zbstr->len); + char *zb_manufacturer = (char *)malloc(zbstr->len + 1); + if (zb_manufacturer == nullptr) { + log_e("Failed to allocate memory for manufacturer data"); + xSemaphoreGive(lock); + return; + } + memcpy(zb_manufacturer, zbstr->data, zbstr->len); string[zbstr->len] = '\0'; - log_i("Peer Manufacturer is \"%s\"", string); - _read_manufacturer = string; + log_i("Peer Manufacturer is \"%s\"", zb_manufacturer); + _read_manufacturer = zb_manufacturer; xSemaphoreGive(lock); } if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *string = (char *)malloc(zbstr->len + 1); - memcpy(string, zbstr->data, zbstr->len); + char *zb_model = (char *)malloc(zbstr->len + 1); + if (zb_model == nullptr) { + log_e("Failed to allocate memory for model data"); + xSemaphoreGive(lock); + return; + } + memcpy(zb_model, zbstr->data, zbstr->len); string[zbstr->len] = '\0'; - log_i("Peer Model is \"%s\"", string); - _read_model = string; + log_i("Peer Model is \"%s\"", zb_model); + _read_model = zb_model; xSemaphoreGive(lock); } } From 7ad636f33cf420bc86c43c53dfbda8d4e272f962 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 28 Mar 2025 14:53:12 -0300 Subject: [PATCH 02/14] fix(zigbeeEP): destructor shall free any allocated memory --- libraries/Zigbee/src/ZigbeeEP.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.h b/libraries/Zigbee/src/ZigbeeEP.h index bd142344929..521b198eb54 100644 --- a/libraries/Zigbee/src/ZigbeeEP.h +++ b/libraries/Zigbee/src/ZigbeeEP.h @@ -42,7 +42,10 @@ typedef enum { class ZigbeeEP { public: ZigbeeEP(uint8_t endpoint = 10); - ~ZigbeeEP() {} + ~ZigbeeEP() { + free(_read_manufacturer); + free(_read_model); + } // Set ep config and cluster list void setEpConfig(esp_zb_endpoint_config_t ep_config, esp_zb_cluster_list_t *cluster_list) { From 2c4406383936b66438e0fd76bee6c46151e3100e Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 28 Mar 2025 14:56:56 -0300 Subject: [PATCH 03/14] fix(zigbee_ep): forgotten var name change --- libraries/Zigbee/src/ZigbeeEP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index a34bed46165..a41a88f9b49 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -261,7 +261,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { return; } memcpy(zb_manufacturer, zbstr->data, zbstr->len); - string[zbstr->len] = '\0'; + zb_manufacturer[zbstr->len] = '\0'; log_i("Peer Manufacturer is \"%s\"", zb_manufacturer); _read_manufacturer = zb_manufacturer; xSemaphoreGive(lock); @@ -275,7 +275,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { return; } memcpy(zb_model, zbstr->data, zbstr->len); - string[zbstr->len] = '\0'; + zb_model[zbstr->len] = '\0'; log_i("Peer Model is \"%s\"", zb_model); _read_model = zb_model; xSemaphoreGive(lock); From 49d983a13382e7e363c28f455a5a78f174d7de53 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Sun, 30 Mar 2025 16:49:09 -0300 Subject: [PATCH 04/14] feat(zigbee_ep): use static heap memory for manufacturer and model names --- libraries/Zigbee/src/ZigbeeEP.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.h b/libraries/Zigbee/src/ZigbeeEP.h index 521b198eb54..38f02cb2be9 100644 --- a/libraries/Zigbee/src/ZigbeeEP.h +++ b/libraries/Zigbee/src/ZigbeeEP.h @@ -41,11 +41,12 @@ typedef enum { /* Zigbee End Device Class */ class ZigbeeEP { public: + // constants and limits + static constexpr size_t ZB_MAX_NAME_LENGTH = 32; + + // constructors and destructor ZigbeeEP(uint8_t endpoint = 10); - ~ZigbeeEP() { - free(_read_manufacturer); - free(_read_model); - } + ~ZigbeeEP() {} // Set ep config and cluster list void setEpConfig(esp_zb_endpoint_config_t ep_config, esp_zb_cluster_list_t *cluster_list) { @@ -141,8 +142,8 @@ class ZigbeeEP { } private: - char *_read_manufacturer; - char *_read_model; + char _read_manufacturer[ZB_MAX_NAME_LENGTH + 1]; // + 1 for the extra '\0' + char _read_model[ZB_MAX_NAME_LENGTH + 1]; // + 1 for the extra '\0' void (*_on_identify)(uint16_t time); time_t _read_time; int32_t _read_timezone; From bf3e5c18196e3f0d4689abcb3d3245f82049a8eb Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Sun, 30 Mar 2025 17:02:34 -0300 Subject: [PATCH 05/14] feat(zigbee_ep): changed model and manufacturer to heap --- libraries/Zigbee/src/ZigbeeEP.cpp | 47 ++++++++----------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index a41a88f9b49..d852e8c4667 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -19,8 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _ep_config.endpoint = 0; _cluster_list = nullptr; _on_identify = nullptr; - _read_model = nullptr; - _read_manufacturer = nullptr; + _read_model[0] = '\0'; + _read_manufacturer[0] = '\0'; _time_status = 0; if (!lock) { lock = xSemaphoreCreateBinary(); @@ -38,7 +38,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { // Convert manufacturer to ZCL string size_t name_length = strlen(name); size_t model_length = strlen(model); - if (name_length > 32 || model_length > 32) { + if (name_length > ZB_MAX_NAME_LENGTH || model_length > ZB_MAX_NAME_LENGTH) { log_e("Manufacturer or model name is too long"); return false; } @@ -49,15 +49,8 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { return false; } // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator) - char *zb_name = (char *) malloc(name_length + 2); - char *zb_model = (char *) malloc(model_length + 2); - if (zb_name == nullptr || zb_model == nullptr) { - log_e("Failed to allocate memory for name and model data"); - // make sure any allocated memory is returned to heap - free(zb_name); - free(zb_model); - return false; - } + char zb_name[ZB_MAX_NAME_LENGTH + 2]; + char zb_model[ZB_MAX_NAME_LENGTH + 2]; // Store the length as the first element zb_name[0] = static_cast(name_length); // Cast size_t to char zb_model[0] = static_cast(model_length); @@ -76,8 +69,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { if (ret_model != ESP_OK) { log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model)); } - free(zb_model); - free(zb_name); return ret_name == ESP_OK && ret_model == ESP_OK; } @@ -176,8 +167,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - free(_read_manufacturer); - _read_manufacturer = nullptr; + _read_manufacturer[0] = '\0'; esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_read_attr_cmd_req(&read_req); @@ -212,8 +202,7 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - free(_read_model); - _read_model = nullptr; + _read_model[0] = '\0'; esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_read_attr_cmd_req(&read_req); @@ -254,30 +243,16 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { /* Basic cluster attributes */ if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *zb_manufacturer = (char *)malloc(zbstr->len + 1); - if (zb_manufacturer == nullptr) { - log_e("Failed to allocate memory for manufacturer data"); - xSemaphoreGive(lock); - return; - } - memcpy(zb_manufacturer, zbstr->data, zbstr->len); - zb_manufacturer[zbstr->len] = '\0'; + memcpy(_read_manufacturer, zbstr->data, zbstr->len); + _read_manufacturer[zbstr->len] = '\0'; log_i("Peer Manufacturer is \"%s\"", zb_manufacturer); - _read_manufacturer = zb_manufacturer; xSemaphoreGive(lock); } if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *zb_model = (char *)malloc(zbstr->len + 1); - if (zb_model == nullptr) { - log_e("Failed to allocate memory for model data"); - xSemaphoreGive(lock); - return; - } - memcpy(zb_model, zbstr->data, zbstr->len); - zb_model[zbstr->len] = '\0'; + memcpy(_read_model, zbstr->data, zbstr->len); + _read_model[zbstr->len] = '\0'; log_i("Peer Model is \"%s\"", zb_model); - _read_model = zb_model; xSemaphoreGive(lock); } } From 6c6b980bddfd752b920817370e5e1fd9924f4536 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Sun, 30 Mar 2025 17:26:07 -0300 Subject: [PATCH 06/14] feat(zigbee_ep): use static heap memory allocation --- libraries/Zigbee/src/ZigbeeEP.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index d852e8c4667..ae20e168aff 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -35,6 +35,11 @@ void ZigbeeEP::setVersion(uint8_t version) { } bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { + // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator) + // This memory space can't be deleted or overwritten, therefore using static + static char zb_name[ZB_MAX_NAME_LENGTH + 2]; + static char zb_model[ZB_MAX_NAME_LENGTH + 2]; + // Convert manufacturer to ZCL string size_t name_length = strlen(name); size_t model_length = strlen(model); @@ -48,9 +53,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { log_e("Failed to get basic cluster"); return false; } - // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator) - char zb_name[ZB_MAX_NAME_LENGTH + 2]; - char zb_model[ZB_MAX_NAME_LENGTH + 2]; // Store the length as the first element zb_name[0] = static_cast(name_length); // Cast size_t to char zb_model[0] = static_cast(model_length); @@ -245,14 +247,14 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; memcpy(_read_manufacturer, zbstr->data, zbstr->len); _read_manufacturer[zbstr->len] = '\0'; - log_i("Peer Manufacturer is \"%s\"", zb_manufacturer); + log_i("Peer Manufacturer is \"%s\"", _read_manufacturer); xSemaphoreGive(lock); } if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; memcpy(_read_model, zbstr->data, zbstr->len); _read_model[zbstr->len] = '\0'; - log_i("Peer Model is \"%s\"", zb_model); + log_i("Peer Model is \"%s\"", _read_model); xSemaphoreGive(lock); } } From c15fbf370e97629f7a7a39c7c26016317e9752d6 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 31 Mar 2025 08:48:26 -0300 Subject: [PATCH 07/14] fix(zigbee_ep): using stack only for adding attribute --- libraries/Zigbee/src/ZigbeeEP.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index ae20e168aff..f78e0c2c2c7 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -36,9 +36,8 @@ void ZigbeeEP::setVersion(uint8_t version) { bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { // Allocate a new array of size length + 2 (1 for the length, 1 for null terminator) - // This memory space can't be deleted or overwritten, therefore using static - static char zb_name[ZB_MAX_NAME_LENGTH + 2]; - static char zb_model[ZB_MAX_NAME_LENGTH + 2]; + char zb_name[ZB_MAX_NAME_LENGTH + 2]; + char zb_model[ZB_MAX_NAME_LENGTH + 2]; // Convert manufacturer to ZCL string size_t name_length = strlen(name); From 0763c2a543903e780db64296a3a22d8c8409b5fa Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 31 Mar 2025 11:38:02 -0300 Subject: [PATCH 08/14] feat(zigbee_ep): reverting back read data type --- libraries/Zigbee/src/ZigbeeEP.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.h b/libraries/Zigbee/src/ZigbeeEP.h index 38f02cb2be9..9bdeea5abe9 100644 --- a/libraries/Zigbee/src/ZigbeeEP.h +++ b/libraries/Zigbee/src/ZigbeeEP.h @@ -142,8 +142,8 @@ class ZigbeeEP { } private: - char _read_manufacturer[ZB_MAX_NAME_LENGTH + 1]; // + 1 for the extra '\0' - char _read_model[ZB_MAX_NAME_LENGTH + 1]; // + 1 for the extra '\0' + char *_read_manufacturer; + char *_read_model; void (*_on_identify)(uint16_t time); time_t _read_time; int32_t _read_timezone; From cfe445731b415686931dcc422476caec8e3c797b Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 31 Mar 2025 11:48:41 -0300 Subject: [PATCH 09/14] fix(zigbee_ep): rooling back to use malloc for remote attr reading --- libraries/Zigbee/src/ZigbeeEP.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index f78e0c2c2c7..c4b0c2ebfd9 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -19,8 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _ep_config.endpoint = 0; _cluster_list = nullptr; _on_identify = nullptr; - _read_model[0] = '\0'; - _read_manufacturer[0] = '\0'; + _read_model = NULL; + _read_manufacturer = NULL; _time_status = 0; if (!lock) { lock = xSemaphoreCreateBinary(); @@ -168,8 +168,11 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - _read_manufacturer[0] = '\0'; - + if (_read_manufacturer != NULL) { + free(_read_manufacturer); + } + _read_manufacturer = NULL; + esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_read_attr_cmd_req(&read_req); esp_zb_lock_release(); @@ -203,7 +206,10 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - _read_model[0] = '\0'; + if (_read_model != NULL) { + free(_read_model); + } + _read_model = NULL; esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_read_attr_cmd_req(&read_req); @@ -244,6 +250,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { /* Basic cluster attributes */ if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; + char *_read_manufacturer = (char *) malloc(zbstr->len + 1); memcpy(_read_manufacturer, zbstr->data, zbstr->len); _read_manufacturer[zbstr->len] = '\0'; log_i("Peer Manufacturer is \"%s\"", _read_manufacturer); @@ -251,6 +258,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { } if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; + char *_read_model = (char *) malloc(zbstr->len + 1); memcpy(_read_model, zbstr->data, zbstr->len); _read_model[zbstr->len] = '\0'; log_i("Peer Model is \"%s\"", _read_model); From a7c77136cded0b76f2261164c6c8f310ff64808e Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 31 Mar 2025 11:53:12 -0300 Subject: [PATCH 10/14] feat(zigbee_ep): check malloc return for null --- libraries/Zigbee/src/ZigbeeEP.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index c4b0c2ebfd9..6b50e6dc635 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -251,6 +251,11 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; char *_read_manufacturer = (char *) malloc(zbstr->len + 1); + if (_read_manufacturer == nullptr) { + log_e("Failed to allocate memory for manufacturer data"); + xSemaphoreGive(lock); + return; + } memcpy(_read_manufacturer, zbstr->data, zbstr->len); _read_manufacturer[zbstr->len] = '\0'; log_i("Peer Manufacturer is \"%s\"", _read_manufacturer); @@ -259,6 +264,11 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; char *_read_model = (char *) malloc(zbstr->len + 1); + if (_read_model == nullptr) { + log_e("Failed to allocate memory for model data"); + xSemaphoreGive(lock); + return; + } memcpy(_read_model, zbstr->data, zbstr->len); _read_model[zbstr->len] = '\0'; log_i("Peer Model is \"%s\"", _read_model); From 48e5f1710c4e1b3488760ed62fb267d78eb8f8e9 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 31 Mar 2025 11:54:35 -0300 Subject: [PATCH 11/14] fix(zigbee_ep): replace nullptr by NULL after C malloc() --- libraries/Zigbee/src/ZigbeeEP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index 6b50e6dc635..5f6eda68d7a 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -251,7 +251,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; char *_read_manufacturer = (char *) malloc(zbstr->len + 1); - if (_read_manufacturer == nullptr) { + if (_read_manufacturer == NULL) { log_e("Failed to allocate memory for manufacturer data"); xSemaphoreGive(lock); return; @@ -264,7 +264,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; char *_read_model = (char *) malloc(zbstr->len + 1); - if (_read_model == nullptr) { + if (_read_model == NULL) { log_e("Failed to allocate memory for model data"); xSemaphoreGive(lock); return; From 4681be43d7e5659bd51e39eb1422a039303088e7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 2 Apr 2025 11:22:30 +0000 Subject: [PATCH 12/14] ci(pre-commit): Apply automatic fixes --- libraries/Zigbee/src/ZigbeeEP.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index 5f6eda68d7a..5ebe7cec0bf 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -20,7 +20,7 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _cluster_list = nullptr; _on_identify = nullptr; _read_model = NULL; - _read_manufacturer = NULL; + _read_manufacturer = NULL; _time_status = 0; if (!lock) { lock = xSemaphoreCreateBinary(); @@ -51,7 +51,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { if (basic_cluster == nullptr) { log_e("Failed to get basic cluster"); return false; - } + } // Store the length as the first element zb_name[0] = static_cast(name_length); // Cast size_t to char zb_model[0] = static_cast(model_length); @@ -172,7 +172,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i free(_read_manufacturer); } _read_manufacturer = NULL; - + esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_read_attr_cmd_req(&read_req); esp_zb_lock_release(); @@ -206,7 +206,7 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add read_req.attr_number = ZB_ARRAY_LENTH(attributes); read_req.attr_field = attributes; - if (_read_model != NULL) { + if (_read_model != NULL) { free(_read_model); } _read_model = NULL; @@ -250,7 +250,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { /* Basic cluster attributes */ if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *_read_manufacturer = (char *) malloc(zbstr->len + 1); + char *_read_manufacturer = (char *)malloc(zbstr->len + 1); if (_read_manufacturer == NULL) { log_e("Failed to allocate memory for manufacturer data"); xSemaphoreGive(lock); @@ -263,7 +263,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { } if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *_read_model = (char *) malloc(zbstr->len + 1); + char *_read_model = (char *)malloc(zbstr->len + 1); if (_read_model == NULL) { log_e("Failed to allocate memory for model data"); xSemaphoreGive(lock); From d113ba2022b4443ec79bc89bd5f121612226b5b1 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 3 Apr 2025 09:09:43 -0300 Subject: [PATCH 13/14] fix(zigbee_ep): fix variable scope Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- libraries/Zigbee/src/ZigbeeEP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index 5ebe7cec0bf..d737dcc13bf 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -250,7 +250,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { /* Basic cluster attributes */ if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *_read_manufacturer = (char *)malloc(zbstr->len + 1); + _read_manufacturer = (char *)malloc(zbstr->len + 1); if (_read_manufacturer == NULL) { log_e("Failed to allocate memory for manufacturer data"); xSemaphoreGive(lock); From 8e03dd0dcba0366fe95d10feed8b472afe56e2c5 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 3 Apr 2025 09:09:57 -0300 Subject: [PATCH 14/14] fix(zigbee_ep): fix variable scope Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- libraries/Zigbee/src/ZigbeeEP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index d737dcc13bf..338a4107cf2 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -263,7 +263,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { } if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) { zbstring_t *zbstr = (zbstring_t *)attribute->data.value; - char *_read_model = (char *)malloc(zbstr->len + 1); + _read_model = (char *)malloc(zbstr->len + 1); if (_read_model == NULL) { log_e("Failed to allocate memory for model data"); xSemaphoreGive(lock);