lib/string.c: introduce memdup_nul() helper

This 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.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
This commit is contained in:
Rasmus Villemoes
2026-05-12 15:38:00 -06:00
committed by Tom Rini
parent 349d148f16
commit 8c664d2135
2 changed files with 28 additions and 0 deletions
+13
View File
@@ -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);