Skip to content

Commit

Permalink
read: don't clear buffer when nothing can be read
Browse files Browse the repository at this point in the history
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
  • Loading branch information
byroot committed May 2, 2024
1 parent 97305cf commit 0845299
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions ext/openssl/ossl_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1958,9 +1958,11 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
else
rb_str_modify_expand(str, ilen - RSTRING_LEN(str));
}
rb_str_set_len(str, 0);
if (ilen == 0)
return str;

if (ilen == 0) {
rb_str_set_len(str, 0);
return str;
}

VALUE io = rb_attr_get(self, id_i_io);

Expand Down
9 changes: 7 additions & 2 deletions test/openssl/test_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,17 @@ def test_read_with_outbuf

buf = +"garbage"
assert_equal :wait_readable, s2.read_nonblock(100, buf, exception: false)
assert_equal "", buf
assert_equal "garbage", buf

s1.close
buf = +"garbage"
assert_equal nil, s2.read(100, buf)
assert_nil s2.read(100, buf)
assert_equal "", buf

buf = +"garbage"
ret = s2.read(0, buf)
assert_same buf, ret
assert_equal "", ret
}
end

Expand Down

0 comments on commit 0845299

Please sign in to comment.