From: Tobias Stoeckmann Date: Sun, 12 Apr 2026 11:20:35 +0000 (+0200) Subject: newgrp: Correctly handle getline error X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=663215b203acd1304432e73dd3c231df0418a314;p=thirdparty%2Futil-linux.git newgrp: Correctly handle getline error 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 --- diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c index 17c58e55e..2fd8c38ff 100644 --- a/login-utils/newgrp.c +++ b/login-utils/newgrp.c @@ -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;