From 9c98bbd79f152fd72b257f9c37ca185587e56225 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 5 May 2011 13:24:36 -0700 Subject: read_write: call rb_str_modify() before rb_str_resize() This is needed under Ruby trunk if the string is not actually resized. --- ext/kgio/read_write.c | 2 ++ test/lib_read_write.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c index 330c2f4..7df7070 100644 --- a/ext/kgio/read_write.c +++ b/ext/kgio/read_write.c @@ -68,6 +68,7 @@ static void prepare_read(struct io_args *a, int argc, VALUE *argv, VALUE io) a->buf = rb_str_new(NULL, a->len); } else { StringValue(a->buf); + rb_str_modify(a->buf); rb_str_resize(a->buf, a->len); } a->ptr = RSTRING_PTR(a->buf); @@ -86,6 +87,7 @@ static int read_check(struct io_args *a, long n, const char *msg, int io_wait) (void)kgio_call_wait_readable(a->io); /* buf may be modified in other thread/fiber */ + rb_str_modify(a->buf); rb_str_resize(a->buf, a->len); a->ptr = RSTRING_PTR(a->buf); return -1; diff --git a/test/lib_read_write.rb b/test/lib_read_write.rb index 593a9e9..d7f50c2 100644 --- a/test/lib_read_write.rb +++ b/test/lib_read_write.rb @@ -30,6 +30,24 @@ module LibReadWriteTest assert_equal "", buf end + def test_read_shared + a = "." * 0x1000 + b = a.dup + @wr.syswrite "a" + assert_equal "a", @rd.kgio_read(0x1000, a) + assert_equal "a", a + assert_equal "." * 0x1000, b + end + + def test_read_shared_2 + a = "." * 0x1000 + b = a.dup + @wr.syswrite "a" + assert_equal "a", @rd.kgio_read(0x1000, b) + assert_equal "a", b + assert_equal "." * 0x1000, a + end + def test_tryread_zero assert_equal "", @rd.kgio_tryread(0) buf = "foo" @@ -37,6 +55,24 @@ module LibReadWriteTest assert_equal "", buf end + def test_tryread_shared + a = "." * 0x1000 + b = a.dup + @wr.syswrite("a") + assert_equal "a", @rd.kgio_tryread(0x1000, b) + assert_equal "a", b + assert_equal "." * 0x1000, a + end + + def test_tryread_shared_2 + a = "." * 0x1000 + b = a.dup + @wr.syswrite("a") + assert_equal "a", @rd.kgio_tryread(0x1000, a) + assert_equal "a", a + assert_equal "." * 0x1000, b + end + def test_read_eof @wr.close assert_nil @rd.kgio_read(5) -- cgit v1.2.3-24-ge0c7