bpo-42246: Fix memory leak in compiler (GH-23256) · python/cpython@fd009e6

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