8f28ebb
From 803bd7c91c63f8f263bed592a33b10cf69f567cf Mon Sep 17 00:00:00 2001
8f28ebb
From: David Mitchell <davem@iabyn.com>
8f28ebb
Date: Fri, 22 Mar 2019 15:43:56 +0000
8f28ebb
Subject: [PATCH] fix leak in BEGIN { threads->new(...) }
8f28ebb
MIME-Version: 1.0
8f28ebb
Content-Type: text/plain; charset=UTF-8
8f28ebb
Content-Transfer-Encoding: 8bit
8f28ebb
8f28ebb
Normally by the time we reach perl_destruct(), PL_parser should be null
8f28ebb
due to having its original (null) value restored by SAVEt_PARSER during
8f28ebb
leaving scope (usually before run-time starts in fact).  But if a thread
8f28ebb
is created within a BEGIN block, the parser is duped, but the
8f28ebb
SAVEt_PARSER savestack entry isn't. So PL_parser never gets cleaned up.
8f28ebb
Clean it up in perl_destruct() instead. This is a bit of a hack.
8f28ebb
8f28ebb
Signed-off-by: Petr Písař <ppisar@redhat.com>
8f28ebb
---
8f28ebb
 perl.c | 15 +++++++++++++++
8f28ebb
 1 file changed, 15 insertions(+)
8f28ebb
8f28ebb
diff --git a/perl.c b/perl.c
8f28ebb
index cdefa99018..1ef425bb25 100644
8f28ebb
--- a/perl.c
8f28ebb
+++ b/perl.c
8f28ebb
@@ -668,6 +668,21 @@ perl_destruct(pTHXx)
8f28ebb
     FREETMPS;
8f28ebb
     assert(PL_scopestack_ix == 0);
8f28ebb
 
8f28ebb
+    /* normally when we get here, PL_parser should be null due to having
8f28ebb
+     * its original (null) value restored by SAVEt_PARSER during leaving
8f28ebb
+     * scope (usually before run-time starts in fact).
8f28ebb
+     * But if a thread is created within a BEGIN block, the parser is
8f28ebb
+     * duped, but the SAVEt_PARSER savestack entry isn't. So PL_parser
8f28ebb
+     * never gets cleaned up.
8f28ebb
+     * Clean it up here instead. This is a bit of a hack.
8f28ebb
+     */
8f28ebb
+    if (PL_parser) {
8f28ebb
+        /* stop parser_free() stomping on PL_curcop */
8f28ebb
+        PL_parser->saved_curcop = PL_curcop;
8f28ebb
+        parser_free(PL_parser);
8f28ebb
+    }
8f28ebb
+
8f28ebb
+
8f28ebb
     /* Need to flush since END blocks can produce output */
8f28ebb
     /* flush stdout separately, since we can identify it */
8f28ebb
 #ifdef USE_PERLIO
8f28ebb
-- 
8f28ebb
2.20.1
8f28ebb