mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shuah Khan <skhan@linuxfoundation.org>
To: Hao Zeng <zenghao@kylinos.cn>
Cc: trenn@suse.com, shuah@kernel.org, linux-pm@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Shuah Khan <skhan@linuxfoundation.org>
Subject: Re: [PATCH v2] cpupower:Fix resource leaks in sysfs_get_enabled()
Date: Fri, 14 Apr 2023 10:41:35 -0600	[thread overview]
Message-ID: <4c04ae6d-f8db-0a2d-0bd5-97c4ec828a59@linuxfoundation.org> (raw)
In-Reply-To: <20230414030830.3829332-1-zenghao@kylinos.cn>

On 4/13/23 21:08, Hao Zeng wrote:
> When the read return value is equal to 1, a file handle leak will occur
> 

Would like a bit more information on how the error paths are
redone, since memory leak isn't the only one that is fixed.

> Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
> Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
> ---
>   tools/power/cpupower/lib/powercap.c | 19 ++++++++++---------
>   1 file changed, 10 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
> index 0ce29ee4c2e4..02ec5b0bff6b 100644
> --- a/tools/power/cpupower/lib/powercap.c
> +++ b/tools/power/cpupower/lib/powercap.c
> @@ -40,7 +40,7 @@ static int sysfs_get_enabled(char *path, int *mode)
>   {
>   	int fd;
>   	char yes_no;
> -
> +	int ret = 0;
>   	*mode = 0;
>   
>   	fd = open(path, O_RDONLY);
> @@ -48,17 +48,18 @@ static int sysfs_get_enabled(char *path, int *mode)
>   		return -1;
>   
>   	if (read(fd, &yes_no, 1) != 1) {
> -		close(fd);
> -		return -1;
> +		ret = -1;
> +		goto err;
>   	}
>   
> -	if (yes_no == '1') {
> -		*mode = 1;
> -		return 0;
> -	} else if (yes_no == '0') {
> -		return 0;
> +	if (yes_no != '1' || yes_no != '0') {
> +		ret = -1;
> +		goto err;
>   	}
> -	return -1;
> +	*mode = yes_no - '0';

I am not seeing much value in changing the above paths.
Leave them unchanged.

> +err:
> +	close(fd);
> +	return ret;
>   }
>   
>   int powercap_get_enabled(int *mode)

thanks,
-- Shuah

  reply	other threads:[~2023-04-14 16:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-14  3:08 Hao Zeng
2023-04-14 16:41 ` Shuah Khan [this message]
2023-04-17  8:21   ` Hao Zeng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c04ae6d-f8db-0a2d-0bd5-97c4ec828a59@linuxfoundation.org \
    --to=skhan@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=trenn@suse.com \
    --cc=zenghao@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®