Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 26 Nov 2023 04:07:59 GMT
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 9c170b3d52bf - stable/13 - grep: don't rely on implementation-defined malloc(0) behavior
Message-ID:  <202311260407.3AQ47xJO028903@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=9c170b3d52bf8e7ca3c14e6788330e79329fcf0b

commit 9c170b3d52bf8e7ca3c14e6788330e79329fcf0b
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-11-05 02:08:36 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-11-26 04:07:17 +0000

    grep: don't rely on implementation-defined malloc(0) behavior
    
    The very few places that rely on malloc/calloc of a zero-size region
    won't attempt to dereference it, so just return NULL rather than rolling
    the dice with the underlying malloc implementation.
    
    Reported by:    brooks, Shawn Webb
    
    (cherry picked from commit e116e040f3091eca914a06dcd0bdd9f1aea23add)
---
 usr.bin/grep/util.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index 68bfd504279e..e3cf6e3cb4d8 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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202311260407.3AQ47xJO028903>