Skip to content

Improve reference handling in encode.c - #9920

Merged
hugovk merged 4 commits into
python-pillow:mainfrom
radarhere:decref
Aug 29, 2026
Merged

Improve reference handling in encode.c#9920
hugovk merged 4 commits into
python-pillow:mainfrom
radarhere:decref

Conversation

@radarhere

Copy link
Copy Markdown
Member

No description provided.

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

There's a couple more error exits in the function that should maybe be covered?

(Should this function use the goto cleanup;/goto failed; pattern?)

Comment thread src/encode.c Outdated

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.

Should this also decref the encoder?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've pushed a commit.

Comment thread src/encode.c Outdated

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.

Should this also decref the encoder?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've pushed a commit.

Comment thread src/encode.c Outdated

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.

Should this also decref the encoder?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've pushed a commit.

@radarhere radarhere changed the title Decref encoder on failure to find packer Decref encoder Aug 29, 2026
Comment thread src/encode.c

pack = ImagingFindPacker(mode, rawmode, &bits);
if (!pack) {
Py_DECREF(encoder);

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.

Hmm, hold on... get_packer already does Py_DECREF(encoder) if ImagingFindPacker fails.

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

Given get_packer decrefs the encoder itself on error return, I think these are wrong to add. Should probably check if there are other pre-existing places that assume this should be done?

Follows up on python-pillow#8216 (GetItemRefs are new references, GetItem used to be a borrowed one)
@radarhere radarhere changed the title Decref encoder Do not decref encoder Aug 29, 2026
akx and others added 2 commits August 29, 2026 12:16
Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
@radarhere radarhere changed the title Do not decref encoder Improve reference handling in encode.c Aug 29, 2026

@akx akx 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 👍

@hugovk
hugovk merged commit e41083f into python-pillow:main Aug 29, 2026
56 of 57 checks passed
@radarhere
radarhere deleted the decref branch August 29, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants