LibTLS: Make TLSv12::can_read_without_blocking() respect the timeout arg

Previously this function's return value was not reliable, as available
data on the underlying socket did not necessarily translate to available
application data on the TLS socket.
This commit is contained in:
Ali Mohammad Pur 2025-05-13 12:03:07 +02:00 committed by Ali Mohammad Pur
commit b374322e38
Notes: github-actions[bot] 2025-06-11 16:18:20 +00:00
2 changed files with 18 additions and 2 deletions

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ElapsedTimer.h>
#include <LibCore/Promise.h>
#include <LibCrypto/OpenSSL.h>
#include <LibTLS/TLSv12.h>
@ -137,7 +138,22 @@ ErrorOr<bool> TLSv12::can_read_without_blocking(int timeout) const
if (!m_ssl)
return Error::from_string_literal("SSL connection is closed");
return m_socket->can_read_without_blocking(timeout);
if (SSL_has_pending(m_ssl))
return true;
auto timer = Core::ElapsedTimer();
while (timeout > 0) {
auto elapsed = timer.elapsed_milliseconds();
if (elapsed >= timeout)
break;
if (!TRY(m_socket->can_read_without_blocking(timeout - elapsed)))
return SSL_has_pending(m_ssl);
if (SSL_has_pending(m_ssl))
return true;
}
return false;
}
ErrorOr<void> TLSv12::set_blocking(bool)

View file

@ -55,7 +55,7 @@ public:
virtual void close() override;
virtual ErrorOr<size_t> pending_bytes() const override;
virtual ErrorOr<bool> can_read_without_blocking(int = 0) const override;
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override;
virtual ErrorOr<void> set_blocking(bool block) override;
virtual ErrorOr<void> set_close_on_exec(bool enabled) override;