Skip to content

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

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

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

Conversation

@alejandro-colomar
Copy link
Copy Markdown
Collaborator

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

Cc: @kees


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) {

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).

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