Skip to content

Commit

Permalink
fix: overflow (oceanbase#309)
Browse files Browse the repository at this point in the history
### What problems were solved in this pull request?

Problem: `compare_int` uses subtraction to determine the size of two
integers, which may cause integer overflow and return incorrect results.

### How to reproduce

When v1 is -2147483648 and v2 is 1, return 2147483647. Returned
incorrect result due to integer overflow

### What is changed and how it works?

Use comparison operators instead of subtraction.
  • Loading branch information
my0sotis authored Oct 27, 2023
1 parent abf5ba7 commit 6d57825
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion deps/common/lang/comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ int compare_int(void *arg1, void *arg2)
{
int v1 = *(int *)arg1;
int v2 = *(int *)arg2;
return v1 - v2;
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0;
}
}

int compare_float(void *arg1, void *arg2)
Expand Down

0 comments on commit 6d57825

Please sign in to comment.