diff mbox series

tools/power turbostat: Fix file pointer leak

Message ID 20220426131607.1520483-1-colin.i.king@gmail.com
State Accepted
Commit 5e5fd36c58d6c820f7292ee492c3731c9a104a41
Headers show
Series tools/power turbostat: Fix file pointer leak | expand

Commit Message

Colin Ian King April 26, 2022, 1:16 p.m. UTC
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(-)

Comments

Chen Yu April 26, 2022, 1:39 p.m. UTC | #1
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
diff mbox series

Patch

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;