From 3f6198b54b40982097b66f84b7c26b2b4455445d Mon Sep 17 00:00:00 2001 From: Federico Francescon Date: Wed, 7 Feb 2024 10:36:25 +0100 Subject: [PATCH] fixed deadlock situation with TLSSocket_read --- hal/tls/mbedtls/tls_mbedtls.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/hal/tls/mbedtls/tls_mbedtls.c b/hal/tls/mbedtls/tls_mbedtls.c index e7641aa4..068a2cc9 100644 --- a/hal/tls/mbedtls/tls_mbedtls.c +++ b/hal/tls/mbedtls/tls_mbedtls.c @@ -923,22 +923,19 @@ TLSSocket_read(TLSSocket self, uint8_t* buf, int size) int len = 0; while (len < size) { int ret = mbedtls_ssl_read(&(self->ssl), (buf + len), (size - len)); - if (ret == 0) { - break; - } else if (ret > 0) { + if (ret > 0) { len += ret; continue; } - // Negative values means errors - switch (ret) - { - case MBEDTLS_ERR_SSL_WANT_READ: // Falling through + switch (ret) { + case 0: // falling through + case MBEDTLS_ERR_SSL_WANT_READ: case MBEDTLS_ERR_SSL_WANT_WRITE: case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS: - continue; - break; + // Known "good" cases indicating the read is done + return len; case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: DEBUG_PRINT("TLS", " connection was closed gracefully\n");