Skip to content

BUG: Fix segfault in nditer.multi_index when __getitem__ raises (#31314)#31336

Merged
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31314
Apr 26, 2026
Merged

BUG: Fix segfault in nditer.multi_index when __getitem__ raises (#31314)#31336
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31314

Conversation

@charris
Copy link
Copy Markdown
Member

@charris charris commented Apr 26, 2026

Backport of #31314.

PR summary

Add a NULL check for PySequence_GetItem in the multi_index setter to prevent
segmentation faults when getitem raises an exception.

As reported in:
https://gist.github.com/devdanzin/ccc2d9553ca1c90ab1835362ee21a40a#reproducer-3-nditermulti_index-segfault-with-raising-sequence
(discussed in #31046)

Reproduce error:

import numpy as np

class BadSequence:
    def __len__(self):
        return 2

    def __getitem__(self, i):
        if i == 1:
            raise RuntimeError("intentional error")
        return 0



arr = np.zeros((3, 4))
it = np.nditer(arr, flags=["multi_index"])

it.multi_index = BadSequence()

Actual behavior:

Segmentation fault         (core dumped)

Expected behavior:

RuntimeError: intentional error

First time committer introduction

AI Disclosure

AI tools were used to assist with English writing and phrasing in this PR.

The technical analysis, debugging, and code changes were performed and verified manually.

@charris charris added this to the 2.4.5 release milestone Apr 26, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Apr 26, 2026
@charris
Copy link
Copy Markdown
Member Author

charris commented Apr 26, 2026

The test failures are unrelated.

@charris charris merged commit f16e67e into numpy:maintenance/2.4.x Apr 26, 2026
72 of 74 checks passed
@charris charris deleted the backport-31314 branch April 26, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants