]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
Merge patch series "add memdup_nul(), use it and memdup() in a few places" next
authorTom Rini <trini@konsulko.com>
Tue, 12 May 2026 21:41:52 +0000 (15:41 -0600)
committerTom Rini <trini@konsulko.com>
Tue, 12 May 2026 21:41:52 +0000 (15:41 -0600)
Rasmus Villemoes <ravi@prevas.dk> 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
1  2 
common/cli.c
drivers/core/ofnode.c
include/linux/string.h
lib/string.c

diff --cc common/cli.c
Simple merge
Simple merge
index a8a6cf4af5054f116d79921ba8a487ca159478c0,b2e38ecf26e4326732777cbd2987712f9fd1799b..850356d7c3fe6ad9ed18b355c67211ba7ca5c03c
@@@ -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);
- #endif
 +extern const char *strdup_const(const char *s);
 +extern void kfree_const(const void *x);
++
  #ifndef __HAVE_ARCH_STRSWAB
  extern char * strswab(const char *);
  #endif
diff --cc lib/string.c
index 302efe048b07b643d03d7141dc8d27f985afece1,5ccd1011ab57f29f903d9f498556955581ebdf74..37ea8c295619180d9cd4855e938d6c08c39721bc
@@@ -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;
  }
  
- #endif
 +/**
 + * 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);
 +}
 +
 +
  #ifndef __HAVE_ARCH_STRSPN
  /**
   * strspn - Calculate the length of the initial substring of @s which only