From: Alejandro Colomar Date: Mon, 12 Aug 2024 00:50:26 +0000 (+0200) Subject: lib/, src/: Use countof() instead of sizeof() with fgets(3) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a252e87b7b728b3531200b72da3ba84b8ed77d59;p=thirdparty%2Fshadow.git lib/, src/: Use countof() instead of sizeof() with fgets(3) Except for the added includes, this patch has been scripted with a semantic patch: $ cat ~/tmp/spatch/fgets_nitems.sp @@ expression a, b, c; @@ - fgets(a, sizeof(b), c) + fgets(a, countof(b), c) @@ expression a, b, c; @@ - fgets(a, sizeof b, c) + fgets(a, countof(b), c) Applied as: $ find lib* src/ -type f \ | xargs spatch --sp-file ~/tmp/spatch/fgets_nitems.sp --in-place; Reviewed-by: Iker Pedrosa Signed-off-by: Alejandro Colomar --- diff --git a/lib/console.c b/lib/console.c index 8e4efb59c..12f32be24 100644 --- a/lib/console.c +++ b/lib/console.c @@ -16,6 +16,7 @@ #include "defines.h" #include "getdef.h" #include "prototypes.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" #include "string/strcpy/strtcpy.h" @@ -76,7 +77,7 @@ is_listed(const char *cfgin, const char *tty, bool def) * See if this tty is listed in the console file. */ - while (fgets(buf, sizeof(buf), fp) != NULL) { + while (fgets(buf, countof(buf), fp) != NULL) { stpsep(buf, "\n"); if (streq(buf, tty)) { (void) fclose (fp); diff --git a/lib/getdef.c b/lib/getdef.c index b3fc9e15b..c850ac4f5 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -550,7 +550,7 @@ static void def_load (void) /* * Go through all of the lines in the file. */ - while (fgets(buf, sizeof(buf), fp) != NULL) { + while (fgets(buf, countof(buf), fp) != NULL) { /* * Trim trailing whitespace. diff --git a/lib/hushed.c b/lib/hushed.c index c2df9a4b9..1de80f5da 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -21,6 +21,7 @@ #include "defines.h" #include "getdef.h" #include "prototypes.h" +#include "sizeof.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" #include "string/strtok/stpsep.h" @@ -74,7 +75,7 @@ bool hushed (const char *username) if (NULL == fp) { return false; } - for (found = false; !found && (fgets(buf, sizeof(buf), fp) != NULL);) { + for (found = false; !found && (fgets(buf, countof(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 ab519582a..6a2046065 100644 --- a/lib/loginprompt.c +++ b/lib/loginprompt.c @@ -17,6 +17,7 @@ #include "defines.h" #include "getdef.h" #include "prototypes.h" +#include "sizeof.h" #include "string/memset/memzero.h" #include "string/strcpy/strtcpy.h" #include "string/strspn/stpspn.h" @@ -82,7 +83,7 @@ login_prompt(char *name, int namesize) */ memzero_a(buf); - if (fgets(buf, sizeof(buf), stdin) == NULL) + if (fgets(buf, countof(buf), stdin) == NULL) exit (EXIT_FAILURE); if (stpsep(buf, "\n") == NULL) diff --git a/lib/port.c b/lib/port.c index 357be218c..565054d24 100644 --- a/lib/port.c +++ b/lib/port.c @@ -19,6 +19,7 @@ #include "defines.h" #include "port.h" #include "prototypes.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" @@ -139,7 +140,7 @@ getportent(void) */ next: - if (fgets(buf, sizeof(buf), ports) == NULL) { + if (fgets(buf, countof(buf), ports) == NULL) { errno = saveerr; return NULL; } diff --git a/lib/setupenv.c b/lib/setupenv.c index 8017741be..5e36bcf67 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -24,6 +24,7 @@ #include #include "getdef.h" #include "shadowlog.h" +#include "sizeof.h" #include "string/sprintf/aprintf.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" @@ -53,7 +54,7 @@ static void read_env_file (const char *filename) if (NULL == fp) { return; } - while (fgets(buf, sizeof(buf), fp) != NULL) { + while (fgets(buf, countof(buf), fp) != NULL) { if (stpsep(buf, "\n") == NULL) break; diff --git a/lib/ttytype.c b/lib/ttytype.c index ee50ce3bc..9fb046e0a 100644 --- a/lib/ttytype.c +++ b/lib/ttytype.c @@ -18,6 +18,7 @@ #include "defines.h" #include "getdef.h" #include "prototypes.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" #include "string/strtok/stpsep.h" @@ -48,7 +49,7 @@ void ttytype (const char *line) perror (typefile); return; } - while (fgets(buf, sizeof(buf), fp) != NULL) { + while (fgets(buf, countof(buf), fp) != NULL) { if (strprefix(buf, "#")) { continue; } diff --git a/lib/tz.c b/lib/tz.c index 78f6593cf..9e530a921 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -20,6 +20,7 @@ #include "defines.h" #include "getdef.h" #include "prototypes.h" +#include "sizeof.h" #include "string/strtok/stpsep.h" @@ -37,7 +38,7 @@ fp = fopen (fname, "r"); if ( (NULL == fp) - || (fgets(tzbuf, sizeof(tzbuf), fp) == NULL)) { + || (fgets(tzbuf, countof(tzbuf), fp) == NULL)) { result = "TZ=CST6CDT"; } else { stpsep(tzbuf, "\n"); diff --git a/lib/user_busy.c b/lib/user_busy.c index 97fa1427b..8cf5c8937 100644 --- a/lib/user_busy.c +++ b/lib/user_busy.c @@ -25,6 +25,7 @@ #include "subordinateio.h" #endif /* ENABLE_SUBIDS */ #include "shadowlog.h" +#include "sizeof.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" #include "string/strcmp/strneq.h" @@ -128,7 +129,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) != NULL) { + while (fgets(line, countof(line), sfile) != NULL) { if (strprefix(line, "Uid:\t")) { unsigned long ruid, euid, suid; diff --git a/src/chgpasswd.c b/src/chgpasswd.c index aec4f63b7..1430a48b2 100644 --- a/src/chgpasswd.c +++ b/src/chgpasswd.c @@ -31,6 +31,7 @@ #include "exitcodes.h" #include "shadow/gshadow/sgrp.h" #include "shadowlog.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strerrno.h" #include "string/strtok/stpsep.h" @@ -383,7 +384,7 @@ int main (int argc, char **argv) * group entry for each group will be looked up in the appropriate * file (gshadow or group) and the password changed. */ - while (fgets(buf, sizeof(buf), stdin) != NULL) { + while (fgets(buf, countof(buf), stdin) != NULL) { line++; if (stpsep(buf, "\n") == NULL) { fprintf (stderr, _("%s: line %jd: line too long\n"), diff --git a/src/chpasswd.c b/src/chpasswd.c index 3c8838d1a..9d865ba3c 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -34,6 +34,7 @@ /*@-exitarg@*/ #include "exitcodes.h" #include "shadowlog.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strerrno.h" #include "string/strtok/stpsep.h" @@ -423,14 +424,14 @@ int main (int argc, char **argv) * last change date is set in the age only if aging information is * present. */ - while (fgets(buf, sizeof(buf), stdin) != NULL) { + while (fgets(buf, countof(buf), stdin) != NULL) { char *cp; line++; if (stpsep(buf, "\n") == NULL) { if (feof (stdin) == 0) { // Drop all remaining characters on this line. - while (fgets(buf, sizeof(buf), stdin) != NULL) { + while (fgets(buf, countof(buf), stdin) != NULL) { if (strchr(buf, '\n')) break; } diff --git a/src/login_nopam.c b/src/login_nopam.c index 08702fa49..4890a9a4f 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) != NULL)) + && (fgets(line, countof(line), fp) != NULL)) { char *p; diff --git a/src/newusers.c b/src/newusers.c index 1f58b7753..f079bc837 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -50,6 +50,7 @@ #endif /* ENABLE_SUBIDS */ #include "shadow/gshadow/sgrp.h" #include "shadowlog.h" +#include "sizeof.h" #include "sssd.h" #include "string/sprintf/snprintf.h" #include "string/strcmp/streq.h" @@ -1021,7 +1022,7 @@ int main (int argc, char **argv) * over 100 is allocated. The pw_gid field will be updated with that * value. */ - while (fgets(buf, sizeof(buf), stdin) != NULL) { + while (fgets(buf, countof(buf), stdin) != NULL) { line++; if (stpsep(buf, "\n") == NULL && feof(stdin) == 0) { fprintf (stderr, _("%s: line %jd: line too long\n"), diff --git a/src/suauth.c b/src/suauth.c index edf22f043..935f2637a 100644 --- a/src/suauth.c +++ b/src/suauth.c @@ -19,6 +19,7 @@ #include "defines.h" #include "prototypes.h" +#include "sizeof.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" #include "string/strspn/stpspn.h" @@ -72,7 +73,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root) return DENY; } - while (fgets(temp, sizeof(temp), authfile_fd) != NULL) { + while (fgets(temp, countof(temp), authfile_fd) != NULL) { char *p; lines++; diff --git a/src/useradd.c b/src/useradd.c index 93373e96a..de75fb0e8 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -59,6 +59,7 @@ #endif #include "shadow/gshadow/sgrp.h" #include "shadowlog.h" +#include "sizeof.h" #include "sssd.h" #include "string/memset/memzero.h" #include "string/sprintf/aprintf.h" @@ -354,7 +355,7 @@ get_defaults(const 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) != NULL) { + while (fgets(buf, countof(buf), fp) != NULL) { stpsep(buf, "\n"); cp = stpsep(buf, "="); @@ -598,7 +599,7 @@ set_defaults(void) goto skip; } - while (fgets(buf, sizeof(buf), ifp) != NULL) { + while (fgets(buf, countof(buf), ifp) != NULL) { char *val; if (stpsep(buf, "\n") == NULL) {