]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
qsort.c: prevent undefined behavior
authorStefan Eßer <se@FreeBSD.org>
Thu, 13 Jan 2022 10:09:38 +0000 (11:09 +0100)
committerStefan Eßer <se@FreeBSD.org>
Thu, 13 Jan 2022 10:09:38 +0000 (11:09 +0100)
commitd106f982a54cd299671ccad58bc456138a22ae7b
treef701b546cbf8753a63d14d8739c65681d4c6ddb3
parent027d0c1c043ab274f00e51b826c0d4ebea219623
qsort.c: prevent undefined behavior

Mark Milliard has detected a case of undefined behavior with the LLVM
UBSAN. The mandoc program called qsort with a==NULL and n==0, which is
allowed by the POSIX standard. The qsort() in FreeBSD did not attempt
to perform any accesses using the passed pointer for n==0, but it did
add an offset to the pointer value, which is undefined behavior in
case of a NULL pointer. This operation has no adverse effects on any
achitecture supported by FreeBSD, but could be caught in more strict
environments.

After some discussion in the freebsd-current mail list, it was
concluded that the case of a==NULL and n!=0 should still be caught by
UBSAN (or cause a program abort due to an illegal access) in order to
not hide errors in programs incorrectly invoking qsort().

Only the the case of a==NULL and n==0 should be fixed to not perform
the undefined operation on a NULL pointer.

This commit makes qsort() exit before reaching the point of
potentially undefined behvior for the case n==0, but does not test
the value of a, since the result will not depend on whether this
pointer is NULL or an actual pointer to an array if n==0.

The issue found by Mark Milliard in the whatis command has been
reported to the upstream (OpenBSD) and has already been patched
there.

MFC after: 1 week
lib/libc/stdlib/qsort.c