Skip to content

Commit

Permalink
bpo-42246: Fix memory leak in compiler (pythonGH-23256)
Browse files Browse the repository at this point in the history
* Fix potential memory leak in assembler init.

* Fix reference leak when encountering error during compilation of function body.
  • Loading branch information
markshannon authored and adorilson committed Mar 11, 2021
1 parent 441e23d commit 0e5c9d5
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Python/compile.c
Expand Up @@ -2276,7 +2276,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
VISIT(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
}
co = assemble(c, 1);
qualname = c->u->u_qualname;
Expand Down Expand Up @@ -5533,18 +5533,24 @@ assemble_init(struct assembler *a, int nblocks, int firstlineno)
{
memset(a, 0, sizeof(struct assembler));
a->a_prevlineno = a->a_lineno = firstlineno;
a->a_lnotab = NULL;
a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
if (!a->a_bytecode)
return 0;
if (a->a_bytecode == NULL) {
goto error;
}
a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
if (!a->a_lnotab)
return 0;
if (a->a_lnotab == NULL) {
goto error;
}
if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
PyErr_NoMemory();
return 0;
goto error;
}

return 1;
error:
Py_XDECREF(a->a_bytecode);
Py_XDECREF(a->a_lnotab);
return 0;
}

static void
Expand Down

0 comments on commit 0e5c9d5

Please sign in to comment.