]> Savannah Git Hosting - gnulib.git/commitdiff
readutmp: simplify extract_trimmed_name via ximemdup0
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 3 Aug 2023 22:31:48 +0000 (15:31 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 4 Aug 2023 01:20:50 +0000 (18:20 -0700)
* 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
lib/readutmp.c
modules/readutmp

index b84cece6ffbdca57973fed25f4cdebc8d20b38dc..7fa4e7b64a8617a3fa36118c4de7f94a08551583 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-08-03  Paul Eggert  <eggert@cs.ucla.edu>
+
+       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  <bruno@clisp.org>
 
        alignasof, stdalign: Avoid some -Wundef warnings from config.h.
index 11dd1655c50de5878726a098e0bde2e2799e6683..9057a364942a6fc7f09995b755b5beebf29439ba 100644 (file)
 # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
 #endif
 
+/* Work around <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109614>.  */
+#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
index 04893a4487b2838190f16e67a2ae2ae52ff5a231..484edd18428ffbd032dd134f3cf6d6a57056eec5 100644 (file)
@@ -12,6 +12,7 @@ extensions
 xalloc
 stdbool
 stdint
+strnlen
 sys_time
 fopen-gnu
 unlocked-io-internal