summaryrefslogtreecommitdiff
path: root/include/linux/string.h
diff options
context:
space:
mode:
authorRasmus Villemoes <[email protected]>2026-04-21 09:54:34 +0200
committerTom Rini <[email protected]>2026-05-12 15:38:00 -0600
commit8c664d2135723a110a60b792a8614c4864ad82a3 (patch)
tree636ba1bc4f8c7f85056c246ed2f65a879d336d3e /include/linux/string.h
parent349d148f16d83da3b1e3475be0e43bfda4f4ab71 (diff)
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 <[email protected]> Signed-off-by: Rasmus Villemoes <[email protected]>
Diffstat (limited to 'include/linux/string.h')
-rw-r--r--include/linux/string.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h
index a28150fa578..b2e38ecf26e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -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);