From: Alejandro Colomar Date: Wed, 16 Jul 2025 15:38:48 +0000 (+0200) Subject: lib/string/strdup/: memdup_T(): Add API X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab76e7abc2fa5d34e73932e549d720d702b03730;p=thirdparty%2Fshadow.git lib/string/strdup/: memdup_T(): Add API And update lib/string/README: - Rename MEMDUP() => memdup_T(), as we're moving away from upper-case macros to ones that actually say something about what they do in the name (_T for type-safe, and _a for array-safe). - memdup() is unimplemented. Cc: "Evgeny Grin (Karlson2k)" Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index bf9cda724..6a9a16443 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -225,6 +225,8 @@ libshadow_la_SOURCES = \ string/strcpy/strncpy.h \ string/strcpy/strtcpy.c \ string/strcpy/strtcpy.h \ + string/strdup/memdup.c \ + string/strdup/memdup.h \ string/strdup/strdup.c \ string/strdup/strdup.h \ string/strdup/strndupa.c \ diff --git a/lib/string/README b/lib/string/README index 9fbcbd842..03f3db6c3 100644 --- a/lib/string/README +++ b/lib/string/README @@ -162,9 +162,9 @@ strdup/ - Memory duplication Like strndup(3), but takes an array. m/ - memdup() + memdup() // Unimplemented Duplicate an object. Returns a pointer to the dup. - MEMDUP() + memdup_T() Like memdup(), but with type-safety checks. strcpy/ - String copying diff --git a/lib/string/strdup/memdup.c b/lib/string/strdup/memdup.c new file mode 100644 index 000000000..c574e3cff --- /dev/null +++ b/lib/string/strdup/memdup.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include "config.h" + +#include "string/strdup/memdup.h" diff --git a/lib/string/strdup/memdup.h b/lib/string/strdup/memdup.h new file mode 100644 index 000000000..cbae5158f --- /dev/null +++ b/lib/string/strdup/memdup.h @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_MEMDUP_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRDUP_MEMDUP_H_ + + +#include "config.h" + +#include + +#include "alloc/malloc.h" + + +// memdup_T - memory duplicate type-safe +#define memdup_T(p, T) memdup_T_(p, typeas(T)) +#define memdup_T_(p, T) \ +({ \ + T *new_; \ + \ + new_ = malloc_T(1, T); \ + if (new_ != NULL) \ + *new_ = *(p); \ + new_; \ +}) + + +#endif // include guard