Skip to content

Commit d2b4c19

Browse files
authored
bpo-35879: Fix type comment leaks (GH-11728)
* Fix leak for # type: ignore * Fix the type comment leak
1 parent ac19081 commit d2b4c19

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Parser/parsetok.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
330330
}
331331

332332
if (type == TYPE_IGNORE) {
333+
PyObject_FREE(str);
333334
if (!growable_int_array_add(&type_ignores, tok->lineno)) {
334335
err_ret->error = E_NOMEM;
335336
break;

Python/ast.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,16 @@ ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
699699
*/
700700

701701
static string
702-
new_type_comment(const char *s)
702+
new_type_comment(const char *s, struct compiling *c)
703703
{
704-
return PyUnicode_DecodeUTF8(s, strlen(s), NULL);
704+
PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
705+
if (PyArena_AddPyObject(c->c_arena, res) < 0) {
706+
Py_DECREF(res);
707+
return NULL;
708+
}
709+
return res;
705710
}
706-
#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n))
711+
#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
707712

708713
static int
709714
num_stmts(const node *n)

0 commit comments

Comments
 (0)