Skip to content

Commit d3a44fc

Browse files
authored
fix(zigbeeEP): review of names and memory allocation
1 parent de184bd commit d3a44fc

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

libraries/Zigbee/src/ZigbeeEP.cpp

+40-21
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 = nullptr;
23+
_read_manufacturer = nullptr;
2224
_time_status = 0;
2325
if (!lock) {
2426
lock = xSemaphoreCreateBinary();
@@ -40,9 +42,22 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
4042
log_e("Manufacturer or model name is too long");
4143
return false;
4244
}
45+
// Get and check the basic cluster
46+
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);
47+
if (basic_cluster == nullptr) {
48+
log_e("Failed to get basic cluster");
49+
return false;
50+
}
4351
// 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];
52+
char *zb_name = (char *) malloc(name_length + 2);
53+
char *zb_model = (char *) malloc(model_length + 2);
54+
if (zb_name == nullptr || zb_model == nullptr) {
55+
log_e("Failed to allocate memory for name and model data");
56+
// make sure any allocated memory is returned to heap
57+
free(zb_name);
58+
free(zb_model);
59+
return false;
60+
}
4661
// Store the length as the first element
4762
zb_name[0] = static_cast<char>(name_length); // Cast size_t to char
4863
zb_model[0] = static_cast<char>(model_length);
@@ -52,9 +67,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
5267
// Null-terminate the array
5368
zb_name[name_length + 1] = '\0';
5469
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);
70+
// Update the manufacturer and model attributes
5871
esp_err_t ret_name = esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)zb_name);
5972
if (ret_name != ESP_OK) {
6073
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) {
6376
if (ret_model != ESP_OK) {
6477
log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model));
6578
}
66-
delete[] zb_name;
67-
delete[] zb_model;
79+
free(zb_model);
80+
free(zb_name);
6881
return ret_name == ESP_OK && ret_model == ESP_OK;
6982
}
7083

@@ -163,9 +176,7 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
163176
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
164177
read_req.attr_field = attributes;
165178

166-
if (_read_manufacturer != nullptr) {
167-
free(_read_manufacturer);
168-
}
179+
free(_read_manufacturer);
169180
_read_manufacturer = nullptr;
170181

171182
esp_zb_lock_acquire(portMAX_DELAY);
@@ -201,9 +212,7 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
201212
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
202213
read_req.attr_field = attributes;
203214

204-
if (_read_model != nullptr) {
205-
free(_read_model);
206-
}
215+
free(_read_model);
207216
_read_model = nullptr;
208217

209218
esp_zb_lock_acquire(portMAX_DELAY);
@@ -245,20 +254,30 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
245254
/* Basic cluster attributes */
246255
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
247256
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
248-
char *string = (char *)malloc(zbstr->len + 1);
249-
memcpy(string, zbstr->data, zbstr->len);
257+
char *zb_manufacturer = (char *)malloc(zbstr->len + 1);
258+
if (zb_manufacturer == nullptr) {
259+
log_e("Failed to allocate memory for manufacturer data");
260+
xSemaphoreGive(lock);
261+
return;
262+
}
263+
memcpy(zb_manufacturer, zbstr->data, zbstr->len);
250264
string[zbstr->len] = '\0';
251-
log_i("Peer Manufacturer is \"%s\"", string);
252-
_read_manufacturer = string;
265+
log_i("Peer Manufacturer is \"%s\"", zb_manufacturer);
266+
_read_manufacturer = zb_manufacturer;
253267
xSemaphoreGive(lock);
254268
}
255269
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
256270
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
257-
char *string = (char *)malloc(zbstr->len + 1);
258-
memcpy(string, zbstr->data, zbstr->len);
271+
char *zb_model = (char *)malloc(zbstr->len + 1);
272+
if (zb_model == nullptr) {
273+
log_e("Failed to allocate memory for model data");
274+
xSemaphoreGive(lock);
275+
return;
276+
}
277+
memcpy(zb_model, zbstr->data, zbstr->len);
259278
string[zbstr->len] = '\0';
260-
log_i("Peer Model is \"%s\"", string);
261-
_read_model = string;
279+
log_i("Peer Model is \"%s\"", zb_model);
280+
_read_model = zb_model;
262281
xSemaphoreGive(lock);
263282
}
264283
}

0 commit comments

Comments
 (0)