@@ -1959,9 +1959,10 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1959
1959
1960
1960
VALUE io = rb_attr_get (self , id_i_io );
1961
1961
1962
- rb_str_locktmp (str );
1963
1962
for (;;) {
1963
+ rb_str_locktmp (str );
1964
1964
int nread = SSL_read (ssl , RSTRING_PTR (str ), ilen );
1965
+ rb_str_unlocktmp (str );
1965
1966
1966
1967
cb_state = rb_attr_get (self , ID_callback_state );
1967
1968
if (!NIL_P (cb_state )) {
@@ -1972,32 +1973,27 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1972
1973
1973
1974
switch (ssl_get_error (ssl , nread )) {
1974
1975
case SSL_ERROR_NONE :
1975
- rb_str_unlocktmp (str );
1976
1976
rb_str_set_len (str , nread );
1977
1977
return str ;
1978
1978
case SSL_ERROR_ZERO_RETURN :
1979
- rb_str_unlocktmp (str );
1980
1979
if (no_exception_p (opts )) { return Qnil ; }
1981
1980
rb_eof_error ();
1982
1981
case SSL_ERROR_WANT_WRITE :
1983
1982
if (nonblock ) {
1984
- rb_str_unlocktmp (str );
1985
1983
if (no_exception_p (opts )) { return sym_wait_writable ; }
1986
1984
write_would_block (nonblock );
1987
1985
}
1988
1986
io_wait_writable (io );
1989
- continue ;
1987
+ break ;
1990
1988
case SSL_ERROR_WANT_READ :
1991
1989
if (nonblock ) {
1992
- rb_str_unlocktmp (str );
1993
1990
if (no_exception_p (opts )) { return sym_wait_readable ; }
1994
1991
read_would_block (nonblock );
1995
1992
}
1996
1993
io_wait_readable (io );
1997
- continue ;
1994
+ break ;
1998
1995
case SSL_ERROR_SYSCALL :
1999
1996
if (!ERR_peek_error ()) {
2000
- rb_str_unlocktmp (str );
2001
1997
if (errno )
2002
1998
rb_sys_fail (0 );
2003
1999
else {
@@ -2014,9 +2010,13 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
2014
2010
}
2015
2011
/* fall through */
2016
2012
default :
2017
- rb_str_unlocktmp (str );
2018
2013
ossl_raise (eSSLError , "SSL_read" );
2019
2014
}
2015
+
2016
+ // Ensure the buffer is not modified during io_wait_*able()
2017
+ rb_str_modify (str );
2018
+ if (rb_str_capacity (str ) < (size_t )ilen )
2019
+ rb_raise (eSSLError , "read buffer was modified" );
2020
2020
}
2021
2021
}
2022
2022
0 commit comments