Skip to content

Commit 4211b57

Browse files
authored
fix(zigbee): memory leak issue with malloc
1 parent de184bd commit 4211b57

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

libraries/Zigbee/src/ZigbeeEP.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
4040
log_e("Manufacturer or model name is too long");
4141
return false;
4242
}
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];
43+
// Allocate an array of size length + 2 (1 for the length, 1 for null terminator)
44+
char zb_name[name_length + 2];
45+
char zb_model[model_length + 2];
4646
// Store the length as the first element
4747
zb_name[0] = static_cast<char>(name_length); // Cast size_t to char
4848
zb_model[0] = static_cast<char>(model_length);
@@ -63,8 +63,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
6363
if (ret_model != ESP_OK) {
6464
log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model));
6565
}
66-
delete[] zb_name;
67-
delete[] zb_model;
6866
return ret_name == ESP_OK && ret_model == ESP_OK;
6967
}
7068

@@ -245,7 +243,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
245243
/* Basic cluster attributes */
246244
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
247245
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
248-
char *string = (char *)malloc(zbstr->len + 1);
246+
char string[zbstr->len + 1];
249247
memcpy(string, zbstr->data, zbstr->len);
250248
string[zbstr->len] = '\0';
251249
log_i("Peer Manufacturer is \"%s\"", string);
@@ -254,7 +252,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
254252
}
255253
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
256254
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
257-
char *string = (char *)malloc(zbstr->len + 1);
255+
char string[zbstr->len + 1];
258256
memcpy(string, zbstr->data, zbstr->len);
259257
string[zbstr->len] = '\0';
260258
log_i("Peer Model is \"%s\"", string);

0 commit comments

Comments
 (0)