Skip to contentSkip to conversation

tools/thermal/thermometer: close fd on realloc failure

PATCH Superseded [email protected] 2 participants 3 messages Jun 20 – Jul 22 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/thermal/thermometer: close fd on realloc failure

From: Amarjeet <[email protected]>

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.

---
 tools/thermal/thermometer/thermometer.c | 1 +
 1 file changed, 1 insertion(+)
tools/thermal/thermometer/thermometer.c +1 -0
index 022865da8e3c..103db7a2a8e4 100644
@@ -266,6 +266,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
 	fd = open(tz_path, O_RDONLY);
 	if (fd < 0) {
 		ERROR("Failed to open '%s': %m\n", tz_path);
+		close(fd);
 		return -1;
 	}
 
Signature · 1 line
2.34.1
#1 lore raw

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

From: Amarjeet <[email protected]>

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.

---
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(+)
tools/thermal/thermometer/thermometer.c +1 -0
index 022865da8e3c..1ae6c3ecedb4 100644
@@ -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;
 	}
 
Signature · 1 line
2.34.1
maintainer Applied
#2 lore raw
Quoted 23 lines · On Sat, Jun 20, 2026 at 2:27 PM <[email protected]> wrote:

From: Amarjeet <[email protected]>

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.

---
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(+)
tools/thermal/thermometer/thermometer.c +0 -0
index 022865da8e3c..1ae6c3ecedb4 100644

tools/thermal/thermometer/thermometer.c static int thermometer_add_tz(const char *path, const char *name, int polling,

Quoted 8 lines
tools/thermal/thermometer/thermometer.c +1 -1
@@ -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;
        }
 
--

Applied as 7.3 material, thanks!