From: Karel Zak Date: Wed, 5 Aug 2015 09:32:59 +0000 (+0200) Subject: rtcwake: improve open() usage [coverity scan] X-Git-Tag: v2.27-rc2~42 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6b497c0e19145cb3a70637c9d27c430b4602f8c0;p=thirdparty%2Futil-linux.git rtcwake: improve open() usage [coverity scan] Signed-off-by: Karel Zak --- diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c index 5dbacb247d..842ea509cb 100644 --- a/sys-utils/rtcwake.c +++ b/sys-utils/rtcwake.c @@ -242,19 +242,24 @@ static int setup_alarm(struct rtcwake_control *ctl, int fd, time_t *wakeup) static char **get_sys_power_states(struct rtcwake_control *ctl) { + int fd = -1; + if (!ctl->possible_modes) { - int fd; char buf[256] = { 0 }; - if (!(fd = open(SYS_POWER_STATE_PATH, O_RDONLY))) - return NULL; - if (read(fd, &buf, sizeof buf) < 0) { - close(fd); - return NULL; - } + fd = open(SYS_POWER_STATE_PATH, O_RDONLY); + if (fd < 0) + goto nothing; + if (read(fd, &buf, sizeof buf) <= 0) + goto nothing; ctl->possible_modes = strv_split(buf, " \n"); + close(fd); } return ctl->possible_modes; +nothing: + if (fd >= 0) + close(fd); + return NULL; } static void suspend_system(struct rtcwake_control *ctl)