From d4b0418b4bf86787bfd8692fb2328fe39c1105a8 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 19 Jul 2025 10:48:06 +0200 Subject: [PATCH] src/chfn.c: Simplify checking for a long GECOS field 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 --- src/chfn.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/chfn.c b/src/chfn.c index b4f913ba1..ca654e0d7 100644 --- a/src/chfn.c +++ b/src/chfn.c @@ -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); -- 2.47.2