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

When the read return value is equal to 1, a file handle leak will occur

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

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


No virus found
		Checked by Hillstone Network AntiVirus

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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-14  3:08 [PATCH v2] cpupower:Fix resource leaks in sysfs_get_enabled() Hao Zeng
2023-04-14 16:41 ` Shuah Khan
2023-04-17  8: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®