From: Alejandro Colomar Date: Sun, 21 Jul 2024 16:21:02 +0000 (+0200) Subject: lib/, src/: Consistently use NULL with fgets(3) X-Git-Tag: 4.19.0-rc1~55 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1ebca415b8478ade8ddaf3fcacf872add99c4ae0;p=thirdparty%2Fshadow.git lib/, src/: Consistently use NULL with fgets(3) fgets(3) returns either NULL or the input pointer. Checking for NULL is more explicit, and simpler. is the header that provides NULL; add it where appropriate. The meat of this patch can be approximated with the following semantic patch: $ cat ~/tmp/spatch/fgets_null.sp @@ expression a, b, c; @@ - fgets(a, b, c) == a + fgets(a, b, c) != NULL @@ expression a, b, c; @@ - fgetsx(a, b, c) == a + fgetsx(a, b, c) != NULL @@ expression a, b, c, p; @@ - p->cio_fgets(a, b, c) == a + p->cio_fgets(a, b, c) != NULL @@ expression a, b, c; @@ - fgets(a, b, c) != a + fgets(a, b, c) == NULL @@ expression a, b, c; @@ - fgetsx(a, b, c) != a + fgetsx(a, b, c) == NULL @@ expression a, b, c, p; @@ - p->cio_fgets(a, b, c) != a + p->cio_fgets(a, b, c) == NUL Applied as $ find contrib/ lib* src/ -type f \ | xargs spatch --sp-file ~/tmp/spatch/fgets_null.sp --in-place; The differences between the actual patch and the approximation via the semantic patch from above are includes, whitespace, braces, and a case where there was an implicit pointer-to-bool comparison which I made explicit. When reviewing, it'll be useful to use git-diff(1) with '-w' and '--color-words=.'. Signed-off-by: Alejandro Colomar --- diff --git a/lib/commonio.c b/lib/commonio.c index 85bde16b9..5341f7b7e 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -638,7 +639,7 @@ int commonio_open (struct commonio_db *db, int mode) if (NULL == buf) goto cleanup_errno; - while (db->ops->cio_fgets(buf, buflen, db->fp) == buf) { + while (db->ops->cio_fgets(buf, buflen, db->fp) != NULL) { struct commonio_entry *p; while ( (strrchr (buf, '\n') == NULL) diff --git a/lib/fields.c b/lib/fields.c index 2a6f54e5f..144e5fd09 100644 --- a/lib/fields.c +++ b/lib/fields.c @@ -12,8 +12,9 @@ #include "fields.h" #include -#include +#include #include +#include #include "prototypes.h" #include "string/ctype/strisascii/strisprint.h" @@ -68,9 +69,8 @@ change_field(char *buf, size_t maxsize, const char *prompt) printf ("\t%s [%s]: ", prompt, buf); (void) fflush (stdout); - if (fgets (newf, maxsize, stdin) != newf) { + if (fgets(newf, maxsize, stdin) == NULL) return; - } if (stpsep(newf, "\n") == NULL) return; diff --git a/lib/fputsx.c b/lib/fputsx.c index 73495e4de..0f86da4f3 100644 --- a/lib/fputsx.c +++ b/lib/fputsx.c @@ -9,6 +9,7 @@ #include "config.h" +#include #include #include @@ -24,7 +25,7 @@ fgetsx(/*@returned@*/char *restrict buf, int cnt, FILE *restrict f) char *ep; while (cnt > 0) { - if (fgets (cp, cnt, f) != cp) { + if (fgets(cp, cnt, f) == NULL) { if (cp == buf) { return NULL; } else { diff --git a/lib/hushed.c b/lib/hushed.c index aadc28722..c2df9a4b9 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -13,6 +13,7 @@ #ident "$Id$" #include +#include #include #include #include @@ -73,7 +74,7 @@ bool hushed (const char *username) if (NULL == fp) { return false; } - for (found = false; !found && (fgets(buf, sizeof(buf), fp) == buf);) { + for (found = false; !found && (fgets(buf, sizeof(buf), fp) != NULL);) { stpsep(buf, "\n"); found = streq(buf, pw->pw_shell) || streq(buf, pw->pw_name); diff --git a/lib/loginprompt.c b/lib/loginprompt.c index 36ae70616..4798d1c61 100644 --- a/lib/loginprompt.c +++ b/lib/loginprompt.c @@ -12,8 +12,9 @@ #ident "$Id$" #include -#include #include +#include +#include #include "attr.h" #include "defines.h" @@ -83,9 +84,8 @@ login_prompt(char *name, int namesize) */ memzero_a(buf); - if (fgets(buf, sizeof(buf), stdin) != buf) { + if (fgets(buf, sizeof(buf), stdin) == NULL) exit (EXIT_FAILURE); - } if (stpsep(buf, "\n") == NULL) exit(EXIT_FAILURE); diff --git a/lib/setupenv.c b/lib/setupenv.c index 1bf8b3a35..263221636 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -16,6 +16,7 @@ #ident "$Id$" #include +#include #include #include #include @@ -55,7 +56,7 @@ static void read_env_file (const char *filename) if (NULL == fp) { return; } - while (fgets(buf, sizeof(buf), fp) == buf) { + while (fgets(buf, sizeof(buf), fp) != NULL) { if (stpsep(buf, "\n") == NULL) break; diff --git a/lib/shadow/gshadow/fgetsgent.c b/lib/shadow/gshadow/fgetsgent.c index 29ef11b72..64e7d4541 100644 --- a/lib/shadow/gshadow/fgetsgent.c +++ b/lib/shadow/gshadow/fgetsgent.c @@ -66,9 +66,8 @@ fgetsgent(FILE *fp) buflen *= 2; len = strlen (buf); - if (fgetsx(&buf[len], buflen - len, fp) != &buf[len]) { + if (fgetsx(&buf[len], buflen - len, fp) == NULL) return NULL; - } } stpsep(buf, "\n"); return sgetsgent(buf); diff --git a/lib/ttytype.c b/lib/ttytype.c index b3c60be9d..ee50ce3bc 100644 --- a/lib/ttytype.c +++ b/lib/ttytype.c @@ -11,6 +11,7 @@ #ident "$Id$" +#include #include #include @@ -47,7 +48,7 @@ void ttytype (const char *line) perror (typefile); return; } - while (fgets(buf, sizeof(buf), fp) == buf) { + while (fgets(buf, sizeof(buf), fp) != NULL) { if (strprefix(buf, "#")) { continue; } diff --git a/lib/user_busy.c b/lib/user_busy.c index c265d49d4..d08229cbe 100644 --- a/lib/user_busy.c +++ b/lib/user_busy.c @@ -12,6 +12,7 @@ #ident "$Id: $" #include +#include #include #include #include @@ -127,7 +128,7 @@ static int check_status (const char *name, const char *sname, uid_t uid) if (NULL == sfile) { return 0; } - while (fgets(line, sizeof(line), sfile) == line) { + while (fgets(line, sizeof(line), sfile) != NULL) { if (strprefix(line, "Uid:\t")) { unsigned long ruid, euid, suid; diff --git a/src/login_nopam.c b/src/login_nopam.c index 9cd147417..a41a3f0bd 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -100,7 +100,7 @@ login_access(const char *user, const char *from) if (NULL != fp) { intmax_t lineno = 0; /* for diagnostics */ while ( !match - && (fgets(line, sizeof(line), fp) == line)) + && (fgets(line, sizeof(line), fp) != NULL)) { char *p; diff --git a/src/useradd.c b/src/useradd.c index fcf9b0d21..7cce5b703 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -18,17 +18,18 @@ #include #include #ifdef ENABLE_LASTLOG -#include +# include #endif /* ENABLE_LASTLOG */ #include #include #include #ifdef ACCT_TOOLS_SETUID -#ifdef USE_PAM -#include "pam_defs.h" -#endif /* USE_PAM */ +# ifdef USE_PAM +# include "pam_defs.h" +# endif /* USE_PAM */ #endif /* ACCT_TOOLS_SETUID */ #include +#include #include #include #include @@ -355,7 +356,7 @@ get_defaults(struct option_flags *flags) * Read the file a line at a time. Only the lines that have relevant * values are used, everything else can be ignored. */ - while (fgets(buf, sizeof(buf), fp) == buf) { + while (fgets(buf, sizeof(buf), fp) != NULL) { stpsep(buf, "\n"); cp = stpsep(buf, "="); @@ -587,7 +588,7 @@ set_defaults(void) goto skip; } - while (fgets(buf, sizeof(buf), ifp) == buf) { + while (fgets(buf, sizeof(buf), ifp) != NULL) { char *val; if (stpsep(buf, "\n") == NULL) {