In perl.git, the branch blead has been updated

<http://perl5.git.perl.org/perl.git/commitdiff/bdbefedf6ca6c8253d0fccc6b9d99d7ae86dcd96?hp=279018d8f7ee0701721a840ba1e85752012463bf>

- Log -----------------------------------------------------------------
commit bdbefedf6ca6c8253d0fccc6b9d99d7ae86dcd96
Author: David Mitchell <[email protected]>
Date:   Sat May 8 21:25:47 2010 +0100

    RT #34604 didn't honour tied overloaded values
    
    A tied hash lookup could return an overloaded object but sort wouldn't
    notice that it was overloaded because it checked for overload before doing
    mg_get().

M       pp_sort.c
M       t/op/sort.t

commit aee036bb6c99459a0e305ff4008b983591ce8a4b
Author: David Mitchell <[email protected]>
Date:   Sat May 8 17:23:56 2010 +0100

    add SV_SKIP_OVERLOAD flag to sv_2*v_flags fns
    
    While trying to coerce an SV into a string or whatever, stop if you
    suddenly discover it's overloaded (this may not happen until after you've
    called it's get magic)

M       sv.c
M       sv.h

commit 39d5de13bc6d138baf12ad5224e3af8834356929
Author: David Mitchell <[email protected]>
Date:   Sat May 8 17:06:45 2010 +0100

    add flags arg to sv_2nv (as sv_2nv_flags)

M       embed.fnc
M       embed.h
M       global.sym
M       mathoms.c
M       proto.h
M       sv.c
M       sv.h
-----------------------------------------------------------------------

Summary of changes:
 embed.fnc   |    2 +-
 embed.h     |    4 ++--
 global.sym  |    2 +-
 mathoms.c   |   12 ++++++++++++
 pp_sort.c   |   28 +++++++++-------------------
 proto.h     |    2 +-
 sv.c        |   29 +++++++++++++++++++++--------
 sv.h        |    4 ++++
 t/op/sort.t |   31 ++++++++++++++++++++++++++++++-
 9 files changed, 81 insertions(+), 33 deletions(-)

diff --git a/embed.fnc b/embed.fnc
index 4f3ee7d..87143f0 100644
--- a/embed.fnc
+++ b/embed.fnc
@@ -1093,7 +1093,7 @@ s |bool   |glob_2number   |NN GV* const gv
 Amb    |IV     |sv_2iv         |NULLOK SV *sv
 Apd    |IV     |sv_2iv_flags   |NULLOK SV *const sv|const I32 flags
 Apd    |SV*    |sv_2mortal     |NULLOK SV *const sv
-Apd    |NV     |sv_2nv         |NULLOK SV *const sv
+Apd    |NV     |sv_2nv_flags   |NULLOK SV *const sv|const I32 flags
 : Used in pp.c, pp_hot.c, sv.c
 pMd    |SV*    |sv_2num        |NN SV *const sv
 Amb    |char*  |sv_2pv         |NULLOK SV *sv|NULLOK STRLEN *lp
diff --git a/embed.h b/embed.h
index af2350d..76352c6 100644
--- a/embed.h
+++ b/embed.h
@@ -912,7 +912,7 @@
 #endif
 #define sv_2iv_flags           Perl_sv_2iv_flags
 #define sv_2mortal             Perl_sv_2mortal
-#define sv_2nv                 Perl_sv_2nv
+#define sv_2nv_flags           Perl_sv_2nv_flags
 #ifdef PERL_CORE
 #define sv_2num                        Perl_sv_2num
 #endif
@@ -3326,7 +3326,7 @@
 #endif
 #define sv_2iv_flags(a,b)      Perl_sv_2iv_flags(aTHX_ a,b)
 #define sv_2mortal(a)          Perl_sv_2mortal(aTHX_ a)
-#define sv_2nv(a)              Perl_sv_2nv(aTHX_ a)
+#define sv_2nv_flags(a,b)      Perl_sv_2nv_flags(aTHX_ a,b)
 #ifdef PERL_CORE
 #define sv_2num(a)             Perl_sv_2num(aTHX_ a)
 #endif
diff --git a/global.sym b/global.sym
index 4dd25e6..116fb19 100644
--- a/global.sym
+++ b/global.sym
@@ -519,7 +519,7 @@ Perl_sv_2io
 Perl_sv_2iv
 Perl_sv_2iv_flags
 Perl_sv_2mortal
-Perl_sv_2nv
+Perl_sv_2nv_flags
 Perl_sv_2pv
 Perl_sv_2pv_flags
 Perl_sv_2pvutf8
diff --git a/mathoms.c b/mathoms.c
index aaae227..4322f66 100644
--- a/mathoms.c
+++ b/mathoms.c
@@ -42,6 +42,7 @@ PERL_CALLCONV void Perl_sv_unref(pTHX_ SV *sv);
 PERL_CALLCONV void Perl_sv_taint(pTHX_ SV *sv);
 PERL_CALLCONV IV Perl_sv_2iv(pTHX_ register SV *sv);
 PERL_CALLCONV UV Perl_sv_2uv(pTHX_ register SV *sv);
+PERL_CALLCONV NV Perl_sv_2nv(pTHX_ register SV *sv);
 PERL_CALLCONV char * Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp);
 PERL_CALLCONV char * Perl_sv_2pv_nolen(pTHX_ register SV *sv);
 PERL_CALLCONV char * Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv);
@@ -141,6 +142,17 @@ Perl_sv_2uv(pTHX_ register SV *sv)
     return sv_2uv_flags(sv, SV_GMAGIC);
 }
 
+/* sv_2nv() is now a macro using Perl_sv_2nv_flags();
+ * this function provided for binary compatibility only
+ */
+
+NV
+Perl_sv_2nv(pTHX_ register SV *sv)
+{
+    return sv_2nv_flags(sv, SV_GMAGIC);
+}
+
+
 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
  * this function provided for binary compatibility only
  */
diff --git a/pp_sort.c b/pp_sort.c
index 12e77f9..b0f2be1 100644
--- a/pp_sort.c
+++ b/pp_sort.c
@@ -1589,33 +1589,23 @@ PP(pp_sort)
            if (!PL_sortcop) {
                if (priv & OPpSORT_NUMERIC) {
                    if (priv & OPpSORT_INTEGER) {
-                       if (!SvIOK(*p1)) {
-                           if (SvAMAGIC(*p1))
-                               overloading = 1;
-                           else
-                               (void)sv_2iv(*p1);
-                       }
+                       if (!SvIOK(*p1))
+                           (void)sv_2iv_flags(*p1, SV_GMAGIC|SV_SKIP_OVERLOAD);
                    }
                    else {
-                       if (!SvNSIOK(*p1)) {
-                           if (SvAMAGIC(*p1))
-                               overloading = 1;
-                           else
-                               (void)sv_2nv(*p1);
-                       }
+                       if (!SvNSIOK(*p1))
+                           (void)sv_2nv_flags(*p1, SV_GMAGIC|SV_SKIP_OVERLOAD);
                        if (all_SIVs && !SvSIOK(*p1))
                            all_SIVs = 0;
                    }
                }
                else {
-                   if (!SvPOK(*p1)) {
-                       if (SvAMAGIC(*p1))
-                           overloading = 1;
-                       else
-                           (void)sv_2pv_flags(*p1, 0,
-                                              SV_GMAGIC|SV_CONST_RETURN);
-                   }
+                   if (!SvPOK(*p1))
+                       (void)sv_2pv_flags(*p1, 0,
+                           SV_GMAGIC|SV_CONST_RETURN|SV_SKIP_OVERLOAD);
                }
+               if (SvAMAGIC(*p1))
+                   overloading = 1;
            }
            p1++;
        }
diff --git a/proto.h b/proto.h
index ce76039..22aad52 100644
--- a/proto.h
+++ b/proto.h
@@ -3176,7 +3176,7 @@ STATIC bool       S_glob_2number(pTHX_ GV* const gv)
 /* PERL_CALLCONV IV    Perl_sv_2iv(pTHX_ SV *sv); */
 PERL_CALLCONV IV       Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags);
 PERL_CALLCONV SV*      Perl_sv_2mortal(pTHX_ SV *const sv);
-PERL_CALLCONV NV       Perl_sv_2nv(pTHX_ SV *const sv);
+PERL_CALLCONV NV       Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags);
 PERL_CALLCONV SV*      Perl_sv_2num(pTHX_ SV *const sv)
                        __attribute__nonnull__(pTHX_1);
 #define PERL_ARGS_ASSERT_SV_2NUM       \
diff --git a/sv.c b/sv.c
index 4fd9445..8cbd3a0 100644
--- a/sv.c
+++ b/sv.c
@@ -2322,7 +2322,10 @@ Perl_sv_2iv_flags(pTHX_ register SV *const sv, const I32 
flags)
        if (SvROK(sv)) {
        return_rok:
            if (SvAMAGIC(sv)) {
-               SV * const tmpstr=AMG_CALLun(sv,numer);
+               SV * tmpstr;
+               if (flags & SV_SKIP_OVERLOAD)
+                   return 0;
+               tmpstr=AMG_CALLun(sv,numer);
                if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
                    return SvIV(tmpstr);
                }
@@ -2398,7 +2401,10 @@ Perl_sv_2uv_flags(pTHX_ register SV *const sv, const I32 
flags)
        if (SvROK(sv)) {
        return_rok:
            if (SvAMAGIC(sv)) {
-               SV *const tmpstr = AMG_CALLun(sv,numer);
+               SV *tmpstr;
+               if (flags & SV_SKIP_OVERLOAD)
+                   return 0;
+               tmpstr = AMG_CALLun(sv,numer);
                if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
                    return SvUV(tmpstr);
                }
@@ -2428,14 +2434,14 @@ Perl_sv_2uv_flags(pTHX_ register SV *const sv, const 
I32 flags)
 =for apidoc sv_2nv
 
 Return the num value of an SV, doing any necessary string or integer
-conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
-macros.
+conversion. If flags includes SV_GMAGIC, does an mg_get() first.
+Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
 
 =cut
 */
 
 NV
-Perl_sv_2nv(pTHX_ register SV *const sv)
+Perl_sv_2nv_flags(pTHX_ register SV *const sv, const I32 flags)
 {
     dVAR;
     if (!sv)
@@ -2443,7 +2449,8 @@ Perl_sv_2nv(pTHX_ register SV *const sv)
     if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) {
        /* FBMs use the same flag bit as SVf_IVisUV, so must let them
           cache IVs just in case.  */
-       mg_get(sv);
+       if (flags & SV_GMAGIC)
+           mg_get(sv);
        if (SvNOKp(sv))
            return SvNVX(sv);
        if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) {
@@ -2468,7 +2475,10 @@ Perl_sv_2nv(pTHX_ register SV *const sv)
        if (SvROK(sv)) {
        return_rok:
            if (SvAMAGIC(sv)) {
-               SV *const tmpstr = AMG_CALLun(sv,numer);
+               SV *tmpstr;
+               if (flags & SV_SKIP_OVERLOAD)
+                   return 0;
+               tmpstr = AMG_CALLun(sv,numer);
                 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
                    return SvNV(tmpstr);
                }
@@ -2785,7 +2795,10 @@ Perl_sv_2pv_flags(pTHX_ register SV *const sv, STRLEN 
*const lp, const I32 flags
        if (SvROK(sv)) {
        return_rok:
             if (SvAMAGIC(sv)) {
-               SV *const tmpstr = AMG_CALLun(sv,string);
+               SV *tmpstr;
+               if (flags & SV_SKIP_OVERLOAD)
+                   return NULL;
+               tmpstr = AMG_CALLun(sv,string);
                if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
                    /* Unwrap this:  */
                    /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
diff --git a/sv.h b/sv.h
index 815f109..7d3f1a6 100644
--- a/sv.h
+++ b/sv.h
@@ -1694,6 +1694,9 @@ Like sv_utf8_upgrade, but doesn't do magic on C<sv>
  * This is used when the caller has already determined it is, and avoids
  * redundant work */
 #define SV_FORCE_UTF8_UPGRADE  4096
+/* if (after resolving magic etc), the SV is found to be overloaded,
+ * don't call the overload magic, just return as-is */
+#define SV_SKIP_OVERLOAD       8192
 
 /* The core is safe for this COW optimisation. XS code on CPAN may not be.
    So only default to doing the COW setup if we're in the core.
@@ -1774,6 +1777,7 @@ mg.c:1024: warning: left-hand operand of comma expression 
has no effect
 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
+#define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
 #define sv_insert(bigstr, offset, len, little, littlelen)              \
        Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little),  \
                             (littlelen), SV_GMAGIC)
diff --git a/t/op/sort.t b/t/op/sort.t
index 6261f22..351a194 100644
--- a/t/op/sort.t
+++ b/t/op/sort.t
@@ -6,7 +6,7 @@ BEGIN {
     require 'test.pl';
 }
 use warnings;
-plan( tests => 148 );
+plan( tests => 151 );
 
 # these shouldn't hang
 {
@@ -814,3 +814,32 @@ sub cmp_as_string($$) { $_[0] < $_[1] ? "-1" : $_[0] == 
$_[1] ? "0" : "+1" }
 is("@b", "1 2 3 3 4 5 7", "comparison result as string");
 @b = sort cmp_as_string (1,5,4,7,3,2,3);
 is("@b", "1 2 3 3 4 5 7", "comparison result as string");
+
+# RT #34604: sort didn't honour overloading if the overloaded elements
+# were retrieved via tie
+
+{
+    package RT34604;
+
+    sub TIEHASH { bless {
+                       p => bless({ val => 2 }),
+                       q => bless({ val => 1 }),
+                   }
+               }
+    sub FETCH { $_[0]{$_[1] } }
+
+    my $cc = 0;
+    sub compare { $cc++; $_[0]{val} cmp $_[1]{val} }
+    my $cs = 0;
+    sub str { $cs++; $_[0]{val} }
+
+    use overload 'cmp' => \&compare, '""' => \&str;
+
+    package main;
+
+    tie my %h, 'RT34604';
+    my @sorted = sort @h{qw(p q)};
+    is($cc, 1, 'overload compare called once');
+    is("@sorted","1 2", 'overload sort result');
+    is($cs, 2, 'overload string called twice');
+}

--
Perl5 Master Repository

Reply via email to