git: ee5121192a77 - stable/13 - libc: Add missing object size check to qsort_s(3)
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 30 Apr 2023 06:58:21 UTC
The branch stable/13 has been updated by hselasky:
URL: https://cgit.FreeBSD.org/src/commit/?id=ee5121192a77cb5a713915890d6a6a2cfeeaf1a9
commit ee5121192a77cb5a713915890d6a6a2cfeeaf1a9
Author: Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2023-04-19 10:22:11 +0000
Commit: Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2023-04-30 06:56:20 +0000
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@
Sponsored by: NVIDIA Networking
Differential Revision: https://reviews.freebsd.org/D39687
(cherry picked from commit 27bb0d337c0d82a1a4f310315840236eb239963c)
---
lib/libc/stdlib/qsort.3 | 10 +++++++---
lib/libc/stdlib/qsort.c | 4 ++++
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/lib/libc/stdlib/qsort.3 b/lib/libc/stdlib/qsort.3
index 606185f9baee..dc14a4981fb7 100644
--- a/lib/libc/stdlib/qsort.3
+++ b/lib/libc/stdlib/qsort.3
@@ -32,7 +32,7 @@
.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
-.Dd January 20, 2020
+.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
diff --git a/lib/libc/stdlib/qsort.c b/lib/libc/stdlib/qsort.c
index 410118c5cd70..98f7d02eb099 100644
--- a/lib/libc/stdlib/qsort.c
+++ b/lib/libc/stdlib/qsort.c
@@ -231,6 +231,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);
}
}