libc: Implement bsearch_s(), document bsearch_b(), and add unit tests

- Implement bsearch_s() as per §K.3.6.3.2 in C23, first specified
  in C11.  It behaves identically to bsearch(), except the callback
  is called with a third argument, context, which is passed through
  from the caller, and it also performs runtime constraint checking
  on its arguments.
- Document bsearch_b(), bsearch_s(), and add history section
- Add rudimentary unit tests for bsearch(), bsearch_b(), and bsearch_s()

Reviewed by:	dteske, fuz
Approved by:    dteske (mentor), fuz (mentor)
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D58876
This commit is contained in:
Faraz Vahedi committed 2026-08-22 18:31:27 +03:30
1 parent b5c1ab6c8f
commit d59c7ea270
11 files changed
+503 -6

No files matched your search

+10
View File
@@ -411,6 +411,9 @@ _Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
/* K3.6.1.3 */
void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
/* K.3.6.3.2 */
void *bsearch_s(const void *, const void *, rsize_t, rsize_t,
int (*)(const void *, const void *, void *), void *);
/* K.3.6.3.3 */
errno_t qsort_s(void *, rsize_t, rsize_t,
int (*)(const void *, const void *, void *), void *);
#endif /* __EXT1_VISIBLE */
@@ -428,6 +431,13 @@ __NULLABILITY_PRAGMA_POP
(const void *)(bsearch_b)((key), (base), (nmemb), (size), (compar)), \
(bsearch_b)((key), (base), (nmemb), (size), (compar)))
#endif
#if __EXT1_VISIBLE
#define bsearch_s(key, base, nmemb, size, compar, context) \
__qualsel((base), \
(const void *)(bsearch_s)((key), (base), (nmemb), (size), \
(compar), (context)), \
(bsearch_s)((key), (base), (nmemb), (size), (compar), (context)))
#endif
#endif
#endif /* !_STDLIB_H_ */