Skip to content

Commit dc26965

Browse files
authored
Prevent potential overflow (#41)
Cast a 64 bit integer into 32 bit might cause overflow. When the *a is larger than *b and 31th bit of (*a - *b) is 1, casting it to an 32 bit integer will yeild a negative number, which is an unexpected result.
1 parent a18fdee commit dc26965

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/dudect.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ static void t_init(ttest_ctx_t *ctx) {
200200
}
201201
}
202202

203-
static int cmp(const int64_t *a, const int64_t *b) { return (int)(*a - *b); }
203+
static int cmp(const int64_t *a, const int64_t *b) {
204+
if (*a == *b)
205+
return 0;
206+
return (*a > *b) ? 1 : -1;
207+
}
204208

205209
static int64_t percentile(int64_t *a_sorted, double which, size_t size) {
206210
size_t array_position = (size_t)((double)size * (double)which);

0 commit comments

Comments
 (0)