[ruby-core:90932] [Ruby trunk Bug#15479] Array#reject! modifies literal Array

From: tenderlove@...
Date: 2019-01-08 17:24:23 UTC
List: ruby-core #90932
Issue #15479 has been updated by tenderlovemaking (Aaron Patterson).


It seems like this is also a bug in Ruby 2.5 and Ruby 2.4.  If you modify the test program a little bit like this:

~~~ ruby
def foo
  b = [1, 2, 3, 4]
  3.times do
    a = b.dup
    puts "initial: #{a}"
    begin
      a.reject! do |x|
        case x
        when 2 then true
        when 3 then raise StandardError, 'Oops'
        else false
        end
      end
    rescue StandardError
    end

    puts "after:   #{a}"
  end
end

foo
~~~

It will fail on 2.4.X, 2.5.X, and 2.6.  It seems like "reject!" isn't unsharing the array.  I will make a patch to fix this.


----------------------------------------
Bug #15479: Array#reject! modifies literal Array
https://bugs.ruby-lang.org/issues/15479#change-76129

* Author: Eregon (Benoit Daloze)
* Status: Assigned
* Priority: Normal
* Assignee: tenderlovemaking (Aaron Patterson)
* Target version: 
* ruby -v: ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
* Backport: 2.4: DONTNEED, 2.5: DONTNEED, 2.6: REQUIRED
----------------------------------------
This was found by running ruby/spec with -R2 (such as mspec -R2 -fs core/array/reject_spec.rb).
TravisCI log on https://travis-ci.org/ruby/spec/jobs/473175799#L539

Here is a simple reproducer.
MRI seems to modify the Array literal permanently:

```ruby
3.times do
  a = [1, 2, 3, 4]
  puts "initial: #{a}"
  begin
    a.reject! do |x|
      case x
      when 2 then true
      when 3 then raise StandardError, 'Oops'
      else false
      end
    end
  rescue StandardError
  end

  puts "after:   #{a}"
end
```

prints

```
initial: [1, 2, 3, 4]
after:   [1, 3, 4]
initial: [1, 3, 4, 4]
after:   [1, 3, 4, 4]
initial: [1, 3, 4, 4]
after:   [1, 3, 4, 4]
```

2.5.3 behaves fine, but trunk is also affected.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next