Skip to content

Commit d0d43c2

Browse files
committed
Merge branch 'fix/ble_mesh_rpr_example_fix_v5.4' into 'release/v5.4'
fix(ble_mesh): add proper memory cleanup in composition data parsing (v5.4) See merge request espressif/esp-idf!43839
2 parents d2fa261 + 04a6e32 commit d0d43c2

File tree

1 file changed

+38
-14
lines changed
  • examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main

1 file changed

+38
-14
lines changed

examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/main.c

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* SPDX-FileCopyrightText: 2017 Intel Corporation
5-
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
5+
* SPDX-FileContributor: 2018-2025 Espressif Systems (Shanghai) CO LTD
66
*
77
* SPDX-License-Identifier: Apache-2.0
88
*/
@@ -353,26 +353,22 @@ static void example_ble_mesh_parse_node_comp_data(esp_ble_mesh_node_info_t* node
353353

354354
node->sig_model_num = (uint8_t *)calloc(node->elem_num, sizeof(uint8_t));
355355
if (!node->sig_model_num) {
356-
ESP_LOGW(TAG, "No Free memory to store composition data");
357-
return;
356+
goto calloc_fail;
358357
}
359358

360359
node->vnd_model_num = (uint8_t *)calloc(node->elem_num, sizeof(uint8_t));
361360
if (!node->vnd_model_num) {
362-
ESP_LOGW(TAG, "No Free memory to store composition data");
363-
return;
361+
goto vnd_model_num_fail;
364362
}
365363

366364
node->sig_models = (uint16_t **)calloc(node->elem_num, sizeof(uint16_t*));
367365
if (!node->sig_models) {
368-
ESP_LOGW(TAG, "No Free memory to store composition data");
369-
return;
366+
goto sig_models_fail;
370367
}
371368

372369
node->vnd_models = (uint32_t **)calloc(node->elem_num, sizeof(uint32_t*));
373-
if (!node->sig_models) {
374-
ESP_LOGW(TAG, "No Free memory to store composition data");
375-
return;
370+
if (!node->vnd_models) {
371+
goto vnd_models_fail;
376372
}
377373

378374
ESP_LOGI(TAG, "********************** Composition Data Start **********************");
@@ -387,8 +383,7 @@ static void example_ble_mesh_parse_node_comp_data(esp_ble_mesh_node_info_t* node
387383
if (nums) {
388384
node->sig_models[seq] = (uint16_t *)calloc(nums, sizeof(uint16_t));
389385
if (!(node->sig_models[seq])) {
390-
ESP_LOGW(TAG, "No Free memory to store composition data");
391-
return;
386+
goto sig_mode_seq_fail;
392387
}
393388
} else {
394389
node->sig_models[seq] = NULL;
@@ -397,8 +392,7 @@ static void example_ble_mesh_parse_node_comp_data(esp_ble_mesh_node_info_t* node
397392
if (numv) {
398393
node->vnd_models[seq] = (uint32_t *)calloc(numv, sizeof(uint32_t));
399394
if (!(node->vnd_models[seq])) {
400-
ESP_LOGW(TAG, "No Free memory to store composition data");
401-
return;
395+
goto vnd_model_seq_fail;
402396
}
403397
} else {
404398
node->vnd_models[seq] = NULL;
@@ -422,6 +416,32 @@ static void example_ble_mesh_parse_node_comp_data(esp_ble_mesh_node_info_t* node
422416
seq++;
423417
}
424418
ESP_LOGI(TAG, "*********************** Composition Data End ***********************");
419+
return;
420+
421+
vnd_model_seq_fail:
422+
free(node->sig_models[seq]);
423+
node->sig_models[seq] = NULL;
424+
sig_mode_seq_fail:
425+
for (int j = 0; j < seq; j++) {
426+
free(node->sig_models[j]);
427+
free(node->vnd_models[j]);
428+
node->sig_models[j] = NULL;
429+
node->vnd_models[j] = NULL;
430+
}
431+
free(node->vnd_models);
432+
node->vnd_models = NULL;
433+
vnd_models_fail:
434+
free(node->sig_models);
435+
node->sig_models = NULL;
436+
sig_models_fail:
437+
free(node->vnd_model_num);
438+
node->vnd_model_num = NULL;
439+
vnd_model_num_fail:
440+
free(node->sig_model_num);
441+
node->sig_model_num = NULL;
442+
calloc_fail:
443+
ESP_LOGW(TAG, "No Free memory to store composition data");
444+
return;
425445
}
426446

427447
static bool example_ble_mesh_query_element_have_model(uint16_t elem_addr, uint16_t model_id, uint16_t company_id)
@@ -435,6 +455,10 @@ static bool example_ble_mesh_query_element_have_model(uint16_t elem_addr, uint16
435455

436456
elem_idx = elem_addr - node->unicast;
437457

458+
if (node->sig_model_num == NULL) {
459+
return false;
460+
}
461+
438462
if (company_id == CID_NVAL) {
439463
model_num = node->sig_model_num[elem_idx];
440464
for (i = 0; i < model_num; i++) {

0 commit comments

Comments
 (0)