]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chfn: use xasprintf() rather than bunch of strlen() and malloc() calls
authorSami Kerola <kerolasa@iki.fi>
Sun, 14 Dec 2014 12:28:26 +0000 (12:28 +0000)
committerSami Kerola <kerolasa@iki.fi>
Fri, 19 Dec 2014 09:11:03 +0000 (09:11 +0000)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
login-utils/chfn.c

index ca6f4567057bb2ad7fd930926896fdee0d58f742..4746927c8240f02a955cea7702f55e47c6595315 100644 (file)
@@ -324,12 +324,8 @@ static int save_new_data(struct finfo *pinfo)
                pinfo->other = "";
 
        /* create the new gecos string */
-       len = (strlen(pinfo->full_name) + strlen(pinfo->office) +
-              strlen(pinfo->office_phone) + strlen(pinfo->home_phone) +
-              strlen(pinfo->other) + 4);
-       gecos = (char *)xmalloc(len + 1);
-       sprintf(gecos, "%s,%s,%s,%s,%s", pinfo->full_name, pinfo->office,
-               pinfo->office_phone, pinfo->home_phone, pinfo->other);
+       len = xasprintf(&gecos, "%s,%s,%s,%s,%s", pinfo->full_name, pinfo->office,
+                 pinfo->office_phone, pinfo->home_phone, pinfo->other);
 
        /* remove trailing empty fields (but not subfields of pinfo->other) */
        if (!pinfo->other[0]) {
@@ -351,6 +347,7 @@ static int save_new_data(struct finfo *pinfo)
                       ("Finger information *NOT* changed.  Try again later.\n"));
                return -1;
        }
+       free(gecos);
        printf(_("Finger information changed.\n"));
        return 0;
 }