]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
src/chfn.c: Simplify checking for a long GECOS field
authorAlejandro Colomar <alx@kernel.org>
Sat, 19 Jul 2025 08:48:06 +0000 (10:48 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 9 Aug 2025 23:00:36 +0000 (18:00 -0500)
Use a buffer of the exact size we want, and let SNPRINTF() decide if it
fits or not.

BTW, the old check seemed to be wrong: it wasn't accounting for the
commas in the 80-character limit, but that didn't make much sense.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
src/chfn.c

index b4f913ba1b5665d4d0aaeaf24e67030b4c0e8f33..ca654e0d72089db8507e2ba55c06a32f80b74592 100644 (file)
@@ -561,7 +561,8 @@ static void check_fields (void)
  */
 int main (int argc, char **argv)
 {
-       char                 new_gecos[BUFSIZ];
+       int                  ret;
+       char                 new_gecos[80];
        char                 *user;
        const struct passwd  *pw;
 
@@ -643,14 +644,13 @@ int main (int argc, char **argv)
         * Build the new GECOS field by plastering all the pieces together,
         * if they will fit ...
         */
-       if ((strlen (fullnm) + strlen (roomno) + strlen (workph) +
-            strlen (homeph) + strlen (slop)) > (unsigned int) 80) {
+       ret = SNPRINTF(new_gecos, "%s,%s,%s,%s%s%s",
+                      fullnm, roomno, workph, homeph,
+                      (!streq(slop, "")) ? "," : "", slop);
+       if (ret == -1) {
                fprintf (stderr, _("%s: fields too long\n"), Prog);
                fail_exit (E_NOPERM);
        }
-       SNPRINTF(new_gecos, "%s,%s,%s,%s%s%s",
-                fullnm, roomno, workph, homeph,
-                (!streq(slop, "")) ? "," : "", slop);
 
        /* Rewrite the user's gecos in the passwd file */
        update_gecos (user, new_gecos);