From 4d169305dcb34137dc41acc761d8703eae2c63bf Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Fri, 3 Sep 2021 07:55:40 +0300 Subject: Use inttostr to represent integer values as strings * gnulib.modules: Request inttostr * src/idcache.c (getuser,getgroup): Use umaxtostr instead of sprintf. * src/userspec.c (parse_user_spec): Likewise. --- gnulib.modules | 1 + src/idcache.c | 11 +++++------ src/userspec.c | 9 +++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/gnulib.modules b/gnulib.modules index 1181bcd..15037a0 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -14,6 +14,7 @@ getline gettext-h gitlog-to-changelog hash +inttostr inttypes lchown progname diff --git a/src/idcache.c b/src/idcache.c index 877993f..eb7a1db 100644 --- a/src/idcache.c +++ b/src/idcache.c @@ -34,6 +34,7 @@ #endif #include +#include struct userid { @@ -59,7 +60,6 @@ getuser (uid_t uid) { register struct userid *tail; struct passwd *pwent; - char usernum_string[20]; for (tail = user_alist; tail; tail = tail->next) if (tail->id.u == uid) @@ -70,8 +70,8 @@ getuser (uid_t uid) tail->id.u = uid; if (pwent == 0) { - sprintf (usernum_string, "%u", (unsigned) uid); - tail->name = xstrdup (usernum_string); + char nbuf[UINTMAX_STRSIZE_BOUND]; + tail->name = xstrdup (umaxtostr (uid, nbuf)); } else tail->name = xstrdup (pwent->pw_name); @@ -134,7 +134,6 @@ getgroup (gid_t gid) { register struct userid *tail; struct group *grent; - char groupnum_string[20]; for (tail = group_alist; tail; tail = tail->next) if (tail->id.g == gid) @@ -145,8 +144,8 @@ getgroup (gid_t gid) tail->id.g = gid; if (grent == 0) { - sprintf (groupnum_string, "%u", (unsigned int) gid); - tail->name = xstrdup (groupnum_string); + char nbuf[UINTMAX_STRSIZE_BOUND]; + tail->name = xstrdup (umaxtostr (gid, nbuf)); } else tail->name = xstrdup (grent->gr_name); diff --git a/src/userspec.c b/src/userspec.c index 59a9130..1bb47b4 100644 --- a/src/userspec.c +++ b/src/userspec.c @@ -24,6 +24,7 @@ #include #include #include +#include #ifndef HAVE_ENDPWENT # define endpwent() @@ -141,12 +142,8 @@ parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid, grp = getgrgid (pwd->pw_gid); if (grp == NULL) { - /* This is enough room to hold the unsigned decimal - representation of any 32-bit quantity and the trailing - zero byte. */ - char uint_buf[21]; - sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid)); - V_STRDUP (groupname, uint_buf); + char nbuf[UINTMAX_STRSIZE_BOUND]; + V_STRDUP (groupname, umaxtostr (pwd->pw_gid, nbuf)); } else { -- cgit v1.2.1