Re: [PATCH v2] tools/thermal/thermometer: close fd on realloc failure
From: Rafael J. Wysocki (Intel)
Date: Wed Jul 22 2026 - 11:46:15 EST
On Sat, Jun 20, 2026 at 2:27 PM <amarjeet@xxxxxxxxx> wrote:
>
> 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;
> }
>
> --
Applied as 7.3 material, thanks!