add memdup_nul(), use it and memdup() in a few places
Subsystems (2)
cover add memdup_nul(), use it and memdup() in a few places
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.
Rasmus Villemoes (8):
string: fix prototype of memdup()
stdio: drop stdio_clone
lib/string.c: drop pointless __HAVE_ARCH_STRDUP lib/string.c: introduce memdup_nul() helper lib/string.c: implement strdup() and strndup() in terms of
memdup_nul()
lib/hashtable.c: use memdup_nul() in himport_r common/cli.c: use memdup_nul() in run_command_list() drivers/core: use memdup() instead of malloc()+memcpy()
common/cli.c | 4 +--- common/stdio.c | 18 +-------------- drivers/core/acpi.c | 3 +-- drivers/core/ofnode.c | 3 +-- drivers/core/root.c | 3 +-- include/linux/string.h | 18 ++++++++++++--- include/stdio_dev.h | 1 - lib/hashtable.c | 7 +++--- lib/string.c | 51 +++++++++++++++--------------------------- 9 files changed, 41 insertions(+), 67 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/8 string: fix prototype of memdup()
It doesn't make sense to restrict memdup() to only return char* pointers, especially when it is already defined to accept void*. This makes it uglier to use to e.g. duplicate a struct.
Make it return void*, just as kmemdup() does in the kernel (and which our kmemdup() in fact also does).
While in here, make a small optimization: memcpy() is defined to return the destination register, so we write this in a way that the compiler may do a tail call.
--- include/linux/string.h | 2 +- lib/string.c | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-)
include/linux/string.h +1 -1
@@ -142,7 +142,7 @@ 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);unsigned long ustrtoul (const char *cp,char **endp,unsigned int base);unsigned long long ustrtoull (const char *cp,char **endp,unsigned int base);
lib/string.c +3 -5
@@ -667,17 +667,15 @@ void * memscan(void * addr, int c, size_t size) }#endif -char *memdup (const void *src,size_t len) +void *memdup (const void *src,size_t len) { -char *p; +void *p; p =malloc (len);if (!p)return NULL ; -memcpy (p, src, len); - -return p; +return memcpy (p, src, len); }#ifndef __HAVE_ARCH_STRNSTR
Signature · 1 line
2.53.0
Re: [PATCH 1/8] string: fix prototype of memdup()
Quoted 13 lines · Rasmus Villemoes
string: fix prototype of
memdup()It doesn't make sense to restrict
memdup()to only return char* pointers, especially when it is already defined to accept void*. This makes it uglier to use to e.g. duplicate a struct.Make it return void*, just as
kmemdup()does in the kernel (and which ourkmemdup()in fact also does).While in here, make a small optimization:
memcpy()is defined to return the destination register, so we write this in a way that the compiler may do a tail call.
Quoted 5 lines
Signed-off-by Rasmus Villemoes <[email protected]>
include/linux/string.h| 2 +-lib/string.c | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-)
PATCH 2/8 stdio: drop stdio_clone
In reply to Rasmus VillemoesThe helper stdio_clone only has a single caller, so it certainly doesn't need to be public. But in fact, it is merely an open-coded memdup() - which for some reason uses calloc() even if the whole allocation is obviously immediately overwritten.
Drop it and just use memdup() directly.
--- common/stdio.c | 18 +----------------- include/stdio_dev.h | 1 - 2 files changed, 1 insertion(+), 18 deletions(-)
common/stdio.c +1 -17
@@ -217,27 +217,11 @@ struct stdio_dev *stdio_get_by_name(const char *name)return NULL ; } -struct stdio_dev *stdio_clone (struct stdio_dev *dev) -{ -struct stdio_dev *_dev; - -if (!dev) -return NULL ; - - _dev =calloc (1 ,sizeof (struct stdio_dev )); -if (!_dev) -return NULL ; - -memcpy (_dev, dev,sizeof (struct stdio_dev )); - -return _dev; -} -int stdio_register_dev (struct stdio_dev *dev,struct stdio_dev **devp) {struct stdio_dev *_dev; - _dev =stdio_clone (dev); + _dev =memdup (dev,sizeof (*dev));if (!_dev)return -ENODEV ;list_add_tail (&_dev->list, &devs.list);
include/stdio_dev.h +0 -1
@@ -96,7 +96,6 @@ int stdio_add_devices(void);int stdio_deregister_dev (struct stdio_dev *dev,int force);struct list_head *stdio_get_list (void );struct stdio_dev *stdio_get_by_name (const char *name); -struct stdio_dev *stdio_clone (struct stdio_dev *dev);int drv_lcd_init (void );int drv_video_init (void );
Signature · 1 line
2.53.0
Re: [PATCH 2/8] stdio: drop stdio_clone
Quoted 7 lines · Rasmus Villemoes
stdio: drop
stdio_cloneThe helper
stdio_cloneonly has a single caller, so it certainly doesn't need to be public. But in fact, it is merely an open-codedmemdup()- which for some reason usescalloc()even if the whole allocation is obviously immediately overwritten.
Quoted 7 lines
Drop it and just use
memdup()directly.Signed-off-by Rasmus Villemoes <[email protected]>
common/stdio.c| 18 +-----------------include/stdio_dev.h | 1 - 2 files changed, 1 insertion(+), 18 deletions(-)
PATCH 3/8 lib/string.c: drop pointless __HAVE_ARCH_STRDUP
In reply to Rasmus VillemoesThere have never been an arch-specific optimized implementation of str[n]dup, nor is there likely to ever be one, because unlike their cousins strlen(), strcpy() and similar that simply read/write the src/dst, the dup functions by definition involve memory allocation. So drop this irrelevant cpp guard.
--- include/linux/string.h | 3 +-- lib/string.c | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-)
include/linux/string.h +1 -2
@@ -101,10 +101,9 @@ size_t strcspn(const char *s, const char *reject);# define strndup sandbox_strndup#endif -#ifndef __HAVE_ARCH_STRDUPextern char *strdup (const char *);extern char *strndup (const char *,size_t ); -#endif +#ifndef __HAVE_ARCH_STRSWABextern char *strswab (const char *);#endif
lib/string.c +0 -2
@@ -343,7 +343,6 @@ size_t strcspn(const char *s, const char *reject) }#endif -#ifndef __HAVE_ARCH_STRDUPchar *strdup (const char *s) {char *new;
@@ -379,7 +378,6 @@ char * strndup(const char *s, size_t n)return new; } -#endif #ifndef __HAVE_ARCH_STRSPN/**
Signature · 1 line
2.53.0
Re: [PATCH 3/8] lib/string.c: drop pointless __HAVE_ARCH_STRDUP
Hi Rasmus,
Quoted 8 lines · Rasmus Villemoes
lib/string.c: drop pointless__HAVE_ARCH_STRDUPThere have never been an arch-specific optimized implementation of str[n]dup, nor is there likely to ever be one, because unlike their cousins
strlen(),strcpy()and similar that simply read/write the src/dst, the dup functions by definition involve memory allocation. So drop this irrelevant cpp guard.
Quoted 7 lines
Signed-off-by Rasmus Villemoes <[email protected]>
include/linux/string.h| 3 +--lib/string.c | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-)There have never been an arch-specific optimized implementation of
There has never been
PATCH 4/8 lib/string.c: introduce memdup_nul() helper
In reply to Rasmus VillemoesThis 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.
--- include/linux/string.h | 13 +++++++++++++ lib/string.c | 15 +++++++++++++++ 2 files changed, 28 insertions(+)
include/linux/string.h +13 -0
@@ -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);
lib/string.c +15 -0
@@ -343,6 +343,21 @@ size_t strcspn(const char *s, const char *reject) }#endif +void *memdup_nul (const void *src,size_t len) +{ +char *dst; + +if (len +1 < len) +return NULL ; + + dst =malloc (len +1 ); +if (!dst) +return NULL ; + + dst[len] ='\0' ; +return memcpy (dst, src, len); +} +char *strdup (const char *s) {char *new;
Signature · 1 line
2.53.0
Re: [PATCH 4/8] lib/string.c: introduce memdup_nul() helper
Quoted 7 lines · Rasmus Villemoes
lib/string.c: introducememdup_nul()helperThis is completely analogous to the linux kernel's
kmemdup_nul()helper, apart from the lack of thegfp_targument: Allocate a buffer of size {len}+1, copy {len} bytes from the given buffer, and add a final nul byte.
Quoted 8 lines
This pattern exists in a number of places, so this helper can reduce some boilerplate code.
Signed-off-by Rasmus Villemoes <[email protected]>
include/linux/string.h| 13 +++++++++++++lib/string.c | 15 +++++++++++++++ 2 files changed, 28 insertions(+)
PATCH 5/8 lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
In reply to Rasmus VillemoesWith the addition of memdup_nul(), strdup() and strndup() can be implemented as one-liners.
While not required by POSIX or C, do keep the behaviour of gracefully accepting a NULL source and simply return NULL.
--- lib/string.c | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-)
lib/string.c +2 -28
@@ -360,38 +360,12 @@ void *memdup_nul(const void *src, size_t len)char *strdup (const char *s) { -char *new; - -if ((s ==NULL ) || - ((new =malloc (strlen (s) +1 )) ==NULL ) ) { -return NULL ; - } - -strcpy (new, s); -return new; +return s ?memdup_nul (s,strlen (s)) :NULL ; }char *strndup (const char *s,size_t n) { -size_t len; -char *new; - -if (s ==NULL ) -return NULL ; - - len =strlen (s); - -if (n < len) - len = n; - - new =malloc (len +1 ); -if (new ==NULL ) -return NULL ; - -strncpy (new, s, len); - new[len] ='\0' ; - -return new; +return s ?memdup_nul (s,strnlen (s, n)) :NULL ; }#ifndef __HAVE_ARCH_STRSPN
Signature · 1 line
2.53.0
Re: [PATCH 5/8] lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
Quoted 5 lines · Rasmus Villemoes
lib/string.c: implementstrdup()andstrndup()in terms ofmemdup_nul()With the addition of
memdup_nul(),strdup()andstrndup()can be implemented as one-liners.
Quoted 7 lines
While not required by POSIX or C, do keep the behaviour of gracefully accepting a NULL source and simply return NULL.
Signed-off-by Rasmus Villemoes <[email protected]>
lib/string.c| 30 ++----------------------------1 file changed, 2 insertions(+), 28 deletions(-)
PATCH 6/8 lib/hashtable.c: use memdup_nul() in himport_r
In reply to Rasmus VillemoesWe have memdup_nul() for exactly this pattern of duplicating a block of memory and ensuring there's a nul byte after the copy.
--- lib/hashtable.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
lib/hashtable.c +3 -4
@@ -821,13 +821,12 @@ int himport_r(struct hsearch_data *htab, }/* we allocate new space to make sure we can write to the array */ -if ((data =malloc (size +1 )) ==NULL ) { -debug ("himport_r: can't malloc %lu bytes\n" , (ulong )size +1 ); + data =memdup_nul (env, size); +if (data ==NULL ) { +debug ("himport_r: can't duplicate env block\n" );__set_errno (ENOMEM );return 0 ; } -memcpy (data, env, size); - data[size] ='\0' ; dp = data;/* make a local copy of the list of variables */
Signature · 1 line
2.53.0
Re: [PATCH 6/8] lib/hashtable.c: use memdup_nul() in himport_r
Quoted 9 lines · Rasmus Villemoes
lib/hashtable.c: usememdup_nul()inhimport_rWe have
memdup_nul()for exactly this pattern of duplicating a block of memory and ensuring there's a nul byte after the copy.Signed-off-by Rasmus Villemoes <[email protected]>
lib/hashtable.c| 7 +++----1 file changed, 3 insertions(+), 4 deletions(-)
PATCH 7/8 common/cli.c: use memdup_nul() in run_command_list()
In reply to Rasmus VillemoesUse memdup_nul() instead of open-coding it.
--- common/cli.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
common/cli.c +1 -3
@@ -138,11 +138,9 @@ int run_command_list(const char *cmd, int len, int flag)#endif }if (need_buff) { - buff =malloc (len +1 ); + buff =memdup_nul (cmd, len);if (!buff)return 1 ; -memcpy (buff, cmd, len); - buff[len] ='\0' ; }#ifdef CONFIG_HUSH_PARSER if (use_hush_old ()) {
Signature · 1 line
2.53.0
Re: [PATCH 7/8] common/cli.c: use memdup_nul() in run_command_list()
Quoted 8 lines · Rasmus Villemoes
common/cli.c: usememdup_nul()inrun_command_list()Use
memdup_nul()instead of open-coding it.Signed-off-by Rasmus Villemoes <[email protected]>
common/cli.c| 4 +---1 file changed, 1 insertion(+), 3 deletions(-)
PATCH 8/8 drivers/core: use memdup() instead of malloc()+memcpy()
In reply to Rasmus VillemoesUse memdup() instead of open-coding it.
In the dm_setup_inst() case, there was never any reason to use calloc(), as the whole allocation is definitely initialized via the immediately following memcpy().
--- drivers/core/acpi.c | 3 +-- drivers/core/ofnode.c | 3 +-- drivers/core/root.c | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-)
drivers/core/acpi.c +1 -2
@@ -154,10 +154,9 @@ static int add_item(struct acpi_ctx *ctx, struct udevice *dev,if (!item->size)return 0 ;if (type !=TYPE_OTHER ) { - item->buf =malloc (item->size); + item->buf =memdup (start, item->size);if (!item->buf)return log_msg_ret ("mem" , -ENOMEM ); -memcpy (item->buf, start, item->size); } item_count++; log_debug("* %s: Added type %d, %p, size %x\n",
drivers/core/ofnode.c +1 -2
@@ -1750,10 +1750,9 @@ int ofnode_write_prop(ofnode node, const char *propname, const void *value,void *newval;if (copy) { - newval =malloc (len); + newval =memdup (value, len);if (!newval)return log_ret (-ENOMEM ); -memcpy (newval, value, len); value = newval; } ret =of_write_prop (ofnode_to_np (node), propname, len, value);
drivers/core/root.c +1 -2
@@ -81,10 +81,9 @@ static int dm_setup_inst(void)/* Now allocate space for the priv/plat data, and copy it in */ size = __priv_data_end - __priv_data_start; - base =calloc (1 , size); + base =memdup (__priv_data_start, size);if (!base)return log_msg_ret ("priv" , -ENOMEM ); -memcpy (base, __priv_data_start, size);gd_set_dm_priv_base (base); }
Signature · 1 line
2.53.0
Re: [PATCH 8/8] drivers/core: use memdup() instead of malloc()+memcpy()
Quoted 8 lines · Rasmus Villemoes
drivers/core: usememdup()instead of malloc()+memcpy()Use
memdup()instead of open-coding it.In the
dm_setup_inst()case, there was never any reason to usecalloc(), as the whole allocation is definitely initialized via the immediately followingmemcpy().
Quoted 6 lines
Signed-off-by Rasmus Villemoes <[email protected]>
drivers/core/acpi.c| 3 +--drivers/core/ofnode.c | 3 +-- drivers/core/root.c | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-)
Hi Rasmus,
Quoted 1 line · Rasmus Villemoes
lib/string.c: introducememdup_nul()helper
This looks like a nice cleanup series.
Since you are adding a new public helper, please can you add a unit test for memdup_nul() in test/lib/string.c?
Regards,
Simon