Skip to content

Fix incorrect Py_DECREF call on stolen reference after PyList_SetItem failure - #19720

Merged
pllim merged 5 commits into
astropy:mainfrom
wr-web:fix-reference-counting-behavior
May 11, 2026
Merged

pllim merged 5 commits into
astropy:mainfrom
wr-web:fix-reference-counting-behavior

Conversation

@wr-web

@wr-web wr-web commented May 9, 2026

Copy link
Copy Markdown
Contributor

Close #19718

@wr-web
wr-web requested a review from mcara as a code owner May 9, 2026 16:20
@github-actions github-actions Bot added the wcs label May 9, 2026
@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

Comment thread docs/changes/wcs/19720.bugfix.rst Resolved
@pllim pllim added this to the v7.2.1 milestone May 9, 2026
@pllim pllim added Bug backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x labels May 9, 2026
Comment thread docs/changes/wcs/19720.bugfix.rst Resolved
Comment thread docs/changes/wcs/19720.bugfix.rst Outdated
Comment on lines +1 to +3
The issue occurs in multiple locations in `pyutil.c` and `wcslib_wrap.c`, where the error-handling block erroneously calls `Py_DECREF` on an item whose reference has already been stolen and decremented by the failed `PyList_SetItem` call.

Fixed a bug where a reference to an item is incorrectly decremented after a failed `PyList_SetItem` call, causing a potential use-after-free or double-decrement crash because `PyList_SetItem` always steals the reference even on failure.

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.

or like this:

Suggested change
The issue occurs in multiple locations in `pyutil.c` and `wcslib_wrap.c`, where the error-handling block erroneously calls `Py_DECREF` on an item whose reference has already been stolen and decremented by the failed `PyList_SetItem` call.
Fixed a bug where a reference to an item is incorrectly decremented after a failed `PyList_SetItem` call, causing a potential use-after-free or double-decrement crash because `PyList_SetItem` always steals the reference even on failure.
Fix ``PyList_SetItem`` error paths to avoid use-after-free from decref of stolen references

@wr-web
wr-web requested review from mcara and pllim May 10, 2026 09:19
@pllim

pllim commented May 11, 2026

Copy link
Copy Markdown
Member

@mcara does this look good to you now? Thanks!

@mcara mcara 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.

I thought I have already approved it - I was wrong. Thanks for the fix!

@pllim
pllim merged commit b0d3571 into astropy:main May 11, 2026
34 checks passed
pllim pushed a commit that referenced this pull request May 11, 2026
pllim added a commit that referenced this pull request May 11, 2026
…e after `PyList_SetItem` failure (#19723)

Co-authored-by: Wang Rui <[email protected]>
pllim pushed a commit that referenced this pull request May 11, 2026
pllim added a commit that referenced this pull request May 11, 2026
…e after `PyList_SetItem` failure (#19724)

Co-authored-by: Wang Rui <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x Bug wcs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reference Counting (Use-After-Free) Bugs for PyList_SetItem

3 participants