cpupower:Fix resource leaks in sysfs_get_enabled()

[ Upstream commit e652be0f59 ]

The sysfs_get_enabled() opened file processor not closed,
may cause a file handle leak.
Putting error handling and resource cleanup code together
makes the code easy to maintain and read.
Removed the unnecessary else if branch from the original
function, as it should return an error in cases other than '0'.

Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Hao Zeng 2023-04-18 09:30:56 +08:00 committed by Greg Kroah-Hartman
parent 23d41a123d
commit a62d40c1de
1 changed files with 16 additions and 7 deletions

View File

@ -40,25 +40,34 @@ static int sysfs_get_enabled(char *path, int *mode)
{
int fd;
char yes_no;
int ret = 0;
*mode = 0;
fd = open(path, O_RDONLY);
if (fd == -1)
return -1;
if (fd == -1) {
ret = -1;
goto out;
}
if (read(fd, &yes_no, 1) != 1) {
close(fd);
return -1;
ret = -1;
goto out_close;
}
if (yes_no == '1') {
*mode = 1;
return 0;
goto out_close;
} else if (yes_no == '0') {
return 0;
goto out_close;
} else {
ret = -1;
goto out_close;
}
return -1;
out_close:
close(fd);
out:
return ret;
}
int powercap_get_enabled(int *mode)