Skip to content

BUG: Fix out-of-bounds access in safe_sort with an empty array and non-empty codes #59489

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 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ Reshaping
^^^^^^^^^
- Bug in :func:`qcut` where values at the quantile boundaries could be incorrectly assigned (:issue:`59355`)
- Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)

Expand Down
12 changes: 1 addition & 11 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,7 @@ def safe_sort(
order2 = sorter.argsort()
if verify:
mask = (codes < -len(values)) | (codes >= len(values))
codes[mask] = 0
else:
mask = None
codes[mask] = -1
new_codes = take_nd(order2, codes, fill_value=-1)
else:
reverse_indexer = np.empty(len(sorter), dtype=int)
Expand All @@ -1540,14 +1538,6 @@ def safe_sort(
# may deal with them here without performance loss using `mode='wrap'`
new_codes = reverse_indexer.take(codes, mode="wrap")

if use_na_sentinel:
mask = codes == -1
if verify:
mask = mask | (codes < -len(values)) | (codes >= len(values))

if use_na_sentinel and mask is not None:
np.putmask(new_codes, mask, -1)

return ordered, ensure_platform_int(new_codes)


Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ def test_codes_out_of_bound(self):
tm.assert_numpy_array_equal(result, expected)
tm.assert_numpy_array_equal(result_codes, expected_codes)

@pytest.mark.parametrize("codes", [[-1, -1], [2, -1], [2, 2]])
def test_codes_empty_array_out_of_bound(self, codes):
empty_values = np.array([])
expected_codes = -np.ones_like(codes, dtype=np.intp)
_, result_codes = safe_sort(empty_values, codes)
tm.assert_numpy_array_equal(result_codes, expected_codes)

def test_mixed_integer(self):
values = np.array(["b", 1, 0, "a", 0, "b"], dtype=object)
result = safe_sort(values)
Expand Down
Loading