]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rtcwake: improve open() usage [coverity scan]
authorKarel Zak <kzak@redhat.com>
Wed, 5 Aug 2015 09:32:59 +0000 (11:32 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 5 Aug 2015 09:33:29 +0000 (11:33 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/rtcwake.c

index 5dbacb247dabc8d09afdc05318f665f41b9e73dd..842ea509cb6b697b34a79d06ea7d0b22ab8e363f 100644 (file)
@@ -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)