]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-150207: Raise MemoryError on tokenizer allocation failure instead of crashi...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 6 Jun 2026 02:46:55 +0000 (04:46 +0200)
committerGitHub <noreply@github.com>
Sat, 6 Jun 2026 02:46:55 +0000 (03:46 +0100)
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>
Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-21-52-38.gh-issue-150207.l2BUtI.rst [new file with mode: 0644]
Parser/lexer/state.c
Parser/tokenizer/file_tokenizer.c
Parser/tokenizer/helpers.c
Parser/tokenizer/readline_tokenizer.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-21-52-38.gh-issue-150207.l2BUtI.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-21-52-38.gh-issue-150207.l2BUtI.rst
new file mode 100644 (file)
index 0000000..12fbffc
--- /dev/null
@@ -0,0 +1 @@
+Fix a crash when a memory allocation fails during tokenizer initialization. A proper :exc:`MemoryError` is now raised instead.
index 3663dc3eb7f9f696d2f4e5c561bfc4c393b48c24..5cf9b4d768c3ebb2ecc4b07b7c950724780b234e 100644 (file)
@@ -15,8 +15,11 @@ _PyTokenizer_tok_new(void)
     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;
index 8c836a3f725829662e5ccf1e02f6ff0c4c2fa5fa..a11702557a07af305b3e924f69edfab7307627ad 100644 (file)
@@ -378,6 +378,7 @@ _PyTokenizer_FromFile(FILE *fp, const char* enc,
         return NULL;
     if ((tok->buf = (char *)PyMem_Malloc(BUFSIZ)) == NULL) {
         _PyTokenizer_Free(tok);
+        PyErr_NoMemory();
         return NULL;
     }
     tok->cur = tok->inp = tok->buf;
index 9542969ad3127b9519462ef01792a4a8a7318d03..c69e66d0ab9b7a8cad838d156b93478f29f5fb0e 100644 (file)
@@ -193,6 +193,7 @@ _PyTokenizer_new_string(const char *s, Py_ssize_t len, struct tok_state *tok)
     char* result = (char *)PyMem_Malloc(len + 1);
     if (!result) {
         tok->done = E_NOMEM;
+        PyErr_NoMemory();
         return NULL;
     }
     memcpy(result, s, len);
@@ -221,6 +222,7 @@ _PyTokenizer_translate_newlines(const char *s, int exec_input, int preserve_crlf
     buf = PyMem_Malloc(needed_length);
     if (buf == NULL) {
         tok->done = E_NOMEM;
+        PyErr_NoMemory();
         return NULL;
     }
     for (current = buf; *s; s++, current++) {
index 0f7769aeb8fd57054f685f893e6bd40218a8524a..917f7b40cfbbfed1f35ea63392280eb8ed5967d7 100644 (file)
@@ -114,6 +114,7 @@ _PyTokenizer_FromReadline(PyObject* readline, const char* enc,
         return NULL;
     if ((tok->buf = (char *)PyMem_Malloc(BUFSIZ)) == NULL) {
         _PyTokenizer_Free(tok);
+        PyErr_NoMemory();
         return NULL;
     }
     tok->cur = tok->inp = tok->buf;