Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-126105: Fix crash inast 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Eclips4 merged 2 commits intopython:mainfromsobolevn:issue-126105
Oct 29, 2024

Conversation

@sobolevn
Copy link
Member

@sobolevnsobolevn commentedOct 29, 2024
edited by bedevere-appbot
Loading

This PR changes the logic a bit, but it looks like a correct thing to me.
Previously we were handlingNULL offields case forremaining_fields

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

But, we didn't really handle thefields itself.
Further calls assume thatfields cannot beNULL.

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

if (fields==NULL) {fields=PyList_New(0);if (fields==NULL) {            gotocleanup;        }    }

In this caseast.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. SinceAST objects are mostly internal, there are no real cases of missing_fields.

Copy link
Member

@picnixzpicnixz 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 eagerPyObject_GetAttr?

@sobolevn
Copy link
MemberAuthor

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.

picnixz reacted with thumbs up emoji

@sobolevn
Copy link
MemberAuthor

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

Copy link
Member

@Eclips4Eclips4 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!

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) {

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 reacted with thumbs up emoji
@Eclips4Eclips4 merged commitb2eaa75 intopython:mainOct 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 requestOct 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 commitb2eaa75)Co-authored-by: sobolevn <mail@sobolevn.me>
@miss-islington-app
Copy link

Sorry,@sobolevn and@Eclips4, I could not cleanly backport this to3.12 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker b2eaa75b176e07730215d76d8dce4d63fb493391 3.12

@bedevere-app
Copy link

GH-126130 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelOct 29, 2024
@Eclips4
Copy link
Member

Eclips4 commentedOct 29, 2024
edited
Loading

I'll take care of backport to 3.12.

Eclips4 pushed a commit to Eclips4/cpython that referenced this pull requestOct 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 commitb2eaa75)Co-authored-by: sobolevn <mail@sobolevn.me>
@bedevere-app
Copy link

GH-126132 is a backport of this pull request to the3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelOct 29, 2024
Eclips4 pushed a commit that referenced this pull requestOct 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 commitb2eaa75)Co-authored-by: sobolevn <mail@sobolevn.me>
@sobolevn
Copy link
MemberAuthor

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

picnixz reacted with heart emoji

Eclips4 added a commit that referenced this pull requestOct 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 commitb2eaa75)Co-authored-by: sobolevn <mail@sobolevn.me>
picnixz pushed a commit to picnixz/cpython that referenced this pull requestDec 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 requestJan 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@picnixzpicnixzpicnixz left review comments

@JelleZijlstraJelleZijlstraJelleZijlstra approved these changes

@Eclips4Eclips4Eclips4 approved these changes

@isidenticalisidenticalAwaiting requested review from isidentical

Assignees

@Eclips4Eclips4

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@sobolevn@Eclips4@JelleZijlstra@picnixz

[8]ページ先頭

©2009-2025 Movatter.jp