From 8ce7860e8ae08b7622a3f2e5df17eeb10ff58014 Mon Sep 17 00:00:00 2001 From: WoodyAtHome Date: Mon, 16 May 2022 07:44:34 +0200 Subject: [PATCH] net.openssl: read doesn't block infinitely (#14406) --- vlib/net/openssl/ssl_connection.v | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/vlib/net/openssl/ssl_connection.v b/vlib/net/openssl/ssl_connection.v index ec79c6d261..3de3c92ca8 100644 --- a/vlib/net/openssl/ssl_connection.v +++ b/vlib/net/openssl/ssl_connection.v @@ -151,30 +151,31 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) ?int { mut res := 0 for { res = C.SSL_read(voidptr(s.ssl), buf_ptr, len) - if res < 0 { + if res >= 0 { + return res + } else { err_res := ssl_error(res, s.ssl)? - if err_res == .ssl_error_want_read { - for { + match err_res { + .ssl_error_want_read { ready := @select(s.handle, .read, s.duration)? - if ready { - break + if !ready { + return net.err_timed_out } } - continue - } else if err_res == .ssl_error_want_write { - for { + .ssl_error_want_write { ready := @select(s.handle, .write, s.duration)? - if ready { - break + if !ready { + return net.err_timed_out } } - continue - } else if err_res == .ssl_error_zero_return { - return 0 + .ssl_error_zero_return { + return 0 + } + else { + return error('Could not read using SSL. ($err_res)') + } } - return error('Could not read using SSL. ($err_res)') } - break } return res }