diff options
author | Jim Meyering <jim@meyering.net> | 2004-06-12 08:07:30 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2004-06-12 08:07:30 +0000 |
commit | 33c1c51c49a08e1640805c3de31759846010b54b (patch) | |
tree | 7814387d6fac41d9a0e1adad2cb9147fa5bb6e01 /lib | |
parent | b5e189c8ef4216bea567d58527b5530a1ce9eecf (diff) | |
download | coreutils-33c1c51c49a08e1640805c3de31759846010b54b.tar.xz |
(extract_trimmed_name): Don't apply strchr to a
non-string; this leads to undefined behavior.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/readutmp.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/readutmp.c b/lib/readutmp.c index 896e976dc..508c91ee8 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; } |