Skip to content

Conversation

@sergey-miryanov
Copy link
Contributor

@sergey-miryanov sergey-miryanov commented Oct 13, 2025

Following code should decref key and value if PyTuple_New fails:

cpython/Objects/dictobject.c

Lines 5720 to 5726 in 6481539

else {
result = PyTuple_New(2);
if (result == NULL)
return NULL;
PyTuple_SET_ITEM(result, 0, key);
PyTuple_SET_ITEM(result, 1, value);
}

@sergey-miryanov
Copy link
Contributor Author

I believe this should skip news.
Should be backported to 3.13 and 3.14.

@picnixz
Copy link
Member

picnixz commented Oct 13, 2025

@eendebakpt Couldn't we actually use the PyTuple_FromArray API here? If so, I would suggest fixing the refleak as part of the change (I don't know if we gain something)

@eendebakpt
Copy link
Contributor

eendebakpt commented Oct 14, 2025

@eendebakpt Couldn't we actually use the PyTuple_FromArray API here? If so, I would suggest fixing the refleak as part of the change (I don't know if we gain something)

The code here steals the references. When using the PyPuple_FromArray we would have additional increfs/decrefs. The PyTuple_MakePairSteal would work though.

Update: If this is to be backported, I prefer to use a direct approach (as in this PR). Conversion to PyTuple_MakePairSteal can be a followup PR.

@eendebakpt eendebakpt added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Oct 14, 2025
@kumaraditya303 kumaraditya303 merged commit ded59f7 into python:main Oct 14, 2025
59 checks passed
@miss-islington-app
Copy link

Thanks @sergey-miryanov for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 14, 2025
…ter_iternextitem` (pythonGH-140059)

(cherry picked from commit ded59f7)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 14, 2025
…ter_iternextitem` (pythonGH-140059)

(cherry picked from commit ded59f7)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Oct 14, 2025

GH-140107 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Oct 14, 2025
@bedevere-app
Copy link

bedevere-app bot commented Oct 14, 2025

GH-140108 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 14, 2025
kumaraditya303 pushed a commit that referenced this pull request Oct 14, 2025
…iter_iternextitem` (GH-140059) (#140108)

GH-140058: Clear key and value if `PyTuple_New` fails in `dictiter_iternextitem` (GH-140059)
(cherry picked from commit ded59f7)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
kumaraditya303 pushed a commit that referenced this pull request Oct 14, 2025
…iter_iternextitem` (GH-140059) (#140107)

GH-140058: Clear key and value if `PyTuple_New` fails in `dictiter_iternextitem` (GH-140059)
(cherry picked from commit ded59f7)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
@sergey-miryanov
Copy link
Contributor Author

Thanks all!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants