bpo-35879: Fix type comment leaks (GH-11728) · python/cpython@d2b4c19

2 files changed

lines changed

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;

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)