]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442) (GH-12470)
authorVictor Stinner <vstinner@redhat.com>
Wed, 20 Mar 2019 12:03:41 +0000 (13:03 +0100)
committerGitHub <noreply@github.com>
Wed, 20 Mar 2019 12:03:41 +0000 (13:03 +0100)
(cherry picked from commit cb90c89de14aab636739b3e810cf949e47b54a0c)

Parser/tokenizer.c

index c6e61df533e248c6ab98d7b3f64e15e3174e2ce4..6d7869c88e22feb2026aa0b40b1f274156528b54 100644 (file)
@@ -656,9 +656,14 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
     }
     *current = '\0';
     final_length = current - buf + 1;
-    if (final_length < needed_length && final_length)
+    if (final_length < needed_length && final_length) {
         /* should never fail */
-        buf = PyMem_REALLOC(buf, final_length);
+        char* result = PyMem_REALLOC(buf, final_length);
+        if (result == NULL) {
+            PyMem_FREE(buf);
+        }
+        buf = result;
+    }
     return buf;
 }
 
@@ -974,6 +979,7 @@ tok_nextc(register struct tok_state *tok)
                 newbuf = (char *)PyMem_REALLOC(newbuf,
                                                newsize);
                 if (newbuf == NULL) {
+                    PyMem_FREE(tok->buf);
                     tok->done = E_NOMEM;
                     tok->cur = tok->inp;
                     return EOF;