From b999ea3c4348e40e0fa738331a250eb6fa4e4ea8 Mon Sep 17 00:00:00 2001 From: iAbdulrrahman Date: Mon, 26 Oct 2026 14:13:29 +0300 Subject: [PATCH] fix(libc): return success for heapsort with at most one element regardless of size --- core/libs/prx/libc/src/FileAndHeap.cpp | 2 +- core/libs/tests/GuestQsort.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libc/src/FileAndHeap.cpp b/core/libs/prx/libc/src/FileAndHeap.cpp index 42c79b4255..64082b9b9a 100644 --- a/core/libs/prx/libc/src/FileAndHeap.cpp +++ b/core/libs/prx/libc/src/FileAndHeap.cpp @@ -753,11 +753,11 @@ int APS5_VABI mergesort_nid_postfix(void* base, size_t count, size_t size, Guest } int APS5_VABI heapsort_nid_postfix(void* base, size_t count, size_t size, GuestCompare compare) { + if (count <= 1) return 0; if (size == 0) { errno = 22; return -1; } - if (count <= 1) return 0; if (!compare) throw std::invalid_argument("heapsort: null comparator"); if (!base) throw std::invalid_argument("heapsort: null base"); if (count > std::numeric_limits::max() / size) throw std::overflow_error("heapsort: array size overflow"); diff --git a/core/libs/tests/GuestQsort.cpp b/core/libs/tests/GuestQsort.cpp index 53e368003e..17a380e5c7 100644 --- a/core/libs/tests/GuestQsort.cpp +++ b/core/libs/tests/GuestQsort.cpp @@ -225,6 +225,9 @@ bool CheckHeapsort() { } unsigned char bytes[3] = {3, 1, 2}; *__error_nid_postfix() = 0; + correct &= heapsort_nid_postfix(nullptr, 0, 0, nullptr) == 0; + unsigned char single = 42; + correct &= heapsort_nid_postfix(&single, 1, 0, nullptr) == 0; correct &= heapsort_nid_postfix(bytes, 3, 0, CompareFirstByte) == -1 && *__error_nid_postfix() == 22 && bytes[0] == 3; correct &= heapsort_nid_postfix(nullptr, 0, 4, CompareKeys) == 0; if (!correct) std::fprintf(stderr, "heapsort failed\n");