about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-05-05 13:24:36 -0700
committerEric Wong <normalperson@yhbt.net>2011-05-05 13:29:15 -0700
commit9c98bbd79f152fd72b257f9c37ca185587e56225 (patch)
tree4112448567043f78b832faeff484f903f013127d
parentc821ebeb851807840f74c4cb4f1a10e691bf222a (diff)
downloadkgio-9c98bbd79f152fd72b257f9c37ca185587e56225.tar.gz
This is needed under Ruby trunk if the string is not actually
resized.
-rw-r--r--ext/kgio/read_write.c2
-rw-r--r--test/lib_read_write.rb36
2 files changed, 38 insertions, 0 deletions
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)