Index: io.c =================================================================== --- io.c (revision 29690) +++ io.c (working copy) @@ -2724,6 +2724,8 @@ long limit; prepare_getline_args(argc, argv, &rs, &limit, io); + if (limit == 0) + rb_raise(rb_eArgError, "invalid limit: 0 for readlines"); ary = rb_ary_new(); while (!NIL_P(line = rb_io_getline_1(rs, limit, io))) { rb_ary_push(ary, line); @@ -2773,6 +2775,8 @@ RETURN_ENUMERATOR(io, argc, argv); prepare_getline_args(argc, argv, &rs, &limit, io); + if (limit == 0) + rb_raise(rb_eArgError, "invalid limit: 0 for each_line"); while (!NIL_P(str = rb_io_getline_1(rs, limit, io))) { rb_yield(str); } Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 29690) +++ test/ruby/test_io.rb (working copy) @@ -1702,4 +1702,24 @@ GC.start end end + + def test_readlines_limit_0 + bugxxx = "[ruby-dev:xxx]" + t = make_tempfile + assert_raise(ArgumentError, bugxxx) do + open(t.path, "r") do |io| + io.readlines(0) + end + end + end + + def test_each_line_limit_0 + bugxxx = "[ruby-dev:xxx]" + t = make_tempfile + assert_raise(ArgumentError, bugxxx) do + open(t.path, "r") do |io| + io.each_line(0).next + end + end + end end