Skip to content

BUG: Fix precise_xstrtod segfault on long exponent #38789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fixed regressions
- The deprecated attributes ``_AXIS_NAMES`` and ``_AXIS_NUMBERS`` of :class:`DataFrame` and :class:`Series` will no longer show up in ``dir`` or ``inspect.getmembers`` calls (:issue:`38740`)
- :meth:`to_csv` created corrupted zip files when there were more rows than ``chunksize`` (issue:`38714`)
- Bug in repr of float-like strings of an ``object`` dtype having trailing 0's truncated after the decimal (:issue:`38708`)
- Bug in :meth:`read_csv` with ``float_precision``="high" caused segfault or wrong parsing of long exponent strings (:issue:`38753`)
-

.. ---------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions pandas/_libs/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ double precise_xstrtod(const char *str, char **endptr, char decimal,
// Process string of digits.
num_digits = 0;
n = 0;
while (isdigit_ascii(*p)) {
while (num_digits < max_digits && isdigit_ascii(*p)) {
n = n * 10 + (*p - '0');
num_digits++;
p++;
Expand All @@ -1747,10 +1747,13 @@ double precise_xstrtod(const char *str, char **endptr, char decimal,
} else if (exponent > 0) {
number *= e[exponent];
} else if (exponent < -308) { // Subnormal
if (exponent < -616) // Prevent invalid array access.
if (exponent < -616) { // Prevent invalid array access.
number = 0.;
number /= e[-308 - exponent];
number /= e[308];
} else {
number /= e[-308 - exponent];
number /= e[308];
}

} else {
number /= e[-exponent];
}
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,30 @@ def test_numeric_range_too_wide(all_parsers, exp_data):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("neg_exp", [-617, -100000, -99999999999999999])
def test_very_negative_exponent(all_parsers, neg_exp):
# GH#38753
parser = all_parsers
data = f"data\n10E{neg_exp}"
for precision in parser.float_precision_choices:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can parameterize on these too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue I ran into is that the parsers don't have consistent potential float_precisions, eg PythonParser only has None. Could parameterize and just assert failure on disallowed combinations of float_precision and parser, but that seems ugly. Will try to think of a better solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some kind of fixture-within-a-fixture functionality would be nice, though not sure if that's feasible...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new fixture, all_parsers_all_precisions. Not the most elegant solution, essentially just precomputes all allowed combos and uses that as the source for the fixture. If you have a better idea please let me know!

result = parser.read_csv(StringIO(data), float_precision=precision)
expected = DataFrame({"data": [0.0]})
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("exp", [999999999999999999, -999999999999999999])
def test_too_many_exponent_digits(all_parsers, exp):
# GH#38753
parser = all_parsers
data = f"data\n10E{exp}"
for precision in parser.float_precision_choices:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

if precision == "round_trip":
continue
result = parser.read_csv(StringIO(data), float_precision=precision)
expected = DataFrame({"data": [f"10E{exp}"]})
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("iterator", [True, False])
def test_empty_with_nrows_chunksize(all_parsers, iterator):
# see gh-9535
Expand Down