Skip to content

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

Merged
vstinner merged 3 commits intopython: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 intopython:mainfrom
hyongtao-code:fix-memory-leak

Conversation

@hyongtao-code
Copy link
Copy Markdown
Contributor

@hyongtao-code hyongtao-code commented Apr 1, 2026

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

Copy link
Copy Markdown
Contributor

@eendebakpt eendebakpt left a comment

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.

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

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:main Apr 1, 2026
57 checks passed
@vstinner
Copy link
Copy Markdown
Member

vstinner commented Apr 1, 2026

Merged, thanks for the fix.

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.

3 participants