Skip to content

gh-147960: Avoid memory leak in _tuple_shared() on allocation failure - #147961

Merged
vstinner merged 3 commits into
python:mainfrom
hyongtao-code:fix-memory-leak
Apr 1, 2026
Merged

gh-147960: Avoid memory leak in _tuple_shared() on allocation failure#147961
vstinner merged 3 commits into
python:mainfrom
hyongtao-code:fix-memory-leak

Conversation

@hyongtao-code

@hyongtao-codehyongtao-code commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Fix a memory leak in _tuple_shared() when allocating the items array fails.

If PyMem_Calloc() for shared->items fails, the previously allocated
shared structure was not freed before returning, resulting in a leak.

Add the missing PyMem_RawFree(shared) on the error path.

Signed-off-by: Yongtao Huang yongtaoh2022@gmail.com

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

We do not always add a news entry for leaks in the failure paths, but the fix seems good.

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@vstinner
vstinner merged commit 4810bed into python:mainApr 1, 2026
57 checks passed
@vstinner

Copy link
Copy Markdown
Member

Merged, thanks for the fix.

clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@hyongtao-code
hyongtao-code deleted the fix-memory-leak branch May 19, 2026 13:59
Sign up for freeto 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

@hyongtao-code@vstinner@eendebakpt