Skip to content

gh-151515: Fix incorrect function pointer cast in asdl_c.py generator#151514

Open
CoderSilicon wants to merge 1 commit into
python:mainfrom
CoderSilicon:patch-1
Open

gh-151515: Fix incorrect function pointer cast in asdl_c.py generator#151514
CoderSilicon wants to merge 1 commit into
python:mainfrom
CoderSilicon:patch-1

Conversation

@CoderSilicon

@CoderSilicon CoderSilicon commented Jun 15, 2026

Copy link
Copy Markdown

Summary

This PR fixes an undefined behavior bug in the C code generated by Parser/asdl_c.py for get_ast_state().

The Problem

In the generated code, _PyOnceFlag_CallOnce is invoked like this:
_PyOnceFlag_CallOnce(&state->once, (_Py_once_fn_t *)&init_types, state)

Using &init_types passes a pointer to a function pointer (effectively a double pointer), rather than the function pointer itself. by forcing this with an explicit cast (_Py_once_fn_t *) silences the compiler but triggers a strict aliasing violation and undefined behavior at runtime when the pointer is dereferenced. This can lead to compiler-optimization-driven segmentation faults, particularly in free-threaded/GIL-disabled builds where one-time initialization flags are heavily relied upon.

The Fix

Updated Parser/asdl_c.py to pass init_types directly with a standard function pointer cast (_Py_once_fn_t)init_types (removing the extra & and *). Ran make regen-ast to cleanly update the generated files.

@bedevere-app

bedevere-app Bot commented Jun 15, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

1 similar comment
@bedevere-app

bedevere-app Bot commented Jun 15, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@CoderSilicon CoderSilicon changed the title gh-00000: Fix incorrect function pointer cast in asdl_c.py generator gh-#151515: Fix incorrect function pointer cast in asdl_c.py generator Jun 15, 2026
@CoderSilicon CoderSilicon changed the title gh-#151515: Fix incorrect function pointer cast in asdl_c.py generator gh-151515: Fix incorrect function pointer cast in asdl_c.py generator Jun 15, 2026

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Plsease revert all cosmetic changes in this PR. Only change the part that may need to change.

@bedevere-app

bedevere-app Bot commented Jun 15, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Comment thread Parser/asdl_c.py Outdated
@bedevere-app

bedevere-app Bot commented Jun 16, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@CoderSilicon

Copy link
Copy Markdown
Author

I have made the requested changes and applied the requested suggestions; please review again

@chris-eibl

Copy link
Copy Markdown
Member

I don’t see how the original code produces undefined behaviour. However, why not just remove the cast?

I agree with @vadmium, I do not see undefined behaviour here and the cast could be removed.

All other occurences of _PyOnceFlag_CallOnce do not use the cast either, since their fn argument matches (like in this case).

I am unsure whether that removal is worth a PR. At best +0 from me ...

@chris-eibl

Copy link
Copy Markdown
Member

@CoderSilicon : to get CI green, you'll need to run make regen-ast again ...

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.

4 participants