|
Message-Id: <26b68deb9fc65f8331c7fedaa6d807f4d973a4e6.1684932942.git.Jens.Gustedt@inria.fr> Date: Thu, 25 May 2023 16:44:59 +0200 From: Jens Gustedt <Jens.Gustedt@...ia.fr> To: musl@...ts.openwall.com Subject: [C23 const 1/2] C23: change bsearch to a macro that respects the const contract This adds a macro interface to stdlib that has an additional cast of the return value to `void const*` for the case that the argument to the call was also const-qualified. Nothing changes for the function itself, only the identifier has to be protected with (), such that the macro does not expand for the function declaration or definition. --- include/stdlib.h | 12 +++++++++++- src/include/stdlib.h | 2 ++ src/stdlib/bsearch.c | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index 68ccd467..f5281777 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -60,7 +60,17 @@ char *getenv (const char *); int system (const char *); -void *bsearch (const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); +void * (bsearch) (const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); +#if __STDC_VERSION__ > 201112L +# define bsearch(K, B, N, S, C) \ + _Generic( \ + /* ensure conversion to a void pointer */ \ + 1 ? (B) : (void*)1, \ + void const*: (void const*)bsearch((K), (void const*)(B), (N), (S), (C)), \ + /* volatile qualification of *B is an error for this call */ \ + default: bsearch((K), (B), (N), (S), (C)) \ +) +#endif void qsort (void *, size_t, size_t, int (*)(const void *, const void *)); int abs (int); diff --git a/src/include/stdlib.h b/src/include/stdlib.h index 812b04de..f0b03df9 100644 --- a/src/include/stdlib.h +++ b/src/include/stdlib.h @@ -16,4 +16,6 @@ hidden void *__libc_calloc(size_t, size_t); hidden void *__libc_realloc(void *, size_t); hidden void __libc_free(void *); +#undef bsearch + #endif diff --git a/src/stdlib/bsearch.c b/src/stdlib/bsearch.c index fe050ea3..4f62ea37 100644 --- a/src/stdlib/bsearch.c +++ b/src/stdlib/bsearch.c @@ -1,6 +1,6 @@ #include <stdlib.h> -void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) +void *(bsearch)(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) { void *try; int sign; -- 2.34.1
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.