mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled()
@ 2023-04-17  7:56 Hao Zeng
  2023-04-17 21:09 ` Shuah Khan
  0 siblings, 1 reply; 3+ messages in thread
From: Hao Zeng @ 2023-04-17  7:56 UTC (permalink / raw)
  To: skhan; +Cc: trenn, shuah, linux-pm, linux-kernel, zenghao

The sysfs_get_enabled() opened file processor not closed,
may cause a file handle leak.
Putting error handling and resource cleanup code together
makes the code easy to maintain and read.
Removed the unnecessary else if branch from the original
function, as it should return an error in cases other than '0'.

Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
---
 tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
index 0ce29ee4c2e4..f0334a5f1acf 100644
--- a/tools/power/cpupower/lib/powercap.c
+++ b/tools/power/cpupower/lib/powercap.c
@@ -40,25 +40,31 @@ static int sysfs_get_enabled(char *path, int *mode)
 {
 	int fd;
 	char yes_no;
+	int ret = 0;
 
 	*mode = 0;
 
 	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return -1;
+	if (fd == -1) {
+		ret = -1;
+		goto out;
+	}
 
 	if (read(fd, &yes_no, 1) != 1) {
-		close(fd);
-		return -1;
+		ret = -1;
+		goto out_close;
 	}
 
 	if (yes_no == '1') {
 		*mode = 1;
-		return 0;
-	} else if (yes_no == '0') {
-		return 0;
+	} else if (yes_no != '0') {
+		ret = -1;
+		goto out_close;
 	}
-	return -1;
+out_close:
+	close(fd);
+out:
+	return ret;
 }
 
 int powercap_get_enabled(int *mode)
-- 
2.37.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled()
  2023-04-17  7:56 [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled() Hao Zeng
@ 2023-04-17 21:09 ` Shuah Khan
  2023-04-18  2:21   ` Hao Zeng
  0 siblings, 1 reply; 3+ messages in thread
From: Shuah Khan @ 2023-04-17 21:09 UTC (permalink / raw)
  To: Hao Zeng; +Cc: trenn, shuah, linux-pm, linux-kernel, Shuah Khan

On 4/17/23 01:56, Hao Zeng wrote:
> The sysfs_get_enabled() opened file processor not closed,
> may cause a file handle leak.
> Putting error handling and resource cleanup code together
> makes the code easy to maintain and read.
> Removed the unnecessary else if branch from the original
> function, as it should return an error in cases other than '0'.
> 
> Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
> Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
> ---
>   tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++--------
>   1 file changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
> index 0ce29ee4c2e4..f0334a5f1acf 100644
> --- a/tools/power/cpupower/lib/powercap.c
> +++ b/tools/power/cpupower/lib/powercap.c
> @@ -40,25 +40,31 @@ static int sysfs_get_enabled(char *path, int *mode)
>   {
>   	int fd;
>   	char yes_no;
> +	int ret = 0;
>   
>   	*mode = 0;
>   
>   	fd = open(path, O_RDONLY);
> -	if (fd == -1)
> -		return -1;
> +	if (fd == -1) {
> +		ret = -1;
> +		goto out;
> +	}
>   
>   	if (read(fd, &yes_no, 1) != 1) {
> -		close(fd);
> -		return -1;
> +		ret = -1;
> +		goto out_close;
>   	}
>   
>   	if (yes_no == '1') {
>   		*mode = 1;
> -		return 0;

You can just add goto out_close here

> -	} else if (yes_no == '0') {
> -		return 0;

Keep == '0' check and add goto out_close here
> +	} else if (yes_no != '0') {

This can be just else with the above change.

> +		ret = -1;
> +		goto out_close;
>   	}
> -	return -1;
> +out_close:
> +	close(fd);
> +out:
> +	return ret;
>   }
>   
>   int powercap_get_enabled(int *mode)

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled()
  2023-04-17 21:09 ` Shuah Khan
@ 2023-04-18  2:21   ` Hao Zeng
  0 siblings, 0 replies; 3+ messages in thread
From: Hao Zeng @ 2023-04-18  2:21 UTC (permalink / raw)
  To: Shuah Khan; +Cc: trenn, shuah, linux-pm, linux-kernel

Dear Shuah:

Thank you again for your patience and guidance. This has benefited me a
lot. I will apply what I have learned this time to my work and kernel
contribution

Best regards
-- Hao

On Mon, 2023-04-17 at 15:09 -0600, Shuah Khan wrote:
> On 4/17/23 01:56, Hao Zeng wrote:
> > The sysfs_get_enabled() opened file processor not closed,
> > may cause a file handle leak.
> > Putting error handling and resource cleanup code together
> > makes the code easy to maintain and read.
> > Removed the unnecessary else if branch from the original
> > function, as it should return an error in cases other than '0'.
> > 
> > Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
> > Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
> > ---
> >   tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++--------
> >   1 file changed, 14 insertions(+), 8 deletions(-)
> > 
> > diff --git a/tools/power/cpupower/lib/powercap.c
> > b/tools/power/cpupower/lib/powercap.c
> > index 0ce29ee4c2e4..f0334a5f1acf 100644
> > --- a/tools/power/cpupower/lib/powercap.c
> > +++ b/tools/power/cpupower/lib/powercap.c
> > @@ -40,25 +40,31 @@ static int sysfs_get_enabled(char *path, int
> > *mode)
> >   {
> >         int fd;
> >         char yes_no;
> > +       int ret = 0;
> >   
> >         *mode = 0;
> >   
> >         fd = open(path, O_RDONLY);
> > -       if (fd == -1)
> > -               return -1;
> > +       if (fd == -1) {
> > +               ret = -1;
> > +               goto out;
> > +       }
> >   
> >         if (read(fd, &yes_no, 1) != 1) {
> > -               close(fd);
> > -               return -1;
> > +               ret = -1;
> > +               goto out_close;
> >         }
> >   
> >         if (yes_no == '1') {
> >                 *mode = 1;
> > -               return 0;
> 
> You can just add goto out_close here
> 
> > -       } else if (yes_no == '0') {
> > -               return 0;
> 
> Keep == '0' check and add goto out_close here
> > +       } else if (yes_no != '0') {
> 
> This can be just else with the above change.
> 
> > +               ret = -1;
> > +               goto out_close;
> >         }
> > -       return -1;
> > +out_close:
> > +       close(fd);
> > +out:
> > +       return ret;
> >   }
> >   
> >   int powercap_get_enabled(int *mode)
> 
> thanks,
> -- Shuah


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-18  2:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-17  7:56 [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled() Hao Zeng
2023-04-17 21:09 ` Shuah Khan
2023-04-18  2:21   ` Hao Zeng

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®