]> granicus.if.org Git - python/commitdiff
bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442)
authorPablo Galindo <Pablogsal@gmail.com>
Tue, 19 Mar 2019 17:17:58 +0000 (17:17 +0000)
committerGitHub <noreply@github.com>
Tue, 19 Mar 2019 17:17:58 +0000 (17:17 +0000)
Parser/tokenizer.c

index 8f0a9c810053a92683e2e7ca96dd4f10bd355cce..ad054975689ee1a04fd5ed1b6f2cf660b1f0290e 100644 (file)
@@ -649,9 +649,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;
 }
 
@@ -958,6 +963,7 @@ tok_nextc(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;