gh-151515: Remove redundant cast in asdl_c.py generator by CoderSilicon · Pull Request #151514 · python/cpython
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.