]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use countof() instead of sizeof() with fgets(3)
authorAlejandro Colomar <alx@kernel.org>
Mon, 12 Aug 2024 00:50:26 +0000 (02:50 +0200)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Mon, 16 Mar 2026 11:12:58 +0000 (12:12 +0100)
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 <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
15 files changed:
lib/console.c
lib/getdef.c
lib/hushed.c
lib/loginprompt.c
lib/port.c
lib/setupenv.c
lib/ttytype.c
lib/tz.c
lib/user_busy.c
src/chgpasswd.c
src/chpasswd.c
src/login_nopam.c
src/newusers.c
src/suauth.c
src/useradd.c

index 8e4efb59ccf165d423d16289924b012ace30d151..12f32be24d5fa34d58c52c7e2e00c591067c9b2d 100644 (file)
@@ -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);
index b3fc9e15b44d1618f8df19d7ddc934ff14caa964..c850ac4f51bd7a8df91f15ae5aaeafeada8a5ac5 100644 (file)
@@ -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.
index c2df9a4b90dba62da4f4e8da54bda73e329f5715..1de80f5daf4f44a817b1b4738b8d47d8404d0469 100644 (file)
@@ -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);
index ab519582a81087fcbc9faccfdf3bd7a49af3ac1f..6a2046065a765269fd04e0cfe870249113f74dd1 100644 (file)
@@ -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)
index 357be218cc6b67fe7b11213e4252f317a6749946..565054d24983f7cdbaa8871fba6be964e384cf81 100644 (file)
@@ -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;
        }
index 8017741be5e13a48fb4939e3fc90821ce465a1ca..5e36bcf67567302fe579259697b5c014740b8ca0 100644 (file)
@@ -24,6 +24,7 @@
 #include <pwd.h>
 #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;
 
index ee50ce3bc225830f9be8d96356ec0fb628e7f956..9fb046e0a9b016bf625e793f4e52dc288c575f57 100644 (file)
@@ -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;
                }
index 78f6593cfeaf0a78f25d133631ce12df95babe74..9e530a921cbe991135df28296bad5b800ece637a 100644 (file)
--- 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");
index 97fa1427b8ea8628f54ea0f3a1d49fecc70ae434..8cf5c8937b56e772094f382e2105c7d6eeccc0c7 100644 (file)
@@ -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;
 
index aec4f63b7d576cde5e60aa607e28fcd1c1e3feec..1430a48b2a760edbcc240c82c1b13aeb1e5df58d 100644 (file)
@@ -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"),
index 3c8838d1a5a69f9c9676b7e5d9098a3560376f92..9d865ba3c79f969968bd95110a177b6e4d05b72c 100644 (file)
@@ -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;
                                }
index 08702fa498fbee290d6cd13d4c555df0c52e44cb..4890a9a4fcc2116dd9b5465a12c962b6b939f297 100644 (file)
@@ -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;
 
index 1f58b775306afffe970e815ce4d1f4dad9fa4c35..f079bc837f620d25df98643f967271fae1f00920 100644 (file)
@@ -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"),
index edf22f0432b60f22289fc392ab5bf008e5673059..935f2637af60dccb556c1efc91428e6562e8c462 100644 (file)
@@ -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++;
index 93373e96a95516e75cfb9939c5fc4d8cee335a1d..de75fb0e8ffcee47bc3d1601d9b1d458864b7dc3 100644 (file)
@@ -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) {