From 58136c7ec41806ecb2d9f61cdc261ed308cda20a Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 13 Nov 2020 10:54:02 +0000 Subject: [PATCH 1/2] Fix potential memory leak in assemnbler init. --- Python/compile.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 5a0292646b5c65..093cb01ae24416 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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 From 55d9d2b90f2208e86448ce68665f2da73ac1aab9 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 13 Nov 2020 11:51:10 +0000 Subject: [PATCH 2/2] Fix reference leak when encountering error during compilation of function body. --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index 093cb01ae24416..c2fcf096fbad44 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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;