Skip to content

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Jul 16, 2025


Revisions:

v2
  • Simplify static-assertions by using SIZEOF_ARRAY.
$ git rd 
1:  2a4bb60f = 1:  2a4bb60f lib/: Use simple assignment instead of memcpy(3)
2:  1a06250a = 2:  1a06250a lib/string/strlen/: STRNLEN(): Add macro
3:  a85b0961 = 3:  a85b0961 lib/utmp.c: Use countof() for the number of elements of an array
4:  475a76c0 = 4:  475a76c0 lib/utmp.c: Only write ut_syslen if we've written ut_host
5:  813f0912 = 5:  813f0912 lib/: Use STRNLEN() instead of its pattern
6:  5933a030 < -:  -------- lib/typetraits.h: is_array(): Add macro
7:  2b527cb3 ! 6:  fb1c59bd lib/string/strcpy/: MEMCPY(): Add macro
    @@ lib/string/strcpy/memcpy.h (new)
     +
     +#include <string.h>
     +
    -+#include "typetraits.h"
    ++#include "sizeof.h"
     +
     +#include <assert.h>
     +
     +
     +#define MEMCPY(dst, src)                                              \
     +({                                                                    \
    -+  static_assert(is_array(src), "");                             \
    -+  static_assert(is_array(dst), "");                             \
    -+  static_assert(sizeof(dst) == sizeof(src), "");                \
    ++  static_assert(SIZEOF_ARRAY(dst) == SIZEOF_ARRAY(src), "");    \
     +                                                                      \
    -+  memcpy(dst, src, sizeof(dst));                                \
    ++  memcpy(dst, src, SIZEOF_ARRAY(dst));                          \
     +})
     +
     +
8:  166e380a = 7:  0825296f lib/utmp.c: prepare_utmp(): Use MEMCPY() instead of its pattern
v2b
  • Rebase
$ git rd 
1:  2a4bb60f = 1:  21385568 lib/: Use simple assignment instead of memcpy(3)
2:  1a06250a = 2:  4a711c5a lib/string/strlen/: STRNLEN(): Add macro
3:  a85b0961 = 3:  43454a8e lib/utmp.c: Use countof() for the number of elements of an array
4:  475a76c0 = 4:  94f310af lib/utmp.c: Only write ut_syslen if we've written ut_host
5:  813f0912 = 5:  b1698021 lib/: Use STRNLEN() instead of its pattern
6:  fb1c59bd = 6:  1c6b62eb lib/string/strcpy/: MEMCPY(): Add macro
7:  0825296f ! 7:  0fd00afd lib/utmp.c: prepare_utmp(): Use MEMCPY() instead of its pattern
    @@ Commit message
     
      ## lib/utmp.c ##
     @@
    - #include "sizeof.h"
    + #include "string/strchr/strnul.h"
      #include "string/strcmp/streq.h"
      #include "string/strcmp/strprefix.h"
     +#include "string/strcpy/memcpy.h"
v2c
  • Rebase
$ git range-diff 21385568d8a6^..gh/strnlen shadow/master..strnlen 
1:  21385568 = 1:  6571d0fd lib/: Use simple assignment instead of memcpy(3)
2:  4a711c5a = 2:  dc7dcb5b lib/string/strlen/: STRNLEN(): Add macro
3:  43454a8e = 3:  06ada7ec lib/utmp.c: Use countof() for the number of elements of an array
4:  94f310af = 4:  c12b4f12 lib/utmp.c: Only write ut_syslen if we've written ut_host
5:  b1698021 = 5:  622bde1d lib/: Use STRNLEN() instead of its pattern
6:  1c6b62eb = 6:  682b8a6b lib/string/strcpy/: MEMCPY(): Add macro
7:  0fd00afd = 7:  1db02350 lib/utmp.c: prepare_utmp(): Use MEMCPY() instead of its pattern
v3
$ git range-diff shadow/master..gh/strnlen gh/logind_utmp..strnlen 
1:  6571d0fd = 1:  6a1e4f8e lib/: Use simple assignment instead of memcpy(3)
2:  dc7dcb5b = 2:  6ae9cd00 lib/string/strlen/: STRNLEN(): Add macro
3:  06ada7ec ! 3:  770bc281 lib/utmp.c: Use countof() for the number of elements of an array
    @@ Commit message
     
      ## lib/utmp.c ##
     @@ lib/utmp.c: prepare_utmp(const char *name, const char *line, const char *host,
    - #endif
    +           STRNCPY(utent->ut_host, hostname);
      #if defined(HAVE_STRUCT_UTMPX_UT_SYSLEN)
                utent->ut_syslen = MIN (strlen (hostname),
     -                                  sizeof (utent->ut_host));
4:  c12b4f12 < -:  -------- lib/utmp.c: Only write ut_syslen if we've written ut_host
5:  622bde1d ! 4:  6404cd44 lib/: Use STRNLEN() instead of its pattern
    @@ lib/string/strdup/xstrndup.h
     
      ## lib/utmp.c ##
     @@
    - #include "string/strcpy/strtcpy.h"
    + #include "string/strdup/strndup.h"
      #include "string/strdup/xstrdup.h"
      #include "string/strdup/xstrndup.h"
    --
    --#ident "$Id$"
     +#include "string/strlen/strnlen.h"
      
      
      #define UTX_LINESIZE  countof(memberof(struct utmpx, ut_line))
     @@ lib/utmp.c: prepare_utmp(const char *name, const char *line, const char *host,
    - #if defined(HAVE_STRUCT_UTMPX_UT_HOST)
    +           struct addrinfo *info = NULL;
                STRNCPY(utent->ut_host, hostname);
    - # if defined(HAVE_STRUCT_UTMPX_UT_SYSLEN)
    + #if defined(HAVE_STRUCT_UTMPX_UT_SYSLEN)
     -          utent->ut_syslen = MIN (strlen (hostname),
     -                                  countof(utent->ut_host));
     +          utent->ut_syslen = STRNLEN(utent->ut_host);
    - # endif
      #endif
      #if defined(HAVE_STRUCT_UTMPX_UT_ADDR) || defined(HAVE_STRUCT_UTMPX_UT_ADDR_V6)
    +           if (getaddrinfo (hostname, NULL, NULL, &info) == 0) {
6:  682b8a6b = 5:  213de3d3 lib/string/strcpy/: MEMCPY(): Add macro
7:  1db02350 ! 6:  1218c16f lib/utmp.c: prepare_utmp(): Use MEMCPY() instead of its pattern
    @@ lib/utmp.c
     +#include "string/strcpy/memcpy.h"
      #include "string/strcpy/strncpy.h"
      #include "string/strcpy/strtcpy.h"
    - #include "string/strdup/xstrdup.h"
    + #include "string/strdup/strndup.h"
     @@ lib/utmp.c: prepare_utmp(const char *name, const char *line, const char *host,
                        } else if (info->ai_family == AF_INET6) {
                                struct sockaddr_in6 *sa =
v4
  • Move some commits to separate PRs.
$ git range-diff gh/logind_utmp gh/strnlen strnlen 
1:  6a1e4f8e < -:  -------- lib/: Use simple assignment instead of memcpy(3)
2:  6ae9cd00 = 1:  3641970d lib/string/strlen/: STRNLEN(): Add macro
3:  770bc281 = 2:  8dfd9df7 lib/utmp.c: Use countof() for the number of elements of an array
4:  6404cd44 = 3:  d2657a74 lib/: Use STRNLEN() instead of its pattern
5:  213de3d3 < -:  -------- lib/string/strcpy/: MEMCPY(): Add macro
6:  1218c16f < -:  -------- lib/utmp.c: prepare_utmp(): Use MEMCPY() instead of its pattern
v4b
  • Rebase
$ git range-diff gh/logind_utmp..gh/strnlen logind_utmp..strnlen 
1:  35529f23 = 1:  bb829e7f lib/string/strlen/: STRNLEN(): Add macro
2:  bbfdfb4e = 2:  e3dad729 lib/utmp.c: Use countof() for the number of elements of an array
3:  ca7a891f = 3:  a2e44f57 lib/: Use STRNLEN() instead of its pattern
v4c
  • Update lib/string/README.
$ git range-diff gh/logind_utmp gh/strnlen strnlen 
1:  bb829e7f ! 1:  92791d58 lib/string/strlen/: STRNLEN(): Add macro
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        string/strspn/stpspn.h \
        string/strspn/stprcspn.c \
     
    + ## lib/string/README ##
    +@@ lib/string/README: strsep/ - String separation
    +   Variant of strsep2ls() that allocates the array of strings.
    +   (But the strings themselves are not duplicated.)
    + 
    ++strlen/ - String length
    ++
    ++    STRNLEN()
    ++  Like strnlen(3), but take an array.
    ++
    + strftime.h
    +     STRFTIME()
    +   Like strftime(3), but takes an array.
    +
      ## lib/string/strlen/strnlen.c (new) ##
     @@
     +// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
2:  e3dad729 = 2:  21810b5b lib/utmp.c: Use countof() for the number of elements of an array
3:  a2e44f57 = 3:  f199c2c7 lib/: Use STRNLEN() instead of its pattern
v4d
  • Rebase
$ git range-diff gh/logind_utmp..gh/strnlen logind_utmp..strnlen 
1:  92791d58 = 1:  47ca1fee lib/string/strlen/: STRNLEN(): Add macro
2:  21810b5b = 2:  b0d5de65 lib/utmp.c: Use countof() for the number of elements of an array
3:  f199c2c7 = 3:  979b0aab lib/: Use STRNLEN() instead of its pattern

@alejandro-colomar alejandro-colomar marked this pull request as ready for review July 16, 2025 23:16
@alejandro-colomar alejandro-colomar force-pushed the strnlen branch 2 times, most recently from b3cf6f6 to fd97871 Compare July 16, 2025 23:34
@alejandro-colomar alejandro-colomar changed the title Simplify code, using STRNLEN() and others Simplify code, using STRNLEN(), MEMCPY(), and others Jul 16, 2025
@alejandro-colomar alejandro-colomar force-pushed the strnlen branch 2 times, most recently from 83fbc58 to 166e380 Compare July 16, 2025 23:54
@alejandro-colomar alejandro-colomar force-pushed the strnlen branch 4 times, most recently from 0fd00af to 1db0235 Compare July 18, 2025 20:13
@alejandro-colomar alejandro-colomar marked this pull request as draft July 22, 2025 10:14
@alejandro-colomar alejandro-colomar changed the title Simplify code, using STRNLEN(), MEMCPY(), and others Simplify code, using STRNLEN(), and others Jul 22, 2025
@alejandro-colomar alejandro-colomar changed the title Simplify code, using STRNLEN(), and others Add and use STRNLEN() instead of more complex code Jul 22, 2025
@alejandro-colomar alejandro-colomar force-pushed the strnlen branch 2 times, most recently from 9fc9da5 to ca7a891 Compare August 16, 2025 12:37
@alejandro-colomar alejandro-colomar force-pushed the strnlen branch 2 times, most recently from a2e44f5 to f199c2c Compare September 30, 2025 18:30
Signed-off-by: Alejandro Colomar <[email protected]>
libsystemd functions don't read the original contents of the second
argument, and unconditionally set it on success.  The NULL was
unnecessary.

The count initialization was always overwritten by assignment.

Signed-off-by: Alejandro Colomar <[email protected]>
This allows us using the [[gnu::malloc()]] attribute, by returning the
newly allocated string.  This is easier to use.  Move the error code to
errno, where it belongs.

Signed-off-by: Alejandro Colomar <[email protected]>
…ote_host()

This allows us using the [[gnu::malloc()]] attribute, by returning the
newly allocated string.  This is easier to use.  Move the error code to
errno, where it belongs.

Signed-off-by: Alejandro Colomar <[email protected]>
glibc, musl libc, FreeBSD, NetBSD, and OpenBSD, all have ut_host.

Signed-off-by: Alejandro Colomar <[email protected]>
This allows us using the [[gnu::malloc()]] attribute, and makes it
easier to use.  The error code is moved to errno, where it belongs.

Signed-off-by: Alejandro Colomar <[email protected]>
This function already reports some errors, so it doesn't make sense to
exit(3) on allocation error.  We can report it just like any other
error.

Signed-off-by: Alejandro Colomar <[email protected]>
sizeof() worked because it's an array of char, but it's brittle.

Signed-off-by: Alejandro Colomar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant