linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools/power turbostat: Fix file pointer leak
@ 2022-04-26 13:16 Colin Ian King
  2022-04-26 13:39 ` Chen Yu
  0 siblings, 1 reply; 2+ messages in thread
From: Colin Ian King @ 2022-04-26 13:16 UTC (permalink / raw)
  To: Len Brown, Chen Yu, linux-pm; +Cc: kernel-janitors, linux-kernel

Currently if a fscanf fails then an early return leaks an open
file pointer. Fix this by fclosing the file before the return.
Detected using static analysis with cppcheck:

tools/power/x86/turbostat/turbostat.c:2039:3: error: Resource leak: fp [resourceLeak]

Fixes: eae97e053fe3 ("tools/power turbostat: Support thermal throttle count print")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
---
 tools/power/x86/turbostat/turbostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index e6779f599a8e..db431b31c4df 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -2035,9 +2035,9 @@ int get_core_throt_cnt(int cpu, unsigned long long *cnt)
 	if (!fp)
 		return -1;
 	ret = fscanf(fp, "%lld", &tmp);
+	fclose(fp);
 	if (ret != 1)
 		return -1;
-	fclose(fp);
 	*cnt = tmp;
 
 	return 0;
-- 
2.35.1


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

* Re: [PATCH] tools/power turbostat: Fix file pointer leak
  2022-04-26 13:16 [PATCH] tools/power turbostat: Fix file pointer leak Colin Ian King
@ 2022-04-26 13:39 ` Chen Yu
  0 siblings, 0 replies; 2+ messages in thread
From: Chen Yu @ 2022-04-26 13:39 UTC (permalink / raw)
  To: Colin Ian King; +Cc: Len Brown, linux-pm, kernel-janitors, linux-kernel

On Tue, Apr 26, 2022 at 02:16:07PM +0100, Colin Ian King wrote:
> Currently if a fscanf fails then an early return leaks an open
> file pointer. Fix this by fclosing the file before the return.
> Detected using static analysis with cppcheck:
> 
> tools/power/x86/turbostat/turbostat.c:2039:3: error: Resource leak: fp [resourceLeak]
> 
> Fixes: eae97e053fe3 ("tools/power turbostat: Support thermal throttle count print")
> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
> ---
>  tools/power/x86/turbostat/turbostat.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
> index e6779f599a8e..db431b31c4df 100644
> --- a/tools/power/x86/turbostat/turbostat.c
> +++ b/tools/power/x86/turbostat/turbostat.c
> @@ -2035,9 +2035,9 @@ int get_core_throt_cnt(int cpu, unsigned long long *cnt)
>  	if (!fp)
>  		return -1;
>  	ret = fscanf(fp, "%lld", &tmp);
> +	fclose(fp);
>  	if (ret != 1)
>  		return -1;
> -	fclose(fp);
>  	*cnt = tmp;
>  
>  	return 0;
Acked-by: Chen Yu <yu.c.chen@intel.com>

Thanks for fixing it.


Chenyu

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

end of thread, other threads:[~2022-04-26 13:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-26 13:16 [PATCH] tools/power turbostat: Fix file pointer leak Colin Ian King
2022-04-26 13:39 ` Chen Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).