8446b7
From 07319fdbb283f93cb655c3106b5237cbc7272038 Mon Sep 17 00:00:00 2001
8446b7
From: Tomasz Konojacki <me@xenu.pl>
8446b7
Date: Wed, 30 Dec 2020 14:03:02 +0100
8446b7
Subject: [PATCH] op.c: croak on "my $_" when "use utf8" is in effect
8446b7
MIME-Version: 1.0
8446b7
Content-Type: text/plain; charset=UTF-8
8446b7
Content-Transfer-Encoding: 8bit
8446b7
8446b7
Fixes #18449
8446b7
8446b7
Signed-off-by: Petr Písař <ppisar@redhat.com>
8446b7
---
8446b7
 op.c         | 16 +++++++++-------
8446b7
 t/op/mydef.t | 11 +++++++++--
8446b7
 2 files changed, 18 insertions(+), 9 deletions(-)
8446b7
8446b7
diff --git a/op.c b/op.c
8446b7
index b2e12dd0c0..dce844d297 100644
8446b7
--- a/op.c
8446b7
+++ b/op.c
8446b7
@@ -730,6 +730,7 @@ PADOFFSET
8446b7
 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
8446b7
 {
8446b7
     PADOFFSET off;
8446b7
+    bool is_idfirst, is_default;
8446b7
     const bool is_our = (PL_parser->in_my == KEY_our);
8446b7
 
8446b7
     PERL_ARGS_ASSERT_ALLOCMY;
8446b7
@@ -738,14 +739,15 @@ Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
8446b7
 	Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
8446b7
 		   (UV)flags);
8446b7
 
8446b7
+    is_idfirst = flags & SVf_UTF8
8446b7
+        ? isIDFIRST_utf8_safe((U8*)name + 1, name + len)
8446b7
+        : isIDFIRST_A(name[1]);
8446b7
+
8446b7
+    /* $_, @_, etc. */
8446b7
+    is_default = len == 2 && name[1] == '_';
8446b7
+
8446b7
     /* complain about "my $<special_var>" etc etc */
8446b7
-    if (   len
8446b7
-        && !(  is_our
8446b7
-            || isALPHA(name[1])
8446b7
-            || (   (flags & SVf_UTF8)
8446b7
-                && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
8446b7
-            || (name[1] == '_' && len > 2)))
8446b7
-    {
8446b7
+    if (!is_our && (!is_idfirst || is_default)) {
8446b7
         const char * const type =
8446b7
               PL_parser->in_my == KEY_sigvar ? "subroutine signature" :
8446b7
               PL_parser->in_my == KEY_state  ? "\"state\""     : "\"my\"";
8446b7
diff --git a/t/op/mydef.t b/t/op/mydef.t
8446b7
index 42a81d9ab0..225ce98e51 100644
8446b7
--- a/t/op/mydef.t
8446b7
+++ b/t/op/mydef.t
8446b7
@@ -6,10 +6,17 @@ BEGIN {
8446b7
     set_up_inc('../lib');
8446b7
 }
8446b7
 
8446b7
-plan tests => 1;
8446b7
-
8446b7
 use strict;
8446b7
 
8446b7
 eval 'my $_';
8446b7
 like $@, qr/^Can't use global \$_ in "my" at /;
8446b7
 
8446b7
+{
8446b7
+    # using utf8 allows $_ to be declared with 'my'
8446b7
+    # GH #18449
8446b7
+    use utf8;
8446b7
+    eval 'my $_;';
8446b7
+    like $@, qr/^Can't use global \$_ in "my" at /;
8446b7
+}
8446b7
+
8446b7
+done_testing;
8446b7
-- 
8446b7
2.26.2
8446b7