Skip to content

Commit fef71b0

Browse files
committed
Fix integer overflow in comparison function
The comparison function used in qsort() returns *a - *b, which can cause integer overflow when the difference exceeds the range of int. This leads to incorrect sorting results when execution time measurements exceed approximately 2.1 billion cycles, which is possible in certain cases. Fix this by returning -1, 0, or 1 explicitly based on the comparison of *a and *b, avoiding overflow while maintaining correct sorting behavior. Change-Id: Ie6b237417d302632f625ded4c19d204fce9a4caa
1 parent a5b3a1b commit fef71b0

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

dudect/fixture.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ static int64_t percentile(const int64_t *a_sorted, double which, size_t size)
6969

7070
static int cmp(const int64_t *a, const int64_t *b)
7171
{
72-
return *a - *b;
72+
if (*a == *b)
73+
return 0;
74+
return (*a - *b) > 0 ? 1 : -1;
7375
}
7476

7577
/* This function is used to set different thresholds for cropping measurements.

0 commit comments

Comments
 (0)