This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #45147] Issue with the exists function
authorPeter Martini <PeterCMartini@GMail.com>
Mon, 3 Jan 2011 06:54:04 +0000 (22:54 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Mon, 3 Jan 2011 07:48:19 +0000 (23:48 -0800)
Perl_av_exists tested to see if regdata magic was present,
but did not have any logic to fetch that data in the positive
key case.  Additionally, in the negative key case, if AvFILL
indicated the key existed, it wouldn't return, and would then
fall through to the logic that treated it like a real array.

AUTHORS
av.c
t/re/pat.t

diff --git a/AUTHORS b/AUTHORS
index f27059d..5ae2362 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -821,6 +821,7 @@ Peter J. Farley III         <pjfarley@banet.net>
 Peter J. Holzer                        <hjp@hjp.at>
 Peter Jaspers-Fayer
 Peter John Acklam              <pjacklam@online.no>
+Peter Martini                   <PeterCMartini@GMail.com>
 Peter O'Gorman                 <peter@pogma.com>
 Peter Prymmer                  <PPrymmer@factset.com>
 Peter Rabbitson                        <rabbit@rabbit.us>
diff --git a/av.c b/av.c
index d6db6bf..8408220 100644 (file)
--- a/av.c
+++ b/av.c
@@ -901,7 +901,9 @@ Perl_av_exists(pTHX_ AV *av, I32 key)
     if (SvRMAGICAL(av)) {
         const MAGIC * const tied_magic
            = mg_find((const SV *)av, PERL_MAGIC_tied);
-        if (tied_magic || mg_find((const SV *)av, PERL_MAGIC_regdata)) {
+        const MAGIC * const regdata_magic
+            = mg_find((const SV *)av, PERL_MAGIC_regdata);
+        if (tied_magic || regdata_magic) {
            SV * const sv = sv_newmortal();
             MAGIC *mg;
             /* Handle negative array indices 20020222 MJD */
@@ -920,9 +922,18 @@ Perl_av_exists(pTHX_ AV *av, I32 key)
                     key += AvFILL(av) + 1;
                     if (key < 0)
                         return FALSE;
+                    else
+                        return TRUE;
                 }
             }
 
+            if(key >= 0 && regdata_magic) {
+                if (key <= AvFILL(av))
+                    return TRUE;
+                else
+                    return FALSE;
+            }
+
             mg_copy(MUTABLE_SV(av), sv, 0, key);
             mg = mg_find(sv, PERL_MAGIC_tiedelem);
             if (mg) {
index 6c3af0a..2dd9503 100644 (file)
@@ -23,7 +23,7 @@ BEGIN {
 }
 
 
-plan tests => 411;  # Update this when adding/deleting tests.
+plan tests => 423;  # Update this when adding/deleting tests.
 
 run_tests() unless caller;
 
@@ -640,6 +640,19 @@ sub run_tests {
         ok !defined $+ [3] && !defined $- [3] &&
            !defined $+ [4] && !defined $- [4];
 
+        # Exists has a special check for @-/@+ - bug 45147
+        ok exists $-[0];
+        ok exists $+[0];
+        ok exists $-[2];
+        ok exists $+[2];
+        ok !exists $-[3];
+        ok !exists $+[3];
+        ok exists $-[-1];
+        ok exists $+[-1];
+        ok exists $-[-3];
+        ok exists $+[-3];
+        ok !exists $-[-4];
+        ok !exists $+[-4];
 
         /.(a)(b)?(a)/;
         iseq $#+, 3;