]> git.ipfire.org Git - thirdparty/glibc.git/commit
stdlib: Fix qsort memory leak if callback throws (BZ 32058)
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 27 Mar 2025 15:30:48 +0000 (12:30 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 2 Apr 2025 18:01:55 +0000 (18:01 +0000)
commitc8e73a1492b01b9b0c189d6a5c53a5a697827bae
tree50a812e61c87a41d6001a5de105ecd0fe8a7e90b
parente8514ac7aaf1bd0cf791dbdac0b2584ef3c42e98
stdlib: Fix qsort memory leak if callback throws (BZ 32058)

If the input buffer exceeds the stack auxiliary buffer, qsort will
malloc a temporary one to call mergesort.  Since C++ standard does
allow the callback comparison function to throw [1], the glibc
implementation can potentially leak memory.

The fixes uses a pthread_cleanup_combined_push and
pthread_cleanup_combined_pop, so it can work with and without
exception enables.  The qsort code path that calls malloc now
requires some extra setup and a call to __pthread_cleanup_push
anmd __pthread_cleanup_pop (which should be ok since they just
setup some buffer state).

Checked on x86_64-linux-gnu.

[1] https://timsong-cpp.github.io/cppwp/n4950/alg.c.library#4

Reviewed-by: DJ Delorie <dj@redhat.com>
stdlib/Makefile
stdlib/qsort.c
stdlib/tst-qsort4.c
stdlib/tst-qsort7.c [new file with mode: 0644]
stdlib/tst-qsortx7.c [new file with mode: 0644]
sysdeps/htl/pthreadP.h
sysdeps/mach/hurd/Makefile
sysdeps/nptl/pthreadP.h