Skip to content

Commit

Permalink
Don't save exceptions as std::exception
Browse files Browse the repository at this point in the history
That type is not guaranteed to preserve anything useful at all. Instead,
try to either preserve a more specific type, or use std::runtime_error
which at least has a guaranteed message.
  • Loading branch information
CendioOssman committed Nov 26, 2024
1 parent 1cc5bb2 commit 5c14cd9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions common/rdr/TLSInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
} catch (std::exception& e) {
vlog.error("Failure reading TLS data: %s", e.what());
gnutls_transport_set_errno(self->session, EINVAL);
self->saved_exception = new std::exception(e);
self->saved_exception = new std::runtime_error(e.what());
return -1;
}

Expand Down Expand Up @@ -117,8 +117,12 @@ size_t TLSInStream::readTLS(uint8_t* buf, size_t len)
break;
};

if (n == GNUTLS_E_PULL_ERROR)
throw *saved_exception;
if (n == GNUTLS_E_PULL_ERROR) {
if (dynamic_cast<socket_error*>(saved_exception))
throw *dynamic_cast<socket_error*>(saved_exception);
else
throw std::runtime_error(saved_exception->what());
}

if (n < 0)
throw tls_error("readTLS", n);
Expand Down
10 changes: 7 additions & 3 deletions common/rdr/TLSOutStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ssize_t TLSOutStream::push(gnutls_transport_ptr_t str, const void* data,
} catch (std::exception& e) {
vlog.error("Failure sending TLS data: %s", e.what());
gnutls_transport_set_errno(self->session, EINVAL);
self->saved_exception = new std::exception(e);
self->saved_exception = new std::runtime_error(e.what());
return -1;
}

Expand Down Expand Up @@ -114,8 +114,12 @@ size_t TLSOutStream::writeTLS(const uint8_t* data, size_t length)
if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
return 0;

if (n == GNUTLS_E_PUSH_ERROR)
throw *saved_exception;
if (n == GNUTLS_E_PUSH_ERROR) {
if (dynamic_cast<socket_error*>(saved_exception))
throw *dynamic_cast<socket_error*>(saved_exception);
else
throw std::runtime_error(saved_exception->what());
}

if (n < 0)
throw tls_error("writeTLS", n);
Expand Down
2 changes: 1 addition & 1 deletion common/rfb/DecodeManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void DecodeManager::throwThreadException()
if (threadException == nullptr)
return;

std::exception e(*threadException);
std::runtime_error e(threadException->what());

delete threadException;
threadException = nullptr;
Expand Down

0 comments on commit 5c14cd9

Please sign in to comment.