mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 2/4] lib: wchar: guard against NULL in strdup_wchar
Date: Tue, 11 Jun 2024 08:57:16 +0200	[thread overview]
Message-ID: <20240611065718.2899625-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240611065718.2899625-1-a.fatoum@pengutronix.de>

strdup(NULL) returns NULL, so the wchar_t-Variants should behave
the same way.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 lib/wchar.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/wchar.c b/lib/wchar.c
index 250538dd8511..49e946a09424 100644
--- a/lib/wchar.c
+++ b/lib/wchar.c
@@ -39,9 +39,13 @@ size_t wcsnlen(const wchar_t * s, size_t count)
 
 wchar_t *strdup_wchar(const wchar_t *src)
 {
-	int len = wcslen(src);
+	int len;
 	wchar_t *tmp, *dst;
 
+	if (!src)
+		return NULL;
+
+	len = wcslen(src);
 	if (!(dst = malloc((len + 1) * sizeof(wchar_t))))
 		return NULL;
 
@@ -97,8 +101,9 @@ wchar_t *strcpy_char_to_wchar(wchar_t *dst, const char *src)
 
 wchar_t *strdup_char_to_wchar(const char *src)
 {
-	wchar_t *dst = malloc((strlen(src) + 1) * sizeof(wchar_t));
+	wchar_t *dst;
 
+	dst = src ? malloc((strlen(src) + 1) * sizeof(wchar_t)) : NULL;
 	if (!dst)
 		return NULL;
 
@@ -109,8 +114,9 @@ wchar_t *strdup_char_to_wchar(const char *src)
 
 char *strdup_wchar_to_char(const wchar_t *src)
 {
-	char *dst = malloc((wcslen(src) + 1));
+	char *dst;
 
+	dst = src ? malloc((wcslen(src) + 1)) : NULL;
 	if (!dst)
 		return NULL;
 
-- 
2.39.2




  reply	other threads:[~2024-06-11  6:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-11  6:57 [PATCH master 1/4] efi: hide EFI related symbols when !EFI Ahmad Fatoum
2024-06-11  6:57 ` Ahmad Fatoum [this message]
2024-06-11  6:57 ` [PATCH master 3/4] efi: payload: gracefully handle NULL parent image device path Ahmad Fatoum
2024-06-11  6:57 ` [PATCH master 4/4] fs: legacy: gracefully handle non existent files Ahmad Fatoum
2024-06-13  7:30 ` [PATCH master 1/4] efi: hide EFI related symbols when !EFI Sascha Hauer

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=20240611065718.2899625-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --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