ruby-changes:73064
From: Jeremy <ko1@a...>
Date: Fri, 26 Aug 2022 05:36:35 +0900 (JST)
Subject: [ruby-changes:73064] 21cac42385 (master): Raise ArgumentError for IO.foreach with limit of 0
https://git.ruby-lang.org/ruby.git/commit/?id=21cac42385 From 21cac42385e1a116d287e155e461453b830640d2 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Thu, 26 May 2022 10:37:01 -0700 Subject: Raise ArgumentError for IO.foreach with limit of 0 Makes behavior consistent with IO.readlines. Fixes [Bug #18767] --- io.c | 2 ++ test/ruby/test_io.rb | 2 ++ 2 files changed, 4 insertions(+) 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) https://github.com/ruby/ruby/blob/trunk/io.c#L11518 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2602 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) -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/