4dad76
From 3f8dbf40138bd2bcb569b23c88888a41ede9c355 Mon Sep 17 00:00:00 2001
4dad76
From: Tony Cook <tony@develop-help.com>
4dad76
Date: Mon, 5 Aug 2019 15:23:45 +1000
4dad76
Subject: [PATCH] (perl #134266) make sure $@ is writable when we write to it
4dad76
MIME-Version: 1.0
4dad76
Content-Type: text/plain; charset=UTF-8
4dad76
Content-Transfer-Encoding: 8bit
4dad76
4dad76
when unwinding.
4dad76
4dad76
Since except_sv might be ERRSV we try to preserve it's value,
4dad76
if not the actual SV (which we have an extra refcount on if it is
4dad76
except_sv).
4dad76
4dad76
Petr Písař: Ported to 5.30.0 from
4dad76
933e3e630076d4fdbe32a101eeb5f12e37ec4ac2.
4dad76
4dad76
Signed-off-by: Petr Písař <ppisar@redhat.com>
4dad76
---
4dad76
 perl.h             | 17 +++++++++++++++++
4dad76
 pp_ctl.c           | 10 ++++++++--
4dad76
 t/lib/croak/pp_ctl |  8 ++++++++
4dad76
 3 files changed, 33 insertions(+), 2 deletions(-)
4dad76
4dad76
diff --git a/perl.h b/perl.h
4dad76
index e5a5585..383487c 100644
4dad76
--- a/perl.h
4dad76
+++ b/perl.h
4dad76
@@ -1357,6 +1357,23 @@ EXTERN_C char *crypt(const char *, const char *);
4dad76
     }									\
4dad76
     } STMT_END
4dad76
 
4dad76
+/* contains inlined gv_add_by_type */
4dad76
+#define SANE_ERRSV() STMT_START {					\
4dad76
+    SV ** const svp = &GvSV(PL_errgv);					\
4dad76
+    if (!*svp) {							\
4dad76
+        *svp = newSVpvs("");                                            \
4dad76
+    } else if (SvREADONLY(*svp)) {					\
4dad76
+        SV *dupsv = newSVsv(*svp);					\
4dad76
+	SvREFCNT_dec_NN(*svp);						\
4dad76
+	*svp = dupsv;							\
4dad76
+    } else {								\
4dad76
+	SV *const errsv = *svp;						\
4dad76
+	if (SvMAGICAL(errsv)) {						\
4dad76
+	    mg_free(errsv);						\
4dad76
+	}								\
4dad76
+    }									\
4dad76
+    } STMT_END
4dad76
+
4dad76
 
4dad76
 #ifdef PERL_CORE
4dad76
 # define DEFSV (0 + GvSVn(PL_defgv))
4dad76
diff --git a/pp_ctl.c b/pp_ctl.c
4dad76
index a38b9c1..1f2d812 100644
4dad76
--- a/pp_ctl.c
4dad76
+++ b/pp_ctl.c
4dad76
@@ -1720,9 +1720,13 @@ Perl_die_unwind(pTHX_ SV *msv)
4dad76
 	 * perls 5.13.{1..7} which had late setting of $@ without this
4dad76
 	 * early-setting hack.
4dad76
 	 */
4dad76
-	if (!(in_eval & EVAL_KEEPERR))
4dad76
+	if (!(in_eval & EVAL_KEEPERR)) {
4dad76
+            /* remove any read-only/magic from the SV, so we don't
4dad76
+               get infinite recursion when setting ERRSV */
4dad76
+            SANE_ERRSV();
4dad76
 	    sv_setsv_flags(ERRSV, exceptsv,
4dad76
                         (SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL));
4dad76
+        }
4dad76
 
4dad76
 	if (in_eval & EVAL_KEEPERR) {
4dad76
 	    Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf,
4dad76
@@ -1784,8 +1788,10 @@ Perl_die_unwind(pTHX_ SV *msv)
4dad76
              */
4dad76
             S_pop_eval_context_maybe_croak(aTHX_ cx, exceptsv, 2);
4dad76
 
4dad76
-	    if (!(in_eval & EVAL_KEEPERR))
4dad76
+	    if (!(in_eval & EVAL_KEEPERR)) {
4dad76
+                SANE_ERRSV();
4dad76
 		sv_setsv(ERRSV, exceptsv);
4dad76
+            }
4dad76
 	    PL_restartjmpenv = restartjmpenv;
4dad76
 	    PL_restartop = restartop;
4dad76
 	    JMPENV_JUMP(3);
4dad76
diff --git a/t/lib/croak/pp_ctl b/t/lib/croak/pp_ctl
4dad76
index b1e754c..de0221b 100644
4dad76
--- a/t/lib/croak/pp_ctl
4dad76
+++ b/t/lib/croak/pp_ctl
4dad76
@@ -51,3 +51,11 @@ use 5.01;
4dad76
 default{}
4dad76
 EXPECT
4dad76
 Can't "default" outside a topicalizer at - line 2.
4dad76
+########
4dad76
+# NAME croak with read only $@
4dad76
+eval '"a" =~ /${*@=\_})/';
4dad76
+die;
4dad76
+# this would previously recurse infinitely in the eval
4dad76
+EXPECT
4dad76
+Unmatched ) in regex; marked by <-- HERE in m/_) <-- HERE / at (eval 1) line 1.
4dad76
+	...propagated at - line 2.
4dad76
-- 
4dad76
2.21.0
4dad76