mirror of
https://github.com/neovim/neovim.git
synced 2025-10-16 14:56:08 +00:00
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: vim/vim#13980
e06e437665
Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
@@ -3436,7 +3436,11 @@ static int fuzzy_match_str_compare(const void *const s1, const void *const s2)
|
||||
const int idx1 = ((fuzmatch_str_T *)s1)->idx;
|
||||
const int idx2 = ((fuzmatch_str_T *)s2)->idx;
|
||||
|
||||
return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
|
||||
if (v1 == v2) {
|
||||
return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
|
||||
} else {
|
||||
return v1 > v2 ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Sort fuzzy matches by score
|
||||
@@ -3465,7 +3469,11 @@ static int fuzzy_match_func_compare(const void *const s1, const void *const s2)
|
||||
if (*str1 == '<' && *str2 != '<') {
|
||||
return 1;
|
||||
}
|
||||
return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
|
||||
if (v1 == v2) {
|
||||
return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
|
||||
} else {
|
||||
return v1 > v2 ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Sort fuzzy matches of function names by score.
|
||||
|
Reference in New Issue
Block a user