From c384173830bd13f93739997a6c21ed76f3ee67f0 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Fri, 24 Jan 2025 16:47:59 +0800 Subject: [PATCH] [SensorBHI260AP]Fix possible memory leaks --- src/SensorBHI260AP.cpp | 10 ++-- src/bosch/BoschSensorInfo.hpp | 94 ++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/src/SensorBHI260AP.cpp b/src/SensorBHI260AP.cpp index 267b4b3..996e852 100644 --- a/src/SensorBHI260AP.cpp +++ b/src/SensorBHI260AP.cpp @@ -685,10 +685,12 @@ BoschSensorInfo SensorBHI260AP::getSensorInfo() if (bhy2_get_error_value(&sensorInfo.sensor_error, _bhy2.get()) != BHY2_OK) { log_e("bhy2_get_error_value failed!"); } - for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) { - if (bhy2_is_sensor_available(i, _bhy2.get())) { - if (bhy2_get_sensor_info(i, &sensorInfo.info[i], _bhy2.get()) != BHY2_OK) { - log_e("bhy2_get_sensor_info [%u] failed!", i); + if (sensorInfo.info) { + for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) { + if (bhy2_is_sensor_available(i, _bhy2.get())) { + if (bhy2_get_sensor_info(i, &sensorInfo.info[i], _bhy2.get()) != BHY2_OK) { + log_e("bhy2_get_sensor_info [%u] failed!", i); + } } } } diff --git a/src/bosch/BoschSensorInfo.hpp b/src/bosch/BoschSensorInfo.hpp index f15dbe6..ab38676 100644 --- a/src/bosch/BoschSensorInfo.hpp +++ b/src/bosch/BoschSensorInfo.hpp @@ -60,7 +60,93 @@ class BoschSensorInfo ~BoschSensorInfo() { - free(info); + if (info) { + free(info); + } + } + + BoschSensorInfo(const BoschSensorInfo &other) + : kernel_version(other.kernel_version), + user_version(other.user_version), + rom_version(other.rom_version), + product_id(other.product_id), + host_status(other.host_status), + feat_status(other.feat_status), + boot_status(other.boot_status), + sensor_error(other.sensor_error), + dev(other.dev), + info(nullptr) + { + if (other.info) { + info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info)); + for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) { + info[i] = other.info[i]; + } + } + } + + BoschSensorInfo &operator=(const BoschSensorInfo &other) + { + if (this != &other) { + if (info) { + free(info); + } + kernel_version = other.kernel_version; + user_version = other.user_version; + rom_version = other.rom_version; + product_id = other.product_id; + host_status = other.host_status; + feat_status = other.feat_status; + boot_status = other.boot_status; + sensor_error = other.sensor_error; + dev = other.dev; + info = nullptr; + if (other.info) { + info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info)); + for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) { + info[i] = other.info[i]; + } + } + } + return *this; + } + + BoschSensorInfo(BoschSensorInfo &&other) noexcept + : kernel_version(other.kernel_version), + user_version(other.user_version), + rom_version(other.rom_version), + product_id(other.product_id), + host_status(other.host_status), + feat_status(other.feat_status), + boot_status(other.boot_status), + sensor_error(other.sensor_error), + dev(other.dev), + info(other.info) + { + other.info = nullptr; + other.dev = nullptr; + } + + BoschSensorInfo &operator=(BoschSensorInfo &&other) noexcept + { + if (this != &other) { + if (info) { + free(info); + } + kernel_version = other.kernel_version; + user_version = other.user_version; + rom_version = other.rom_version; + product_id = other.product_id; + host_status = other.host_status; + feat_status = other.feat_status; + boot_status = other.boot_status; + sensor_error = other.sensor_error; + dev = other.dev; + info = other.info; + other.info = nullptr; + other.dev = nullptr; + } + return *this; } #ifdef ARDUINO @@ -69,6 +155,9 @@ class BoschSensorInfo if (!dev) { return; } + if (!info) { + return; + } if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) { stream.printf("Virtual sensor list.\n"); stream.printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n"); @@ -138,6 +227,9 @@ class BoschSensorInfo if (!dev) { return; } + if (!info) { + return; + } if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) { printf("Virtual sensor list.\n"); printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");