8000 BUG: avoid negating unsigned integers in resize implementation (#29230) by charris · Pull Request #29233 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: avoid negating unsigned integers in resize implementation (#29230) #29233

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 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
BUG: avoid negating unsigned integers in resize implementation (#29230)
The negation of an unsigned int underflows and creates a large positive repeats, which leads to allocations failures and/or swapping.
  • Loading branch information
ngoldbaum authored and charris committed Jun 19, 2025
commit da9f4a58cab699654433148714b8075a28a74827
3 changes: 2 additions & 1 deletion numpy/_core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,8 @@ def resize(a, new_shape):
# First case must zero fill. The second would have repeats == 0.
return np.zeros_like(a, shape=new_shape)

repeats = -(-new_size // a.size) # ceil division
# ceiling division without negating new_size
repeats = (new_size + a.size - 1) // a.size
a = concatenate((a,) * repeats)[:new_size]

return reshape(a, new_shape)
Expand Down
7 changes: 7 additions & 0 deletions 90D4 numpy/_core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def test_negative_resize(self):
with pytest.raises(ValueError, match=r"negative"):
np.resize(A, new_shape=new_shape)

def test_unsigned_resize(self):
# ensure unsigned integer sizes don't lead to underflows
for dt_pair in [(np.int32, np.uint32), (np.int64, np.uint64)]:
arr = np.array([[23, 95], [66, 37]])
assert_array_equal(np.resize(arr, dt_pair[0](1)),
np.resize(arr, dt_pair[1](1)))

def test_subclass(self):
class MyArray(np.ndarray):
__array_priority__ = 1.
Expand Down
Loading
0