Skip to contentSkip to conversation

tools/power turbostat: close file after use

PATCH In review Tom Rix 2 participants 3 messages Apr 30, 2022 Turbostat utility

lore
Share & tools
lore

Tom Rix

author
#0 lore raw

PATCH 1/1 tools/power turbostat: close file after use

The cppcheck reports this issue
turbostat.c:2039]: (error) Resource leak: fp

When the fscanf fails an error is returned without closing fp. Move the fclose so even if the fscanf fails, the file will be closed.

Fixes eae97e053fe3 ("tools/power turbostat: Support thermal throttle count print")
---
 tools/power/x86/turbostat/turbostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/x86/turbostat/turbostat.c +1 -1
index ede31a4287a0..2e9a751af260 100644
@@ -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;
Signature · 1 line
2.27.0
#1 lore raw
Quoted 18 lines · Tom Rix

The cppcheck reports this issue
turbostat.c:2039]: (error) Resource leak: fp

When the fscanf fails an error is returned without closing fp. Move the fclose so even if the fscanf fails, the file will be closed.

Fixes eae97e053fe3 ("tools/power turbostat: Support thermal throttle count print")
---
 tools/power/x86/turbostat/turbostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/x86/turbostat/turbostat.c +0 -0
index ede31a4287a0..2e9a751af260 100644
@@ -2035,9 +2035,9 @@ int get_core_throt_cnt(int cpu, unsigned long long *cnt)
 	if (!fp)

tools/power/x86/turbostat/turbostat.c int get_core_throt_cnt(int cpu, unsigned long long *cnt)

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c +1 -2
 		return -1;
 	ret = fscanf(fp, "%lld", &tmp);
+	fclose(fp);
 	if (ret != 1)
 		return -1;
-	fclose(fp);
 	*cnt = tmp;
 
 	return 0;
--

Thank you Tom, and Colin has proposed a fix here:
https://lore.kernel.org/lkml/[email protected]/

thanks,
Chenyu

author Reviewed-by
#2 lore raw
Quoted 27 lines · Chen Yu
Quoted 26 lines · Tom Rix

The cppcheck reports this issue
turbostat.c:2039]: (error) Resource leak: fp

When the fscanf fails an error is returned without closing fp. Move the fclose so even if the fscanf fails, the file will be closed.

Fixes eae97e053fe3 ("tools/power turbostat: Support thermal throttle count print")
---
  tools/power/x86/turbostat/turbostat.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/x86/turbostat/turbostat.c +1 -1
index ede31a4287a0..2e9a751af260 100644
@@ -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;
 
Quoted 4 lines

tools/power/x86/turbostat/turbostat.c int get_core_throt_cnt(int cpu, unsigned long long *cnt)

Quoted 2 lines
tools/power/x86/turbostat/turbostat.c +0 -1
  	return 0;
--

Thank you Tom, and Colin has proposed a fix here:
https://lore.kernel.org/lkml/[email protected]/

Obviously I approve of Colin's fix :)

If it is not too much trouble, please add

If it is already on its way to linux-next, don't worry about it.

Thanks,

Tom

Quoted 4 lines

thanks,
Chenyu