mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-05 09:06:08 +00:00
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.
This commit is contained in:
parent
cfac3be0b3
commit
1649138a29
Notes:
sideshowbarker
2024-07-17 03:16:02 +09:00
Author: https://github.com/h3xOo 🔰
Commit: 1649138a29
Pull-request: https://github.com/SerenityOS/serenity/pull/18385
Reviewed-by: https://github.com/awesomekling
1 changed files with 4 additions and 0 deletions
|
@ -72,6 +72,8 @@ char* strdup(char const* str)
|
||||||
{
|
{
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
char* new_str = (char*)malloc(len + 1);
|
char* new_str = (char*)malloc(len + 1);
|
||||||
|
if (!new_str)
|
||||||
|
return nullptr;
|
||||||
memcpy(new_str, str, len);
|
memcpy(new_str, str, len);
|
||||||
new_str[len] = '\0';
|
new_str[len] = '\0';
|
||||||
return new_str;
|
return new_str;
|
||||||
|
@ -82,6 +84,8 @@ char* strndup(char const* str, size_t maxlen)
|
||||||
{
|
{
|
||||||
size_t len = strnlen(str, maxlen);
|
size_t len = strnlen(str, maxlen);
|
||||||
char* new_str = (char*)malloc(len + 1);
|
char* new_str = (char*)malloc(len + 1);
|
||||||
|
if (!new_str)
|
||||||
|
return nullptr;
|
||||||
memcpy(new_str, str, len);
|
memcpy(new_str, str, len);
|
||||||
new_str[len] = 0;
|
new_str[len] = 0;
|
||||||
return new_str;
|
return new_str;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue