summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2012-07-07 08:27:07 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2012-07-07 08:27:07 -0700
commit7eb86a4aafdf81247bdf17a6806d44b916d8003f (patch)
tree0649e7d0ff77578825cc4b3612f9593ab5283910 /src
parent3b8ad4c30cb96ad35841d5c9317c1d953d38398c (diff)
parenta064038d85758c2e8add556f9aafa7823df53ef4 (diff)
downloadmruby-7eb86a4aafdf81247bdf17a6806d44b916d8003f.tar.gz
mruby-7eb86a4aafdf81247bdf17a6806d44b916d8003f.zip
Merge pull request #351 from monaka/pr-brush-up-mrb_calloc
Brush up mrb_calloc().
Diffstat (limited to 'src')
-rw-r--r--src/gc.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/gc.c b/src/gc.c
index 2663e9780..2149a2d43 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -167,10 +167,17 @@ mrb_malloc(mrb_state *mrb, size_t len)
void*
mrb_calloc(mrb_state *mrb, size_t nelem, size_t len)
{
- void *p = mrb_realloc(mrb, 0, nelem*len);
+ void *p = NULL;
+ size_t size;
+
+ if (nelem <= SIZE_MAX / len) {
+ size = nelem * len;
+ p = mrb_realloc(mrb, 0, size);
+
+ if (p && size > 0)
+ memset(p, 0, size);
+ }
- if (len > 0)
- memset(p, 0, nelem*len);
return p;
}