Skip to content

Commit

Permalink
stdlib/calloc: fix size overflow check
Browse files Browse the repository at this point in the history
JIRA: RTOS-769
  • Loading branch information
lukileczo committed Feb 8, 2024
1 parent 10af1b0 commit 6391678
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions stdlib/malloc_dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,18 +467,19 @@ void *malloc(size_t size)

void *calloc(size_t nitems, size_t size)
{
void *ptr;
uint64_t allocSize = (uint64_t)nitems * size;

if (allocSize > (uint64_t)UINT_MAX) {
if ((nitems != 0) && (size > (size_t)-1 / nitems)) {
errno = ENOMEM;
return NULL;
}

if ((ptr = malloc((size_t) allocSize)) == NULL)
size_t allocSize = nitems * size;

void *ptr = malloc(allocSize);
if (ptr == NULL) {
return NULL;
}

memset(ptr, 0, (size_t)allocSize);
memset(ptr, 0, allocSize);
return ptr;
}

Expand Down

0 comments on commit 6391678

Please sign in to comment.