mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:34:59 +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
1 changed files with 4 additions and 0 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue