From: Jim Meyering Date: Sat, 12 Jun 2004 08:07:30 +0000 (+0000) Subject: (extract_trimmed_name): Don't apply strchr to a X-Git-Tag: v5.3.0~1349 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=33c1c51c49a08e1640805c3de31759846010b54b;p=thirdparty%2Fcoreutils.git (extract_trimmed_name): Don't apply strchr to a non-string; this leads to undefined behavior. --- diff --git a/lib/readutmp.c b/lib/readutmp.c index 896e976dc4..508c91ee8d 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -40,12 +40,14 @@ extract_trimmed_name (const STRUCT_UTMP *ut) trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1); strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut))); - /* Append a trailing space character. Some systems pad names shorter than - the maximum with spaces, others pad with NULs. Remove any spaces. */ - trimmed_name[sizeof (UT_USER (ut))] = ' '; - p = strchr (trimmed_name, ' '); - if (p != NULL) - *p = '\0'; + /* Append a trailing NUL. Some systems pad names shorter than the + maximum with spaces, others pad with NULs. Remove any trailing + spaces. */ + trimmed_name[sizeof (UT_USER (ut))] = '\0'; + for (p = trimmed_name + strlen (trimmed_name); + trimmed_name < p && p[-1] == ' '; + *--p = '\0') + continue; return trimmed_name; }