diff --git a/io.c b/io.c index 348e001b0d..8cbe34706c 100644 --- a/io.c +++ b/io.c @@ -3310,6 +3310,12 @@ rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, int chomp) read_buffered_data(RSTRING_PTR(str)+len, pending - chomplen, fptr); fptr->rbuf.off += chomplen; fptr->rbuf.len -= chomplen; + if (pending == 1 && chomplen == 1 && len > 0) { + if (RSTRING_PTR(str)[len-1] == '\r') { + rb_str_resize(str, --len); + break; + } + } } len += pending - chomplen; if (cr != ENC_CODERANGE_BROKEN) diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index c83dc1c30d..f646c41a69 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -233,6 +233,19 @@ class TestIO < Test::Unit::TestCase assert_nil r.gets r.close end) + + (0..3).each do |i| + pipe(proc do |w| + w.write("a" * ((4096 << i) - 4), "\r\n" "a\r\n") + w.close + end, + proc do |r| + r.gets + assert_equal "a", r.gets(chomp: true) + assert_nil r.gets + r.close + end) + end end def test_gets_chomp_rs_nil