asort: replace old quicksort fallback with glibc 2.43 mergesort+heapsort#129
Open
nGoline wants to merge 1 commit into
Open
asort: replace old quicksort fallback with glibc 2.43 mergesort+heapsort#129nGoline wants to merge 1 commit into
nGoline wants to merge 1 commit into
Conversation
The old fallback (used when the native `qsort_r` lacks the glibc calling convention) was a copy of glibc's 2004 median-of-three quicksort. It had a subtle self-comparison bug: when the left and right scan pointers both converged on the pivot with an all-equal array, the inner loop evaluated `cmp(pivot, pivot)` before detecting `left_ptr == right_ptr` and breaking. Comparators that use pointer identity to detect distinct elements would spuriously fire on this. Replace it with glibc 2.43's stable mergesort (`heapsort` fallback on `malloc` failure). Mergesort always compares from two distinct halves and never produces self-comparisons. Portability adaptations to remove glibc-internal dependencies: - `__compar_d_fn_t` -> typedef from `_total_order_cb` - `__memswap` / `__mempcpy` -> local static inline helpers - `pthread_cleanup_*` -> removed (no thread cancellation needed) - `__set_errno` → `errno = …` - `libc_hidden_def` / `weak_alias` / `__qsort_r` -> removed / renamed to `_asort` Add tests for all-equal and single-element arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
The old fallback
_asort(a copy of glibc's 2004 median-of-three quicksort) calledcmp(a, a), the same pointer on both sides, when sorting arrays where all elements compare equal. In the inner loop, after the left and right scan pointers both stop on the pivot element, two comparisons of the formcmp(pivot, pivot)execute before theleft_ptr == right_ptrguard fires and breaks.This violates the implicit contract that comparators can assume
a != bwhen called by a sort routine, which is a reasonable expectation since the C standard says comparator behaviour for equal-valued distinct elements is unspecified but never requires handling identical pointers.The fix
Replace the old quicksort with the algorithm from glibc 2.43: a stable top-down mergesort that uses a stack buffer for small arrays and falls back to in-place heapsort if malloc fails. Mergesort always compares across two distinct sub-array halves, so self-comparisons cannot occur.
Portability
The glibc 2.43 source uses several glibc-internal headers and symbols that are not available outside glibc (
memswap.h,pthreadP.h,__compar_d_fn_t,pthread_cleanup_combined_*, etc.). This PR replaces each with a portable equivalent; the algorithm itself is unchanged.Tests
Added four new test cases:
cmp(&val, &val, NULL)explicitly and asserts it returns 0, pinning the contract that comparators passed toasortmust handle identical pointers gracefully.The
diagline reports whether a self-comparison occurred anywhere during the test run, serving as a canary if a future implementation reintroduces them.Signed-off-by: Nickolas Goline @nGoline