]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
newgrp: Correctly handle getline error
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 12 Apr 2026 11:20:35 +0000 (13:20 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 12 Apr 2026 11:20:35 +0000 (13:20 +0200)
If getline fails, errno might be overridden by isatty, leading a wrong
error message.

Also, getline returns -1 on EOF. In which case errno is not set. Use a
custom error message and still handle this as an error condition.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
login-utils/newgrp.c

index 17c58e55eb64a9070a5654d55ab3eb4ef532a831..2fd8c38ff39b2e47a9fd9670818acd6b08fa62ff 100644 (file)
@@ -38,6 +38,7 @@ static char *xgetpass(FILE *input, const char *prompt)
        const int fd = fileno(input);
        size_t dummy = 0;
        ssize_t len;
+       int save_errno;
 
        fputs(prompt, stdout);
        if (isatty(fd)) {
@@ -50,12 +51,17 @@ static char *xgetpass(FILE *input, const char *prompt)
                        err(EXIT_FAILURE, _("could not set terminal attributes"));
        }
        len = getline(&pass, &dummy, input);
+       save_errno = errno;
        if (isatty(fd))
                /* restore terminal */
                if (tcsetattr(fd, TCSANOW, &saved))
                        err(EXIT_FAILURE, _("could not set terminal attributes"));
-       if (len < 0)
+       if (len < 0) {
+               if (feof(input))
+                       errx(EXIT_FAILURE, _("failed to read password"));
+               errno = save_errno;
                err(EXIT_FAILURE, _("getline() failed"));
+       }
        if (0 < len && *(pass + len - 1) == '\n')
                *(pass + len - 1) = '\0';
        return pass;