calloc: check multiplication overflow

This commit is contained in:
cinap_lenrek 2012-08-19 10:50:39 +02:00
parent feb58cce49
commit be0301f458
2 changed files with 13 additions and 8 deletions

View file

@ -2,12 +2,14 @@
#include <string.h>
void *
calloc(size_t nmemb, size_t size)
calloc(size_t n, size_t s)
{
void *mp;
void *v;
nmemb = nmemb*size;
if(mp = malloc(nmemb))
memset(mp, 0, nmemb);
return(mp);
if(n > 1 && ((size_t)-1)/n < s)
return 0;
n *= s;
if(v = malloc(n))
memset(v, 0, n);
return v;
}

View file

@ -280,10 +280,13 @@ msize(void *v)
}
void*
calloc(ulong n, ulong szelem)
calloc(ulong n, ulong s)
{
void *v;
if(v = mallocz(n*szelem, 1))
if(n > 1 && ((ulong)-1)/n < s)
return nil;
if(v = mallocz(n*s, 1))
setmalloctag(v, getcallerpc(&n));
return v;
}