From: Tom Rini Date: Tue, 12 May 2026 21:41:52 +0000 (-0600) Subject: Merge patch series "add memdup_nul(), use it and memdup() in a few places" X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fnext;p=thirdparty%2Fu-boot.git Merge patch series "add memdup_nul(), use it and memdup() in a few places" Rasmus Villemoes 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/20260421075439.16696-1-ravi@prevas.dk --- e3e651c480c46b332f16a7555b97c6c6fd640a40 diff --cc include/linux/string.h index a8a6cf4af50,b2e38ecf26e..850356d7c3f --- a/include/linux/string.h +++ b/include/linux/string.h @@@ -101,12 -101,9 +101,12 @@@ size_t strcspn(const char *s, const cha # 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 diff --cc lib/string.c index 302efe048b0,5ccd1011ab5..37ea8c29561 --- a/lib/string.c +++ b/lib/string.c @@@ -343,75 -343,31 +343,62 @@@ size_t strcspn(const char *s, const cha } #endif - #ifndef __HAVE_ARCH_STRDUP - char * strdup(const char *s) + void *memdup_nul(const void *src, size_t len) { - char *new; + char *dst; - if ((s == NULL) || - ((new = malloc (strlen(s) + 1)) == NULL) ) { + if (len + 1 < len) return NULL; - } - strcpy (new, s); - return new; - } - - char * strndup(const char *s, size_t n) - { - size_t len; - char *new; - - if (s == NULL) + dst = malloc(len + 1); + if (!dst) return NULL; - len = strlen(s); - - if (n < len) - len = n; - - new = malloc(len + 1); - if (new == NULL) - return NULL; + dst[len] = '\0'; + return memcpy(dst, src, len); + } - strncpy(new, s, len); - new[len] = '\0'; + char * strdup(const char *s) + { + return s ? memdup_nul(s, strlen(s)) : NULL; + } - return new; + char * strndup(const char *s, size_t n) + { + return s ? memdup_nul(s, strnlen(s, n)) : NULL; } +/** + * strdup_const - conditionally duplicate an existing const string + * @s: the string to duplicate + * + * Note: Strings allocated by kstrdup_const should be freed by kfree_const and + * must not be passed to krealloc(). + * + * Return: source string if it is in .rodata section otherwise + * fallback to kstrdup. + */ +const char *strdup_const(const char *s) +{ + if (is_kernel_rodata((unsigned long)s)) + return s; + + return strdup(s); +} + +/** + * kfree_const - conditionally free memory + * @x: pointer to the memory + * + * Function calls kfree only if @x is not in .rodata section. + */ +void kfree_const(const void *x) +{ + if (!is_kernel_rodata((unsigned long)x)) + free((void *)x); +} + - #endif + #ifndef __HAVE_ARCH_STRSPN /** * strspn - Calculate the length of the initial substring of @s which only