diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 1805cc74bfc6..936abc41b3ef 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -650,6 +650,8 @@ grep_malloc(size_t size) { void *ptr; + if (size == 0) + return (NULL); if ((ptr = malloc(size)) == NULL) err(2, "malloc"); return (ptr); @@ -663,6 +665,8 @@ grep_calloc(size_t nmemb, size_t size) { void *ptr; + if (nmemb == 0 || size == 0) + return (NULL); if ((ptr = calloc(nmemb, size)) == NULL) err(2, "calloc"); return (ptr);