GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: Refactor error handling in array_setstate to prevent typecode leak by whyvineet · Pull Request #31701 · numpy/numpy

Conversation

@whyvineet

@whyvineet whyvineet commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

PR summary

This fixes the reference-count imbalance reported in #31678

AI Disclosure

No AI tools used

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@whyvineet
FIX: Refactor error handling in array_setstate to prevent typecode leak
5a23f1e
da-woods
da-woods reviewed Jun 20, 2026
View reviewed changes

@da-woods da-woods left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is a fairly complicated function so it isn't too easy to follow the flow and all the ownership. But I find it easier to reason about if you have fewer exit points and fewer paths through.

That typically means that an owned reference should either be NULL or set. References shouldn't change between being owned or unowned (like rawdata does).

So you'd have something like

PyObject *result = NULL, *typecode = NULL, *rawdata = NULL;

// ... code goes here ...

result = final_thing_that_creates_the_result();
end:
Py_XDECREF(typecode);  // either NULL, or owned, XDECREF is happy both ways
Py_XDECREF(rawdata);
return result; // either NULL or set...

and then you have one exit point and everything goes through that. Every return should go through the end (except possibly right at the start of the function before anything is owned).

This probably requires taking ownership of both typecode and rawdata with a Py_INCREF immediately after PyArg_ParseTuple.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated

@da-woods da-woods Jun 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This seems like it might still be leaking typecode

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated

@da-woods da-woods Jun 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

and here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated

@da-woods da-woods Jun 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

and here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@whyvineet

whyvineet commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

I agree @da-woods; the current goto chain does not handle the shared failure paths very well. Using a single exit point for cleanup would make ownership easier to understand.

From what I understand, the goal is to keep local references consistent, either owned or NULL, and send all error paths through a shared cleanup block with Py_XDECREF. This avoids switching between borrowed and owned references in the function.

I'll take a look at restructuring the ownership and cleanup logic along those lines.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@whyvineet
FIX: Prevent typecode and rawdata leaks in array_setstate
99dc7ca
da-woods
da-woods reviewed Jun 21, 2026
View reviewed changes

@da-woods da-woods left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

A few small comments (only one of which is a real error I think) but to me that looks much more likely to work right.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated
Comment on lines 2254 to 2260
* Direct pointer path: PyArray_SetBaseObject steals the rawdata
* reference. NULL rawdata afterward so Py_XDECREF at the end
* label does not double-decref a stolen ref.
*/
fa->mem_handler = NULL;
fa->data = datastr;
if (PyArray_SetBaseObject(self, rawdata) < 0) {

@da-woods da-woods Jun 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Most reference steeling functions (including PyArray_SetBaseObject it looks like) steal a reference on both success and failure to be consistent

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@whyvineet whyvineet Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I’ll recheck the ownership contract here... and adjust the cleanup accordingly

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated
Comment on lines +2227 to +2228
Py_DECREF(fa->descr);
fa->descr = swapped;

@da-woods da-woods Jun 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You might be better doing Py_SETREF(fa->descr, swapped) (because can in principle cause arbitrary code to execute so you're leaving it in an potentially invalid state while that happens).

I don't think it can actually happen here, but I think it's a good idea anyway

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@whyvineet whyvineet Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Okay... I’ll switch this assignment.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated
Comment on lines +2242 to +2243
Py_DECREF(fa->descr);
fa->descr = swapped;

@da-woods da-woods Jun 21, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

And here with Py_SETREF

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@whyvineet whyvineet Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Here as well...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@whyvineet
FIX: Use Py_SETREF to prevent typecode leaks in array_setstate
5d9f5a4
@whyvineet

whyvineet commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

I looked at how PyArray_SetBaseObject() is implemented in arrayobject.c. This function takes ownership of the reference and decreases it if there is a failure, so the local cleanup does not need to handle that reference after the call.

I changed the code to transfer ownership before calling PyArray_SetBaseObject() by clearing the local rawdata reference. This keeps the single-exit cleanup safe.

Could you take another look, @da-woods? Does this match what you had in mind?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@da-woods

da-woods commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

I changed the code to transfer ownership before calling PyArray_SetBaseObject() by clearing the local rawdata reference. This keeps the single-exit cleanup safe.

Could you take another look, @da-woods? Does this match what you had in mind?

Yes that looks right to me.

whyvineet reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg
seberg approved these changes Jun 23, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks @whyvineet and especially also @da-woods for reviewing. This is a bit of a dark corner, the fix is I think in a sense meaningless (you would need a bad pickle to hit it and at that point anything is possible).

(We use finish: more often than end: but I don't feel like style nitpicking, let's get it in.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@seberg
seberg merged commit 123678e into numpy:main Jun 23, 2026
82 of 85 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@seberg seberg added the 00 - Bug label Jun 23, 2026
@ngoldbaum ngoldbaum mentioned this pull request Jun 23, 2026
BUG: typecode reference leak in array_setstate on 4 error paths (methods.c) #31678
Closed
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jun 23, 2026
@charris charris changed the title FIX: Refactor error handling in array_setstate to prevent typecode leak BUG: Refactor error handling in array_setstate to prevent typecode leak Jun 23, 2026
@rgommers rgommers mentioned this pull request Jun 24, 2026
BUG: segfaults in macOS arm64 wheel builds #31733
Closed
@charris charris mentioned this pull request Jul 2, 2026
FIX: Refactor error handling in array_setstate to prevent typecode leak (#31701) #31838
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 2, 2026
charris pushed a commit that referenced this pull request Jul 3, 2026
@whyvineet @charris
FIX: Refactor error handling in array_setstate to prevent typecode le…
a800d42
…ak (#31701)
charris added a commit that referenced this pull request Jul 3, 2026
@charris
Merge pull request #31838 from charris/backport-31701
42839d9
FIX: Refactor error handling in array_setstate to prevent typecode leak (#31701)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seberg seberg seberg approved these changes
+1 more reviewer
@da-woods da-woods da-woods left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@whyvineet @da-woods @seberg @charris
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.