Prevent calling `SSL_read` with `num == 0`

If whole packet was read from socket buffer, then `*actual_len ==
bytes`. But this means that `SSL_read` will be called with `num == 0`;
in this case `ssl3_read_bytes` from OpenSSL, for example, return 0 and
MQTT library treats connection as closed by remote peer.

Signed-off-by: Andrey Starodubtsev <andrey.starodubtsev@gmail.com>
This commit is contained in:
Andrey Starodubtsev 2021-11-11 00:00:23 +03:00
parent 2e57b65ca0
commit ed2a680b9a
1 changed files with 14 additions and 11 deletions

View File

@ -884,23 +884,26 @@ char *SSLSocket_getdata(SSL* ssl, int socket, size_t bytes, size_t* actual_len,
buf = SocketBuffer_getQueuedData(socket, bytes, actual_len);
ERR_clear_error();
if ((*rc = SSL_read(ssl, buf + (*actual_len), (int)(bytes - (*actual_len)))) < 0)
if (*actual_len != bytes)
{
*rc = SSLSocket_error("SSL_read - getdata", ssl, socket, *rc, NULL, NULL);
if (*rc != SSL_ERROR_WANT_READ && *rc != SSL_ERROR_WANT_WRITE)
ERR_clear_error();
if ((*rc = SSL_read(ssl, buf + (*actual_len), (int)(bytes - (*actual_len)))) < 0)
{
*rc = SSLSocket_error("SSL_read - getdata", ssl, socket, *rc, NULL, NULL);
if (*rc != SSL_ERROR_WANT_READ && *rc != SSL_ERROR_WANT_WRITE)
{
buf = NULL;
goto exit;
}
}
else if (*rc == 0) /* rc 0 means the other end closed the socket */
{
buf = NULL;
goto exit;
}
else
*actual_len += *rc;
}
else if (*rc == 0) /* rc 0 means the other end closed the socket */
{
buf = NULL;
goto exit;
}
else
*actual_len += *rc;
if (*actual_len == bytes)
{