summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gc.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/gc.c b/src/gc.c
index 5f30570de..2149a2d43 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -167,14 +167,16 @@ mrb_malloc(mrb_state *mrb, size_t len)
void*
mrb_calloc(mrb_state *mrb, size_t nelem, size_t len)
{
- void *p;
+ void *p = NULL;
size_t size;
- size = nelem * len;
- p = mrb_realloc(mrb, 0, size);
+ if (nelem <= SIZE_MAX / len) {
+ size = nelem * len;
+ p = mrb_realloc(mrb, 0, size);
- if (p && size > 0)
- memset(p, 0, size);
+ if (p && size > 0)
+ memset(p, 0, size);
+ }
return p;
}