Skip to contentSkip to conversation

add memdup_nul(), use it and memdup() in a few places

PATCH Superseded Rasmus Villemoes 2 participants 18 messages Apr 12 – Apr 13

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index d943fcce690..9e47fe01c16 100644
@@ -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
index d56f88d4a84..c2813e0f854 100644
@@ -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
Reviewed-by on v1 1/8
#2 lore raw

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 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.

Quoted 5 lines

include/linux/string.h | 2 +-

 lib/string.c           | 8 +++-----
 2 files changed, 4 insertions(+), 6 deletions(-)
#3 lore raw

PATCH 2/8 stdio: drop stdio_clone

In reply to Rasmus Villemoes

The 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
index fc965944209..038e576147b 100644
@@ -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
index f7f9c10199e..d93604331ff 100644
@@ -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
Reviewed-by on v1 2/8
#4 lore raw

Re: [PATCH 2/8] stdio: drop stdio_clone

Quoted 7 lines · Rasmus Villemoes

stdio: drop stdio_clone

The 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.

Quoted 7 lines

Drop it and just use memdup() directly.

common/stdio.c | 18 +-----------------

 include/stdio_dev.h |  1 -
 2 files changed, 1 insertion(+), 18 deletions(-)
#5 lore raw

PATCH 3/8 lib/string.c: drop pointless __HAVE_ARCH_STRDUP

In reply to Rasmus Villemoes

There 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
index 9e47fe01c16..a28150fa578 100644
@@ -101,10 +101,9 @@ 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);
-#endif
+
 #ifndef __HAVE_ARCH_STRSWAB
 extern char * strswab(const char *);
 #endif
lib/string.c +0 -2
index c2813e0f854..2c1baa568b9 100644
@@ -343,7 +343,6 @@ size_t strcspn(const char *s, const char *reject)
 }
 #endif
 
-#ifndef __HAVE_ARCH_STRDUP
 char * 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
Reviewed-by on v1 3/8
#6 lore raw

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_STRDUP

There 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

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

#7 lore raw

PATCH 4/8 lib/string.c: introduce memdup_nul() helper

In reply to Rasmus Villemoes

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.

---
 include/linux/string.h | 13 +++++++++++++
 lib/string.c           | 15 +++++++++++++++
 2 files changed, 28 insertions(+)
include/linux/string.h +13 -0
index a28150fa578..b2e38ecf26e 100644
@@ -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
index 2c1baa568b9..3923cce5561 100644
@@ -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
Reviewed-by on v1 4/8
#8 lore raw

Re: [PATCH 4/8] lib/string.c: introduce memdup_nul() helper

Quoted 7 lines · Rasmus Villemoes

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.

Quoted 8 lines

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(+)
#9 lore raw

PATCH 5/8 lib/string.c: implement strdup() and strndup() in terms of memdup_nul()

In reply to Rasmus Villemoes

With 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
index 3923cce5561..5ccd1011ab5 100644
@@ -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
Reviewed-by on v1 5/8
#10 lore raw

Re: [PATCH 5/8] lib/string.c: implement strdup() and strndup() in terms of memdup_nul()

Quoted 5 lines · Rasmus Villemoes

lib/string.c: implement strdup() and strndup() in terms of memdup_nul()

With the addition of memdup_nul(), strdup() and strndup() 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.

lib/string.c | 30 ++----------------------------

 1 file changed, 2 insertions(+), 28 deletions(-)
#11 lore raw

PATCH 6/8 lib/hashtable.c: use memdup_nul() in himport_r

In reply to Rasmus Villemoes

We 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
index 75c263b5053..f96a8e686f6 100644
@@ -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
Reviewed-by on v1 6/8
#12 lore raw

Re: [PATCH 6/8] lib/hashtable.c: use memdup_nul() in himport_r

Quoted 9 lines · Rasmus Villemoes

lib/hashtable.c: use memdup_nul() in himport_r

We 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(-)
#13 lore raw

PATCH 7/8 common/cli.c: use memdup_nul() in run_command_list()

In reply to Rasmus Villemoes

Use memdup_nul() instead of open-coding it.

---
 common/cli.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
common/cli.c +1 -3
index 4694a35cd0e..ccf7e26e821 100644
@@ -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
#15 lore raw

PATCH 8/8 drivers/core: use memdup() instead of malloc()+memcpy()

In reply to Rasmus Villemoes

Use 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
index 4763963914b..6a431171c8d 100644
@@ -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
index 3a36b6fdd03..12511f10aa9 100644
@@ -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
index d43645f34dd..1f32f33b295 100644
@@ -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
Reviewed-by on v1 8/8
#16 lore raw

Re: [PATCH 8/8] drivers/core: use memdup() instead of malloc()+memcpy()

Quoted 8 lines · Rasmus Villemoes

drivers/core: use memdup() instead of malloc()+memcpy()

Use 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().

Quoted 6 lines

drivers/core/acpi.c | 3 +--

 drivers/core/ofnode.c | 3 +--
 drivers/core/root.c   | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
#17 lore raw
In reply to Rasmus Villemoes

Hi Rasmus,

Quoted 1 line · Rasmus Villemoes

lib/string.c: introduce memdup_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