mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Subject: [PATCH 06/15] string: implement kmemdup_nul
Date: Tue, 27 May 2025 23:22:51 +0200	[thread overview]
Message-ID: <20250527212300.575031-7-a.fatoum@barebox.org> (raw)
In-Reply-To: <20250527212300.575031-1-a.fatoum@barebox.org>

From: Ahmad Fatoum <a.fatoum@pengutronix.de>

Unlike memdup and strdup, memdup_nul ensures that the output is NUL
terminated, while not expecting the input to be so.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 include/linux/string.h |  6 ++++++
 lib/string.c           | 42 ++++++++++++++++++++++++++----------------
 2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 0fa84f095e02..953484ebc9f6 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -175,6 +175,12 @@ static inline void *memdup(const void *buf, size_t size)
 }
 #endif
 
+extern char *memdup_nul(const char *s, size_t len);
+static inline char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
+{
+	return memdup_nul(s, len);
+}
+
 #define memdup_array(arr, count) memdup(arr, array_size(count, sizeof(*arr)));
 
 static inline void *kmemdup(const void *src, size_t len, gfp_t gfp)
diff --git a/lib/string.c b/lib/string.c
index f2272be37e76..33d61add8189 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -423,42 +423,52 @@ size_t strnlen(const char * s, size_t count)
 #endif
 EXPORT_SYMBOL(strnlen);
 
-#ifndef __HAVE_ARCH_STRDUP
-char * strdup(const char *s)
+static __always_inline char *__memdup_nul(const char *s, size_t len)
 {
 	char *new;
 
 	if ((s == NULL)	||
-	    ((new = malloc (strlen(s) + 1)) == NULL) ) {
+	    ((new = malloc (len + 1)) == NULL) ) {
 		return NULL;
 	}
 
-	strcpy (new, s);
+	memcpy (new, s, len);
+	/* Ensure the buf is always NUL-terminated, regardless of @s. */
+	new[len] = '\0';
 	return new;
 }
+
+#ifndef __HAVE_ARCH_STRDUP
+char * strdup(const char *s)
+{
+	return s ? __memdup_nul(s, strlen(s)) : NULL;
+}
 #endif
 EXPORT_SYMBOL(strdup);
 
 #ifndef __HAVE_ARCH_STRNDUP
 char *strndup(const char *s, size_t n)
 {
-	char *new;
-	size_t len = strnlen(s, n);
-
-	if ((s == NULL) ||
-	    ((new = malloc(len + 1)) == NULL)) {
-		return NULL;
-	}
-
-	memcpy(new, s, len);
-	new[len] = '\0';
-
-	return new;
+	return s ? __memdup_nul(s, strnlen(s, n)) : NULL;
 }
 
 #endif
 EXPORT_SYMBOL(strndup);
 
+/**
+ * memdup_nul - Create a NUL-terminated string from @s, which might be unterminated.
+ * @s: The data to copy
+ * @len: The size of the data, not including the NUL terminator
+ *
+ * Return: newly allocated copy of @s with NUL-termination or %NULL in
+ * case of error
+ */
+char *memdup_nul(const char *s, size_t n)
+{
+	return s ? __memdup_nul(s, n) : NULL;
+}
+EXPORT_SYMBOL(memdup_nul);
+
 #ifndef __HAVE_ARCH_STRSPN
 /**
  * strspn - Calculate the length of the initial substring of @s which only
-- 
2.39.5




  parent reply	other threads:[~2025-05-27 21:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-27 21:22 [PATCH 00/15] efi: loader preparatory patches Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 01/15] lib: wchar: add wide char string comparison functions Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 02/15] ARM: select HW_HAS_PCI architecture wide Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 03/15] efi: types: define efi_char16_t as wchar_t Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 04/15] efi: types: document efi_physical_addr_t being always 64-bit Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 05/15] efi: payload: early-mem: EFI_ALLOCATE_ANY_PAGES on non-x86 Ahmad Fatoum
2025-05-27 21:22 ` Ahmad Fatoum [this message]
2025-05-27 21:22 ` [PATCH 07/15] efi: types: implement efi_phys_to_virt/efi_virt_to_phys helpers Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 08/15] efi: return pointer from efi_earlymem_alloc Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 09/15] efi: payload: image: use new efi_phys_to_virt helper Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 10/15] efi: payload: iomem: use virt_start if set Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 11/15] efi: use size_t for UINTN array sizes instead of unsigned long Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 12/15] efi: payload: unify duplicate code in ifdef Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 13/15] efi: payload: use efi_virt_to_phys instead of pointer to u64 casts Ahmad Fatoum
2025-05-27 21:22 ` [PATCH 14/15] clocksource: efi: use DIV_ROUND_DOWN_ULL for 64-bit devision Ahmad Fatoum
2025-05-27 21:23 ` [PATCH 15/15] efi: payload: use ktime_to_us to avoid plain 64-bit division Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250527212300.575031-7-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox