Skip to content

Commit c384173

Browse files
committed
[SensorBHI260AP]Fix possible memory leaks
1 parent 7bc3c4f commit c384173

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

src/SensorBHI260AP.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -685,10 +685,12 @@ BoschSensorInfo SensorBHI260AP::getSensorInfo()
685685
if (bhy2_get_error_value(&sensorInfo.sensor_error, _bhy2.get()) != BHY2_OK) {
686686
log_e("bhy2_get_error_value failed!");
687687
}
688-
for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) {
689-
if (bhy2_is_sensor_available(i, _bhy2.get())) {
690-
if (bhy2_get_sensor_info(i, &sensorInfo.info[i], _bhy2.get()) != BHY2_OK) {
691-
log_e("bhy2_get_sensor_info [%u] failed!", i);
688+
if (sensorInfo.info) {
689+
for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) {
690+
if (bhy2_is_sensor_available(i, _bhy2.get())) {
691+
if (bhy2_get_sensor_info(i, &sensorInfo.info[i], _bhy2.get()) != BHY2_OK) {
692+
log_e("bhy2_get_sensor_info [%u] failed!", i);
693+
}
692694
}
693695
}
694696
}

src/bosch/BoschSensorInfo.hpp

+93-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,93 @@ class BoschSensorInfo
6060

6161
~BoschSensorInfo()
6262
{
63-
free(info);
63+
if (info) {
64+
free(info);
65+
}
66+
}
67+
68+
BoschSensorInfo(const BoschSensorInfo &other)
69+
: kernel_version(other.kernel_version),
70+
user_version(other.user_version),
71+
rom_version(other.rom_version),
72+
product_id(other.product_id),
73+
host_status(other.host_status),
74+
feat_status(other.feat_status),
75+
boot_status(other.boot_status),
76+
sensor_error(other.sensor_error),
77+
dev(other.dev),
78+
info(nullptr)
79+
{
80+
if (other.info) {
81+
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
82+
for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) {
83+
info[i] = other.info[i];
84+
}
85+
}
86+
}
87+
88+
BoschSensorInfo &operator=(const BoschSensorInfo &other)
89+
{
90+
if (this != &other) {
91+
if (info) {
92+
free(info);
93+
}
94+
kernel_version = other.kernel_version;
95+
user_version = other.user_version;
96+
rom_version = other.rom_version;
97+
product_id = other.product_id;
98+
host_status = other.host_status;
99+
feat_status = other.feat_status;
100+
boot_status = other.boot_status;
101+
sensor_error = other.sensor_error;
102+
dev = other.dev;
103+
info = nullptr;
104+
if (other.info) {
105+
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
106+
for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) {
107+
info[i] = other.info[i];
108+
}
109+
}
110+
}
111+
return *this;
112+
}
113+
114+
BoschSensorInfo(BoschSensorInfo &&other) noexcept
115+
: kernel_version(other.kernel_version),
116+
user_version(other.user_version),
117+
rom_version(other.rom_version),
118+
product_id(other.product_id),
119+
host_status(other.host_status),
120+
feat_status(other.feat_status),
121+
boot_status(other.boot_status),
122+
sensor_error(other.sensor_error),
123+
dev(other.dev),
124+
info(other.info)
125+
{
126+
other.info = nullptr;
127+
other.dev = nullptr;
128+
}
129+
130+
BoschSensorInfo &operator=(BoschSensorInfo &&other) noexcept
131+
{
132+
if (this != &other) {
133+
if (info) {
134+
free(info);
135+
}
136+
kernel_version = other.kernel_version;
137+
user_version = other.user_version;
138+
rom_version = other.rom_version;
139+
product_id = other.product_id;
140+
host_status = other.host_status;
141+
feat_status = other.feat_status;
142+
boot_status = other.boot_status;
143+
sensor_error = other.sensor_error;
144+
dev = other.dev;
145+
info = other.info;
146+
other.info = nullptr;
147+
other.dev = nullptr;
148+
}
149+
return *this;
64150
}
65151

66152
#ifdef ARDUINO
@@ -69,6 +155,9 @@ class BoschSensorInfo
69155
if (!dev) {
70156
return;
71157
}
158+
if (!info) {
159+
return;
160+
}
72161
if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) {
73162
stream.printf("Virtual sensor list.\n");
74163
stream.printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");
@@ -138,6 +227,9 @@ class BoschSensorInfo
138227
if (!dev) {
139228
return;
140229
}
230+
if (!info) {
231+
return;
232+
}
141233
if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) {
142234
printf("Virtual sensor list.\n");
143235
printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");

0 commit comments

Comments
 (0)