Re: [PATCH kvmtool] arm64: Fix resource leaks in find_pmu_cpumask()

Will Deacon <[email protected]>
Newsgroups org.kernel.vger.kvm
Message-ID <[email protected]>
On Thu, Jul 23, 2026 at 05:04:13PM +0800, Zongmin Zhou wrote:
> From: Zongmin Zhou <[email protected]>
> 
> Close file descriptors on read_file() failure and close the directory
> stream before returning from the function.
> 
> Signed-off-by: Zongmin Zhou <[email protected]>
> ---
>  arm64/pmu.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/arm64/pmu.c b/arm64/pmu.c
> index 78c15f1..b4d7605 100644
> --- a/arm64/pmu.c
> +++ b/arm64/pmu.c
> @@ -75,7 +75,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  	unsigned long val;
>  	ssize_t fd_sz;
>  	int fd, ret;
> -	DIR *dir;
> +	DIR *dir = NULL;
>  
>  	memset(buf, 0, sizeof(buf));
>  
> @@ -109,6 +109,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  		fd_sz = read_file(fd, cpulist, PAGE_SIZE);
>  		if (fd_sz < 0) {
>  			pmu_id = -errno;
> +			close(fd);
>  			goto out_free;
>  		}
>  		close(fd);

Seems a bit grotty to have identical calls to close() on the success and
failure paths.

> @@ -142,6 +143,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  		fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1);
>  		if (fd_sz < 0) {
>  			pmu_id = -errno;
> +			close(fd);
>  			goto out_free;
>  		}
>  		close(fd);

Same here...

Will
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.