summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--io.c2
-rw-r--r--test/ruby/test_io.rb2
2 files changed, 4 insertions, 0 deletions
diff --git a/io.c b/io.c
index f2c64989df..1eb48dd19b 100644
--- a/io.c
+++ b/io.c
@@ -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);
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 6a3d7594cf..d6fcf16ddd 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -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)