[PATCH master 2/4] lib: wchar: guard against NULL in strdup_wchar

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Jun 10 23:57:16 PDT 2024


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

Signed-off-by: Ahmad Fatoum <a.fatoum at 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




More information about the barebox mailing list