Date: Fri, 17 May 2024 12:57:25 -0400 From: Grisha Levit <grishale...@gmail.com> Message-ID: <20240517165738.8896-1-grishale...@gmail.com>
| The current cmp implementation for size and blocks subtracts the two | values and returns the difference as an int. This subtraction can | overflow, and the returned int can end up having the wrong sign. This message provides enough info about the implementation to answer the "what order if we get equality" question in my previous message ... just "whatever qsort decides to do" - which is almost always the wrong thing. | - return ((glob_sorttype < SORT_REVERSE) ? g1->st.size - g2->st.size : g2->st.size - g1->st.size); | + return (glob_sorttype < SORT_REVERSE) | + ? (g1->st.size > g2->st.size) - (g1->st.size < g2->st.size) | + : (g2->st.size > g1->st.size) - (g1->st.size < g2->st.size); Shouldn't the 2nd half of the : case have the g1 & g2 swapped as well? (and the same for the blocks comparison). kre ps: you're likely to get finger wagging by those people who believe that booleans in C should be a real type of their own, and performing arithmetic on bool results is naughty. That's not me - I don't believe bool should exist in C, things were fine without it.