Re: [PATCH] tools/thermal: Fix common realloc mistake

From: Daniel Lezcano
Date: Thu Nov 14 2024 - 03:08:50 EST



Hi Zhang,

thanks for spotting the issue

On 08/11/2024 05:47, zhangjiao2 wrote:
From: zhang jiao <zhangjiao2@xxxxxxxxxxxxxxxxxxxx>

Do not set thermometer->tz NULL when realloc failed.

You may describe a bit more the change.

eg.

If the 'realloc' fails, the thermal zones pointer is set to NULL. This makes all thermal zones references which were previously successfully initialized to be lost.

Signed-off-by: zhang jiao <zhangjiao2@xxxxxxxxxxxxxxxxxxxx>
---
tools/thermal/thermometer/thermometer.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
index 1a87a0a77f9f..e08291a97fd8 100644
--- a/tools/thermal/thermometer/thermometer.c
+++ b/tools/thermal/thermometer/thermometer.c
@@ -259,6 +259,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
{
int fd;
char tz_path[PATH_MAX];
+ void *tmp;

Please use right type:

struct tz *tz;

sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
@@ -268,12 +269,13 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
return -1;
}
- thermometer->tz = realloc(thermometer->tz,
+ tmp = realloc(thermometer->tz,
sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
- if (!thermometer->tz) {
+ if (!tmp) {
ERROR("Failed to allocate thermometer->tz\n");
return -1;
}
+ thermometer->tz = tmp;
thermometer->tz[thermometer->nr_tz].fd_temp = fd;
thermometer->tz[thermometer->nr_tz].name = strdup(name);


--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog