1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Raise ArgumentError for IO.foreach with limit of 0

Makes behavior consistent with IO.readlines.

Fixes [Bug #18767]
This commit is contained in:
Jeremy Evans 2022-05-26 10:37:01 -07:00
parent 881bc2a176
commit 21cac42385
Notes: git 2022-08-26 05:36:40 +09:00
2 changed files with 4 additions and 0 deletions

2
io.c
View file

@ -11518,6 +11518,8 @@ io_s_foreach(VALUE v)
struct getline_arg *arg = (void *)v;
VALUE str;
if (arg->limit == 0)
rb_raise(rb_eArgError, "invalid limit: 0 for foreach");
while (!NIL_P(str = rb_io_getline_1(arg->rs, arg->limit, arg->chomp, arg->io))) {
rb_lastline_set(str);
rb_yield(str);

View file

@ -2602,6 +2602,8 @@ class TestIO < Test::Unit::TestCase
bug = '[ruby-dev:31525]'
assert_raise(ArgumentError, bug) {IO.foreach}
assert_raise(ArgumentError, "[Bug #18767] [ruby-core:108499]") {IO.foreach(__FILE__, 0){}}
a = nil
assert_nothing_raised(ArgumentError, bug) {a = IO.foreach(t.path).to_a}
assert_equal(["foo\n", "bar\n", "baz\n"], a, bug)