add memdup_nul(), use it and memdup() in a few places
Subsystems (2)
v2 cover add memdup_nul(), use it and memdup() in a few places
There are quite a few places where we allocate X+1 bytes, initialize the first X bytes via memcpy() and then set the last byte to 0.
The kernel has a helper for that, kmemdup_nul(). Introduce a similar one, and start making use of it in a few places. Also the existing memdup() helper can be put to more use.
There are lots more places one could modify. But for code shared with host tools, one would need to do some refactoring, putting memdup() and memdup_nul() in their own str-util.c TU which could then also be included in the tools build.
v2: Fix grammar in commit msg, add Simon's R-B to patches 1-8, add test (new patch 9).
v1: https://lore.kernel.org/u-boot/[email protected]/
Rasmus Villemoes (9):
string: fix prototype of memdup()
stdio: drop stdio_clone
lib/string.c: drop pointless __HAVE_ARCH_STRDUP lib/string.c: introduce memdup_nul() helper lib/string.c: implement strdup() and strndup() in terms of
memdup_nul()
lib/hashtable.c: use memdup_nul() in himport_r common/cli.c: use memdup_nul() in run_command_list() drivers/core: use memdup() instead of malloc()+memcpy()
test: lib: add test of memdup_nul()
common/cli.c | 4 +--- common/stdio.c | 18 +-------------- drivers/core/acpi.c | 3 +-- drivers/core/ofnode.c | 3 +-- drivers/core/root.c | 3 +-- include/linux/string.h | 18 ++++++++++++--- include/stdio_dev.h | 1 - lib/hashtable.c | 7 +++--- lib/string.c | 51 +++++++++++++++--------------------------- test/lib/string.c | 34 ++++++++++++++++++++++++++++ 10 files changed, 75 insertions(+), 67 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/9 string: fix prototype of memdup()
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.
--- include/linux/string.h | 2 +- lib/string.c | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-)
include/linux/string.h +1 -1
@@ -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);
lib/string.c +3 -5
@@ -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
Signature · 1 line
2.53.0
PATCH v2 2/9 stdio: drop stdio_clone
In reply to Rasmus VillemoesThe helper stdio_clone only has a single caller, so it certainly doesn't need to be public. But in fact, it is merely an open-coded memdup() - which for some reason uses calloc() even if the whole allocation is obviously immediately overwritten.
Drop it and just use memdup() directly.
--- common/stdio.c | 18 +----------------- include/stdio_dev.h | 1 - 2 files changed, 1 insertion(+), 18 deletions(-)
common/stdio.c +1 -17
@@ -217,27 +217,11 @@ struct stdio_dev *stdio_get_by_name(const char *name)return NULL ; } -struct stdio_dev *stdio_clone (struct stdio_dev *dev) -{ -struct stdio_dev *_dev; - -if (!dev) -return NULL ; - - _dev =calloc (1 ,sizeof (struct stdio_dev )); -if (!_dev) -return NULL ; - -memcpy (_dev, dev,sizeof (struct stdio_dev )); - -return _dev; -} -int stdio_register_dev (struct stdio_dev *dev,struct stdio_dev **devp) {struct stdio_dev *_dev; - _dev =stdio_clone (dev); + _dev =memdup (dev,sizeof (*dev));if (!_dev)return -ENODEV ;list_add_tail (&_dev->list, &devs.list);
include/stdio_dev.h +0 -1
@@ -96,7 +96,6 @@ int stdio_add_devices(void);int stdio_deregister_dev (struct stdio_dev *dev,int force);struct list_head *stdio_get_list (void );struct stdio_dev *stdio_get_by_name (const char *name); -struct stdio_dev *stdio_clone (struct stdio_dev *dev);int drv_lcd_init (void );int drv_video_init (void );
Signature · 1 line
2.53.0
PATCH v2 3/9 lib/string.c: drop pointless __HAVE_ARCH_STRDUP
In reply to Rasmus VillemoesThere has never been an arch-specific optimized implementation of str[n]dup, nor is there likely to ever be one, because unlike their cousins strlen(), strcpy() and similar that simply read/write the src/dst, the dup functions by definition involve memory allocation. So drop this irrelevant cpp guard.
--- include/linux/string.h | 3 +-- lib/string.c | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-)
include/linux/string.h +1 -2
@@ -101,10 +101,9 @@ size_t strcspn(const char *s, const char *reject);# define strndup sandbox_strndup#endif -#ifndef __HAVE_ARCH_STRDUPextern char *strdup (const char *);extern char *strndup (const char *,size_t ); -#endif +#ifndef __HAVE_ARCH_STRSWABextern char *strswab (const char *);#endif
lib/string.c +0 -2
@@ -343,7 +343,6 @@ size_t strcspn(const char *s, const char *reject) }#endif -#ifndef __HAVE_ARCH_STRDUPchar *strdup (const char *s) {char *new;
@@ -379,7 +378,6 @@ char * strndup(const char *s, size_t n)return new; } -#endif #ifndef __HAVE_ARCH_STRSPN/**
Signature · 1 line
2.53.0
PATCH v2 4/9 lib/string.c: introduce memdup_nul() helper
In reply to Rasmus VillemoesThis is completely analogous to the linux kernel's kmemdup_nul() helper, apart from the lack of the gfp_t argument: Allocate a buffer of size {len}+1, copy {len} bytes from the given buffer, and add a final nul byte.
This pattern exists in a number of places, so this helper can reduce some boilerplate code.
--- include/linux/string.h | 13 +++++++++++++ lib/string.c | 15 +++++++++++++++ 2 files changed, 28 insertions(+)
include/linux/string.h +13 -0
@@ -143,6 +143,19 @@ void *memchr_inv(const void *, int, size_t);*/ void *memdup (const void *src,size_t len); +/** +* memdup_nul() - allocate a buffer and copy in the contents, appending a nul byte +* +* Note that this returns a valid pointer even if @len is 0 +* +* @src: data to copy in +* @len: number of bytes to copy +* Return: allocated buffer with the copied contents and an extra nul byte, +* or NULL if not enough memory is available +* +*/ +void *memdup_nul (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);
lib/string.c +15 -0
@@ -343,6 +343,21 @@ size_t strcspn(const char *s, const char *reject) }#endif +void *memdup_nul (const void *src,size_t len) +{ +char *dst; + +if (len +1 < len) +return NULL ; + + dst =malloc (len +1 ); +if (!dst) +return NULL ; + + dst[len] ='\0' ; +return memcpy (dst, src, len); +} +char *strdup (const char *s) {char *new;
Signature · 1 line
2.53.0
PATCH v2 5/9 lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
In reply to Rasmus VillemoesWith the addition of memdup_nul(), strdup() and strndup() can be implemented as one-liners.
While not required by POSIX or C, do keep the behaviour of gracefully accepting a NULL source and simply return NULL.
--- lib/string.c | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-)
lib/string.c +2 -28
@@ -360,38 +360,12 @@ void *memdup_nul(const void *src, size_t len)char *strdup (const char *s) { -char *new; - -if ((s ==NULL ) || - ((new =malloc (strlen (s) +1 )) ==NULL ) ) { -return NULL ; - } - -strcpy (new, s); -return new; +return s ?memdup_nul (s,strlen (s)) :NULL ; }char *strndup (const char *s,size_t n) { -size_t len; -char *new; - -if (s ==NULL ) -return NULL ; - - len =strlen (s); - -if (n < len) - len = n; - - new =malloc (len +1 ); -if (new ==NULL ) -return NULL ; - -strncpy (new, s, len); - new[len] ='\0' ; - -return new; +return s ?memdup_nul (s,strnlen (s, n)) :NULL ; }#ifndef __HAVE_ARCH_STRSPN
Signature · 1 line
2.53.0
A message that is not in the archive (sent off-list or not yet received): AcudIuPdPJ5-kqSj6e-s_SQbe9Z8Q5HcjiZsubil4biPj9gti7a9LBnOeY9773esCblkiUJI8hX-N9MbZi_fjg==@protonmail.internalid
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): dkoQtduO2VZZSg_twn6aEON3-1NIERFMb5inRq_5NFYBZoJupMdXDBX_2xw5aceIk258QYzLZqn1b6Fyfrr5KQ==@protonmail.internalid
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [REGRESSION] Re: [PATCH v2 5/9] lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
Quoted 13 lines · Rasmus Villemoes
The only other difference I can see is the U-Boot version doing a tail call
return memcpy(dst, src, len);which relies on
memcpy()being defined to return the dst argument. Now, usually nobody uses memcpy's return value, but it can make code slightly more compact, and any custommemcpy()implementation (e.g. in asm) _must_ honour this, as the compiler might transformmemcpy(a, b, c); return a;
Quoted 7 lines
into
return memcpy(a, b, c);[I know this because I played around with this in godbolt a few years ago, and I think found that gcc did this but clang did not, or something like that].
Just for my own future reference: I misremembered slightly regarding the gcc/clang behaviour, but I did file an llvm bug report back then. That was just very hard to re-discover because my name was not included when llvm migrated their issue tracker to github, but it's this one:
https://github.com/llvm/llvm-project/issues/46610 .
Quoted 5 lines
So my best bet is that your platform uses a custom
memcpy()which does not have to right return value. Can you try changing that lastreturn memcpy(dst, src, len);
Quoted 7 lines
into
memcpy(dst, src, len); return dst;and verify in the disassembly that the compiler hasn't changed it back, and then see if your board boots?
As for this hypothesis, I did quickly look at riscv's asm memcpy(), and I don't see anything obviously wrong (it does have a "save for return value"). But the arithmetic and comparisons are somewhat risky and not-obviously-correct, I think. For example, doing
/* Unrolled wordwise copy */ addi t0, t0, -(16*SZREG-1) bgeu a0, t0, 2f
to see if the end of the destination buffer is at least 16 words ahead of the a0 doesn't work if we happen to work with addresses very close to 0, i.e. something like
memcpy(32, from_somewhere, 256)
would certainly fail. But I know nothing about riscv, so I can't really read the asm properly, and the "close to 0" thing is unlikely to be the real problem.
Nevertheless, another thing to try is disable CONFIG_SPL_ARCH_MEMPCY (and CONFIG_ARCH_MEMCPY for good measure) and see if that makes a difference.
Rasmus
Re: [REGRESSION] Re: [PATCH v2 5/9] lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
Thanks Rasmus
Quoted 1 line · Rasmus Villemoes
On Tue, Oct 13 2026, "Rasmus Villemoes" <[email protected]> wrote:
Yeah, overflow check sounded too easy, but I did try that for a start. Still hangs.
Then, I tried which one actually fails and it is strdup.
If I just add size_t len = strlen(s); to the top of the old working strdup() function, it also hangs. Strange.
No, wait a minute, it just gets called with null strings, yes.
But the new implementation should catch that as well. Strange.
Somehow it does not like another level of indirection. But I haven't quite figured out what exactly it is that goes wrong. Will keep prodding it...
Quoted 21 lines
Quoted 13 lines
The only other difference I can see is the U-Boot version doing a tail call
return memcpy(dst, src, len);which relies on
memcpy()being defined to return the dst argument. Now, usually nobody uses memcpy's return value, but it can make code slightly more compact, and any custommemcpy()implementation (e.g. in asm) _must_ honour this, as the compiler might transform
memcpy(a, b, c);
return a;Quoted 7 lines
into
return
memcpy(a, b, c);[I know this because I played around with this in godbolt a few years ago, and I think found that gcc did this but clang did not, or something like that].
Quoted 18 lines
Just for my own future reference: I misremembered slightly regarding the gcc/clang behaviour, but I did file an llvm bug report back then. That was just very hard to re-discover because my name was not included when llvm migrated their issue tracker to github, but it's this one:
https://github.com/llvm/llvm-project/issues/46610 .Quoted 12 lines
So my best bet is that your platform uses a custom
memcpy()which does not have to right return value. Can you try changing that lastreturn memcpy(dst, src, len);into
memcpy(dst, src, len); return dst;and verify in the disassembly that the compiler hasn't changed it back, and then see if your board boots?
Tried that but also did not help. But I will do some more disassembly analysis...
Quoted 15 lines
As for this hypothesis, I did quickly look at riscv's asm
memcpy(), and I don't see anything obviously wrong (it does have a "save for return value"). But the arithmetic and comparisons are somewhat risky and not-obviously-correct, I think. For example, doing/* Unrolled wordwise copy */ addi t0, t0, -(16*SZREG-1) bgeu a0, t0, 2fto see if the end of the destination buffer is at least 16 words ahead of the a0 doesn't work if we happen to work with addresses very close to 0, i.e. something like
memcpy(32, from_somewhere, 256)
Quoted 7 lines
would certainly fail. But I know nothing about riscv, so I can't really read the asm properly, and the "close to 0" thing is unlikely to be the real problem.
Nevertheless, another thing to try is disable
CONFIG_SPL_ARCH_MEMPCY(andCONFIG_ARCH_MEMCPYfor good measure) and see if that makes a difference.
Tried that which let to:
/riscv64-glibc-ubuntu-24.04-gcc-2026.08.27-nightly/bin/riscv64-unknown-linux-gnu-ld:
arch/riscv/lib/memmove.o:(.text+0x8): undefined reference to __memcpy' /riscv64-glibc-ubuntu-24.04-gcc-2026.08.27-nightly/bin/riscv64-unknown-linux-gnu-ld: relocation R_RISCV_CALL_PLT against __memcpy' which may bind externally can not be used when making a shared object; recompile with -fPIC
make: *** [Makefile:2113: u-boot] Error 1
So I also had to get rid of CONFIG_USE_ARCH_MEMMOVE.
Outcome is still the same hang.
Quoted 1 line
Rasmus
Cheers
Marcel
Re: [REGRESSION] Re: [PATCH v2 5/9] lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
Quoted 9 lines · Marcel Ziswiler
Thanks Rasmus
Quoted 1 line · Rasmus Villemoes
On Tue, Oct 13 2026, "Rasmus Villemoes" <[email protected]> wrote:
Yeah, overflow check sounded too easy, but I did try that for a start. Still hangs.
Then, I tried which one actually fails and it is strdup.
Quoted 8 lines
If I just add
size_tlen = strlen(s); to the top of the old workingstrdup()function, it also hangs. Strange.No, wait a minute, it just gets called with null strings, yes.
But the new implementation should catch that as well. Strange.
Somehow it does not like another level of indirection. But I haven't quite figured out what exactly it is that goes wrong. Will keep prodding it...
Can you tell me the exact compiler/toolchain versions, and if using clang, can you try gcc (and vice versa of course). Is it a 32 bit or 64 bit target?
Also, even if I can't read risc-v asm very well, can you paste the disassembly of the old and new strdup() functions. And the exact command line used to build lib/string.c, found in lib/.string.o.cmd . For both U-Boot proper and SPL (i.e. spl/lib/.string.o.cmd for the latter).
Because playing around with this code in godbolt I can definitely see the compiler eliding the NULL checks in some cases, but that also applies to the old implementation, i.e. I see an unconditional call of strlen(). But I assume godbolt's toolchain always sees userspace <string.h> which has __attribute__((nonnull(1))) on strdup(), and we don't. But it can also be the compiler which "knows" that strdup() has a non-null contract, i.e. the compiler might have a built-in prototype for strdup(). Anyway, there are too many variables in play here, so I'd like to narrow it down a bit to your exact setup.
Rasmus
PATCH v2 6/9 lib/hashtable.c: use memdup_nul() in himport_r
In reply to Rasmus VillemoesWe have memdup_nul() for exactly this pattern of duplicating a block of memory and ensuring there's a nul byte after the copy.
--- lib/hashtable.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
lib/hashtable.c +3 -4
@@ -821,13 +821,12 @@ int himport_r(struct hsearch_data *htab, }/* we allocate new space to make sure we can write to the array */ -if ((data =malloc (size +1 )) ==NULL ) { -debug ("himport_r: can't malloc %lu bytes\n" , (ulong )size +1 ); + data =memdup_nul (env, size); +if (data ==NULL ) { +debug ("himport_r: can't duplicate env block\n" );__set_errno (ENOMEM );return 0 ; } -memcpy (data, env, size); - data[size] ='\0' ; dp = data;/* make a local copy of the list of variables */
Signature · 1 line
2.53.0
PATCH v2 7/9 common/cli.c: use memdup_nul() in run_command_list()
In reply to Rasmus VillemoesUse memdup_nul() instead of open-coding it.
--- common/cli.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
common/cli.c +1 -3
@@ -138,11 +138,9 @@ int run_command_list(const char *cmd, int len, int flag)#endif }if (need_buff) { - buff =malloc (len +1 ); + buff =memdup_nul (cmd, len);if (!buff)return 1 ; -memcpy (buff, cmd, len); - buff[len] ='\0' ; }#ifdef CONFIG_HUSH_PARSER if (use_hush_old ()) {
Signature · 1 line
2.53.0
PATCH v2 8/9 drivers/core: use memdup() instead of malloc()+memcpy()
In reply to Rasmus VillemoesUse memdup() instead of open-coding it.
In the dm_setup_inst() case, there was never any reason to use calloc(), as the whole allocation is definitely initialized via the immediately following memcpy().
--- drivers/core/acpi.c | 3 +-- drivers/core/ofnode.c | 3 +-- drivers/core/root.c | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-)
drivers/core/acpi.c +1 -2
@@ -154,10 +154,9 @@ static int add_item(struct acpi_ctx *ctx, struct udevice *dev,if (!item->size)return 0 ;if (type !=TYPE_OTHER ) { - item->buf =malloc (item->size); + item->buf =memdup (start, item->size);if (!item->buf)return log_msg_ret ("mem" , -ENOMEM ); -memcpy (item->buf, start, item->size); } item_count++; log_debug("* %s: Added type %d, %p, size %x\n",
drivers/core/ofnode.c +1 -2
@@ -1750,10 +1750,9 @@ int ofnode_write_prop(ofnode node, const char *propname, const void *value,void *newval;if (copy) { - newval =malloc (len); + newval =memdup (value, len);if (!newval)return log_ret (-ENOMEM ); -memcpy (newval, value, len); value = newval; } ret =of_write_prop (ofnode_to_np (node), propname, len, value);
drivers/core/root.c +1 -2
@@ -81,10 +81,9 @@ static int dm_setup_inst(void)/* Now allocate space for the priv/plat data, and copy it in */ size = __priv_data_end - __priv_data_start; - base =calloc (1 , size); + base =memdup (__priv_data_start, size);if (!base)return log_msg_ret ("priv" , -ENOMEM ); -memcpy (base, __priv_data_start, size);gd_set_dm_priv_base (base); }
Signature · 1 line
2.53.0
PATCH v2 9/9 test: lib: add test of memdup_nul()
In reply to Rasmus VillemoesAdd a very basic test of the new memdup_nul() helper.
--- test/lib/string.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
test/lib/string.c +34 -0
@@ -223,6 +223,40 @@ static int lib_memdup(struct unit_test_state *uts) }LIB_TEST (lib_memdup,0 ); +/** lib_memdup_nul() - unit test for memdup_nul() */ +static int lib_memdup_nul (struct unit_test_state *uts) +{ +char buf[BUFLEN ]; +size_t len; +char *p, *q; + +/* Zero size should return a buffer containing a single nul byte */ + p =memdup_nul (NULL ,0 ); +ut_assertnonnull (p); +ut_assert (p[0 ] =='\0' ); +free (p); + + p =memdup_nul (buf,0 ); +ut_assertnonnull (p); +ut_assert (p[0 ] =='\0' ); +free (p); + +strcpy (buf,TEST_STR ); + len =sizeof (TEST_STR ); + p =memdup_nul (buf, len); +ut_asserteq_mem (p, buf, len); +ut_assert (p[len] =='\0' ); + + q =memdup_nul (p, len); +ut_asserteq_mem (q, buf, len); +ut_assert (q[len] =='\0' ); +free (q); +free (p); + +return 0 ; +} +LIB_TEST (lib_memdup_nul,0 ); +/** lib_strnstr() - unit test for strnstr() */ static int lib_strnstr (struct unit_test_state *uts) {
Signature · 1 line
2.53.0
Re: [PATCH v2 9/9] test: lib: add test of memdup_nul()
Quoted 8 lines · Rasmus Villemoes
test: lib: add test of
memdup_nul()Add a very basic test of the new
memdup_nul()helper.Signed-off-by Rasmus Villemoes <[email protected]>
test/lib/string.c| 34 ++++++++++++++++++++++++++++++++++1 file changed, 34 insertions(+)
Quoted 8 lines · Rasmus Villemoes
There are quite a few places where we allocate X+1 bytes, initialize the first X bytes via
memcpy()and then set the last byte to 0.The kernel has a helper for that,
kmemdup_nul(). Introduce a similar one, and start making use of it in a few places. Also the existingmemdup()helper can be put to more use.[...]
Applied to u-boot/next, thanks!
[1/9] string: fix prototype of memdup()
commit: ca1c292d2ee6bcb06be71400d25ae37e9dc2c1aa
[2/9] stdio: drop stdio_clone
commit: 719cacb92e039308e23cbd6b653275e939a5aca5
[3/9] lib/string.c: drop pointless __HAVE_ARCH_STRDUP
commit: 349d148f16d83da3b1e3475be0e43bfda4f4ab71
[4/9] lib/string.c: introduce memdup_nul() helper
commit: 8c664d2135723a110a60b792a8614c4864ad82a3
[5/9] lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
commit: ee8be5d4a1035de232b3497563fe9f6773775f96
[6/9] lib/hashtable.c: use memdup_nul() in himport_r
commit: b87ff4878d081103c45f0ebea7528609a0b173f3
[7/9] common/cli.c: use memdup_nul() in run_command_list()
commit: 11168813bf9c088e1fce8a96f4b493ee815c966b
[8/9] drivers/core: use memdup() instead of malloc()+memcpy()
commit: 4ef201e607ebed2432ee929446e3fb9b57c53a54
[9/9] test: lib: add test of memdup_nul()
commit: 8d209186a1e4aca4ec44745d05d51de7e80f7e3eSignature · 1 line
Tom