From 1649138a291e697faafe4f4c3b4a05a603d0c0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Wi=C5=9Bniewski?= Date: Sat, 15 Apr 2023 08:09:22 +0200 Subject: [PATCH] LibC: Return nullptr if allocation fails in strdup() and strndup() If allocation would fail, we would continue to UB at memcpy and nullptr dereference before returning. --- Userland/Libraries/LibC/string.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Userland/Libraries/LibC/string.cpp b/Userland/Libraries/LibC/string.cpp index 28fb2ce34f..7e31d1f053 100644 --- a/Userland/Libraries/LibC/string.cpp +++ b/Userland/Libraries/LibC/string.cpp @@ -72,6 +72,8 @@ char* strdup(char const* str) { size_t len = strlen(str); char* new_str = (char*)malloc(len + 1); + if (!new_str) + return nullptr; memcpy(new_str, str, len); new_str[len] = '\0'; return new_str; @@ -82,6 +84,8 @@ char* strndup(char const* str, size_t maxlen) { size_t len = strnlen(str, maxlen); char* new_str = (char*)malloc(len + 1); + if (!new_str) + return nullptr; memcpy(new_str, str, len); new_str[len] = 0; return new_str;