gh-150207: Raise MemoryError on tokenizer allocation failure instead of crashing (GH-150275)
(cherry picked from commit
262625fa30e5a1b5cf33c9dbce5d2b713093c7be)
Co-authored-by: Grant Herman <grantlouisherman041@gmail.com>
--- /dev/null
+Fix a crash when a memory allocation fails during tokenizer initialization. A proper :exc:`MemoryError` is now raised instead.
struct tok_state *tok = (struct tok_state *)PyMem_Calloc(
1,
sizeof(struct tok_state));
- if (tok == NULL)
+ if (tok == NULL) {
+ PyErr_NoMemory();
return NULL;
+ }
+
tok->buf = tok->cur = tok->inp = NULL;
tok->fp_interactive = 0;
tok->interactive_src_start = NULL;
return NULL;
if ((tok->buf = (char *)PyMem_Malloc(BUFSIZ)) == NULL) {
_PyTokenizer_Free(tok);
+ PyErr_NoMemory();
return NULL;
}
tok->cur = tok->inp = tok->buf;
char* result = (char *)PyMem_Malloc(len + 1);
if (!result) {
tok->done = E_NOMEM;
+ PyErr_NoMemory();
return NULL;
}
memcpy(result, s, len);
buf = PyMem_Malloc(needed_length);
if (buf == NULL) {
tok->done = E_NOMEM;
+ PyErr_NoMemory();
return NULL;
}
for (current = buf; *s; s++, current++) {
return NULL;
if ((tok->buf = (char *)PyMem_Malloc(BUFSIZ)) == NULL) {
_PyTokenizer_Free(tok);
+ PyErr_NoMemory();
return NULL;
}
tok->cur = tok->inp = tok->buf;