From: Sami Kerola Date: Sun, 14 Dec 2014 12:28:26 +0000 (+0000) Subject: chfn: use xasprintf() rather than bunch of strlen() and malloc() calls X-Git-Tag: v2.26-rc1~112^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b7a19ebbb61c608265b552f4426bfe01373b1d0;p=thirdparty%2Futil-linux.git chfn: use xasprintf() rather than bunch of strlen() and malloc() calls Signed-off-by: Sami Kerola --- diff --git a/login-utils/chfn.c b/login-utils/chfn.c index ca6f456705..4746927c82 100644 --- a/login-utils/chfn.c +++ b/login-utils/chfn.c @@ -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; }