Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anonymous glob breaks when assigned through #873

Closed
p5pRT opened this issue Nov 18, 1999 · 7 comments
Closed

Anonymous glob breaks when assigned through #873

p5pRT opened this issue Nov 18, 1999 · 7 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 18, 1999

Migrated from rt.perl.org#1804 (status was 'resolved')

Searchable as RT1804$

@p5pRT
Copy link
Author

p5pRT commented Nov 18, 1999

From @mjdominus

  #!/usr/bin/perl
 
  $x = \*F;
  print "$x\n";
  *{$x} = sub {};
  print "$x\n";
  print *$x, "\n";
 
  print "-------------------------\n";
 
  $y = \do { local *F };
  print "$y\n";
  *{$y} = sub {};
  print "$y\n";
  print *$y, "\n";

I want these two sections of code to do the same thing. I make a glob
reference. Then I try to assign a subroutine to the CODE part of the
glob. If the reference is to a glob like *F, it works as it should.
But if the reference is to a fake glob, as in the second section, the
assignment turns the globref into a scalar ref.

The output of the program is​:

  GLOB(0x81308a4)
  GLOB(0x81308a4)
  *main​::F
  -------------------------
  GLOB(0x812a23c)
  SCALAR(0x812a23c)
  Not a GLOB reference at /tmp/oops.pl line 15.

I want it to be​:
 
  GLOB(0x81308a4)
  GLOB(0x81308a4)
  *main​::F
  -------------------------
  GLOB(0x812a23c)
  GLOB(0x812a23c)
  *main​::F

The behavior is the same under perl 5.005_02, _55, _56, _57, _61, and _62.

Perl Info


This perlbug was built using Perl 5.00502 - Sun Oct 18 04:25:09 CDT 1998
It is being executed now by  Perl 5.00556 - Wed May 19 22:26:34 EDT 1999.

Site configuration information for perl 5.00502:

Configured by root at Sun Oct 18 04:25:09 CDT 1998.

Summary of my perl5 (5.0 patchlevel 5 subversion 2) configuration:
  Platform:
    osname=linux, osvers=2.0.35, archname=i586-linux
    uname='linux darkstar 2.0.35 #10 tue oct 13 18:04:13 cdt 1998 i586 unknown '
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=2.7.2.3
    cppflags='-Dbool=char -DHAS_BOOL -I/usr/local/include'
    ccflags ='-Dbool=char -DHAS_BOOL -I/usr/local/include'
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /shlib /lib /usr/lib
    libs=-lndbm -lgdbm -ldbm -ldb -ldl -lm -lc
    libc=/lib/libc.so.5.4.46, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl 5.00502:
    /usr/lib/perl5/5.00502/i586-linux
    /usr/lib/perl5/5.00502
    /usr/lib/perl5/site_perl/5.005/i586-linux
    /usr/lib/perl5/site_perl/5.005
    .


Environment for perl 5.00502:
    HOME=/home/mjd
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/lib:/usr/lib:/usr/X11R6/lib
    LOGDIR (unset)
    PATH=/home/mjd/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11/bin:/usr/games:/sbin:/usr/sbin:/usr/local/bin/X11:/usr/local/bin/mh:/data/mysql/bin:/home/mjd/TPI/bin:/usr/local/mysql/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Mar 16, 2010

From @cpansprout

Here is a much simpler case​:

$ perl -le'$x = *F; *$x = sub{}; print $x;'
CODE(0x826da0)

The problem here is that globs are scalars and the = operator can only
distinguish between scalar and glob assignments by the flags on the
glob. It only sees the return value of *{}, not the *{} itself. We can
fix this by having the pp_sassign look for a rv2gv (*{}) on its LHS,
to decide what type of assignment to do. The attached patch (if there
is one :-) does just this.

@p5pRT
Copy link
Author

p5pRT commented Mar 16, 2010

From @cpansprout

Inline Patch
diff -Nurp blead copy 2/pp_hot.c bleadcopy/pp_hot.c
--- blead copy 2/pp_hot.c	2010-01-14 07:42:08.000000000 -0800
+++ bleadcopy/pp_hot.c	2010-03-14 14:11:27.000000000 -0700
@@ -110,6 +110,7 @@ PP(pp_and)
 PP(pp_sassign)
 {
     dVAR; dSP; dPOPTOPssrl;
+    U32 wasfake = 0;
 
     if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
 	SV * const temp = left;
@@ -195,7 +196,14 @@ PP(pp_sassign)
 	}
 
     }
+    /* Allow glob assignments like *$x = ..., which, when the glob has a
+       SVf_FAKE flag, cannot be distinguished from $x = ... without looking
+       at the op tree. */
+    if( SvTYPE(right) == SVt_PVGV && cBINOP->op_last->op_type == OP_RV2GV
+     && (wasfake = SvFLAGS(right) & SVf_FAKE) )
+	SvFLAGS(right) &= ~SVf_FAKE;
     SvSetMagicSV(right, left);
+    if(wasfake) SvFLAGS(right) |= SVf_FAKE;
     SETs(right);
     RETURN;
 }
diff -Nurp blead copy 2/t/op/gv.t bleadcopy/t/op/gv.t
--- blead copy 2/t/op/gv.t	2010-03-10 11:58:12.000000000 -0800
+++ bleadcopy/t/op/gv.t	2010-03-14 14:21:00.000000000 -0700
@@ -12,7 +12,7 @@ BEGIN {
 use warnings;
 
 require './test.pl';
-plan( tests => 188 );
+plan( tests => 189 );
 
 # type coersion on assignment
 $foo = 'foo';
@@ -614,6 +614,17 @@ ok(exists($RT72740a::{s4}), "RT72740a::s
 is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
 is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
 
+# [perl #1804] *$x assignment when $x is a copy of another glob
+{
+    no warnings 'once';
+    my $x = *_random::glob_that_is_not_used_elsewhere;
+    *$x = sub{};
+    is(
+      "$x", '*_random::glob_that_is_not_used_elsewhere',
+      '[perl #1804] *$x assignment when $x is FAKE',
+    );
+}
+
 __END__
 Perl
 Rules

@p5pRT
Copy link
Author

p5pRT commented Jul 10, 2010

From @gannett-ggreer

On Tue Mar 16 09​:56​:18 2010, sprout@​cpan.org wrote​:

Here is a much simpler case​:

$ perl -le'$x = *F; *$x = sub{}; print $x;'
CODE(0x826da0)

The problem here is that globs are scalars and the = operator can only
distinguish between scalar and glob assignments by the flags on the
glob. It only sees the return value of *{}, not the *{} itself. We can
fix this by having the pp_sassign look for a rv2gv (*{}) on its LHS,
to decide what type of assignment to do. The attached patch (if there
is one :-) does just this.

I have updated this patch to apply against the current blead (a2d3de1).
I set Father Chrysostomos as the author when I committed the patch to my
git fork.

http​://m-l.org/~perl/misc/0001-Fix-for-RT-1804-Anonymous-glob-breaks-when-assigned-.patch

http​://github.com/greerga/perl/commit/df1c903b0bbf4fb1071904ee197fe138242b0885

--
George Greer

@p5pRT
Copy link
Author

p5pRT commented Jul 10, 2010

From @gannett-ggreer

0001-Fix-for-RT-1804-Anonymous-glob-breaks-when-assigned-.patch
From df1c903b0bbf4fb1071904ee197fe138242b0885 Mon Sep 17 00:00:00 2001
From: Father Chrysostomos <sprout@cpan.org>
Date: Sat, 10 Jul 2010 15:09:51 -0400
Subject: [PATCH] Fix for RT#1804: Anonymous glob breaks when assigned through

The problem here is that globs are scalars and the = operator can only
distinguish between scalar and glob assignments by the flags on the
glob. It only sees the return value of *{}, not the *{} itself. We can
fix this by having the pp_sassign look for a rv2gv (*{}) on its LHS,
to decide what type of assignment to do.
---
 pp_hot.c  |    8 ++++++++
 t/op/gv.t |   13 ++++++++++++-
 2 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/pp_hot.c b/pp_hot.c
index 6f48d5a..2eeba08 100644
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -112,6 +112,7 @@ PP(pp_and)
 PP(pp_sassign)
 {
     dVAR; dSP; dPOPTOPssrl;
+    U32 wasfake = 0;
 
     if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
 	SV * const temp = left;
@@ -197,7 +198,14 @@ PP(pp_sassign)
 	}
 
     }
+    /* Allow glob assignments like *$x = ..., which, when the glob has a
+       SVf_FAKE flag, cannot be distinguished from $x = ... without looking
+       at the op tree. */
+    if( SvTYPE(right) == SVt_PVGV && cBINOP->op_last->op_type == OP_RV2GV
+     && (wasfake = SvFLAGS(right) & SVf_FAKE) )
+	SvFLAGS(right) &= ~SVf_FAKE;
     SvSetMagicSV(right, left);
+    if(wasfake) SvFLAGS(right) |= SVf_FAKE;
     SETs(right);
     RETURN;
 }
diff --git a/t/op/gv.t b/t/op/gv.t
index f3511e3..13da980 100644
--- a/t/op/gv.t
+++ b/t/op/gv.t
@@ -12,7 +12,7 @@ BEGIN {
 use warnings;
 
 require './test.pl';
-plan( tests => 191 );
+plan( tests => 192 );
 
 # type coersion on assignment
 $foo = 'foo';
@@ -623,6 +623,17 @@ is ($@, '', "Can localize FAKE glob that's present in stash");
 is (scalar $::{fake}, "*main::sym",
 	"Localized FAKE glob's value was correctly restored");
 
+# [perl #1804] *$x assignment when $x is a copy of another glob
+{
+    no warnings 'once';
+    my $x = *_random::glob_that_is_not_used_elsewhere;
+    *$x = sub{};
+    is(
+      "$x", '*_random::glob_that_is_not_used_elsewhere',
+      '[perl #1804] *$x assignment when $x is FAKE',
+    );
+}
+
 __END__
 Perl
 Rules
-- 
1.7.0.4

@p5pRT
Copy link
Author

p5pRT commented Jul 26, 2010

From @rgarcia

On 10 July 2010 21​:17, George Greer via RT <perlbug-followup@​perl.org> wrote​:

On Tue Mar 16 09​:56​:18 2010, sprout@​cpan.org wrote​:

Here is a much simpler case​:

$ perl -le'$x = *F; *$x = sub{}; print $x;'
CODE(0x826da0)

The problem here is that globs are scalars and the = operator can only
distinguish between scalar and glob assignments by the flags on the
glob. It only sees the return value of *{}, not the *{} itself. We can
fix this by having the pp_sassign look for a rv2gv (*{}) on its LHS,
to decide what type of assignment to do. The attached patch (if there
is one :-) does just this.

I have updated this patch to apply against the current blead (a2d3de1).
I set Father Chrysostomos as the author when I committed the patch to my
git fork.

Thanks to both, applied as change
0fe688f to bleadperl.

@p5pRT
Copy link
Author

p5pRT commented Jul 26, 2010

@rgs - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant