[PATCH 1/8] string: fix prototype of memdup()

Rasmus Villemoes ravi at prevas.dk
Sun Apr 12 21:59:40 CEST 2026


It doesn't make sense to restrict memdup() to only return char*
pointers, especially when it is already defined to accept void*. This
makes it uglier to use to e.g. duplicate a struct.

Make it return void*, just as kmemdup() does in the kernel (and which
our kmemdup() in fact also does).

While in here, make a small optimization: memcpy() is defined to
return the destination register, so we write this in a way that the
compiler may do a tail call.

Signed-off-by: Rasmus Villemoes <ravi at prevas.dk>
---
 include/linux/string.h | 2 +-
 lib/string.c           | 8 +++-----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index d943fcce690..9e47fe01c16 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -142,7 +142,7 @@ void *memchr_inv(const void *, int, size_t);
  *	memory is available
  *
  */
-char *memdup(const void *src, size_t len);
+void *memdup(const void *src, size_t len);
 
 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base);
 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base);
diff --git a/lib/string.c b/lib/string.c
index d56f88d4a84..c2813e0f854 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -667,17 +667,15 @@ void * memscan(void * addr, int c, size_t size)
 }
 #endif
 
-char *memdup(const void *src, size_t len)
+void *memdup(const void *src, size_t len)
 {
-	char *p;
+	void *p;
 
 	p = malloc(len);
 	if (!p)
 		return NULL;
 
-	memcpy(p, src, len);
-
-	return p;
+	return memcpy(p, src, len);
 }
 
 #ifndef __HAVE_ARCH_STRNSTR
-- 
2.53.0



More information about the U-Boot mailing list