Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd009e6

Browse files
authored
bpo-42246: Fix memory leak in compiler (GH-23256)
* Fix potential memory leak in assembler init. * Fix reference leak when encountering error during compilation of function body.
1 parent cc75ab7 commit fd009e6

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Python/compile.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22762276
c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
22772277
c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
22782278
for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
2279-
VISIT(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
2279+
VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
22802280
}
22812281
co = assemble(c, 1);
22822282
qualname = c->u->u_qualname;
@@ -5533,18 +5533,24 @@ assemble_init(struct assembler *a, int nblocks, int firstlineno)
55335533
{
55345534
memset(a, 0, sizeof(struct assembler));
55355535
a->a_prevlineno = a->a_lineno = firstlineno;
5536+
a->a_lnotab = NULL;
55365537
a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5537-
if (!a->a_bytecode)
5538-
return 0;
5538+
if (a->a_bytecode == NULL) {
5539+
goto error;
5540+
}
55395541
a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5540-
if (!a->a_lnotab)
5541-
return 0;
5542+
if (a->a_lnotab == NULL) {
5543+
goto error;
5544+
}
55425545
if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
55435546
PyErr_NoMemory();
5544-
return 0;
5547+
goto error;
55455548
}
5546-
55475549
return 1;
5550+
error:
5551+
Py_XDECREF(a->a_bytecode);
5552+
Py_XDECREF(a->a_lnotab);
5553+
return 0;
55485554
}
55495555

55505556
static void

0 commit comments

Comments
 (0)