Skip to content

BUG: fix lifetime issues in object dtype operations - #33596

Open
ngoldbaum wants to merge 1 commit into
numpy:mainfrom
ngoldbaum:fix-object-references
Open

ngoldbaum wants to merge 1 commit into
numpy:mainfrom
ngoldbaum:fix-object-references

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

PR summary

Fixes #33594 (and a lot more!). @nevercodecorrect I'd appreciate your opinion on this. Also ping @vstinner in case this piques your interest.

This fixes systemic lifetime issues in object operations. See #33594 for details, but in short see the docs for Py_SETREF which describe the problem. Decrefing can execute Python code, and if that code reads or assigns to the array element while it still points at the object being destroyed, we get UB. So we need to store the new value in the slot before releasing the old one.

I can't use Py_SETREF everywhere because we support unaligned object fields in structured dtypes and SETREF needs aligned pointers.

AI Disclosure

I worked on this with an AI model.

@ngoldbaum ngoldbaum added this to the 2.7.0 Release milestone Dec 5, 2026
@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Dec 5, 2026
Py_XDECREF(src_ref);
memset(src, 0, sizeof(PyObject *));
Py_XDECREF(src_ref);
NPY_DT_DBG_REFTRACE("dec src ref (cast object -> not object)", src_ref);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since we decref src_ref on line 692, should we move the debug log on line 693 up? It can read a freed object.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 09 - Backport-Candidate PRs tagged should be backported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Heap use-after-free / double-finalization in OBJECT_setitem() when __del__ re-entrantly assigns to the same object-array slot

2 participants