Skip to content

Commit 0845299

Browse files
committed
read: don't clear buffer when nothing can be read
To be consistent with regular Ruby IOs: ```ruby r, _ = IO.pipe buf = "garbage".b r.read_nonblock(10, buf, exception: false) # => :wait_readable p buf # => "garbage" ``` Ref: redis-rb/redis-client@98b8944
1 parent 97305cf commit 0845299

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

ext/openssl/ossl_ssl.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,9 +1958,11 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
19581958
else
19591959
rb_str_modify_expand(str, ilen - RSTRING_LEN(str));
19601960
}
1961-
rb_str_set_len(str, 0);
1962-
if (ilen == 0)
1963-
return str;
1961+
1962+
if (ilen == 0) {
1963+
rb_str_set_len(str, 0);
1964+
return str;
1965+
}
19641966

19651967
VALUE io = rb_attr_get(self, id_i_io);
19661968

test/openssl/test_pair.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,17 @@ def test_read_with_outbuf
250250

251251
buf = +"garbage"
252252
assert_equal :wait_readable, s2.read_nonblock(100, buf, exception: false)
253-
assert_equal "", buf
253+
assert_equal "garbage", buf
254254

255255
s1.close
256256
buf = +"garbage"
257-
assert_equal nil, s2.read(100, buf)
257+
assert_nil s2.read(100, buf)
258258
assert_equal "", buf
259+
260+
buf = +"garbage"
261+
ret = s2.read(0, buf)
262+
assert_same buf, ret
263+
assert_equal "", ret
259264
}
260265
end
261266

0 commit comments

Comments
 (0)