Skip to content

Commit b333bf2

Browse files
SuGliderP-R-O-C-H-Ypre-commit-ci-lite[bot]Copilot
authored
fix(zigbeeEP): review of names and memory allocation (#11199)
* fix(zigbeeEP): review of names and memory allocation * fix(zigbeeEP): destructor shall free any allocated memory * fix(zigbee_ep): forgotten var name change * feat(zigbee_ep): use static heap memory for manufacturer and model names * feat(zigbee_ep): changed model and manufacturer to heap * feat(zigbee_ep): use static heap memory allocation * fix(zigbee_ep): using stack only for adding attribute * feat(zigbee_ep): reverting back read data type * fix(zigbee_ep): rooling back to use malloc for remote attr reading * feat(zigbee_ep): check malloc return for null * fix(zigbee_ep): replace nullptr by NULL after C malloc() * ci(pre-commit): Apply automatic fixes * fix(zigbee_ep): fix variable scope Co-authored-by: Copilot <[email protected]> * fix(zigbee_ep): fix variable scope Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Jan Procházka <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent 23ded93 commit b333bf2

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

libraries/Zigbee/src/ZigbeeEP.cpp

+36-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) {
1919
_ep_config.endpoint = 0;
2020
_cluster_list = nullptr;
2121
_on_identify = nullptr;
22+
_read_model = NULL;
23+
_read_manufacturer = NULL;
2224
_time_status = 0;
2325
if (!lock) {
2426
lock = xSemaphoreCreateBinary();
@@ -33,16 +35,23 @@ void ZigbeeEP::setVersion(uint8_t version) {
3335
}
3436

3537
bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
38+
// Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
39+
char zb_name[ZB_MAX_NAME_LENGTH + 2];
40+
char zb_model[ZB_MAX_NAME_LENGTH + 2];
41+
3642
// Convert manufacturer to ZCL string
3743
size_t name_length = strlen(name);
3844
size_t model_length = strlen(model);
39-
if (name_length > 32 || model_length > 32) {
45+
if (name_length > ZB_MAX_NAME_LENGTH || model_length > ZB_MAX_NAME_LENGTH) {
4046
log_e("Manufacturer or model name is too long");
4147
return false;
4248
}
43-
// Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
44-
char *zb_name = new char[name_length + 2];
45-
char *zb_model = new char[model_length + 2];
49+
// Get and check the basic cluster
50+
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);
51+
if (basic_cluster == nullptr) {
52+
log_e("Failed to get basic cluster");
53+
return false;
54+
}
4655
// Store the length as the first element
4756
zb_name[0] = static_cast<char>(name_length); // Cast size_t to char
4857
zb_model[0] = static_cast<char>(model_length);
@@ -52,9 +61,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
5261
// Null-terminate the array
5362
zb_name[name_length + 1] = '\0';
5463
zb_model[model_length + 1] = '\0';
55-
56-
// Get the basic cluster and update the manufacturer and model attributes
57-
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);
64+
// Update the manufacturer and model attributes
5865
esp_err_t ret_name = esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)zb_name);
5966
if (ret_name != ESP_OK) {
6067
log_e("Failed to set manufacturer: 0x%x: %s", ret_name, esp_err_to_name(ret_name));
@@ -63,8 +70,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
6370
if (ret_model != ESP_OK) {
6471
log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model));
6572
}
66-
delete[] zb_name;
67-
delete[] zb_model;
6873
return ret_name == ESP_OK && ret_model == ESP_OK;
6974
}
7075

@@ -163,10 +168,10 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
163168
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
164169
read_req.attr_field = attributes;
165170

166-
if (_read_manufacturer != nullptr) {
171+
if (_read_manufacturer != NULL) {
167172
free(_read_manufacturer);
168173
}
169-
_read_manufacturer = nullptr;
174+
_read_manufacturer = NULL;
170175

171176
esp_zb_lock_acquire(portMAX_DELAY);
172177
esp_zb_zcl_read_attr_cmd_req(&read_req);
@@ -201,10 +206,10 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
201206
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
202207
read_req.attr_field = attributes;
203208

204-
if (_read_model != nullptr) {
209+
if (_read_model != NULL) {
205210
free(_read_model);
206211
}
207-
_read_model = nullptr;
212+
_read_model = NULL;
208213

209214
esp_zb_lock_acquire(portMAX_DELAY);
210215
esp_zb_zcl_read_attr_cmd_req(&read_req);
@@ -245,20 +250,28 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
245250
/* Basic cluster attributes */
246251
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
247252
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
248-
char *string = (char *)malloc(zbstr->len + 1);
249-
memcpy(string, zbstr->data, zbstr->len);
250-
string[zbstr->len] = '\0';
251-
log_i("Peer Manufacturer is \"%s\"", string);
252-
_read_manufacturer = string;
253+
_read_manufacturer = (char *)malloc(zbstr->len + 1);
254+
if (_read_manufacturer == NULL) {
255+
log_e("Failed to allocate memory for manufacturer data");
256+
xSemaphoreGive(lock);
257+
return;
258+
}
259+
memcpy(_read_manufacturer, zbstr->data, zbstr->len);
260+
_read_manufacturer[zbstr->len] = '\0';
261+
log_i("Peer Manufacturer is \"%s\"", _read_manufacturer);
253262
xSemaphoreGive(lock);
254263
}
255264
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
256265
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
257-
char *string = (char *)malloc(zbstr->len + 1);
258-
memcpy(string, zbstr->data, zbstr->len);
259-
string[zbstr->len] = '\0';
260-
log_i("Peer Model is \"%s\"", string);
261-
_read_model = string;
266+
_read_model = (char *)malloc(zbstr->len + 1);
267+
if (_read_model == NULL) {
268+
log_e("Failed to allocate memory for model data");
269+
xSemaphoreGive(lock);
270+
return;
271+
}
272+
memcpy(_read_model, zbstr->data, zbstr->len);
273+
_read_model[zbstr->len] = '\0';
274+
log_i("Peer Model is \"%s\"", _read_model);
262275
xSemaphoreGive(lock);
263276
}
264277
}

libraries/Zigbee/src/ZigbeeEP.h

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ typedef enum {
4141
/* Zigbee End Device Class */
4242
class ZigbeeEP {
4343
public:
44+
// constants and limits
45+
static constexpr size_t ZB_MAX_NAME_LENGTH = 32;
46+
47+
// constructors and destructor
4448
ZigbeeEP(uint8_t endpoint = 10);
4549
~ZigbeeEP() {}
4650

0 commit comments

Comments
 (0)