Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 31 Aug 2021 19:06:30 GMT
From:      Dimitry Andric <dim@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: ef314cd4bae2 - stable/13 - Fix null pointer subtraction in mergesort()
Message-ID:  <202108311906.17VJ6Uqm073674@gitrepo.freebsd.org>

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

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

commit ef314cd4bae273be01eb45bae3af63680e65465c
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2021-08-27 17:45:43 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2021-08-31 19:05:43 +0000

    Fix null pointer subtraction in mergesort()
    
    Clang 13 produces the following warning for this function:
    
    lib/libc/stdlib/merge.c:137:41: error: performing pointer subtraction with a null pointer has undefined behavior [-Werror,-Wnull-pointer-subtraction]
            if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
                                                   ^ ~~~~~~~~~
    
    This is meant to check whether the size and base parameters are aligned
    to the size of an int, so use our __is_aligned() macro instead.
    
    Also remove the comment that indicated this "stupid subtraction" was
    done to pacify some ancient and unknown Cray compiler, and which has
    been there since the BSD 4.4 Lite Lib Sources were imported.
    
    (cherry picked from commit 4e5d32a445f90d37966cd6de571978551654e3f3)
---
 lib/libc/stdlib/merge.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/lib/libc/stdlib/merge.c b/lib/libc/stdlib/merge.c
index 853d6ae93fcb..7d8484ed10b9 100644
--- a/lib/libc/stdlib/merge.c
+++ b/lib/libc/stdlib/merge.c
@@ -129,12 +129,8 @@ mergesort(void *base, size_t nmemb, size_t size, cmp_t cmp)
 	if (nmemb == 0)
 		return (0);
 
-	/*
-	 * XXX
-	 * Stupid subtraction for the Cray.
-	 */
 	iflag = 0;
-	if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
+	if (__is_aligned(size, ISIZE) && __is_aligned(base, ISIZE))
 		iflag = 1;
 
 	if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)



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