Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-131311: Fix memory leak in PyCStructUnionType_update_stginfo #131312

Conversation

sergey-miryanov
Copy link
Contributor

@sergey-miryanov sergey-miryanov commented Mar 15, 2025

Fix memory leak in PyCStructUnionType_update_stginfo on fail path.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

In order to locate the error label I needed to expand the entire file. This shows that this function should be probably splitted into multiple steps if possible, or that the if () at the end should be separate.

WDYT @encukou? any reason to keep a single large function?

@sergey-miryanov
Copy link
Contributor Author

@picnixz Thanks for explanation.

I will try to explain further issues better. About this function - I fully agree that it has large complexity, and it is better to split it.

@encukou
Copy link
Member

encukou commented Mar 17, 2025

Thank you for the fix!

Since the function has the error label for cleanup, could you instead make layout_func use that?
(Declare it at the beginning with = NULL, XDECREF it in error, and Py_CLEAR instead of Py_DECREF in the main code path?)

(If you want to go further, you can do the same for type_block, and any other similar local.)


this function should be probably split into multiple steps if possible, or the if () at the end should be separate

Yes (though I don't think it's a priority).
We could do with a helper that combines PyObject_GetAttr + PyLong_AsSsize_t.
For format_spec, I think it might be worth a try to make StgInfo.format a Python string. (A bigger refactoring, but it seems PyUnicodeWriter would be a win over juggling strcats.)
The if can be split out of course.

@sergey-miryanov
Copy link
Contributor Author

OK, will move cleanup to error section.


Would you like to me to open a new PR to split this function and maybe another one for StgInfo.format?

@sergey-miryanov
Copy link
Contributor Author

I moved cleanup of locals to the error section except of type_block. I prefer to extract this

if (arrays_seen && (total_size <= MAX_STRUCT_SIZE)) {
to other function and do cleanup of type_block there. Should I do it in this PR or add new one?

@encukou encukou merged commit 812074e into python:main Mar 18, 2025
41 checks passed
@encukou
Copy link
Member

encukou commented Mar 18, 2025

Thank you for the fix!
Please open a new PR for the follow-ups.

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