Skip to content

Commit 47b87f9

Browse files
committed
fix: move errno handling to avoid polution and early return
1 parent ffcb7c2 commit 47b87f9

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

pandas/_libs/src/parser/tokenizer.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,12 @@ int64_t str_to_int64(const char *p_item, int64_t int_min, int64_t int_max,
19071907
char *endptr;
19081908
int64_t result = strtoll(p, &endptr, 10);
19091909

1910+
if (errno == ERANGE || result > int_max || result < int_min) {
1911+
*error = ERROR_OVERFLOW;
1912+
errno = 0;
1913+
return 0;
1914+
}
1915+
19101916
// Skip trailing spaces.
19111917
while (isspace_ascii(*endptr)) {
19121918
++endptr;
@@ -1915,15 +1921,10 @@ int64_t str_to_int64(const char *p_item, int64_t int_min, int64_t int_max,
19151921
// Did we use up all the characters?
19161922
if (*endptr) {
19171923
*error = ERROR_INVALID_CHARS;
1918-
result = 0;
1919-
} else if (errno == ERANGE || result > int_max || result < int_min) {
1920-
*error = ERROR_OVERFLOW;
1921-
errno = 0;
1922-
result = 0;
1923-
} else {
1924-
*error = 0;
1924+
return 0;
19251925
}
19261926

1927+
*error = 0;
19271928
return result;
19281929
}
19291930

@@ -1966,6 +1967,12 @@ uint64_t str_to_uint64(uint_state *state, const char *p_item, int64_t int_max,
19661967
char *endptr;
19671968
uint64_t result = strtoull(p, &endptr, 10);
19681969

1970+
if (errno == ERANGE || result > uint_max) {
1971+
*error = ERROR_OVERFLOW;
1972+
errno = 0;
1973+
return 0;
1974+
}
1975+
19691976
// Skip trailing spaces.
19701977
while (isspace_ascii(*endptr)) {
19711978
++endptr;
@@ -1974,18 +1981,13 @@ uint64_t str_to_uint64(uint_state *state, const char *p_item, int64_t int_max,
19741981
// Did we use up all the characters?
19751982
if (*endptr) {
19761983
*error = ERROR_INVALID_CHARS;
1977-
result = 0;
1978-
} else if (errno == ERANGE || result > uint_max) {
1979-
*error = ERROR_OVERFLOW;
1980-
errno = 0;
1981-
result = 0;
1982-
} else {
1983-
*error = 0;
1984+
return 0;
19841985
}
19851986

19861987
if (result > (uint64_t)int_max) {
19871988
state->seen_uint = 1;
19881989
}
19891990

1991+
*error = 0;
19901992
return result;
19911993
}

0 commit comments

Comments
 (0)