This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Prevent require() from attempting to open directories and block
[perl5.git] / gv.c
diff --git a/gv.c b/gv.c
index e6993ad..0ad4c0f 100644 (file)
--- a/gv.c
+++ b/gv.c
@@ -101,7 +101,7 @@ Perl_gv_fetchfile(pTHX_ const char *name)
     if (tmplen < sizeof smallbuf)
        tmpbuf = smallbuf;
     else
-       New(603, tmpbuf, tmplen + 1, char);
+       Newx(tmpbuf, tmplen + 1, char);
     /* This is where the debugger's %{"::_<$filename"} hash is created */
     tmpbuf[0] = '_';
     tmpbuf[1] = '<';
@@ -139,7 +139,7 @@ Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
        } else
            Safefree(SvPVX_mutable(gv));
     }
-    Newz(602, gp, 1, GP);
+    Newxz(gp, 1, GP);
     GvGP(gv) = gp_ref(gp);
 #ifdef PERL_DONT_CREATE_GVSV
     GvSV(gv) = 0;
@@ -164,7 +164,7 @@ Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
        SvIOK_off(gv);
        ENTER;
        /* XXX unsafe for threads if eval_owner isn't held */
-       start_subparse(0,0);            /* Create CV in compcv. */
+       (void) start_subparse(0,0);     /* Create empty CV in compcv. */
        GvCV(gv) = PL_compcv;
        LEAVE;
 
@@ -302,8 +302,8 @@ Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
        /* NOTE: No support for tied ISA */
        I32 items = AvFILLp(av) + 1;
        while (items--) {
-           SV* sv = *svp++;
-           HV* basestash = gv_stashsv(sv, FALSE);
+           SV* const sv = *svp++;
+           HV* const basestash = gv_stashsv(sv, FALSE);
            if (!basestash) {
                if (ckWARN(WARN_MISC))
                    Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
@@ -320,9 +320,9 @@ Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
     /* if at top level, try UNIVERSAL */
 
     if (level == 0 || level == -1) {
-       HV* lastchance;
+       HV* const lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE);
 
-       if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) {
+       if (lastchance) {
            if ((gv = gv_fetchmeth(lastchance, name, len,
                                  (level >= 0) ? level + 1 : level - 1)))
            {
@@ -397,20 +397,6 @@ Perl_gv_fetchmeth_autoload(pTHX_ HV *stash, const char *name, STRLEN len, I32 le
 }
 
 /*
-=for apidoc gv_fetchmethod
-
-See L<gv_fetchmethod_autoload>.
-
-=cut
-*/
-
-GV *
-Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
-{
-    return gv_fetchmethod_autoload(stash, name, TRUE);
-}
-
-/*
 =for apidoc gv_fetchmethod_autoload
 
 Returns the glob which contains the subroutine to call to invoke the method
@@ -547,8 +533,9 @@ Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
     /*
      * Inheriting AUTOLOAD for non-methods works ... for now.
      */
-    if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX) && !method &&
-       (GvCVGEN(gv) || GvSTASH(gv) != stash))
+    if (!method && (GvCVGEN(gv) || GvSTASH(gv) != stash)
+       && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)
+    )
        Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
          "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
             packname, (int)len, name);
@@ -582,7 +569,7 @@ Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
 #endif
     }
     LEAVE;
-    varsv = GvSV(vargv);
+    varsv = GvSVn(vargv);
     sv_setpvn(varsv, packname, packname_len);
     sv_catpvn(varsv, "::", 2);
     sv_catpvn(varsv, name, len);
@@ -654,7 +641,7 @@ Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
     if (namelen + 3 < sizeof smallbuf)
        tmpbuf = smallbuf;
     else
-       New(606, tmpbuf, namelen + 3, char);
+       Newx(tmpbuf, namelen + 3, char);
     Copy(name,tmpbuf,namelen,char);
     tmpbuf[namelen++] = ':';
     tmpbuf[namelen++] = ':';
@@ -668,7 +655,7 @@ Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
        GvHV(tmpgv) = newHV();
     stash = GvHV(tmpgv);
     if (!HvNAME_get(stash))
-       Perl_hv_name_set(aTHX_ stash, name, namelen, 0);
+       hv_name_set(stash, name, namelen, 0);
     return stash;
 }
 
@@ -713,7 +700,8 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
     register const char *namend;
     HV *stash = 0;
     const I32 add = flags & ~SVf_UTF8;
-    (void)full_len;
+
+    PERL_UNUSED_ARG(full_len);
 
     if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
        name++;
@@ -735,7 +723,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
                if (len + 3 < sizeof (smallbuf))
                    tmpbuf = smallbuf;
                else
-                   New(601, tmpbuf, len+3, char);
+                   Newx(tmpbuf, len+3, char);
                Copy(name, tmpbuf, len, char);
                tmpbuf[len++] = ':';
                tmpbuf[len++] = ':';
@@ -757,7 +745,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
                    stash = GvHV(gv) = newHV();
 
                if (!HvNAME_get(stash))
-                   Perl_hv_name_set(aTHX_ stash, nambeg, namend - nambeg, 0);
+                   hv_name_set(stash, nambeg, namend - nambeg, 0);
            }
 
            if (*namend == ':')
@@ -948,15 +936,15 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
                    HV *hv;
                    I32 i;
                    if (!PL_psig_ptr) {
-                       Newz(73, PL_psig_ptr,  SIG_SIZE, SV*);
-                       Newz(73, PL_psig_name, SIG_SIZE, SV*);
-                       Newz(73, PL_psig_pend, SIG_SIZE, int);
+                       Newxz(PL_psig_ptr,  SIG_SIZE, SV*);
+                       Newxz(PL_psig_name, SIG_SIZE, SV*);
+                       Newxz(PL_psig_pend, SIG_SIZE, int);
                    }
                    GvMULTI_on(gv);
                    hv = GvHVn(gv);
                    hv_magic(hv, Nullgv, PERL_MAGIC_sig);
                    for (i = 1; i < SIG_SIZE; i++) {
-                       SV ** const init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
+                       SV * const * const init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
                        if (init)
                            sv_setsv(*init, &PL_sv_undef);
                        PL_psig_ptr[i] = 0;
@@ -1181,38 +1169,10 @@ Perl_gv_fullname4(pTHX_ SV *sv, const GV *gv, const char *prefix, bool keepmain)
 }
 
 void
-Perl_gv_fullname3(pTHX_ SV *sv, const GV *gv, const char *prefix)
-{
-    gv_fullname4(sv, gv, prefix, TRUE);
-}
-
-void
 Perl_gv_efullname4(pTHX_ SV *sv, const GV *gv, const char *prefix, bool keepmain)
 {
-    const GV *egv = GvEGV(gv);
-    if (!egv)
-       egv = gv;
-    gv_fullname4(sv, egv, prefix, keepmain);
-}
-
-void
-Perl_gv_efullname3(pTHX_ SV *sv, const GV *gv, const char *prefix)
-{
-    gv_efullname4(sv, gv, prefix, TRUE);
-}
-
-/* compatibility with versions <= 5.003. */
-void
-Perl_gv_fullname(pTHX_ SV *sv, const GV *gv)
-{
-    gv_fullname3(sv, gv, sv == (const SV*)gv ? "*" : "");
-}
-
-/* compatibility with versions <= 5.003. */
-void
-Perl_gv_efullname(pTHX_ SV *sv, const GV *gv)
-{
-    gv_efullname3(sv, gv, sv == (const SV*)gv ? "*" : "");
+    const GV * const egv = GvEGV(gv);
+    gv_fullname4(sv, egv ? egv : gv, prefix, keepmain);
 }
 
 IO *
@@ -1222,7 +1182,10 @@ Perl_newIO(pTHX)
     IO * const io = (IO*)NEWSV(0,0);
 
     sv_upgrade((SV *)io,SVt_PVIO);
-    SvREFCNT(io) = 1;
+    /* This used to read SvREFCNT(io) = 1;
+       It's not clear why the reference count needed an explicit reset. NWC
+    */
+    assert (SvREFCNT(io) == 1);
     SvOBJECT_on(io);
     /* Clear the stashcache because a new IO could overrule a package name */
     hv_clear(PL_stashcache);
@@ -1260,14 +1223,14 @@ Perl_gv_check(pTHX_ HV *stash)
                file = GvFILE(gv);
                /* performance hack: if filename is absolute and it's a standard
                 * module, don't bother warning */
-               if (file
-                   && PERL_FILE_IS_ABSOLUTE(file)
 #ifdef MACOS_TRADITIONAL
-                   && (instr(file, ":lib:")
+#   define LIB_COMPONENT ":lib:"
 #else
-                   && (instr(file, "/lib/")
+#   define LIB_COMPONENT "/lib/"
 #endif
-                   || instr(file, ".pm")))
+               if (file
+                   && PERL_FILE_IS_ABSOLUTE(file)
+                   && (instr(file, LIB_COMPONENT) || instr(file, ".pm")))
                {
                    continue;
                }
@@ -1526,7 +1489,7 @@ Perl_gv_handler(pTHX_ HV *stash, I32 id)
               "Inherited AUTOLOAD for a non-method deprecated", since
               our caller is going through a function call, not a method call.
               So return the CV for AUTOLOAD, setting $AUTOLOAD. */
-           GV *gv = gv_fetchmethod(stash, PL_AMG_names[id]);
+           GV * const gv = gv_fetchmethod(stash, PL_AMG_names[id]);
 
            if (gv && GvCV(gv))
                return GvCV(gv);
@@ -1611,13 +1574,13 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
                  * SV* ref causes confusion with the interpreter variable of
                  * the same name
                  */
-            SV* tmpRef=SvRV(left);
+            SV* const tmpRef=SvRV(left);
             if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
                /*
                 * Just to be extra cautious.  Maybe in some
                 * additional cases sv_setsv is safe, too.
                 */
-               SV* newref = newSVsv(tmpRef);
+               SV* const newref = newSVsv(tmpRef);
                SvOBJECT_on(newref);
                SvSTASH_set(newref, (HV*)SvREFCNT_inc(SvSTASH(tmpRef)));
                return newref;
@@ -1627,13 +1590,13 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
         case abs_amg:
           if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
               && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
-            SV* nullsv=sv_2mortal(newSViv(0));
+            SV* const nullsv=sv_2mortal(newSViv(0));
             if (off1==lt_amg) {
-              SV* lessp = amagic_call(left,nullsv,
+              SV* const lessp = amagic_call(left,nullsv,
                                       lt_amg,AMGf_noright);
               logic = SvTRUE(lessp);
             } else {
-              SV* lessp = amagic_call(left,nullsv,
+              SV* const lessp = amagic_call(left,nullsv,
                                       ncmp_amg,AMGf_noright);
               logic = (SvNV(lessp) < 0);
             }
@@ -1897,7 +1860,7 @@ bool
 Perl_is_gv_magical_sv(pTHX_ SV *name, U32 flags)
 {
     STRLEN len;
-    const char *temp = SvPV_const(name, len);
+    const char * const temp = SvPV_const(name, len);
     return is_gv_magical(temp, len, flags);
 }