Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit c6d7fcb

Browse files
authored
Fix memory leaks caused by type comment parsing (#91)
Echoes python/cpython#11728 (they were found by the CPython refleak detector, see https://bugs.python.org/issue35879).
1 parent 8f76cc8 commit c6d7fcb

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

ast3/Parser/parsetok.c

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

304304
if (type == TYPE_IGNORE) {
305+
PyObject_FREE(str);
305306
if (!growable_int_array_add(&type_ignores, tok->lineno)) {
306307
err_ret->error = E_NOMEM;
307308
break;

ast3/Python/ast.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,12 @@ new_identifier(const char *n, struct compiling *c)
740740
static string
741741
new_type_comment(const char *s, struct compiling *c)
742742
{
743-
return PyUnicode_DecodeUTF8(s, strlen(s), NULL);
743+
PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
744+
if (PyArena_AddPyObject(c->c_arena, res) < 0) {
745+
Py_DECREF(res);
746+
return NULL;
747+
}
748+
return res;
744749
}
745750
#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
746751

0 commit comments

Comments
 (0)