Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/thermal: Fix common realloc mistake

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.

[dlezcano] : Fixed indentation

Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
Link: https://lore.kernel.org/r/20241114084039.42149-1-zhangjiao2@cmss.chinamobile.com
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

authored by

zhang jiao and committed by
Daniel Lezcano
d303e3dd d8afb8cc

+4 -3
+4 -3
tools/thermal/thermometer/thermometer.c
··· 259 259 { 260 260 int fd; 261 261 char tz_path[PATH_MAX]; 262 + struct tz *tz; 262 263 263 264 sprintf(tz_path, CLASS_THERMAL"/%s/temp", path); 264 265 ··· 269 268 return -1; 270 269 } 271 270 272 - thermometer->tz = realloc(thermometer->tz, 273 - sizeof(*thermometer->tz) * (thermometer->nr_tz + 1)); 274 - if (!thermometer->tz) { 271 + tz = realloc(thermometer->tz, sizeof(*thermometer->tz) * (thermometer->nr_tz + 1)); 272 + if (!tz) { 275 273 ERROR("Failed to allocate thermometer->tz\n"); 276 274 return -1; 277 275 } 278 276 277 + thermometer->tz = tz; 279 278 thermometer->tz[thermometer->nr_tz].fd_temp = fd; 280 279 thermometer->tz[thermometer->nr_tz].name = strdup(name); 281 280 thermometer->tz[thermometer->nr_tz].polling = polling;