diff options
| author | Jakob Kaivo <jkk@ung.org> | 2020-08-13 10:36:39 -0400 |
|---|---|---|
| committer | Jakob Kaivo <jkk@ung.org> | 2020-08-13 10:36:39 -0400 |
| commit | 8500ea906a6b99f0e4dbe41bceb7187c7fb7da8b (patch) | |
| tree | 33c3ff86a8da467ef411eb72cd5473555970ca9b /src/stdlib | |
| parent | 894de819a55a7afcb4ed41563f5cd3f120bee8b8 (diff) | |
fail if nmemb * size overflows
Diffstat (limited to 'src/stdlib')
| -rw-r--r-- | src/stdlib/calloc.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/stdlib/calloc.c b/src/stdlib/calloc.c index aeed4aef..ec5fbfdb 100644 --- a/src/stdlib/calloc.c +++ b/src/stdlib/calloc.c @@ -6,12 +6,17 @@ void * calloc(size_t nmemb, size_t size) { void *p = NULL; + size_t total = nmemb * size; + + if (total < nmemb || total < size) { + return NULL; + } if (nmemb == 0 || size == 0) { return NULL; } - p = realloc(NULL, size * nmemb); + p = malloc(total); if (p != NULL) { memset(p, 0, size * nmemb); } |
