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