diff options
-rw-r--r-- | ext/kgio/read_write.c | 20 | ||||
-rw-r--r-- | test/lib_read_write.rb | 14 |
2 files changed, 27 insertions, 7 deletions
diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c index 0ee7866..141b078 100644 --- a/ext/kgio/read_write.c +++ b/ext/kgio/read_write.c @@ -92,11 +92,14 @@ static VALUE my_read(int io_wait, int argc, VALUE *argv, VALUE io) long n; prepare_read(&a, argc, argv, io); - set_nonblocking(a.fd); + + if (a.len > 0) { + set_nonblocking(a.fd); retry: - n = (long)read(a.fd, a.ptr, a.len); - if (read_check(&a, n, "read", io_wait) != 0) - goto retry; + n = (long)read(a.fd, a.ptr, a.len); + if (read_check(&a, n, "read", io_wait) != 0) + goto retry; + } return a.buf; } @@ -159,10 +162,13 @@ static VALUE my_recv(int io_wait, int argc, VALUE *argv, VALUE io) long n; prepare_read(&a, argc, argv, io); + + if (a.len > 0) { retry: - n = (long)recv(a.fd, a.ptr, a.len, MSG_DONTWAIT); - if (read_check(&a, n, "recv", io_wait) != 0) - goto retry; + n = (long)recv(a.fd, a.ptr, a.len, MSG_DONTWAIT); + if (read_check(&a, n, "recv", io_wait) != 0) + goto retry; + } return a.buf; } diff --git a/test/lib_read_write.rb b/test/lib_read_write.rb index 6296586..6f90041 100644 --- a/test/lib_read_write.rb +++ b/test/lib_read_write.rb @@ -18,6 +18,20 @@ module LibReadWriteTest end end + def test_read_zero + assert_equal "", @rd.kgio_read(0) + buf = "foo" + assert_equal buf.object_id, @rd.kgio_read(0, buf).object_id + assert_equal "", buf + end + + def test_tryread_zero + assert_equal "", @rd.kgio_tryread(0) + buf = "foo" + assert_equal buf.object_id, @rd.kgio_tryread(0, buf).object_id + assert_equal "", buf + end + def test_read_eof @wr.close assert_nil @rd.kgio_read(5) |