git: 4e5d32a445f9 - main - Fix null pointer subtraction in mergesort()

Dimitry Andric dim at FreeBSD.org
Fri Aug 27 17:47:12 UTC 2021


The branch main has been updated by dim:

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

commit 4e5d32a445f90d37966cd6de571978551654e3f3
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-27 17:46:18 +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.
    
    MFC after:      3 days
---
 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)


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