<!-- If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not the right place to seek help. Consider the following options instead: - reading the Python tutorial: https://docs.python.org/3/tutorial/ - posting in the "Users" category on discuss.python.org: https://discuss.python.org/c/users/7 - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list - searching our issue tracker (https://github.com/python/cpython/issues) to see if your problem has already been reported --> # Bug report I have questions when reading the source code of file `pegen.c`. There is a code snippet in function `_PyPegen_Parser_New`: ```c p->tokens[0] = PyMem_Calloc(1, sizeof(Token)); if (!p->tokens) { PyMem_Free(p->tokens); PyMem_Free(p); return (Parser *) PyErr_NoMemory(); } ``` I think it makes more sense to check `p->tokens[0]` but not `p->tokens` in if condition. I look at the PR #19669 where the code was introduced and no one discussed this. Is this an oversight, or am I wrong? # Your environment <!-- Include as many relevant details as possible about the environment you experienced the bug in --> - CPython versions tested on: main branch <!-- You can freely edit this text. Remove any lines you believe are unnecessary. -->
Activity
pythongh-95355: Check tokens[0] after allocating memory
arhadthedev commentedon Jul 28, 2022
@AlexWaygood It seems to be a
type-crashsincePyMem_Calloc()failure is ignored leading to a C null pointer access somewhere below.AlexWaygood commentedon Jul 28, 2022
Sounds plausible, but I'm not much of a C programmer, so I'll let somebody else do the relabelling :)
pythongh-95355: Check tokens[0] after allocating memory (pythonGH-95356)
gh-95355: Check tokens[0] after allocating memory (GH-95356)
pythongh-95355: Check tokens[0] after allocating memory (pythonGH-95356)
gh-95355: Check tokens[0] after allocating memory (GH-95356)
gh-95355: Check tokens[0] after allocating memory (GH-95356)
kumaraditya303 commentedon Jul 28, 2022
Ok done, it can indeed segfault if allocation fails, classic null dereference :)
kumaraditya303 commentedon Jul 28, 2022
Fixed by #95356, thanks for the report!