Bug in Array#slice!

e$BCf@n$H$$$$$^$9!#e(B

[ruby-dev:31761] e$B$G0lEY%l%]!<%H$7$?$N$G$9$,!“e(BArray e$B$N4pe(B
e$BK\E*$J$H$3$m$N%P%0$G=EMW$@$H;W$o$l$k$N$G!”$b$&0lEY>$7$/%le(B
e$B%]!<%H$7$^$9!#e(B

Array#slice! e$B$G!"Ii$NHO0O30$Ne(B index e$B$r;XDj$7$?>l9g!“Nce(B
e$B30$K$J$i$J$$$3$H$,$”$j$^$9!#e(B
trunk e$B$He(B 1.8 e$B$G:F8=$7$^$9!#e(B

e$B6qBNE*$K$O!"e(B

$ ./ruby -e ‘a = %w|a b c d e|; a.slice!(-3,2); p a’
[“a”, “b”, “e”]

$ ./ruby -e ‘a = %w|a b c d e|; a.slice!(-8,2); p a’
[“a”, “b”, “e”]

e$B$N$h$&$J>l9g$G$9!#e(B
e$B$3$N>l9g!"e(B-3 e$B$He(B -8 e$B$,F10l;k$5$l$F$7$^$C$F$$$^$9!#e(B

$ ./ruby -e ‘a = %w|a b c d e|; a.slice!(-13,2); p a’
-e:1:in slice!': index -8 out of array (IndexError) from -e:1:in

e$B$?$a$7$Ke(B -13 e$B$r;XDj$9$k$H!“e(Bindex -8 e$B$OHO0O30$@$He(B
e$B$$$&%(%i!<$K$J$k$N$G!”$I$3$+$Ge(B Array#size e$B$r8m$C$FB-$7e(B
e$B$F$7$^$C$F$$$k$3$H$,$o$+$j$^$9!#e(B

e$B%=!<%9$r8+$k$H!“e(Brb_ary_slice_bang e$B<+BN$H!”$=$3$+$i8F$s$G$$e(B
e$B$ke(B rb_ary_splice e$B$Ge(B2e$BEYB-$79~$s$G$7$^$C$F$$$k$h$&$G$9!#e(B

e$B0J2<$N%Q%C%A$G!"e(B

$ ./ruby -e ‘a = %w|a b c d e|; a.slice!(-3,2); p a’
[“a”, “b”, “e”]

$ ./ruby -e ‘a = %w|a b c d e|; a.slice!(-8,2); p a’
-e:1:in slice!': index -8 out of array (IndexError) from -e:1:in

e$B$H@5$7$/F0:n$9$k$3$H$r3NG’$7$^$7$?!#e(B

Index: array.c

— array.c (revision 14178)
+++ array.c (working copy)
@@ -1886,9 +1886,6 @@
pos = NUM2LONG(arg1);
len = NUM2LONG(arg2);
delete_pos_len:

  • if (pos < 0) {
  •  pos = RARRAY_LEN(ary) + pos;
    
  • }
    arg2 = rb_ary_subseq(ary, pos, len);
    rb_ary_splice(ary, pos, len, Qundef); /* Qnil/rb_ary_new2(0) */
    return arg2;

At Mon, 10 Dec 2007 19:27:17 +0900,
Satoshi N. wrote:

[ruby-dev:31761] で一度レポートしたのですが、Array の基 本的なところの
バグで重要だと思われるので、もう一度詳しくレ ポートします。

 これは [ruby-dev:31671] ですかね。見落とされている…。

Array#slice! ã§ã€è² ã®ç¯„å›²å¤–ã® index ã‚’æŒ‡å®šã—ãŸå ´åˆã€ä¾‹ 外にならないこと
があります。
trunk と 1.8 で再現します。

 Array#slice と挙動を合わせるべきだと思うので、ここは例外を
上げずに nil を返しましょうか。

Index: array.c

— array.c (revision 14180)
+++ array.c (working copy)
@@ -1963,6 +1963,7 @@
delete_pos_len:
if (pos < 0) {
pos = RARRAY(ary)->len + pos;

  •  if (pos < 0) return Qnil;
    
    }
    arg2 = rb_ary_subseq(ary, pos, len);
    rb_ary_splice(ary, pos, len, Qnil); /* Qnil/rb_ary_new2(0) */


/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

“Different eyes see different things,
Different hearts beat on different strings –
But there are times for you and me when all such things agree”

On 2007/12/10, at 20:17, Akinori MUSHA wrote:

e$B!!$3$l$Oe(B [ruby-dev:31671] e$B$G$9$+$M!#8+Mn$H$5$l$F$$$k!D!#e(B

e$B$9$_$^$;$s!#$=$NDL$j$G$7$?!#e(B

e$B!!e(BArray#slice e$B$H5sF0$r9g$o$;$k$Y$-$@$H;W$&$N$G!"$3$3e(B
e$B$ONc30$re(B
e$B>e$2$:$Ke(B nil e$B$rJV$7$^$7$g$&$+!#e(B

e$B3N$+$K$=$A$i$N$[$&$,$$$$$H;W$$$^$9!#e(B
Array#slice! e$B$G@5$NHO0O30$r;XDj$7$?>l9g$H!"e(BString#slice!
e$B$G@5Ii$NHO0O30$r;XDj$7$?>l9g$r8+$k$He(B nil e$B$rJV$7$F$$$k$N$G!#e(B

$ ./ruby -e ‘a = %w|a b c d e|; p a.slice!(8,2); p a’
nil
[“a”, “b”, “c”, “d”, “e”, nil, nil, nil]

$ ./ruby -e ‘s = “abcde”; p s.slice!(8,2); p s’
nil
“abcde”
$ ./ruby -e ‘s = “abcde”; p s.slice!(-8,2); p s’
nil
“abcde”

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32519] Re: bug in Array#slice!”
on Mon, 10 Dec 2007 20:17:20 +0900, “Akinori MUSHA”
[email protected] writes:
|
|[1 <text/plain; ISO-2022-JP (7bit)>]
|At Mon, 10 Dec 2007 19:27:17 +0900,
|Satoshi N. wrote:
|> [ruby-dev:31761] e$B$G0lEY%l%]!<%H$7$?$N$G$9$,!“e(BArray e$B$N4pe(B e$BK\E*$J$H$3$m$Ne(B
|> e$B%P%0$G=EMW$@$H;W$o$l$k$N$G!”$b$&0lEY>$7$/%le(B e$B%]!<%H$7$^$9!#e(B
|
|e$B!!$3$l$Oe(B [ruby-dev:31671] e$B$G$9$+$M!#8+Mn$H$5$l$F$$$k!D!#e(B

e$B$=$&$_$?$$$G$9!#$4$a$s$J$5$$!#e(B

|> Array#slice! e$B$G!“Ii$NHO0O30$Ne(B index e$B$r;XDj$7$?>l9g!“Nce(B e$B30$K$J$i$J$$$3$He(B
|> e$B$,$”$j$^$9!#e(B
|> trunk e$B$He(B 1.8 e$B$G:F8=$7$^$9!#e(B
|
|e$B!!e(BArray#slice e$B$H5sF0$r9g$o$;$k$Y$-$@$H;W$&$N$G!”$3$3$ONc30$re(B
|e$B>e$2$:$Ke(B nil e$B$rJV$7$^$7$g$&$+!#e(B

1.8e$B$K%3%_%C%H$7$F$/$@$5$$!#e(Btrunke$B$O;d$,$d$j$^$9!#e(B

At Mon, 10 Dec 2007 20:59:56 +0900,
matz wrote:

|At Mon, 10 Dec 2007 19:27:17 +0900,
|Satoshi N. wrote:
|> [ruby-dev:31761] で一度レポートしたのですが、Array の基 本的なところの
|> バグで重要だと思われるので、もう一度詳しくレ ポートします。
|
| これは [ruby-dev:31671] ですかね。見落とされている…。

そうみたいです。ごめんなさい。

 僕もです。1.8 も該当するし、 Array#slice! という名前を提案した
のは自分だった気がするので、見落としたのは痛恨です。

|> Array#slice! ã§ã€è² ã®ç¯„å›²å¤–ã® index ã‚’æŒ‡å®šã—ãŸå ´åˆã€ä¾‹ 外にならないこと
|> があります。
|> trunk と 1.8 で再現します。
|
| Array#slice と挙動を合わせるべきだと思うので、ここは例外を
|上げずに nil を返しましょうか。

1.8にコミットしてください。trunkは私がやります。

 あ、修正は同じなので一緒にやろうと思いましたが、分かりました。
よろしくお願いします。

ã€€ãƒ†ã‚¹ãƒˆã‚±ãƒ¼ã‚¹ã‚’è¿½åŠ ã—ã¦ãŠãã¾ã™ã€‚


/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

“Different eyes see different things,
Different hearts beat on different strings –
But there are times for you and me when all such things agree”

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32528] Re: bug in Array#slice!”
on Mon, 10 Dec 2007 22:28:50 +0900, “Akinori MUSHA”
[email protected] writes:

|e$B!!@h$N=$@5$@$1$@$He(B Range e$B$rEO$7$?>l9g$Ke(B rb_range_beg_len() e$B$Ke(B
|e$BNc30$r=P$5$l$F$7$^$&$N$G!"$=$A$i$bBP=h$7$^$7$?!#e(B
|
|> e$B!!%F%9%H%1!<%9$rDI2C$7$F$$-$^$9!#e(B
|
|e$B!!e(Btest_array.rb e$B$K$$$/$D$+F~$l$F$
$-$^$7$?!#e(B

e$B$*<j?t$+$1$^$7$?!#$"$j$,$H$&$4$6$$$^$9!#e(B

At Mon, 10 Dec 2007 21:16:51 +0900,
I wrote:

よろしくお願いします。
 先の修正だけだと Range ã‚’æ¸¡ã—ãŸå ´åˆã« rb_range_beg_len() に
例外を出されてしまうので、そちらも対処しました。

ã€€ãƒ†ã‚¹ãƒˆã‚±ãƒ¼ã‚¹ã‚’è¿½åŠ ã—ã¦ãŠãã¾ã™ã€‚

 test_array.rb にいくつか入れておきました。

 あとで 1.8 にも入れます。


/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

“Different eyes see different things,
Different hearts beat on different strings –
But there are times for you and me when all such things agree”