Skip to contentSkip to conversation

tools/power turbostat: Fix file pointer leak

PATCH In review Colin Ian King 2 participants 2 messages Apr 26, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/power turbostat: Fix file pointer leak

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")
---
 tools/power/x86/turbostat/turbostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/x86/turbostat/turbostat.c +1 -1
index e6779f599a8e..db431b31c4df 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.35.1
Acked-by on v1 1/1
#1 lore raw
Quoted 17 lines · Colin Ian King

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")
---
 tools/power/x86/turbostat/turbostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/x86/turbostat/turbostat.c +0 -0
index e6779f599a8e..db431b31c4df 100644

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

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c +1 -1
@@ -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;

Thanks for fixing it.

Chenyu