From ae18ce8a3d2b16d36062adbfb026ad45c5e181c9 Mon Sep 17 00:00:00 2001 From: Sergey Muraviov Date: Wed, 21 May 2025 14:08:51 +0300 Subject: [PATCH 1/5] Fix dangerous reference count decrement --- Python/instrumentation.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 13bdd041becd69..d1fa1eb0193273 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2559,19 +2559,19 @@ PyObject *_Py_CreateMonitoringObject(void) if (err) goto error; PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID); err = PyObject_SetAttrString(mod, "DEBUGGER_ID", val); - Py_DECREF(val); + Py_XDECREF(val); if (err) goto error; val = PyLong_FromLong(PY_MONITORING_COVERAGE_ID); err = PyObject_SetAttrString(mod, "COVERAGE_ID", val); - Py_DECREF(val); + Py_XDECREF(val); if (err) goto error; val = PyLong_FromLong(PY_MONITORING_PROFILER_ID); err = PyObject_SetAttrString(mod, "PROFILER_ID", val); - Py_DECREF(val); + Py_XDECREF(val); if (err) goto error; val = PyLong_FromLong(PY_MONITORING_OPTIMIZER_ID); err = PyObject_SetAttrString(mod, "OPTIMIZER_ID", val); - Py_DECREF(val); + Py_XDECREF(val); if (err) goto error; return mod; error: From 4429178da24e81355a5707fcd380a083ead8ab93 Mon Sep 17 00:00:00 2001 From: Sergey Muraviov Date: Wed, 21 May 2025 15:18:28 +0300 Subject: [PATCH 2/5] Since PyLong_From Long(PY_MONITORING_DEBUGGER_ID) falls to small_int case and can't return NULL. Added assert for extra confidence. https://github.com/python/cpython/issues/134411#issuecomment-2897653868 --- Python/instrumentation.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index d1fa1eb0193273..5bb4dba2391799 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2558,20 +2558,24 @@ PyObject *_Py_CreateMonitoringObject(void) err = PyObject_SetAttrString(events, "NO_EVENTS", _PyLong_GetZero()); if (err) goto error; PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID); + assert(val != NULL); err = PyObject_SetAttrString(mod, "DEBUGGER_ID", val); - Py_XDECREF(val); + Py_DECREF(val); if (err) goto error; val = PyLong_FromLong(PY_MONITORING_COVERAGE_ID); + assert(val != NULL); err = PyObject_SetAttrString(mod, "COVERAGE_ID", val); - Py_XDECREF(val); + Py_DECREF(val); if (err) goto error; val = PyLong_FromLong(PY_MONITORING_PROFILER_ID); + assert(val != NULL); err = PyObject_SetAttrString(mod, "PROFILER_ID", val); - Py_XDECREF(val); + Py_DECREF(val); if (err) goto error; val = PyLong_FromLong(PY_MONITORING_OPTIMIZER_ID); + assert(val != NULL); err = PyObject_SetAttrString(mod, "OPTIMIZER_ID", val); - Py_XDECREF(val); + Py_DECREF(val); if (err) goto error; return mod; error: From bea99f9bd2144ceaaf2f05ddf10a3b3741c5f2b8 Mon Sep 17 00:00:00 2001 From: Sergey Muraviov Date: Thu, 22 May 2025 09:31:03 +0300 Subject: [PATCH 3/5] Fix description added. --- Python/instrumentation.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 5bb4dba2391799..fc51abdef73dc0 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2558,7 +2558,9 @@ PyObject *_Py_CreateMonitoringObject(void) err = PyObject_SetAttrString(events, "NO_EVENTS", _PyLong_GetZero()); if (err) goto error; PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID); - assert(val != NULL); + assert(val != NULL); /* Regression test for PyLong_FromLong which can return + NULL in the general case. But now it can't return + NULL for arguments of small int type. */ err = PyObject_SetAttrString(mod, "DEBUGGER_ID", val); Py_DECREF(val); if (err) goto error; From 591c6e42d4e949874ace3a05e69b264593c3de87 Mon Sep 17 00:00:00 2001 From: Sergey Muraviov Date: Thu, 22 May 2025 11:07:37 +0300 Subject: [PATCH 4/5] Fix error from lint --- Python/instrumentation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index fc51abdef73dc0..c37256ef8be8a7 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2559,7 +2559,7 @@ PyObject *_Py_CreateMonitoringObject(void) if (err) goto error; PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID); assert(val != NULL); /* Regression test for PyLong_FromLong which can return - NULL in the general case. But now it can't return + NULL in the general case. But now it can't return NULL for arguments of small int type. */ err = PyObject_SetAttrString(mod, "DEBUGGER_ID", val); Py_DECREF(val); From f449cbd1888bbb39ebdb0951caf1ef4319012473 Mon Sep 17 00:00:00 2001 From: Sergey Muraviov Date: Mon, 26 May 2025 09:43:38 +0300 Subject: [PATCH 5/5] =?UTF-8?q?Comment=20=D1=81orrection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Python/instrumentation.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index c37256ef8be8a7..5b26a078085806 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2558,9 +2558,7 @@ PyObject *_Py_CreateMonitoringObject(void) err = PyObject_SetAttrString(events, "NO_EVENTS", _PyLong_GetZero()); if (err) goto error; PyObject *val = PyLong_FromLong(PY_MONITORING_DEBUGGER_ID); - assert(val != NULL); /* Regression test for PyLong_FromLong which can return - NULL in the general case. But now it can't return - NULL for arguments of small int type. */ + assert(val != NULL); /* Can't return NULL because the int is small. */ err = PyObject_SetAttrString(mod, "DEBUGGER_ID", val); Py_DECREF(val); if (err) goto error;