diff --git a/ChangeLog b/ChangeLog index 8de7bfc7d2..fd0f41cf3f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Tue Jul 1 03:05:22 2014 Nobuyoshi Nakada + + * io.c (read_all): truncate the buffer before appending read data, + instead of truncating before reading. + [ruby-core:55951] [Bug #8625] + +Tue Jul 1 03:05:22 2014 Nobuyoshi Nakada + + * io.c (io_setstrbuf, io_read): should not shorten the given buffer until + read succeeds. [ruby-core:55951] [Bug #8625] + Mon Jun 30 03:15:59 2014 Nobuyoshi Nakada * vm.c (core_hash_merge_kwd): should return the result hash, which diff --git a/io.c b/io.c index 6deb44187f..e5a4d3b40c 100644 --- a/io.c +++ b/io.c @@ -2291,9 +2291,6 @@ io_setstrbuf(VALUE *str, long len) long clen = RSTRING_LEN(s); if (clen >= len) { rb_str_modify(s); - if (clen != len) { - rb_str_set_len(s, len); - } return; } len -= clen; @@ -2320,23 +2317,27 @@ read_all(rb_io_t *fptr, long siz, VALUE str) int cr; if (NEED_READCONV(fptr)) { + int first = !NIL_P(str); SET_BINARY_MODE(fptr); io_setstrbuf(&str,0); make_readconv(fptr, 0); while (1) { VALUE v; if (fptr->cbuf.len) { + if (first) rb_str_set_len(str, first = 0); io_shift_cbuf(fptr, fptr->cbuf.len, &str); } v = fill_cbuf(fptr, 0); if (v != MORE_CHAR_SUSPENDED && v != MORE_CHAR_FINISHED) { if (fptr->cbuf.len) { + if (first) rb_str_set_len(str, first = 0); io_shift_cbuf(fptr, fptr->cbuf.len, &str); } rb_exc_raise(v); } if (v == MORE_CHAR_FINISHED) { clear_readconv(fptr); + if (first) rb_str_set_len(str, first = 0); return io_enc_str(str, fptr); } } @@ -2807,7 +2808,10 @@ io_read(int argc, VALUE *argv, VALUE io) GetOpenFile(io, fptr); rb_io_check_byte_readable(fptr); - if (len == 0) return str; + if (len == 0) { + io_set_read_length(str, 0); + return str; + } READ_CHECK(fptr); #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32) diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 115a498474..a7c5b2a39a 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -1224,6 +1224,14 @@ class TestIO < Test::Unit::TestCase t.value assert_equal("", s) end + with_pipe do |r, w| + s = "xxx" + t = Thread.new {r.read(2, s)} + Thread.pass until t.stop? + t.kill + t.value + assert_equal("xxx", s) + end end def test_write_nonblock diff --git a/test/ruby/test_pipe.rb b/test/ruby/test_pipe.rb index 34f231ad8c..bcea91bebb 100644 --- a/test/ruby/test_pipe.rb +++ b/test/ruby/test_pipe.rb @@ -13,4 +13,17 @@ class TestPipe < Test::Unit::TestCase r.close end end + class WithConversion < self + def open_file(content) + r, w = IO.pipe + w << content + w.close + r.set_encoding("us-ascii:utf-8") + begin + yield r + ensure + r.close + end + end + end end diff --git a/version.h b/version.h index cc54704b1b..535f2e6122 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.1.2" #define RUBY_RELEASE_DATE "2014-07-01" -#define RUBY_PATCHLEVEL 158 +#define RUBY_PATCHLEVEL 159 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 7