@@ -1881,9 +1881,10 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1881
1881
1882
1882
VALUE io = rb_attr_get (self , id_i_io );
1883
1883
1884
- rb_str_locktmp (str );
1885
1884
for (;;) {
1885
+ rb_str_locktmp (str );
1886
1886
int nread = SSL_read (ssl , RSTRING_PTR (str ), ilen );
1887
+ rb_str_unlocktmp (str );
1887
1888
1888
1889
cb_state = rb_attr_get (self , ID_callback_state );
1889
1890
if (!NIL_P (cb_state )) {
@@ -1894,32 +1895,27 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1894
1895
1895
1896
switch (ssl_get_error (ssl , nread )) {
1896
1897
case SSL_ERROR_NONE :
1897
- rb_str_unlocktmp (str );
1898
1898
rb_str_set_len (str , nread );
1899
1899
return str ;
1900
1900
case SSL_ERROR_ZERO_RETURN :
1901
- rb_str_unlocktmp (str );
1902
1901
if (no_exception_p (opts )) { return Qnil ; }
1903
1902
rb_eof_error ();
1904
1903
case SSL_ERROR_WANT_WRITE :
1905
1904
if (nonblock ) {
1906
- rb_str_unlocktmp (str );
1907
1905
if (no_exception_p (opts )) { return sym_wait_writable ; }
1908
1906
write_would_block (nonblock );
1909
1907
}
1910
1908
io_wait_writable (io );
1911
- continue ;
1909
+ break ;
1912
1910
case SSL_ERROR_WANT_READ :
1913
1911
if (nonblock ) {
1914
- rb_str_unlocktmp (str );
1915
1912
if (no_exception_p (opts )) { return sym_wait_readable ; }
1916
1913
read_would_block (nonblock );
1917
1914
}
1918
1915
io_wait_readable (io );
1919
- continue ;
1916
+ break ;
1920
1917
case SSL_ERROR_SYSCALL :
1921
1918
if (!ERR_peek_error ()) {
1922
- rb_str_unlocktmp (str );
1923
1919
if (errno )
1924
1920
rb_sys_fail (0 );
1925
1921
else {
@@ -1936,9 +1932,13 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1936
1932
}
1937
1933
/* fall through */
1938
1934
default :
1939
- rb_str_unlocktmp (str );
1940
1935
ossl_raise (eSSLError , "SSL_read" );
1941
1936
}
1937
+
1938
+ // Ensure the buffer is not modified during io_wait_*able()
1939
+ rb_str_modify (str );
1940
+ if (rb_str_capacity (str ) < (size_t )ilen )
1941
+ rb_raise (eSSLError , "read buffer was modified" );
1942
1942
}
1943
1943
}
1944
1944
0 commit comments