closes bpo-41533: Fix a potential memory leak when allocating a stack (GH-21847)
Free the stack allocated in va_build_stack if do_mkstack fails
and the stack is not a small_stack
(cherry picked from commit 75c80b0bda89debf312f075716b8c467d411f90e)
Co-authored-by: Tony Solomonik <tony.solomonik@gmail.com>
diff --git a/Python/modsupport.c b/Python/modsupport.c
index 845bdcb..13482c6 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -619,6 +619,9 @@
va_end(lva);
if (res < 0) {
+ if (stack != small_stack) {
+ PyMem_Free(stack);
+ }
return NULL;
}