Signal an error instead of giving the caller less memory than they asked
for when num * size would cause integer overflow. MFC after: 1 week
This commit is contained in:
parent
c3ab10b179
commit
79a3f64da0
@ -37,6 +37,8 @@ static char sccsid[] = "@(#)calloc.c 8.1 (Berkeley) 6/4/93";
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -47,6 +49,11 @@ calloc(num, size)
|
|||||||
{
|
{
|
||||||
void *p;
|
void *p;
|
||||||
|
|
||||||
|
if (size != 0 && SIZE_MAX / size < num) {
|
||||||
|
errno = ENOMEM;
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
size *= num;
|
size *= num;
|
||||||
if ( (p = malloc(size)) )
|
if ( (p = malloc(size)) )
|
||||||
bzero(p, size);
|
bzero(p, size);
|
||||||
|
Loading…
Reference in New Issue
Block a user