From aee54d344c26e5353b95919cbab48b4f479dbaf3 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 1 May 2024 16:06:07 +0100 Subject: [PATCH 1/2] gh-118272: set stacktop to 0 before freeing contents, to avoid access to invalid objects during GC --- Python/frame.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Python/frame.c b/Python/frame.c index ec390e7426ad47..4dedc35e9944a8 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -98,10 +98,11 @@ void _PyFrame_ClearLocals(_PyInterpreterFrame *frame) { assert(frame->stacktop >= 0); - for (int i = 0; i < frame->stacktop; i++) { + int stacktop = 0; + frame->stacktop = 0; + for (int i = 0; i < stacktop; i++) { Py_XDECREF(frame->localsplus[i]); } - frame->stacktop = 0; Py_CLEAR(frame->f_locals); } From d70c8351e85c2b268e2375ea7f37a7387590af91 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Wed, 1 May 2024 17:25:08 +0100 Subject: [PATCH 2/2] Update Python/frame.c --- Python/frame.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/frame.c b/Python/frame.c index 4dedc35e9944a8..2bb12823572028 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -98,7 +98,7 @@ void _PyFrame_ClearLocals(_PyInterpreterFrame *frame) { assert(frame->stacktop >= 0); - int stacktop = 0; + int stacktop = frame->stacktop; frame->stacktop = 0; for (int i = 0; i < stacktop; i++) { Py_XDECREF(frame->localsplus[i]);