This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op.c: Calculate hash for CvNAME_HEK
authorFather Chrysostomos <sprout@cpan.org>
Sat, 30 Aug 2014 17:26:58 +0000 (10:26 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Mon, 8 Sep 2014 05:09:04 +0000 (22:09 -0700)
I assumed when I wrote that code that share_hek would calculate the
hash, like most hash functions; but this internal function assumes
the caller does it.

Hence, CVs were not sharing their heks with other types of thingies
that have heks.  A CV named foo and a GV named foo would cause two
heks of the same name to be present in the shared string table.

op.c
pad.c

diff --git a/op.c b/op.c
index 1e4e980..85a0f0b 100644 (file)
--- a/op.c
+++ b/op.c
@@ -7353,10 +7353,12 @@ Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
        if (CvNAMED(*spot))
            hek = CvNAME_HEK(*spot);
        else {
+           U32 hash;
+           PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
            CvNAME_HEK_set(*spot, hek =
                share_hek(
                    PadnamePV(name)+1,
-                   PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1), 0
+                   PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1), hash
                )
            );
        }
@@ -7486,13 +7488,15 @@ Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
     }
    setname:
     if (!CvNAME_HEK(cv)) {
-       CvNAME_HEK_set(cv,
-        hek
-         ? share_hek_hek(hek)
-         : share_hek(PadnamePV(name)+1,
+       if (hek) share_hek_hek(hek);
+       else {
+           U32 hash;
+           PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
+           hek = share_hek(PadnamePV(name)+1,
                      PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1),
-                     0)
-       );
+                     hash);
+       }
+       CvNAME_HEK_set(cv, hek);
     }
     if (const_sv) goto clone;
 
diff --git a/pad.c b/pad.c
index 42f3734..68e1078 100644 (file)
--- a/pad.c
+++ b/pad.c
@@ -2092,13 +2092,16 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv)
                        /* my sub */
                        /* Just provide a stub, but name it.  It will be
                           upgrade to the real thing on scope entry. */
+                       U32 hash;
+                       PERL_HASH(hash, SvPVX_const(namesv)+1,
+                                 SvCUR(namesv) - 1);
                        sv = newSV_type(SVt_PVCV);
                        CvNAME_HEK_set(
                            sv,
                            share_hek(SvPVX_const(namesv)+1,
                                      SvCUR(namesv) - 1
                                         * (SvUTF8(namesv) ? -1 : 1),
-                                     0)
+                                     hash)
                        );
                    }
                    else sv = SvREFCNT_inc(ppad[ix]);