develooper Front page | perl.perl5.porters | Postings from October 2013

Re: Why does undef *gv vivify a scalar?

Thread Previous | Thread Next
From:
Father Chrysostomos
Date:
October 26, 2013 12:37
Subject:
Re: Why does undef *gv vivify a scalar?
Message ID:
20131026123722.29237.qmail@lists-nntp.develooper.com
Nicholas Clark wrote:
> But I'm thinking that at the point you've shown, there's no need to create
> a new scalar. If instead it's left as NULL, do all Perl-space access to it
> continue to work as expected, lazily creating the scalar at access time?

This:

diff --git a/pp.c b/pp.c
index f3281f3f..67e118d 100644
--- a/pp.c
+++ b/pp.c
@@ -1017,7 +1017,7 @@ PP(pp_undef)
     gp_free(MUTABLE_GV(sv));
     gp_free(MUTABLE_GV(sv));    Newxz(gp, 1, GP);
     gp_free(MUTABLE_GV(sv));    Newxz(gp, 1, GP);    GvGP_set(sv, gp_ref(gp));
--- a/pp.c    GvSV(sv) = newSV(0);
+/*    GvSV(sv) = newSV(0);*/
     gp_free(MUTABLE_GV(sv));    Newxz(gp, 1, GP);    GvGP_set(sv, gp_ref(gp));    GvLINE(sv) = CopLINE(PL_curcop);
     gp_free(MUTABLE_GV(sv));    Newxz(gp, 1, GP);    GvGP_set(sv, gp_ref(gp));    GvLINE(sv) = CopLINE(PL_curcop);    GvEGV(sv) = MUTABLE_GV(sv);
     gp_free(MUTABLE_GV(sv));    Newxz(gp, 1, GP);    GvGP_set(sv, gp_ref(gp));    GvLINE(sv) = CopLINE(PL_curcop);    GvEGV(sv) = MUTABLE_GV(sv);    GvMULTI_on(sv);

causes this:

$ ./miniperl -Ilib -le 'undef *{"ARGV"}; eval "*ARGV if 0"; warn $::{ARGV}; eval "readline"'
Warning: something's wrong at -e line 1.
Attempt to free unreferenced scalar: SV 0x7f9b82029bf8.

which is the bug I was trying to produce when I encountered this. :-)

I don’t know whether there are other pure-Perl ways to do that, but I
think PL_argvgv needs to be refcounted in any case.

What’s happening is that gv_try_downgrade is triggered by the freeing
of the gvop when ‘if’ is folded, and it deletes the glob from the
main stash.

I encountered this bug like this, in fact:

$ PERL5DB=1 perl -de '*DB::DB = sub {} if 0; sub DB::DB{}'
No DB::DB routine defined at -e line 1.

And I was trying to see whether other global GVs are affected.

If I fix them all (making them refcounted where appropriate), then we
can remove that line from pp_undef. :-)


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About