[PATCH v2] tools/thermal/thermometer: close fd on realloc failure

From: amarjeet

Date: Sat Jun 20 2026 - 08:27:29 EST


From: Amarjeet <amarjeet@xxxxxxxxx>

thermometer_add_tz() opens tz_path and then reallocates thermometer->tz.
If realloc fails, the function returns without closing fd.

Close fd before returning on realloc failure to avoid leaking a file descriptor.

Signed-off-by: Amarjeet <amarjeet@xxxxxxxxx>

---
v2:
- drop close(fd) on open() failure path
- keep close(fd) only on realloc failure path
---
tools/thermal/thermometer/thermometer.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
index 022865da8e3c..1ae6c3ecedb4 100644
--- a/tools/thermal/thermometer/thermometer.c
+++ b/tools/thermal/thermometer/thermometer.c
@@ -272,6 +272,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
tz = realloc(thermometer->tz, sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
if (!tz) {
ERROR("Failed to allocate thermometer->tz\n");
+ close(fd);
return -1;
}

--
2.34.1