Skip to content

strmove(): Add API, and use it instead of its pattern - #1424

Open
alejandro-colomar wants to merge 3 commits into
shadow-maint:masterfrom
alejandro-colomar:strmove
Open

strmove(): Add API, and use it instead of its pattern#1424
alejandro-colomar wants to merge 3 commits into
shadow-maint:masterfrom
alejandro-colomar:strmove

Conversation

@alejandro-colomar

@alejandro-colomar alejandro-colomar commented Dec 14, 2025

Copy link
Copy Markdown
Collaborator

Cc: @kees, @uecker


Revisions:

v1b
  • Rebase
$ git rd 
1:  cc0376415cbc = 1:  2969f95a3fdc lib/string/strcpy/: memmove_T(): Add API
2:  96c06d50e92c = 2:  fc6f018d2765 lib/string/strcpy/: strmove(): Add function
3:  28aeb9f6ae4d ! 3:  b1805c7f8806 src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
    @@ src/usermod.c
      #include "string/strdup/strdup.h"
      #include "string/strerrno.h"
      #include "string/strspn/stprspn.h"
    -@@ src/usermod.c: static char *new_pw_passwd (char *pw_pass)
    +@@ src/usermod.c: new_pw_passwd(char *pw_pass, bool process_selinux)
                              "updating-password", user_newname, user_newid, 1);
      #endif
                SYSLOG ((LOG_INFO, "unlock user '%s' password", user_newname));
v2
  • Don't use a statement expression unnecessarily.
$ git rd 
1:  2969f95a3fdc ! 1:  d038ef9e46eb lib/string/strcpy/: memmove_T(): Add API
    @@ lib/string/strcpy/memmove.h (new)
     +// memmove_T - memory move type-safe
     +#define memmove_T(dst, src, n, T)   memmove_T_(dst, src, n, typeas(T))
     +#define memmove_T_(dst, src, n, T)                                    \
    -+({                                                                    \
    -+  _Generic(dst, T *: (void)0);                                  \
    -+  _Generic(src, T *: (void)0);                                  \
    -+  (T *){memmove(dst, src, (n) * sizeof(T))};                    \
    -+})
    ++(                                                                     \
    ++  _Generic(dst, T *: (void)0),                                  \
    ++  _Generic(src, T *: (void)0),                                  \
    ++  (T *){memmove(dst, src, (n) * sizeof(T))}                     \
    ++)
     +
     +
     +#endif  // include guard
2:  fc6f018d2765 = 2:  05f665578fbd lib/string/strcpy/: strmove(): Add function
3:  b1805c7f8806 = 3:  5708a7e9e1d3 src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
v2b
  • Rebase
$ git rd 
1:  d038ef9e = 1:  64fda595 lib/string/strcpy/: memmove_T(): Add API
2:  05f66557 = 2:  c9830723 lib/string/strcpy/: strmove(): Add function
3:  5708a7e9 ! 3:  9bdb78a0 src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
    @@ src/usermod.c
     @@ src/usermod.c: new_pw_passwd(char *pw_pass, bool process_selinux)
                              "updating-password", user_newname, user_newid, 1);
      #endif
    -           SYSLOG ((LOG_INFO, "unlock user '%s' password", user_newname));
    +           SYSLOG(LOG_INFO, "unlock user '%s' password", user_newname);
     -          memmove(pw_pass, pw_pass + 1, strlen(pw_pass));
     +          strmove(pw_pass, pw_pass + 1);
        } else if (pflg) {
v2c
  • Rebase
$ git rd 
1:  64fda595ad05 ! 1:  f262f2d2f274 lib/string/strcpy/: memmove_T(): Add API
    @@ lib/string/README: strcpy/ - String copying
     +
      sprintf/ - Formatted string creation
      
    -     aprintf()
    +     aprintf(3)
     
      ## lib/string/strcpy/memmove.c (new) ##
     @@
2:  c98307235f8b = 2:  0542843b7a34 lib/string/strcpy/: strmove(): Add function
3:  9bdb78a02518 = 3:  9b6c18ce75cf src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
v2d
  • Rebase
$ git rd 
1:  f262f2d2f274 = 1:  cae69c557833 lib/string/strcpy/: memmove_T(): Add API
2:  0542843b7a34 = 2:  f4289243754c lib/string/strcpy/: strmove(): Add function
3:  9b6c18ce75cf ! 3:  e5c2238a5fa5 src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
    @@ src/usermod.c
      #include "string/strcmp/strprefix.h"
     +#include "string/strcpy/strmove.h"
      #include "string/strdup/strdup.h"
    - #include "string/strerrno.h"
      #include "string/strspn/stprspn.h"
    + #include "sysconf.h"
     @@ src/usermod.c: new_pw_passwd(char *pw_pass, bool process_selinux)
                              "updating-password", user_newname, user_newid, 1);
      #endif
v2e
  • Rebase
$ git rd 
1:  cae69c557833 = 1:  deaaad707d90 lib/string/strcpy/: memmove_T(): Add API
2:  f4289243754c = 2:  402e9eeb86cc lib/string/strcpy/: strmove(): Add function
3:  e5c2238a5fa5 = 3:  ff09b98f9a6f src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
v3
  • Use <memory.h> instead of <string.h>. Historically, and morally, it's more appropriate. It's quite portable, so it should be fine, even though it's non-standard. See memory.h(3head).
$ git rd 
1:  deaaad707d90 ! 1:  000b38d7d56e lib/string/strcpy/: memmove_T(): Add API
    @@ lib/string/strcpy/memmove.h (new)
     +
     +#include "config.h"
     +
    -+#include <string.h>
    ++#include <memory.h>
     +
     +#include "sizeof.h"
     +
2:  402e9eeb86cc = 2:  4b15b2b57c99 lib/string/strcpy/: strmove(): Add function
3:  ff09b98f9a6f = 3:  326a707b8eae src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
v4
  • Don't return a value from memmove_T(), since we don't need it.
$ git rd 
1:  000b38d7d56e ! 1:  3433e9779eab lib/string/strcpy/: memmove_T(): Add API
    @@ Commit message
         one, plus some offset, and thus will have the same const qualification
         (that is, it will not be qualified).
     
    +    Don't return anything, as we're not using the return value.  That avoids
    +    having to use a cast (we can't use a compound literal because of
    +    -Werror=unused-value).
    +
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## lib/Makefile.am ##
    @@ lib/string/strcpy/memmove.h (new)
     +
     +// memmove_T - memory move type-safe
     +#define memmove_T(dst, src, n, T)   memmove_T_(dst, src, n, typeas(T))
    -+#define memmove_T_(dst, src, n, T)                                    \
    -+(                                                                     \
    -+  _Generic(dst, T *: (void)0),                                  \
    -+  _Generic(src, T *: (void)0),                                  \
    -+  (T *){memmove(dst, src, (n) * sizeof(T))}                     \
    -+)
    ++#define memmove_T_(dst, src, n, T)  do                                \
    ++{                                                                     \
    ++  _Generic(dst, T *: (void)0);                                  \
    ++  _Generic(src, T *: (void)0);                                  \
    ++  memmove(dst, src, (n) * sizeof(T));                           \
    ++} while (0)
     +
     +
     +#endif  // include guard
2:  4b15b2b57c99 = 2:  cfbf55042e66 lib/string/strcpy/: strmove(): Add function
3:  326a707b8eae = 3:  970b7fd6d474 src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
v4b
  • Don't return anything from strmove() either.
$ git rd -U0
1:  3433e9779eab = 1:  3433e9779eab lib/string/strcpy/: memmove_T(): Add API
2:  cfbf55042e66 ! 2:  839e3a4737bf lib/string/strcpy/: strmove(): Add function
    @@ lib/string/strcpy/strmove.c (new)
    -+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
    ++// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
    @@ lib/string/strcpy/strmove.c (new)
    -+extern inline char *strmove(char *dst, char *src);
    ++extern inline void strmove(char *dst, char *src);
    @@ lib/string/strcpy/strmove.h (new)
    -+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
    ++// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
    @@ lib/string/strcpy/strmove.h (new)
    -+inline char *strmove(char *dst, char *src);
    ++inline void strmove(char *dst, char *src);
    @@ lib/string/strcpy/strmove.h (new)
    -+inline char *
    ++inline void
    @@ lib/string/strcpy/strmove.h (new)
    -+  return memmove_T(dst, src, strlen(src) + 1, char);
    ++  memmove_T(dst, src, strlen(src) + 1, char);
3:  970b7fd6d474 = 3:  7888284b548f src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern
v5
  • Move memmove_T() to a new lib/memory/. [@ikerexxe ]
$ git rd --creation-factor=99
1:  3433e9779eab ! 1:  ad9bf2a0d7e9 lib/string/strcpy/: memmove_T(): Add API
    @@ Metadata
     Author: Alejandro Colomar <alx@kernel.org>
     
      ## Commit message ##
    -    lib/string/strcpy/: memmove_T(): Add API
    +    lib/memory/memcpy/: memmove_T(): Add API
     
         An interesting detail is that we require the second argument to be
         non-const, while the memmove(3) function gets a const void*.  This is
    @@ Commit message
     
      ## lib/Makefile.am ##
     @@ lib/Makefile.am: libshadow_la_SOURCES = \
    -   string/strcmp/strneq.h \
    -   string/strcmp/strprefix.c \
    -   string/strcmp/strprefix.h \
    -+  string/strcpy/memmove.c \
    -+  string/strcpy/memmove.h \
    -   string/strcpy/stpecpy.c \
    -   string/strcpy/stpecpy.h \
    -   string/strcpy/strncat.c \
    +   lockpw.c \
    +   loginprompt.c \
    +   mail.c \
    ++  memory/memcpy/memmove.c \
    ++  memory/memcpy/memmove.h \
    +   motd.c \
    +   myname.c \
    +   nss.c \
     
    - ## lib/string/README ##
    -@@ lib/string/README: strcpy/ - String copying
    -     MEMCPY()
    -   Like memcpy(3), but takes two arrays.
    - 
    -+    memmove_T()
    -+  Like memmove(3), but type safe.
    -+
    - sprintf/ - Formatted string creation
    - 
    -     aprintf(3)
    -
    - ## lib/string/strcpy/memmove.c (new) ##
    + ## lib/memory/memcpy/memmove.c (new) ##
     @@
    -+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
    ++// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
     +// SPDX-License-Identifier: BSD-3-Clause
     +
     +
     +#include "config.h"
     +
    -+#include "string/strcpy/memmove.h"
    ++#include "memory/memcpy/memmove.h"
     
    - ## lib/string/strcpy/memmove.h (new) ##
    + ## lib/memory/memcpy/memmove.h (new) ##
     @@
    -+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
    ++// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
     +// SPDX-License-Identifier: BSD-3-Clause
     +
     +
    -+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_MEMMOVE_H_
    -+#define SHADOW_INCLUDE_LIB_STRING_STRCPY_MEMMOVE_H_
    ++#ifndef SHADOW_INCLUDE_LIB_MEMORY_MEMCPY_MEMMOVE_H_
    ++#define SHADOW_INCLUDE_LIB_MEMORY_MEMCPY_MEMMOVE_H_
     +
     +
     +#include "config.h"
    @@ lib/string/strcpy/memmove.h (new)
     +
     +
     +#endif  // include guard
    +
    + ## lib/string/README ##
    +@@ lib/string/README: strcpy/ - String copying
    +     MEMCPY()
    +   Like memcpy(3), but takes two arrays.
    + 
    ++    memmove_T()
    ++  Like memmove(3), but type safe.
    ++
    + sprintf/ - Formatted string creation
    + 
    +     aprintf(3)
2:  839e3a4737bf ! 2:  7d370dcbf9eb lib/string/strcpy/: strmove(): Add function
    @@ Commit message
     
      ## lib/Makefile.am ##
     @@ lib/Makefile.am: libshadow_la_SOURCES = \
    -   string/strcpy/memmove.h \
    +   string/strcmp/strprefix.h \
        string/strcpy/stpecpy.c \
        string/strcpy/stpecpy.h \
     +  string/strcpy/strmove.c \
    @@ lib/string/strcpy/strmove.h (new)
     +#include <string.h>
     +
     +#include "attr.h"
    -+#include "string/strcpy/memmove.h"
    ++#include "memory/memcpy/memmove.h"
     +
     +
     +ATTR_STRING(2)
3:  7888284b548f = 3:  f32d45cd08e8 src/usermod.c: new_pw_passwd(): Use strmove() instead of its pattern

@alejandro-colomar
alejandro-colomar force-pushed the strmove branch 4 times, most recently from bb758c4 to bef0b44 Compare December 15, 2025 00:44
@alejandro-colomar alejandro-colomar self-assigned this Dec 15, 2025
@alejandro-colomar
alejandro-colomar marked this pull request as ready for review January 1, 2026 14:31
@alejandro-colomar
alejandro-colomar force-pushed the strmove branch 2 times, most recently from b1805c7 to 5708a7e Compare February 25, 2026 15:03
@alejandro-colomar
alejandro-colomar force-pushed the strmove branch 4 times, most recently from 970b7fd to 7888284 Compare August 25, 2026 15:33
An interesting detail is that we require the second argument to be
non-const, while the memmove(3) function gets a const void*.  This is
because the second argument should usually be just the same as the first
one, plus some offset, and thus will have the same const qualification
(that is, it will not be qualified).

Don't return anything, as we're not using the return value.  That avoids
having to use a cast (we can't use a compound literal because of
-Werror=unused-value).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant