]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strdup/: memdup_T(): Add API
authorAlejandro Colomar <alx@kernel.org>
Wed, 16 Jul 2025 15:38:48 +0000 (17:38 +0200)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Tue, 17 Feb 2026 09:45:39 +0000 (10:45 +0100)
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)" <k2k@drgrin.dev>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/README
lib/string/strdup/memdup.c [new file with mode: 0644]
lib/string/strdup/memdup.h [new file with mode: 0644]

index bf9cda7242724745e18b96ccdd96870dd2af4754..6a9a1644339aa6bd2ba549ccdf546ac1fb82e1fd 100644 (file)
@@ -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 \
index 9fbcbd842cd92f79924deefface32d4660153944..03f3db6c33e667b5d3bf37071d659e96558b2af8 100644 (file)
@@ -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 (file)
index 0000000..c574e3c
--- /dev/null
@@ -0,0 +1,7 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// 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 (file)
index 0000000..cbae515
--- /dev/null
@@ -0,0 +1,29 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// 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 <stddef.h>
+
+#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