diff --git a/lib/Makefile.am b/lib/Makefile.am index d709345986..a0a5d82a69 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -115,6 +115,8 @@ libshadow_la_SOURCES = \ lockpw.c \ loginprompt.c \ mail.c \ + memory/memcpy/memmove.c \ + memory/memcpy/memmove.h \ motd.c \ myname.c \ nss.c \ @@ -223,6 +225,8 @@ libshadow_la_SOURCES = \ string/strcmp/strprefix.h \ string/strcpy/stpecpy.c \ string/strcpy/stpecpy.h \ + string/strcpy/strmove.c \ + string/strcpy/strmove.h \ string/strcpy/strncat.c \ string/strcpy/strncat.h \ string/strcpy/strncpy.c \ diff --git a/lib/memory/memcpy/memmove.c b/lib/memory/memcpy/memmove.c new file mode 100644 index 0000000000..44be00d303 --- /dev/null +++ b/lib/memory/memcpy/memmove.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include "config.h" + +#include "memory/memcpy/memmove.h" diff --git a/lib/memory/memcpy/memmove.h b/lib/memory/memcpy/memmove.h new file mode 100644 index 0000000000..68d7867eef --- /dev/null +++ b/lib/memory/memcpy/memmove.h @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_MEMORY_MEMCPY_MEMMOVE_H_ +#define SHADOW_INCLUDE_LIB_MEMORY_MEMCPY_MEMMOVE_H_ + + +#include "config.h" + +#include + +#include "sizeof.h" + + +// 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) do \ +{ \ + _Generic(dst, T *: (void)0); \ + _Generic(src, T *: (void)0); \ + memmove(dst, src, (n) * sizeof(T)); \ +} while (0) + + +#endif // include guard diff --git a/lib/string/README b/lib/string/README index 70274d71de..3db238669e 100644 --- a/lib/string/README +++ b/lib/string/README @@ -186,6 +186,10 @@ strcpy/ - String copying Do NOT use. I'll remove it soon. s/ + strmove() + Like memmove(3), for strings. It takes the length of the string + internally with strlen(3). + strtcpy() Copy from a string into another string with truncation. This is what the Linux kernel calls strscpy(). @@ -208,6 +212,9 @@ 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) diff --git a/lib/string/strcpy/strmove.c b/lib/string/strcpy/strmove.c new file mode 100644 index 0000000000..5618f7796b --- /dev/null +++ b/lib/string/strcpy/strmove.c @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include "config.h" + +#include "string/strcpy/strmove.h" + + +extern inline void strmove(char *dst, char *src); diff --git a/lib/string/strcpy/strmove.h b/lib/string/strcpy/strmove.h new file mode 100644 index 0000000000..299445852c --- /dev/null +++ b/lib/string/strcpy/strmove.h @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRMOVE_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRMOVE_H_ + + +#include "config.h" + +#include + +#include "attr.h" +#include "memory/memcpy/memmove.h" + + +ATTR_STRING(2) +inline void strmove(char *dst, char *src); + + +// strmove - string move +inline void +strmove(char *dst, char *src) +{ + memmove_T(dst, src, strlen(src) + 1, char); +} + + +#endif // include guard diff --git a/src/usermod.c b/src/usermod.c index bd9dc82c64..2a98ef5004 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -65,6 +65,7 @@ #include "string/sprintf/aprintf.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" +#include "string/strcpy/strmove.h" #include "string/strdup/strdup.h" #include "string/strspn/stprspn.h" #include "sysconf.h" @@ -489,7 +490,7 @@ 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); - memmove(pw_pass, pw_pass + 1, strlen(pw_pass)); + strmove(pw_pass, pw_pass + 1); } else if (pflg) { #ifdef WITH_AUDIT audit_logger (AUDIT_USER_CHAUTHTOK,