Bug #2545

Array#delete_if is borked if user calls 'break'

Added by Brian Candler over 4 years ago. Updated almost 3 years ago.

[ruby-core:27366]
Status:Closed
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:-
ruby -v:ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux] Backport:

Description

=begin
Array is corrupted if you break out of a delete_if { ... } loop. I would expect that the elements already marked as deleted would be deleted, and the remainder of the array would be unchanged.

a = [5,6,7,8,9,10]
=> [5, 6, 7, 8, 9, 10]
a.delete_if { |x| break if x > 8; x < 7 }
=> nil
a
=> [7, 8, 7, 8, 9, 10]

RUBYVERSION
=> "1.8.7"
RUBY
PATCHLEVEL
=> 174
=end

Associated revisions

Revision 32360
Added by Nobuyoshi Nakada almost 3 years ago

  • array.c (rbaryrejectbang, rbarydeleteif): rejected elements should be removed. fixed [Bug #2545]

Revision 32373
Added by Nobuyoshi Nakada almost 3 years ago

  • array.c (aryrejectbang): should not remove elements which are not yielded. [Bug #2545]

History

#1 Updated by Nobuyoshi Nakada over 4 years ago

=begin
Hi,

At Sat, 2 Jan 2010 05:55:00 +0900,
Brian Candler wrote in :

Array is corrupted if you break out of a delete_if { ... }
loop. I would expect that the elements already marked as
deleted would be deleted, and the remainder of the array
would be unchanged.

The behavior would be an implementation detail, and should be
undefined (or implementation defined), I guess.

Index: array.c
===================================================================
--- array.c (revision 26229)
+++ array.c (working copy)
@@ -2307,7 +2307,18 @@ rbaryrejectbang(VALUE ary)
for (i1 = i2 = 0; i1 < RARRAY
LEN(ary); i1++) {
VALUE v = RARRAYPTR(ary)[i1];
- if (RTEST(rb
yield(v))) continue;
if (i1 != i2) {
+ int state = 0;
+ if (RTEST(rbprotect(rbyield, v, &state))) continue;
rbarystore(ary, i2, v);
+ if (state) {
+ VALUE *ptr = RARRAYPTR(ary);
+ long len = RARRAY
LEN(ary);
+ MEMCPY(ptr + i2 + 1, ptr + i1 + 1, VALUE, len - i1 - 1);
+ ARYSETLEN(ary, len - i1 + i2);
+ rbjumptag(state);
+ }
+ }
+ else {
+ if (RTEST(rb_yield(v))) continue;
}
i2++;

--
Nobu Nakada

=end

#2 Updated by Shyouhei Urabe over 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

=begin
I don't think so. I'd also expect as the reporter did. Isn't it a bug?

Assigning to matz because this can be a design issue.
=end

#3 Updated by Dave B almost 3 years ago

Just met this problem:
ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]

I'd also consider it a bug and that the ruby implementation should be hidden from the user. Once an element has been selected for deletion, at the end of this iteration, it should be expected to be gone. When using very large arrays, where the programmer knows of a shortcut (e.g. the rest of the array need not be considered), s/he should be encouraged to handle it with 'break'. In testing, I was left wondering whether 'delete_if' was non-destructive, because nothing had changed, and started looking for a bang! method.
To achieve the current behaviour, I only need an Array#dup above the loop.

Thanks to Nobu for working a patch.

daz

#4 Updated by Nobuyoshi Nakada almost 3 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r32360.
Brian, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • array.c (rbaryrejectbang, rbarydeleteif): rejected elements should be removed. fixed [Bug #2545]

#5 Updated by Nobuyoshi Nakada almost 3 years ago

  • Project changed from Ruby 1.8 to ruby-trunk
  • Category changed from core to core
  • Target version deleted (Ruby 1.8.7)

#6 Updated by Tomoyuki Chikanaga almost 3 years ago

Hi,

According to test added by r32360,

a = [ 5, 6, 7, 8, 9, 10 ]
a.delete_if {|i| break i if i > 8; i < 7}

it results
a # => [7, 8]

But I feel it could be [7, 8, 9, 10] because block didn't return true for 9, 10.
Matz, How do you think about it?

Also available in: Atom PDF