Skip to content

Commit ecd7599

Browse files
committed
Fix calloc to return NULL on malloc failure
Ensure calloc returns NULL when malloc fails to allocate memory. This change prevents potential undefined behavior when memory allocation fails.
1 parent cb34939 commit ecd7599

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

lib/c.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ void *malloc(int size)
654654
void *calloc(int n, int size)
655655
{
656656
char *p = malloc(n * size);
657+
658+
if (!p)
659+
return NULL;
657660
for (int i = 0; i < n * size; i++)
658661
p[i] = 0;
659662
return p;

tests/snapshots/fib.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)