From 39a4cb0afdf4f2a1e6c2f3176b84e5b4cfe8996d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 3 Aug 2023 15:31:48 -0700 Subject: [PATCH] readutmp: simplify extract_trimmed_name via ximemdup0 * lib/readutmp.c (extract_trimmed_name): Simplify. * modules/readutmp (Depends-on): Add strnlen, which was a missing dependency. * lib/readutmp.c: Include xmemdup0. (extract_trimmed_name): Simplify. * modules/readutmp (Depends-on): Add xmemdup0. Add strnlen, which was a missing dependency already. --- ChangeLog | 7 +++++++ lib/readutmp.c | 28 +++++++++++----------------- modules/readutmp | 1 + 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index b84cece6ff..7fa4e7b64a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-08-03 Paul Eggert + + readutmp: simplify extract_trimmed_name via ximemdup0 + * lib/readutmp.c (extract_trimmed_name): Simplify. + * modules/readutmp (Depends-on): + Add strnlen, which was a missing dependency. + 2023-08-03 Bruno Haible alignasof, stdalign: Avoid some -Wundef warnings from config.h. diff --git a/lib/readutmp.c b/lib/readutmp.c index 11dd1655c5..9057a36494 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -47,29 +47,23 @@ # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess" #endif +/* Work around . */ +#if 11 <= __GNUC__ +# pragma GCC diagnostic ignored "-Wstringop-overread" +#endif + /* Copy UT_USER (UT) into storage obtained from malloc. Then remove any trailing spaces from the copy, NUL terminate it, and return the copy. */ char * extract_trimmed_name (const STRUCT_UTMP *ut) { - char *p, *trimmed_name; - -#if READUTMP_USE_SYSTEMD - trimmed_name = xstrdup (UT_USER (ut)); -#else - trimmed_name = xmalloc (UT_USER_SIZE + 1); - strncpy (trimmed_name, UT_USER (ut), UT_USER_SIZE); - /* Append a trailing NUL. Some systems pad names shorter than the - maximum with spaces, others pad with NULs. */ - trimmed_name[UT_USER_SIZE] = '\0'; -#endif - /* Remove any trailing spaces. */ - for (p = trimmed_name + strlen (trimmed_name); - trimmed_name < p && p[-1] == ' '; - *--p = '\0') - ; - return trimmed_name; + char const *name = UT_USER (ut); + idx_t len = strnlen (name, UT_USER_SIZE); + char const *p; + for (p = name + len; name < p && p[-1] == ' '; p--) + continue; + return ximemdup0 (name, p - name); } #if READ_UTMP_SUPPORTED diff --git a/modules/readutmp b/modules/readutmp index 04893a4487..484edd1842 100644 --- a/modules/readutmp +++ b/modules/readutmp @@ -12,6 +12,7 @@ extensions xalloc stdbool stdint +strnlen sys_time fopen-gnu unlocked-io-internal -- 2.39.5