summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-13 10:36:39 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-13 10:36:39 -0400
commit8500ea906a6b99f0e4dbe41bceb7187c7fb7da8b (patch)
tree33c3ff86a8da467ef411eb72cd5473555970ca9b /src
parent894de819a55a7afcb4ed41563f5cd3f120bee8b8 (diff)
fail if nmemb * size overflows
Diffstat (limited to 'src')
-rw-r--r--src/stdlib/calloc.c7
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);
}