Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/openssl/ossl_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,8 @@ ossl_ssl_read(VALUE self, VALUE len, VALUE str)
rb_raise(eSSLError, "SSL session is not started yet");

ilen = NUM2INT(len);
if (ilen < 0)
rb_raise(rb_eArgError, "negative length %d given", ilen);
StringValue(str);
if ((rb_len_t)rb_str_capacity(str) >= ilen)
rb_str_modify(str);
Expand Down
30 changes: 9 additions & 21 deletions lib/openssl/buffering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,9 @@ def readbyte
# See IO#read for full details.

def read(size=nil, buf=nil)
if size == 0
if buf
buf.clear
return buf
else
return String.new
end
if size
return buf&.clear || String.new if size == 0
raise ArgumentError, "negative length #{size} given" if size < 0
end
until @eof
break if size && size <= @rbuffer.size
Expand All @@ -139,13 +135,9 @@ def read(size=nil, buf=nil)
# See IO#readpartial for full details.

def readpartial(maxlen, buf=nil)
if maxlen == 0
if buf
buf.clear
return buf
else
return String.new
end
if maxlen
return buf&.clear || String.new if maxlen == 0
raise ArgumentError, "negative length #{maxlen} given" if maxlen < 0
end
if @rbuffer.empty?
return sysread(maxlen, buf)
Expand Down Expand Up @@ -193,13 +185,9 @@ def readpartial(maxlen, buf=nil)
# it will return +nil+ instead of raising EOFError.

def read_nonblock(maxlen, buf=nil, exception: true)
if maxlen == 0
if buf
buf.clear
return buf
else
return String.new
end
if maxlen
return buf&.clear || String.new if maxlen == 0
raise ArgumentError, "negative length #{maxlen} given" if maxlen < 0
end
if @rbuffer.empty?
return sysread_nonblock(maxlen, buf, exception: exception)
Expand Down
10 changes: 10 additions & 0 deletions test/openssl/test_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def test_readpartial
assert_raise(EOFError) { s1.readpartial(10) }
assert_raise(EOFError) { s1.readpartial(10) }
assert_equal("", s1.readpartial(0))
assert_raise(ArgumentError) { s1.readpartial(-1) }
}
end

Expand Down Expand Up @@ -440,6 +441,10 @@ def test_sysread_and_syswrite
obj.define_singleton_method(:to_s) { str }
s1.syswrite(obj)
assert_equal(str, s2.sysread(str.bytesize))

# https://bugs.ruby-lang.org/issues/18880
next if !s2.is_a?(OpenSSL::SSL::SSLSocket) && RUBY_VERSION < "3.2"
assert_raise(ArgumentError) { s2.sysread(-1) }
}
end

Expand All @@ -461,6 +466,8 @@ def test_read_nonblock
s1.close
IO.select([s2])
assert_raise(EOFError) { s2.read_nonblock(10) }

assert_raise(ArgumentError) { s2.read_nonblock(-1) }
}
end

Expand Down Expand Up @@ -506,6 +513,9 @@ def test_read_with_outbuf
ret = s2.read(0, buf)
assert_same buf, ret
assert_equal "", ret

buf = +"garbage"
assert_raise(ArgumentError) { s2.read(-1, buf) }
}
end

Expand Down
Loading