Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 3 Sep 2018 18:53:16 -0400
From: Rich Felker <>
To: Balazs Kezes <>
Subject: Re: qsort_r or qsort_s in musl

On Mon, Sep 03, 2018 at 09:57:05PM +0100, Balazs Kezes wrote:
> Hi!
> I can find a lot of discussions on the web around qsort_r and the pain that musl
> lacks it but I can't find any official word on this from musl devs. If there is
> one, could src/stdlib/qsort.c contain a pointer to it?
> Are there any plans having one of them in musl? I'd prefer qsort_r since that
> would provide greater compatibility with glibc. I even found patches for it:
> qsort_r differs between bsd/osx and glibc though:
> The argument for qsort_s is that it is in the C11 standard as an optional
> feature and has similar interface as glibc's qsort_r.
> To avoid choosing sides it could be even qsort_musl for all I care. I could then
> use preprocessor to choose the right version. I know there are many workarounds:
> global variables, thread local variables, copy pasting and changing qsort from
> musl in my own source tree, using glibc. None of them feel right.
> Any thoughts?

I think it's been discussed several times before, probably on the
list, but I can summarize the state of the topic as far as I'm aware:

qsort_s, as part of Annex K, is pretty much rejected as long as it's
neither a mandatory part of C, nor widely used by applications. If we
were to implement it, it should conform to the standard function by
that name, which would entail doing lots of wrong things like
introducing "runtime constraint handler" as global state. It's ironic
that the function whose purported purpose is being thread-safe with
regard to context ends up introducing mechanisms that make it
fundamentally thread-unsafe.

qsort_r was at first rejected because of the conflicting definitions
-- existence of same-named interfaces with different semantics or
signatures is one of the big criteria for exclusion of nonstandard
extensions in musl. However, from the FreeBSD side at least there
seems to be interest in dropping their version and agreeing upon a
standard aligned with glibc's version, for the sake of POSIX:

I'm not aware of any further progress on the issue, but if it becomes
clear that POSIX is either going to standardize a version that agree
with the GNU definition, or commit to not standardizing any that
conflict, I think the level of consensus we have so far is sufficient
to consider doing it.

In the mean time, you can always implement a thin wrapper defining
qsort_r in terms of qsort, using thread-local storage for the context


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.