diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-02-28 09:33:35 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-02-28 09:33:35 +0000 |
commit | 9b02a72d7183f3a76636f1168429e5d229409c66 (patch) | |
tree | 86dba5aa9318349ed4b68bd2e0f59b5b9c428da9 | |
parent | 007b7fcdcf709cbffb2ee2c6c01f9edf4daaccac (diff) |
* io.c (io_fread, io_getpartial, rb_io_sysread): set buffer size
after check if readable, which can cause thread switch.
[ruby-dev:45297][Bug #6099]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | io.c | 5 | ||||
-rw-r--r-- | test/ruby/test_io.rb | 33 |
3 files changed, 44 insertions, 0 deletions
@@ -1,3 +1,9 @@ +Tue Feb 28 18:33:30 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * io.c (io_fread, io_getpartial, rb_io_sysread): set buffer size + after check if readable, which can cause thread switch. + [ruby-dev:45297][Bug #6099] + Tue Feb 28 17:16:01 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> * lib/time.rb (Time#xmlschema): use strftime specifiers instead of @@ -1872,11 +1872,14 @@ io_bufread(char *ptr, long len, rb_io_t *fptr) return len - n; } +static void io_setstrbuf(VALUE *str, long len); + static long io_fread(VALUE str, long offset, long size, rb_io_t *fptr) { long len; + io_setstrbuf(&str, offset + size); rb_str_locktmp(str); len = io_bufread(RSTRING_PTR(str) + offset, size, fptr); rb_str_unlocktmp(str); @@ -2208,6 +2211,7 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock) if (nonblock) { rb_io_set_nonblock(fptr); } + io_setstrbuf(&str, len); rb_str_locktmp(str); n = rb_read_internal(fptr->fd, RSTRING_PTR(str), len); rb_str_unlocktmp(str); @@ -4269,6 +4273,7 @@ rb_io_sysread(int argc, VALUE *argv, VALUE io) rb_thread_wait_fd(fptr->fd); rb_io_check_closed(fptr); + io_setstrbuf(&str, ilen); rb_str_locktmp(str); n = rb_read_internal(fptr->fd, RSTRING_PTR(str), ilen); rb_str_unlocktmp(str); diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index ebddf14d5e..11f8097479 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -2240,4 +2240,37 @@ End assert_equal(1, $stdout.fileno) assert_equal(2, $stderr.fileno) end + + def test_sysread_locktmp + bug6099 = '[ruby-dev:45297]' + buf = " " * 100 + data = "a" * 100 + with_pipe do |r,w| + th = Thread.new {r.sysread(100, buf)} + Thread.pass until th.stop? + buf.replace("") + assert_empty(buf) + w.write(data) + Thread.pass while th.alive? + th.join + end + assert_equal(data, buf) + end + + def test_readpartial_locktmp + bug6099 = '[ruby-dev:45297]' + buf = " " * 100 + data = "a" * 100 + with_pipe do |r,w| + r.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) + th = Thread.new {r.readpartial(100, buf)} + Thread.pass until th.stop? + buf.replace("") + assert_empty(buf) + w.write(data) + Thread.pass while th.alive? + th.join + end + assert_equal(data, buf) + end end |