1
0
forked from aniani/vim

patch 9.1.0089: qsort() comparison functions should be transitive

Problem:  qsort() comparison functions should be transitive
Solution: Do not subtract values, but rather use explicit comparisons

Improve qsort() comparison functions

There has been a recent report on qsort() causing out-of-bounds read &
write in glibc for non transitive comparison functions
https://www.qualys.com/2024/01/30/qsort.txt

Even so the bug is in glibc's implementation of the qsort() algorithm,
it's bad style to just use substraction for the comparison functions,
which may cause overflow issues and as hinted at in OpenBSD's manual
page for qsort(): "It is almost always an error to use subtraction to
compute the return value of the comparison function."

So check the qsort() comparison functions and change them to be safe.

closes: #13980

Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Christian Brabandt
2024-02-09 19:39:14 +01:00
parent c9e79e5284
commit e06e437665
7 changed files with 37 additions and 13 deletions

View File

@@ -5613,7 +5613,8 @@ tv_nr_compare(const void *a1, const void *a2)
listitem_T *li1 = *(listitem_T **)a1;
listitem_T *li2 = *(listitem_T **)a2;
return li1->li_tv.vval.v_number - li2->li_tv.vval.v_number;
return li1->li_tv.vval.v_number == li2->li_tv.vval.v_number ? 0 :
li1->li_tv.vval.v_number > li2->li_tv.vval.v_number ? 1 : -1;
}
void