Skip to content

Commit c75c1a6

Browse files
committed
components/esp_matter_bridge: fix nvs handle leak and nullptr
dereference - fix NVS handle leak on store_device_persistent_info error path - add null check for device_type_callback before calling - check return value of store_bridged_endpoint_ids
1 parent 2cd90e7 commit c75c1a6

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

components/esp_matter_bridge/esp_matter_bridge.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ static esp_err_t store_device_persistent_info(device_persistent_info_t *persiste
5555
persistent_info, sizeof(device_persistent_info_t));
5656
if (err != ESP_OK) {
5757
ESP_LOGE(TAG, "Failed on nvs_set_blob when storing device_persistent_info");
58+
nvs_close(handle);
59+
return err;
5860
}
5961
err = nvs_commit(handle);
6062
if (err != ESP_OK) {
@@ -255,6 +257,10 @@ esp_err_t set_device_type(device_t *bridged_device, uint32_t device_type_id, voi
255257
ESP_LOGE(TAG, "bridged_device cannot be NULL");
256258
return ESP_ERR_INVALID_ARG;
257259
}
260+
if (!device_type_callback) {
261+
ESP_LOGE(TAG, "device_type_callback is NULL, call initialize() first");
262+
return ESP_ERR_INVALID_STATE;
263+
}
258264
err = device_type_callback(bridged_device->endpoint, device_type_id, priv_data);
259265
if (err != ESP_OK) {
260266
return err;
@@ -345,7 +351,9 @@ device_t *create_device(node_t *node, uint16_t parent_endpoint_id, uint32_t devi
345351
remove_device(dev);
346352
return NULL;
347353
}
348-
store_bridged_endpoint_ids();
354+
if (store_bridged_endpoint_ids() != ESP_OK) {
355+
ESP_LOGE(TAG, "Failed to persist bridged endpoint IDs");
356+
}
349357
return dev;
350358
}
351359

components/esp_matter_bridge/esp_matter_bridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <esp_matter_data_model.h>
2020

2121
#define MAX_BRIDGED_DEVICE_COUNT \
22-
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT - 1 - CONFIG_ESP_MATTER_AGGREGATOR_ENDPOINT_COUNT
22+
(CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT - 1 - CONFIG_ESP_MATTER_AGGREGATOR_ENDPOINT_COUNT)
2323
// There is an endpoint reserved as root endpoint
2424

2525
namespace esp_matter_bridge {

0 commit comments

Comments
 (0)