From d0d40a33f1400269251d06c56ef331482c6b99be Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Thu, 23 Sep 2021 14:58:54 -0700 Subject: Don't use subtraction for qsort numeric value comparisons. Subtraction can overflow, resulting in incorrect sorts. This is especially a concern for the date and size sorting, whose fields are greater than an 'int' size. The index values should be okay, but it's better to be consistent and avoid any possible issues. Define a macro, mutt_numeric_cmp(), that uses direct comparison rather than subtraction. --- thread.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'thread.c') diff --git a/thread.c b/thread.c index 19866aca..ddd1c7c1 100644 --- a/thread.c +++ b/thread.c @@ -594,7 +594,8 @@ static int compare_aux_threads (const void *a, const void *b) if (rc) return (SortAux & SORT_REVERSE) ? -rc : rc; - rc = (*((THREAD **)a))->sort_aux_key->index - (*((THREAD **)b))->sort_aux_key->index; + rc = mutt_numeric_cmp ((*((THREAD **)a))->sort_aux_key->index, + (*((THREAD **)b))->sort_aux_key->index); if (rc) return (SortAux & SORT_REVERSE) ? -rc : rc; @@ -622,7 +623,7 @@ static int compare_aux_sortkeys (const void *a, const void *b) if (rc) return rc; - return (*((HEADER **)a))->index - (*((HEADER **)b))->index; + return mutt_numeric_cmp ((*((HEADER **)a))->index, (*((HEADER **)b))->index); } static int compare_root_threads (const void *a, const void *b) @@ -652,8 +653,8 @@ static int compare_root_threads (const void *a, const void *b) if (rc) return reverse ? -rc : rc; - rc = (*((THREAD **)a))->sort_group_key->index - - (*((THREAD **)b))->sort_group_key->index; + rc = mutt_numeric_cmp ((*((THREAD **)a))->sort_group_key->index, + (*((THREAD **)b))->sort_group_key->index); if (rc) return reverse ? -rc : rc; @@ -686,7 +687,7 @@ static int compare_group_sortkeys (const void *a, const void *b) if (rc) return rc; - return (*((HEADER **)a))->index - (*((HEADER **)b))->index; + return mutt_numeric_cmp ((*((HEADER **)a))->index, (*((HEADER **)b))->index); } THREAD *mutt_sort_subthreads (THREAD *thread, int init) -- cgit v1.2.3