diff options
| author | Tom Rini <[email protected]> | 2026-05-12 15:41:52 -0600 |
|---|---|---|
| committer | Tom Rini <[email protected]> | 2026-05-12 15:41:52 -0600 |
| commit | e3e651c480c46b332f16a7555b97c6c6fd640a40 (patch) | |
| tree | 01f7277b103a10ef301e6981d7f4ca2e33f653db /include/linux | |
| parent | 5732bd0f457b4c671e46574d64d4acb099c0f0a5 (diff) | |
| parent | 8d209186a1e4aca4ec44745d05d51de7e80f7e3e (diff) | |
Merge patch series "add memdup_nul(), use it and memdup() in a few places"
Rasmus Villemoes <[email protected]> says:
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.
Link: https://lore.kernel.org/r/[email protected]
Diffstat (limited to 'include/linux')
| -rw-r--r-- | include/linux/string.h | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index a8a6cf4af50..850356d7c3f 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -101,12 +101,12 @@ size_t strcspn(const char *s, const char *reject); # define strndup sandbox_strndup #endif -#ifndef __HAVE_ARCH_STRDUP extern char * strdup(const char *); extern char * strndup(const char *, size_t); + extern const char *strdup_const(const char *s); extern void kfree_const(const void *x); -#endif + #ifndef __HAVE_ARCH_STRSWAB extern char * strswab(const char *); #endif @@ -144,7 +144,20 @@ 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); + +/** + * 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); |
