1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
nobu 2012-02-28 09:33:35 +00:00
parent 007b7fcdcf
commit 9b02a72d71
3 changed files with 44 additions and 0 deletions

View file

@ -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

5
io.c
View file

@ -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);

View file

@ -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