git: a2ddb544867a - stable/12 - Fix null pointer subtraction in mergesort()

Dimitry Andric dim at FreeBSD.org
Tue Aug 31 19:11:25 UTC 2021


The branch stable/12 has been updated by dim:

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

commit a2ddb544867a6100fc4b684772897134511bb72c
Author:     Dimitry Andric <dim at FreeBSD.org>
AuthorDate: 2021-08-27 17:45:43 +0000
Commit:     Dimitry Andric <dim at FreeBSD.org>
CommitDate: 2021-08-31 19:11:00 +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 3a47e424e4da..566f8cba0a3d 100644
--- a/lib/libc/stdlib/merge.c
+++ b/lib/libc/stdlib/merge.c
@@ -131,12 +131,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)


More information about the dev-commits-src-all mailing list