]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
nologin: don't call fstat() after failed open() [coverity scan]
authorKarel Zak <kzak@redhat.com>
Wed, 17 May 2017 10:59:33 +0000 (12:59 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 17 May 2017 10:59:33 +0000 (12:59 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/nologin.c

index 2e897cc722764f52447db4500bd31ab9d3d8e5b4..e950d2a3f296f312529023efc4dc9d620231fbe2 100644 (file)
@@ -38,7 +38,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 
 int main(int argc, char *argv[])
 {
-       int c, fd;
+       int c, fd = -1;
        struct stat st;
        static const struct option longopts[] = {
                { "help",    0, NULL, 'h' },
@@ -64,16 +64,26 @@ int main(int argc, char *argv[])
        }
 
        fd = open(_PATH_NOLOGIN_TXT, O_RDONLY);
+       if (fd < 0)
+               goto dflt;
+
        c = fstat(fd, &st);
-       if (fd >= 0 && !c && S_ISREG(st.st_mode)) {
+       if (c < 0 || !S_ISREG(st.st_mode))
+               goto dflt;
+       else {
                char buf[BUFSIZ];
                ssize_t rd;
 
                while ((rd = read(fd, buf, sizeof(buf))) > 0)
                        ignore_result( write(STDOUT_FILENO, buf, rd) );
+
                close(fd);
-       } else
-               fprintf(stdout, _("This account is currently not available.\n"));
+               return EXIT_FAILURE;
+       }
 
+dflt:
+       if (fd >= 0)
+               close(fd);
+       fprintf(stdout, _("This account is currently not available.\n"));
        return EXIT_FAILURE;
 }