[PATCH 2/2] tools/thermal/thermometer: Fix missing NULL check after realloc()
From: YanLong Dai
Date: Tue Nov 03 2026 - 08:32:45 EST
In configuration_init(), the return value of realloc() is assigned
directly to config->tz_regex without checking for NULL. If realloc()
fails, the original pointer is lost (memory leak) and subsequent
dereference causes a NULL pointer crash.
Fix by using a temporary variable to receive the realloc() result and
checking for NULL before assigning to config->tz_regex.
Fixes: 110acbc6a451 ("tools/thermal: Add a temperature capture tool")
Signed-off-by: YanLong Dai <daiyanlong@xxxxxxxxxx>
---
tools/thermal/thermometer/thermometer.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
index b4f26d49e4c7..cc84540f467a 100644
--- a/tools/thermal/thermometer/thermometer.c
+++ b/tools/thermal/thermometer/thermometer.c
@@ -142,6 +142,7 @@ static int configuration_init(const char *path, struct configuration *config)
INFO("Found %d thermal zone(s) regular expression\n", length);
for (i = 0; i < length; i++) {
+ struct tz_regex *tmp;
config_setting_t *node;
const char *name;
@@ -163,8 +164,13 @@ static int configuration_init(const char *path, struct configuration *config)
return -1;
}
- config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
- (config->nr_tz_regex + 1));
+ tmp = realloc(config->tz_regex, sizeof(*config->tz_regex) *
+ (config->nr_tz_regex + 1));
+ if (!tmp) {
+ ERROR("Failed to allocate memory for thermal zone regex\n");
+ return -1;
+ }
+ config->tz_regex = tmp;
if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, name,
REG_NOSUB | REG_EXTENDED)) {
--
2.25.1