summaryrefslogtreecommitdiffstats
path: root/thread.c
diff options
context:
space:
mode:
authorKevin McCarthy <kevin@8t8.us>2021-09-23 14:58:54 -0700
committerKevin McCarthy <kevin@8t8.us>2021-09-23 14:58:54 -0700
commitd0d40a33f1400269251d06c56ef331482c6b99be (patch)
treea2586f36617eb51f6aac7fc2c227d5ea6b196c14 /thread.c
parent4153ae9614739821a99b20d744c5a333ec344f73 (diff)
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.
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c11
1 files changed, 6 insertions, 5 deletions
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)