[Python-checkins] bpo-35879: Fix type comment leaks (GH-11728)

Guido van Rossum webhook-mailer at python.org
Fri Feb 1 18:28:23 EST 2019


https://github.com/python/cpython/commit/d2b4c19d53f5f021fb1c7c32d48033a92ac4fe49
commit: d2b4c19d53f5f021fb1c7c32d48033a92ac4fe49
branch: master
author: Guido van Rossum <guido at python.org>
committer: GitHub <noreply at github.com>
date: 2019-02-01T15:28:13-08:00
summary:

bpo-35879: Fix type comment leaks (GH-11728)

* Fix leak for # type: ignore
* Fix the type comment leak

files:
M Parser/parsetok.c
M Python/ast.c

diff --git a/Parser/parsetok.c b/Parser/parsetok.c
index 7fddc5a0e897..1fa4a1286b77 100644
--- a/Parser/parsetok.c
+++ b/Parser/parsetok.c
@@ -330,6 +330,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
         }
 
         if (type == TYPE_IGNORE) {
+            PyObject_FREE(str);
             if (!growable_int_array_add(&type_ignores, tok->lineno)) {
                 err_ret->error = E_NOMEM;
                 break;
diff --git a/Python/ast.c b/Python/ast.c
index c422e9651ced..45578a850f77 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -699,11 +699,16 @@ ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
 */
 
 static string
-new_type_comment(const char *s)
+new_type_comment(const char *s, struct compiling *c)
 {
-  return PyUnicode_DecodeUTF8(s, strlen(s), NULL);
+    PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
+    if (PyArena_AddPyObject(c->c_arena, res) < 0) {
+        Py_DECREF(res);
+        return NULL;
+    }
+    return res;
 }
-#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n))
+#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
 
 static int
 num_stmts(const node *n)



More information about the Python-checkins mailing list