Skip to content

Fix dpnp.insert ignoring out-of-bounds negative indices in multi-element obj - #3041

Merged
antonwolfy merged 9 commits into
masterfrom
fix-insert-oob-negative-indices
Aug 31, 2026
Merged

Fix dpnp.insert ignoring out-of-bounds negative indices in multi-element obj#3041
antonwolfy merged 9 commits into
masterfrom
fix-insert-oob-negative-indices

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

dpnp.insert has two index paths: the singleton path (_insert_singleton_index) already validated bounds, but the multi-element array path (_insert_array_indices) normalized negative indices with indices[indices < 0] += n without any bounds check. As a result, an out-of-bounds negative index mixed with in-bounds ones — e.g. dpnp.insert([0, 1, 2], [-6, 0], [9, 8]) — silently produced a wrong result instead of raising.

This PR adds the same bounds validation NumPy introduced: if any index is < -n or > n, IndexError is raised with the standard index {i} is out of bounds for axis {axis} with size {n} message. In-bounds and out-of-bounds indices mixed in a single call now consistently raise.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@antonwolfy antonwolfy self-assigned this Aug 25, 2026
@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 25, 2026
@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev7=py314ha0e2e8e_16 ran successfully.
Passed: 1375
Failed: 0
Skipped: 7

@coveralls

coveralls commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

No base build to compare — fix-insert-oob-negative-indices into master

@github-actions

github-actions Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/index.html

@antonwolfy
antonwolfy marked this pull request as ready for review August 26, 2026 11:16
…ent obj

Backport of numpy#31782. The multi-element index path in
_insert_array_indices normalized negative indices without any bounds
check, so an out-of-bounds negative index mixed with in-bounds ones
silently produced wrong results instead of raising. Add the same
bounds validation numpy uses so any out-of-bounds index raises
IndexError, and extend the test to cover the mixed case.
Parametrize the out-of-bounds test over both numpy and dpnp and add
multi-element cases that exercise the newly added bounds check in
_insert_array_indices, including the positive (max > n) branch, plus an
ND case validating axis/size reporting for a non-zero axis.
@antonwolfy
antonwolfy force-pushed the fix-insert-oob-negative-indices branch from 68c84e6 to 97b4495 Compare August 26, 2026 17:57
Comment thread dpnp/dpnp_iface_manipulation.py Outdated
antonwolfy and others added 3 commits August 27, 2026 16:24
Move the out-of-bounds validation out of _insert_array_indices into a
shared _check_index_bounds helper called from insert(). When obj lives
on the host (Python sequence/scalar or NumPy array) the bounds are
checked with NumPy, avoiding any device sync; a slice is skipped since
it is always in bounds; only a device-array obj needs a single host
transfer, reading both extremes at once instead of two.
Comment thread dpnp/dpnp_iface_manipulation.py
antonwolfy and others added 2 commits August 31, 2026 14:28
A boolean mask obj passed as a host object (Python sequence or NumPy
array) was validated on its raw boolean values instead of the selected
positions, so an oversized mask could pick indices beyond the axis and
silently produce a wrong result. Flatnonzero the host mask before the
bounds check, matching NumPy and the device path. Add regression tests
covering host list, host NumPy, and device dpnp masks.
@vlad-perevezentsev
vlad-perevezentsev self-requested a review August 31, 2026 13:25

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM
Thank you @antonwolfy

@antonwolfy
antonwolfy merged commit 1d7c6fa into master Aug 31, 2026
120 of 128 checks passed
@antonwolfy
antonwolfy deleted the fix-insert-oob-negative-indices branch August 31, 2026 20:40
github-actions Bot added a commit that referenced this pull request Aug 31, 2026
…ent obj (#3041)

`dpnp.insert` has two index paths: the singleton path
(`_insert_singleton_index`) already validated bounds, but the
multi-element array path (`_insert_array_indices`) normalized negative
indices with `indices[indices < 0] += n` without any bounds check. As a
result, an out-of-bounds negative index mixed with in-bounds ones — e.g.
`dpnp.insert([0, 1, 2], [-6, 0], [9, 8])` — silently produced a wrong
result instead of raising.

This PR adds the same bounds validation NumPy introduced: if any index
is `< -n` or `> n`, `IndexError` is raised with the standard `index {i}
is out of bounds for axis {axis} with size {n}` message. In-bounds and
out-of-bounds indices mixed in a single call now consistently raise. 1d7c6fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants