From 6763bfcc0fbab7895514c1f954d79a2555036323 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Wed, 1 May 2024 21:51:40 +0100 Subject: [PATCH] gh-118272: set stacktop to 0 before freeing contents, to avoid access to invalid objects during GC (#118478) --- Python/frame.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Python/frame.c b/Python/frame.c index ec390e7426a..2bb12823572 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 = frame->stacktop; + frame->stacktop = 0; + for (int i = 0; i < stacktop; i++) { Py_XDECREF(frame->localsplus[i]); } - frame->stacktop = 0; Py_CLEAR(frame->f_locals); }