Skip to content

Commit 4fcac0a

Browse files
Clamp comparisons to -1, 0, 1
fca9ae5 "avoid signed overflow" can still cause undefined behavior because `compare_version_number` can return `INT_MIN` which `version_compare_cb_r` then tries to negate: ``` ../../../../ext/version_sorter/version_sorter.c:94:9: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself #0 0x7f9cde1fe1ba in version_compare_cb_r /tmp/x86_64-linux-gnu/version_sorter/2.3.1/../../../../ext/version_sorter/version_sorter.c:94:9 #1 0x7f9cde75c231 (/lib/x86_64-linux-gnu/libc.so.6+0x39231) #2 0x7f9cde75c69e in qsort_r (/lib/x86_64-linux-gnu/libc.so.6+0x3969e) #3 0x7f9cde1fd68c in rb_version_sort_1 /tmp/x86_64-linux-gnu/version_sorter/2.3.1/../../../../ext/version_sorter/version_sorter.c:202:2 ```
1 parent fca9ae5 commit 4fcac0a

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

ext/version_sorter/version_sorter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ compare_version_number(const struct version_number *a,
5858

5959
if (num_a) {
6060
int64_t cmp64 = (int64_t)ca->number - (int64_t)cb->number;
61-
cmp = (int)max(INT_MIN, min(INT_MAX, cmp64));
61+
cmp = (int)max(-1, min(1, cmp64));
6262
} else {
6363
cmp = strchunk_cmp(
6464
a->original, &ca->string,

0 commit comments

Comments
 (0)