Skip to content

Commit

Permalink
Prevent potential overflow (#41)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
backink authored Mar 19, 2024
1 parent a18fdee commit dc26965
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/dudect.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ static void t_init(ttest_ctx_t *ctx) {
}
}

static int cmp(const int64_t *a, const int64_t *b) { return (int)(*a - *b); }
static int cmp(const int64_t *a, const int64_t *b) {
if (*a == *b)
return 0;
return (*a > *b) ? 1 : -1;
}

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

0 comments on commit dc26965

Please sign in to comment.