freebsd-nq/lib/libc/stdlib
Stefan Eßer d106f982a5 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
2022-01-13 11:09:38 +01:00
..
jemalloc Follow-up r365371 by removing sentences which indicate the state of the 2020-09-06 09:08:06 +00:00
_Exit.c
a64l.3
a64l.c
abort.3 Bump the date on man pages in r334306 2018-05-29 02:41:32 +00:00
abort.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
abs.3
abs.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
alloca.3
at_quick_exit.3
atexit.3
atexit.c Fix order of destructors between main binary and libraries. 2019-04-15 13:03:09 +00:00
atexit.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
atof.3 Note some functions that appeared in First Edition Unix 2020-04-01 22:50:41 +00:00
atof.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
atoi.3 Note some functions that appeared in First Edition Unix 2020-04-01 22:50:41 +00:00
atoi.c libc: remove explicit cast NULL in atoi 2018-06-13 08:52:17 +00:00
atol.3
atol.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
atoll.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
bsearch_b.c
bsearch.3 bsearch.3: Improve the example. 2019-07-17 19:29:55 +00:00
bsearch.c libc: typo fix (s/involes/involves) 2020-10-16 17:51:09 +00:00
cxa_thread_atexit_impl.c
cxa_thread_atexit.c
div.3
div.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
exit.3 _Exit(3): document implementation 2021-08-08 22:39:46 +03:00
exit.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
getenv.3 libc: add clearenv function 2021-11-07 16:20:15 +01:00
getenv.c libc: add clearenv function 2021-11-07 16:20:15 +01:00
getopt_long.3 Fix a typo: argments -> arguments 2020-05-19 11:05:27 +00:00
getopt_long.c getopt: Fix conversion from string-literal to non-const char * 2021-01-19 21:23:25 +00:00
getopt.3
getopt.c getopt: Fix conversion from string-literal to non-const char * 2021-01-19 21:23:25 +00:00
getsubopt.3
getsubopt.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
hcreate_r.c
hcreate.3
hcreate.c libc: further adoption of SPDX licensing ID tags. 2017-11-25 17:12:48 +00:00
hdestroy_r.c
heapsort_b.c
heapsort.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
hsearch_r.c
hsearch.h
imaxabs.3
imaxabs.c libc: further adoption of SPDX licensing ID tags. 2017-11-25 17:12:48 +00:00
imaxdiv.3
imaxdiv.c libc: further adoption of SPDX licensing ID tags. 2017-11-25 17:12:48 +00:00
insque.3
insque.c
l64a.c
labs.3
labs.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
ldiv.3
ldiv.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
llabs.3
llabs.c libc: further adoption of SPDX licensing ID tags. 2017-11-25 17:12:48 +00:00
lldiv.3
lldiv.c libc: further adoption of SPDX licensing ID tags. 2017-11-25 17:12:48 +00:00
lsearch.3
lsearch.c
Makefile.inc libc: add clearenv function 2021-11-07 16:20:15 +01:00
memory.3
merge.c Fix null pointer subtraction in mergesort() 2021-08-27 19:46:18 +02:00
mergesort_b.c
ptsname.3 Implement ptsname_r. 2020-10-17 04:14:38 +00:00
ptsname.c Further refinements of ptsname_r(3) interface: 2020-10-20 01:29:45 +00:00
qsort_r.c
qsort_s.c Add qsort_s(3). Apart from the constraints, it also makes it easier 2020-01-20 11:40:07 +00:00
qsort.3 qsort.3: Bump Dd and note that Annex K is optional 2020-01-20 18:43:10 +00:00
qsort.c qsort.c: prevent undefined behavior 2022-01-13 11:09:38 +01:00
quick_exit.3 C++17 requires quick_exit(3) to be async-signal safe. 2017-11-04 10:52:58 +00:00
quick_exit.c libc: further adoption of SPDX licensing ID tags. 2017-11-25 17:12:48 +00:00
radixsort.3
radixsort.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
rand.3 rand(3): Replace implementation with one backed by random(3) algorithm 2020-02-01 20:33:23 +00:00
rand.c Fix race between first rand(3) calls with _once(). 2021-07-21 11:32:22 -04:00
random.3 rand(3): Replace implementation with one backed by random(3) algorithm 2020-02-01 20:33:23 +00:00
random.c rand(3): Replace implementation with one backed by random(3) algorithm 2020-02-01 20:33:23 +00:00
random.h rand(3): Replace implementation with one backed by random(3) algorithm 2020-02-01 20:33:23 +00:00
reallocarray.3
reallocarray.c
reallocf.3
reallocf.c Regularize my copyright notice 2019-12-04 16:56:11 +00:00
realpath.3
realpath.c vfs: add realpathat syscall 2020-02-20 16:58:19 +00:00
remque.c
set_constraint_handler_s.3 Provide set_constraint_handler_s(3) man page. 2018-08-19 14:39:57 +00:00
set_constraint_handler_s.c Improve standard compliance for memset_s() and abort_handler_s(). 2017-08-12 15:18:17 +00:00
strfmon.3
strfmon.c Compute the correct size of the string to move forward. 2020-09-02 20:04:26 +00:00
strtod.3
strtoimax.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
strtol.3
strtol.c Move strtold wrapper from strtol.c to its own strtold.c. This code 2018-01-31 03:05:14 +00:00
strtold.c Use standard 2-clause license where copyright is held by the FreeBSD Foundation 2018-02-02 16:47:32 +00:00
strtoll.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
strtonum.3
strtonum.c
strtoq.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
strtoul.3
strtoul.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
strtoull.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
strtoumax.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
strtouq.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
Symbol.map libc: add clearenv function 2021-11-07 16:20:15 +01:00
system.3 libc: Fix most issues reported by mandoc 2020-12-19 14:54:28 +00:00
system.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
tdelete.c
tfind.c
tsearch_path.h
tsearch.3 Follow up to r320992, properly escape the backslash so it renders properly. 2017-07-14 17:27:15 +00:00
tsearch.c
twalk.c