Skip to content

gh-126105: Fix crash in ast module, when ._fields is deleted #126115

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

Merged
merged 2 commits into from
Oct 29, 2024

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Oct 29, 2024

This PR changes the logic a bit, but it looks like a correct thing to me.
Previously we were handling NULL of fields case for remaining_fields

cpython/Python/Python-ast.c

Lines 5089 to 5098 in 9b14083

if (fields) {
numfields = PySequence_Size(fields);
if (numfields == -1) {
goto cleanup;
}
remaining_fields = PySet_New(fields);
}
else {
remaining_fields = PySet_New(NULL);
}

But, we didn't really handle the fields itself.
Further calls assume that fields cannot be NULL.

The other way of fixing this is to add a default for fields like:

    if (fields == NULL) {
        fields = PyList_New(0);
        if (fields == NULL) {
            goto cleanup;
        }
    }

In this case ast.AST(arg=1) with no _fields will produce:

DeprecationWarning: ast.AST.__init__ got an unexpected keyword argument 'arg'. Support for arbitrary keyword arguments is deprecated and will be removed in Python 3.15.

It does not seem correct to me, because _fields is a part of our API. Since AST objects are mostly internal, there are no real cases of missing _fields.

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.

I'm wondering why we have:

if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {

This could be because some AST nodes do not have a _fields attribute but then, why don't we simply change it to an eager PyObject_GetAttr?

@sobolevn
Copy link
Member Author

Docs give me more confidence: https://docs.python.org/3/library/ast.html#ast.AST._fields

Each concrete class has an attribute _fields which gives the names of all child nodes.

@sobolevn
Copy link
Member Author

I don't think that idle_tests are related to this change. But, I will take a look.

Copy link
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@Eclips4 Eclips4 added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Oct 29, 2024
@@ -884,19 +884,17 @@ def visitModule(self, mod):
Py_ssize_t i, numfields = 0;
int res = -1;
PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL;
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

A simpler fix would have been this, right?

    if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) <= 0) {

However, your fix is better.

@Eclips4 Eclips4 merged commit b2eaa75 into python:main Oct 29, 2024
47 checks passed
@miss-islington-app
Copy link

Thanks @sobolevn for the PR, and @Eclips4 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 29, 2024
pythonGH-126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
(cherry picked from commit b2eaa75)

Co-authored-by: sobolevn <[email protected]>
@miss-islington-app
Copy link

Sorry, @sobolevn and @Eclips4, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker b2eaa75b176e07730215d76d8dce4d63fb493391 3.12

@bedevere-app
Copy link

bedevere-app bot commented Oct 29, 2024

GH-126130 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 29, 2024
@Eclips4
Copy link
Member

Eclips4 commented Oct 29, 2024

I'll take care of backport to 3.12.

Eclips4 pushed a commit to Eclips4/cpython that referenced this pull request Oct 29, 2024
… deleted (pythonGH-126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
(cherry picked from commit b2eaa75)

Co-authored-by: sobolevn <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 29, 2024

GH-126132 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Oct 29, 2024
Eclips4 pushed a commit that referenced this pull request Oct 29, 2024
…ed (GH-126115) (#126130)

gh-126105: Fix crash in `ast` module, when `._fields` is deleted (GH-126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
(cherry picked from commit b2eaa75)

Co-authored-by: sobolevn <[email protected]>
@sobolevn
Copy link
Member Author

@Eclips4 thank you for your help!
@JelleZijlstra @picnixz thanks for the quick reviews!

Eclips4 added a commit that referenced this pull request Oct 29, 2024
#126132)

[3.12] gh-126105: Fix crash in `ast` module, when `._fields` is deleted (GH-126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
(cherry picked from commit b2eaa75)

Co-authored-by: sobolevn <[email protected]>
picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
python#126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
python#126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants