From 51970001242238a62f2286907b4c2e78973128f5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 11 Aug 2024 18:00:05 +0300 Subject: [PATCH] LibC: Fix qsort for types bigger than 64 bytes --- userspace/libraries/LibC/stdlib.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/userspace/libraries/LibC/stdlib.cpp b/userspace/libraries/LibC/stdlib.cpp index bcede8f7e7..3ed29bd695 100644 --- a/userspace/libraries/LibC/stdlib.cpp +++ b/userspace/libraries/LibC/stdlib.cpp @@ -607,15 +607,19 @@ void* bsearch(const void* key, const void* base, size_t nel, size_t width, int ( static void qsort_swap(void* lhs, void* rhs, size_t width) { + uint8_t* ulhs = static_cast(lhs); + uint8_t* urhs = static_cast(rhs); + uint8_t buffer[64]; - size_t swapped = 0; - while (swapped < width) + while (width > 0) { - const size_t to_swap = BAN::Math::min(width - swapped, sizeof(buffer)); - memcpy(buffer, lhs, to_swap); - memcpy(lhs, rhs, to_swap); - memcpy(rhs, buffer, to_swap); - swapped += to_swap; + const size_t to_swap = BAN::Math::min(width, sizeof(buffer)); + memcpy(buffer, ulhs, to_swap); + memcpy(ulhs, urhs, to_swap); + memcpy(urhs, buffer, to_swap); + width -= to_swap; + ulhs += to_swap; + urhs += to_swap; } }