Skip to content

Commit 5afeb11

Browse files
committed
refactor: use overflow check from numpy
1 parent ef82cf4 commit 5afeb11

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

pandas/_libs/src/parser/tokenizer.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ GitHub. See Python Software Foundation License and BSD licenses for these.
1818
*/
1919
#include "pandas/parser/tokenizer.h"
2020
#include "pandas/portable.h"
21+
#include "pandas/vendored/numpy/datetime/np_datetime.h"
2122

2223
#include <ctype.h>
2324
#include <float.h>
@@ -1898,29 +1899,6 @@ static int power_int(int base, int exponent) {
18981899
return result * base;
18991900
}
19001901

1901-
static inline int64_t add_int_check_overflow(int64_t lhs, int64_t rhs,
1902-
int64_t mul_lhs) {
1903-
// rhs will always be positive, because this function
1904-
// only executes after the first parse, hence the sign will always go to lhs.
1905-
// if lhs > 0:
1906-
// Will overflow if (mul_lhs * lhs) + rhs > INT_MAX
1907-
// iff lhs > (INT_MAX - rhs) / mul_lhs
1908-
// if lhs < 0:
1909-
// Will underflow if (mul_lhs * lhs) - rhs < INT_MIN
1910-
// iff lhs < (INT_MIN + rhs) / mul_lhs
1911-
if (lhs >= 0) {
1912-
if (lhs > (INT_MAX - rhs) / mul_lhs) {
1913-
errno = ERANGE;
1914-
}
1915-
} else {
1916-
if (lhs < (INT_MIN + rhs) / mul_lhs) {
1917-
errno = ERANGE;
1918-
}
1919-
rhs = -rhs;
1920-
}
1921-
return lhs * mul_lhs + rhs;
1922-
}
1923-
19241902
static inline uint64_t add_uint_check_overflow(uint64_t lhs, uint64_t rhs,
19251903
uint64_t mul_lhs) {
19261904
if (lhs > (UINT_MAX - rhs) / mul_lhs) {
@@ -1959,7 +1937,16 @@ int64_t str_to_int64(const char *p_item, int64_t int_min, int64_t int_max,
19591937
int64_t next_part = strtoll(endptr, &new_end, 10);
19601938
ptrdiff_t digits = new_end - endptr;
19611939
int64_t mul_result = power_int(10, (int)digits);
1962-
result = add_int_check_overflow(result, next_part, mul_result);
1940+
// result * mul_result
1941+
if (checked_int64_mul(result, mul_result, &result)) {
1942+
// overflow
1943+
errno = ERANGE;
1944+
}
1945+
// result + next_part
1946+
if (checked_int64_add(result, next_part, &result)) {
1947+
// overflow
1948+
errno = ERANGE;
1949+
}
19631950
endptr = new_end;
19641951
}
19651952

0 commit comments

Comments
 (0)