fix(libc): sort with FreeBSD 11.4's heapsort.c in heapsort #16861
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16861"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by mnoori11 on 2026-11-07T19:10:14Z.
What
heapsortwas a generic top-down heap sort. It now runs FreeBSD 11.4'sheapsort.c(identical inrelease/11.4.0):CREATEbuilds the heap withCOMPAR(child, child + size)andCOMPAR(child, par), andSELECTwalks the hole to a leaf without comparing, then sifts the displaced element, kept in amalloc'd scratch block, back up withCOMPAR(k, par). The sorted result is the same for a consistent comparator, but the comparator is called in a different order, a different number of times and with a pointer outside the array. A failedmallocreturns -1 as in FreeBSD. Null base or comparator and size overflow still throw; the console'sheapsortis unverified, so the debt entry says so. Same approach as #12271 (mergesort); #11629 asked forCREATEandSELECTto be ported.Tested
Linux, WSL2 Ubuntu, kernel 6.6.87.2, GCC 13.3, full build.
guest_qsortfails onmainwith the newCheckHeapsortComparisons: the 5-key case makes 12 calls in a different order, the all-equal case 9 calls (10 expected), the 16-key case 75 (64 expected), and the added allocation-failure check returns 0 where -1 is expected. It passes with this change.heapsort.cat the pinned commit compiled unmodified on the host with a recording comparator, not from this implementation. A throwaway host build ofHeapsort.cppagainst the same file made identical comparator calls, results and bytes over 200,000 random arrays (0 to 301 elements; element sizes 8, 9, 12, 16, 24).ctest: 4,347 of 4,406 pass. The 59 failures are 58agc_driver_*/agc_storage_*tests andlog_file, which fail the same way onmainon this machine (WSL2: no GPU, so Vulkan falls back to lavapipe, and the 6.6 kernel lacks asynchronous userfaultfd write protection).tools/check_conventions.py: all checks passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code