mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
LibC: Some calloc() and realloc() improvements (#3108)
If the space cannot be allocated, the original memory block shall remain unchanged and the function should return nullptr. Also add a function attribute and some null checks.
This commit is contained in:
parent
dcfc54d767
commit
11b9e8b366
2 changed files with 10 additions and 4 deletions
|
@ -397,7 +397,8 @@ void* calloc(size_t count, size_t size)
|
||||||
{
|
{
|
||||||
size_t new_size = count * size;
|
size_t new_size = count * size;
|
||||||
auto* ptr = malloc(new_size);
|
auto* ptr = malloc(new_size);
|
||||||
memset(ptr, 0, new_size);
|
if (ptr)
|
||||||
|
memset(ptr, 0, new_size);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -418,13 +419,18 @@ void* realloc(void* ptr, size_t size)
|
||||||
{
|
{
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
return malloc(size);
|
return malloc(size);
|
||||||
|
if (!size)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
LOCKER(malloc_lock());
|
LOCKER(malloc_lock());
|
||||||
auto existing_allocation_size = malloc_size(ptr);
|
auto existing_allocation_size = malloc_size(ptr);
|
||||||
if (size <= existing_allocation_size)
|
if (size <= existing_allocation_size)
|
||||||
return ptr;
|
return ptr;
|
||||||
auto* new_ptr = malloc(size);
|
auto* new_ptr = malloc(size);
|
||||||
memcpy(new_ptr, ptr, min(existing_allocation_size, size));
|
if (new_ptr) {
|
||||||
free(ptr);
|
memcpy(new_ptr, ptr, min(existing_allocation_size, size));
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
return new_ptr;
|
return new_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ __attribute__((malloc)) __attribute__((alloc_size(1))) void* malloc(size_t);
|
||||||
__attribute__((malloc)) __attribute__((alloc_size(1, 2))) void* calloc(size_t nmemb, size_t);
|
__attribute__((malloc)) __attribute__((alloc_size(1, 2))) void* calloc(size_t nmemb, size_t);
|
||||||
size_t malloc_size(void*);
|
size_t malloc_size(void*);
|
||||||
void free(void*);
|
void free(void*);
|
||||||
void* realloc(void* ptr, size_t);
|
__attribute__((alloc_size(2))) void* realloc(void* ptr, size_t);
|
||||||
char* getenv(const char* name);
|
char* getenv(const char* name);
|
||||||
int putenv(char*);
|
int putenv(char*);
|
||||||
int unsetenv(const char*);
|
int unsetenv(const char*);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue