utils: don't ignore open() return value
authorDaniel Golle <daniel@makrotopia.org>
Mon, 30 Aug 2021 23:35:53 +0000 (00:35 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Mon, 30 Aug 2021 23:46:49 +0000 (00:46 +0100)
In case active console cannot be opened, return NULL early instead
of trying to read from errornous file descriptor.

Coverity CID: 1490087 Argument cannot be negative
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
utils/utils.c

index 2cfcc8e10c6c930de9aa02c6d77dad2cb2bcd970..f0c4a906bae6605cb2fce6a99314e459ca5f4074 100644 (file)
@@ -139,7 +139,12 @@ char *get_active_console(char *out, int len)
 {
        char line[CMDLINE_SIZE + 1];
        int fd = open("/sys/class/tty/console/active", O_RDONLY);
-       ssize_t r = read(fd, line, sizeof(line) - 1);
+       ssize_t r;
+
+       if (fd < 0)
+               return NULL;
+
+       r = read(fd, line, sizeof(line) - 1);
        line[CMDLINE_SIZE] = '\0';
 
        close(fd);