Skip to content

Conversation

@priyanshu2282-cyber
Copy link
Contributor

@priyanshu2282-cyber priyanshu2282-cyber commented Jan 22, 2026

This change fixes a crash in array.fromlist() that can happen if an element’s __index__ method mutates the input list while it is being processed.
Previously, array.fromlist() assumed the list would remain unchanged during conversion. If __index__ cleared the list, the element being converted could be freed while still in use, leading to a crash. The implementation now keeps the element alive for the duration of the conversion, I have added a regression test to cover this case.

@priyanshu2282-cyber
Copy link
Contributor Author

@serhiy-storchaka I have proposed a fix to make array.fromlist() safe against reentrant __index__ calls that mutate the input list, and I have added a regresion test. Please let me know if this approach is appropriate when you have time. Thankyou!

if (!PyLong_Check(v)) {
PyObject *orig_v = v;
Py_INCREF(orig_v);
v = _PyNumber_Index(v);
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to also pass orig_v here. It's technically unnecessary, but it makes the code easier to follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, thanks for pointing it out, from next time I will try to take care of all these stuffs.

Copy link
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. I just left minor coding style suggestions.

priyanshu2282-cyber and others added 3 commits January 23, 2026 22:46
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
@priyanshu2282-cyber
Copy link
Contributor Author

Thanks @vstinner for constant suggestions and changes, your feedback is much appriciated and very helpful for me, I will stick to the format and try not to repeat same mistakes in future.

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