diff --git a/lib/Makefile.am b/lib/Makefile.am index a713f98c..231ed326 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -160,6 +160,8 @@ libshadow_la_SOURCES = \ string/strcpy/strtcpy.h \ string/strdup/strndupa.c \ string/strdup/strndupa.h \ + string/strdup/xstrdup.c \ + string/strdup/xstrdup.h \ string/strdup/xstrndup.c \ string/strdup/xstrndup.h \ string/strftime.c \ diff --git a/lib/alloc.c b/lib/alloc.c index 962f45a1..a9cbcaf0 100644 --- a/lib/alloc.c +++ b/lib/alloc.c @@ -1,12 +1,9 @@ -/* - * SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh - * SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz - * SPDX-FileCopyrightText: 2003 - 2006, Tomasz Kłoczko - * SPDX-FileCopyrightText: 2008 , Nicolas François - * SPDX-FileCopyrightText: 2023 , Alejandro Colomar - * - * SPDX-License-Identifier: BSD-3-Clause - */ +// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh +// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz +// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko +// SPDX-FileCopyrightText: 2008 , Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause /* Replacements for malloc and strdup with error checking. Too trivial to be worth copyrighting :-). I did that because a lot of code used @@ -19,8 +16,6 @@ #include -#ident "$Id$" - #include "alloc.h" #include @@ -36,7 +31,6 @@ extern inline void *xmalloc(size_t size); extern inline void *xmallocarray(size_t nmemb, size_t size); extern inline void *mallocarray(size_t nmemb, size_t size); extern inline void *reallocarrayf(void *p, size_t nmemb, size_t size); -extern inline char *xstrdup(const char *str); void * diff --git a/lib/alloc.h b/lib/alloc.h index 39405a56..cf2e4b2e 100644 --- a/lib/alloc.h +++ b/lib/alloc.h @@ -15,7 +15,6 @@ #include #include "attr.h" -#include "defines.h" #define CALLOC(n, type) ((type *) calloc(n, sizeof(type))) @@ -47,8 +46,6 @@ ATTR_MALLOC(free) inline void *mallocarray(size_t nmemb, size_t size); ATTR_MALLOC(free) inline void *reallocarrayf(void *p, size_t nmemb, size_t size); -ATTR_MALLOC(free) -inline char *xstrdup(const char *str); ATTR_MALLOC(free) void *xcalloc(size_t nmemb, size_t size); @@ -91,11 +88,4 @@ reallocarrayf(void *p, size_t nmemb, size_t size) } -inline char * -xstrdup(const char *str) -{ - return strcpy(XMALLOC(strlen(str) + 1, char), str); -} - - #endif // include guard diff --git a/lib/env.c b/lib/env.c index 8a59468a..2debf35c 100644 --- a/lib/env.c +++ b/lib/env.c @@ -22,6 +22,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/sprintf/xasprintf.h" +#include "string/strdup/xstrdup.h" /* diff --git a/lib/list.c b/lib/list.c index 9fc66089..8604cb63 100644 --- a/lib/list.c +++ b/lib/list.c @@ -15,6 +15,9 @@ #include "alloc.h" #include "prototypes.h" #include "defines.h" +#include "string/strdup/xstrdup.h" + + /* * add_list - add a member to a list of group members * diff --git a/lib/motd.c b/lib/motd.c index d1d5bf6e..52712675 100644 --- a/lib/motd.c +++ b/lib/motd.c @@ -13,10 +13,12 @@ #include -#include "alloc.h" #include "defines.h" #include "getdef.h" #include "prototypes.h" +#include "string/strdup/xstrdup.h" + + /* * motd -- output the /etc/motd file * diff --git a/lib/obscure.c b/lib/obscure.c index 32e8941c..136f26a6 100644 --- a/lib/obscure.c +++ b/lib/obscure.c @@ -15,13 +15,14 @@ #include #include -#include "alloc.h" #include "attr.h" #include "memzero.h" #include "prototypes.h" #include "defines.h" #include "getdef.h" #include "string/sprintf/xasprintf.h" +#include "string/strdup/xstrdup.h" + #if WITH_LIBBSD == 0 #include "freezero.h" diff --git a/lib/setupenv.c b/lib/setupenv.c index 8c58ac53..8eda5d58 100644 --- a/lib/setupenv.c +++ b/lib/setupenv.c @@ -21,13 +21,13 @@ #include #include -#include "alloc.h" #include "prototypes.h" #include "defines.h" #include #include "getdef.h" #include "shadowlog.h" #include "string/sprintf/xasprintf.h" +#include "string/strdup/xstrdup.h" #ifndef USE_PAM diff --git a/lib/string/strdup/xstrdup.c b/lib/string/strdup/xstrdup.c new file mode 100644 index 00000000..00ec9db3 --- /dev/null +++ b/lib/string/strdup/xstrdup.c @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh +// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz +// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko +// SPDX-FileCopyrightText: 2008 , Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strdup/xstrdup.h" + + +extern inline char *xstrdup(const char *str); diff --git a/lib/string/strdup/xstrdup.h b/lib/string/strdup/xstrdup.h new file mode 100644 index 00000000..13164182 --- /dev/null +++ b/lib/string/strdup/xstrdup.h @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh +// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz +// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko +// SPDX-FileCopyrightText: 2008 , Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRDUP_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRDUP_H_ + + +#include + +#include + +#include "alloc.h" +#include "attr.h" + + +ATTR_MALLOC(free) +inline char *xstrdup(const char *str); + + +inline char * +xstrdup(const char *str) +{ + return strcpy(XMALLOC(strlen(str) + 1, char), str); +} + + +#endif // include guard diff --git a/lib/utmp.c b/lib/utmp.c index 40c32c18..ba80f7fd 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -26,6 +26,7 @@ #include "sizeof.h" #include "string/strcpy/strncpy.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" #include "string/strdup/xstrndup.h" #ident "$Id$" diff --git a/src/chage.c b/src/chage.c index b8af89cf..9b85b425 100644 --- a/src/chage.c +++ b/src/chage.c @@ -26,7 +26,6 @@ #endif /* ACCT_TOOLS_SETUID */ #include -#include "alloc.h" #include "atoi/str2i.h" #include "defines.h" #include "memzero.h" @@ -36,6 +35,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" #include "string/strftime.h" #include "time/day_to_str.h" /*@-exitarg@*/ diff --git a/src/chfn.c b/src/chfn.c index e78037d3..1872b2df 100644 --- a/src/chfn.c +++ b/src/chfn.c @@ -18,7 +18,6 @@ #include #include -#include "alloc.h" #include "defines.h" #include "getdef.h" #include "nscd.h" @@ -34,6 +33,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/chsh.c b/src/chsh.c index c211c7a0..4e85678d 100644 --- a/src/chsh.c +++ b/src/chsh.c @@ -17,7 +17,6 @@ #include #include -#include "alloc.h" #include "defines.h" #include "getdef.h" #include "nscd.h" @@ -32,6 +31,7 @@ #include "exitcodes.h" #include "shadowlog.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" #ifndef SHELLS_FILE #define SHELLS_FILE "/etc/shells" diff --git a/src/gpasswd.c b/src/gpasswd.c index 0dc3a5f2..70d7547f 100644 --- a/src/gpasswd.c +++ b/src/gpasswd.c @@ -37,6 +37,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/groupmems.c b/src/groupmems.c index 0d0882db..444882cc 100644 --- a/src/groupmems.c +++ b/src/groupmems.c @@ -27,6 +27,8 @@ #include "sgroupio.h" #endif #include "shadowlog.h" +#include "string/strdup/xstrdup.h" + /* Exit Status Values */ /*@-exitarg@*/ diff --git a/src/groupmod.c b/src/groupmod.c index e39b76b8..3f6d54eb 100644 --- a/src/groupmod.c +++ b/src/groupmod.c @@ -41,6 +41,7 @@ #include "shadowlog.h" #include "string/sprintf/stpeprintf.h" #include "string/strcpy/stpecpy.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/login.c b/src/login.c index f9f8606f..338c1a43 100644 --- a/src/login.c +++ b/src/login.c @@ -40,6 +40,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" #include "string/strftime.h" diff --git a/src/newgrp.c b/src/newgrp.c index b9aec21d..6542abf2 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -26,6 +26,7 @@ #include "exitcodes.h" #include "shadowlog.h" #include "string/sprintf/snprintf.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/newusers.c b/src/newusers.c index c28f8d89..e5d20491 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -53,6 +53,7 @@ #include "chkname.h" #include "shadowlog.h" #include "string/sprintf/snprintf.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/passwd.c b/src/passwd.c index e22788f5..c2468d40 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -20,7 +20,6 @@ #include #include -#include "alloc.h" #include "agetpass.h" #include "atoi/str2i.h" #include "defines.h" @@ -35,10 +34,10 @@ #include "shadowlog.h" #include "string/sprintf/xasprintf.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" #include "time/day_to_str.h" - /* * exit status values */ diff --git a/src/su.c b/src/su.c index cfc5ab1f..4e0d4100 100644 --- a/src/su.c +++ b/src/su.c @@ -62,6 +62,7 @@ #include "string/sprintf/snprintf.h" #include "string/sprintf/xasprintf.h" #include "string/strcpy/strtcpy.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/sulogin.c b/src/sulogin.c index 20971743..6af471b5 100644 --- a/src/sulogin.c +++ b/src/sulogin.c @@ -19,7 +19,6 @@ #include #include "agetpass.h" -#include "alloc.h" #include "attr.h" #include "defines.h" #include "getdef.h" @@ -28,6 +27,7 @@ /*@-exitarg@*/ #include "exitcodes.h" #include "shadowlog.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/useradd.c b/src/useradd.c index 52aed3ee..4db0aa3a 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -67,6 +67,7 @@ #include "shadowlog.h" #include "string/sprintf/snprintf.h" #include "string/sprintf/xasprintf.h" +#include "string/strdup/xstrdup.h" #ifndef SKEL_DIR diff --git a/src/userdel.c b/src/userdel.c index 38158d44..ead69604 100644 --- a/src/userdel.c +++ b/src/userdel.c @@ -20,7 +20,6 @@ #include #include -#include "alloc.h" #ifdef ACCT_TOOLS_SETUID #ifdef USE_PAM #include "pam_defs.h" @@ -53,6 +52,7 @@ #endif /* ENABLE_SUBIDS */ #include "shadowlog.h" #include "string/sprintf/xasprintf.h" +#include "string/strdup/xstrdup.h" /* diff --git a/src/usermod.c b/src/usermod.c index 1291814b..b688ee8e 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -62,6 +62,7 @@ #endif #include "shadowlog.h" #include "string/sprintf/xasprintf.h" +#include "string/strdup/xstrdup.h" #include "time/day_to_str.h"