libc: Add missing object size check to qsort_s(3)

When sorting, both the C11 standard (ISO/IEC 9899:2011, K.3.6.3.2) and
the ISO/IEC JTC1 SC22 WG14 N1172 standard, does not define objects of
zero size as undefined behaviour. However Microsoft's cpp-docs does.

Add proper checks for this. Found while working on bsort(3).

Reviewed by:	kib@ and emaste@
MFC after:	1 week
Sponsored by:	NVIDIA Networking
Differential Revision:	https://reviews.freebsd.org/D39687
This commit is contained in:
Hans Petter Selasky 2023-04-19 12:22:11 +02:00
parent 7d65a450cd
commit 27bb0d337c
2 changed files with 11 additions and 3 deletions

View File

@ -32,7 +32,7 @@
.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd September 30, 2022
.Dd April 19, 2023
.Dt QSORT 3
.Os
.Sh NAME
@ -260,7 +260,7 @@ The order of arguments to
.Fa compar
is different
.It
if
If
.Fa nmemb
or
.Fa size
@ -270,7 +270,11 @@ or
.Fa nmemb
is not zero and
.Fa compar
is NULL, then the runtime-constraint handler is called, and
is
.Dv NULL
or
.Fa size
is zero, then the runtime-constraint handler is called, and
.Fn qsort_s
returns an error.
Note that the handler is called before

View File

@ -247,6 +247,10 @@ qsort_s(void *a, rsize_t n, rsize_t es, cmp_t *cmp, void *thunk)
__throw_constraint_handler_s("qsort_s : cmp == NULL",
EINVAL);
return (EINVAL);
} else if (es <= 0) {
__throw_constraint_handler_s("qsort_s : es <= 0",
EINVAL);
return (EINVAL);
}
}