]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(extract_trimmed_name): Don't apply strchr to a
authorJim Meyering <jim@meyering.net>
Sat, 12 Jun 2004 08:07:30 +0000 (08:07 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 12 Jun 2004 08:07:30 +0000 (08:07 +0000)
non-string; this leads to undefined behavior.

lib/readutmp.c

index 896e976dc4ecb264edaf48067465821b72d598bc..508c91ee8d2c6e63740bfc202c494781919df9cc 100644 (file)
@@ -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;
 }