Array#find can return the wrong value if it's mutated during iteration
----------------------------------------------------------------------

                 Key: JRUBY-4805
                 URL: http://jira.codehaus.org/browse/JRUBY-4805
             Project: JRuby
          Issue Type: Bug
          Components: Core Classes/Modules
    Affects Versions: JRuby 1.5
            Reporter: Charles Oliver Nutter
            Priority: Critical


This is a regression that caused problems for Buildr:

{noformat}
~/projects/buildr ➔ jruby -e "a = [1,2,3,4]; p a.find {|x| foo = x > 2; 
a.delete x if foo; foo}"
4

~/projects/buildr ➔ ruby -e "a = [1,2,3,4]; p a.find {|x| foo = x > 2; 
a.delete x if foo; foo}"
3
{noformat}

Below is a trivial patch, but I'm not sure exactly what other cases aren't 
being handled properly for this. We need to explore other 
mutation-during-search cases for the other methods we fast-pathed in 1.5.

{noformat}
diff --git a/src/org/jruby/RubyArray.java b/src/org/jruby/RubyArray.java
index 05dc4a1..354a989 100644
--- a/src/org/jruby/RubyArray.java
+++ b/src/org/jruby/RubyArray.java
@@ -3793,7 +3793,8 @@ public class RubyArray extends RubyObject implements List 
{
 
     public IRubyObject detectCommon(ThreadContext context, IRubyObject ifnone, 
Block block) {
         for (int i = begin; i < begin + realLength; i++) {
-            if (block.yield(context, values[i]).isTrue()) return values[i];
+            IRubyObject value = values[i];
+            if (block.yield(context, value).isTrue()) return value;
         }
 
         return ifnone != null ? ifnone.callMethod(context, "call") : 
{noformat}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to