From 283fd585d8cb4187962822589cd1e28d3cc95e08 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Tue, 4 Feb 2025 10:44:36 +0100 Subject: [PATCH 1/2] fix `optimize_if_const_subscr` refleaks --- Python/flowgraph.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 9ca7fadb8d7665..1bbbd3f1df8359 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1507,6 +1507,8 @@ optimize_if_const_subscr(basicblock *bb, int n, PyObject *consts, PyObject *cons return ERROR; } PyObject *newconst = PyObject_GetItem(o, key); + Py_DECREF(o); + Py_DECREF(key); if (newconst == NULL) { if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) { return ERROR; From 224c7a68e9815f39cb8173f82d662b1ca4e0e809 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Tue, 4 Feb 2025 11:14:59 +0100 Subject: [PATCH 2/2] decref even if fail --- Python/flowgraph.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 1bbbd3f1df8359..95ab53ce64301c 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1500,11 +1500,11 @@ optimize_if_const_subscr(basicblock *bb, int n, PyObject *consts, PyObject *cons if (!find_load_const_pair(bb, n-1, &arg, &idx)) { return SUCCESS; } - PyObject *o, *key; + PyObject *o = NULL, *key = NULL; if ((o = get_const_value(arg->i_opcode, arg->i_oparg, consts)) == NULL || (key = get_const_value(idx->i_opcode, idx->i_oparg, consts)) == NULL) { - return ERROR; + goto error; } PyObject *newconst = PyObject_GetItem(o, key); Py_DECREF(o); @@ -1522,6 +1522,10 @@ optimize_if_const_subscr(basicblock *bb, int n, PyObject *consts, PyObject *cons INSTR_SET_OP0(arg, NOP); INSTR_SET_OP0(idx, NOP); return SUCCESS; +error: + Py_XDECREF(o); + Py_XDECREF(key); + return ERROR; } #define VISITED (-1)