diff options
author | Pierre Ossman <ossman@cendio.se> | 2017-10-12 15:05:07 +0200 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2017-10-12 15:05:07 +0200 |
commit | 19225507cca088ff965ccfc91bcf6f1fcd9960b3 (patch) | |
tree | 5b9f9462cdf9f6dec8c81f4569f73be5be023758 /common/rfb/SConnection.cxx | |
parent | ae7c2002d0f92fe84d204137461e91b4d1b70ca3 (diff) | |
download | tigervnc-19225507cca088ff965ccfc91bcf6f1fcd9960b3.tar.gz tigervnc-19225507cca088ff965ccfc91bcf6f1fcd9960b3.zip |
Make exception classes have clearer messages
Include the type of exception in the string generated by each
subclass. Also simplify the constructs to what is needed.
Diffstat (limited to 'common/rfb/SConnection.cxx')
-rw-r--r-- | common/rfb/SConnection.cxx | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/common/rfb/SConnection.cxx b/common/rfb/SConnection.cxx index c5c9038c..6b810559 100644 --- a/common/rfb/SConnection.cxx +++ b/common/rfb/SConnection.cxx @@ -116,11 +116,9 @@ void SConnection::processVersionMsg() if (cp.majorVersion != 3) { // unknown protocol version - char msg[256]; - sprintf(msg,"Error: client needs protocol version %d.%d, server has %d.%d", - cp.majorVersion, cp.minorVersion, - defaultMajorVersion, defaultMinorVersion); - throwConnFailedException(msg); + throwConnFailedException("Client needs protocol version %d.%d, server has %d.%d", + cp.majorVersion, cp.minorVersion, + defaultMajorVersion, defaultMinorVersion); } if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) { @@ -150,10 +148,8 @@ void SConnection::processVersionMsg() if (*i == secTypeNone || *i == secTypeVncAuth) break; } if (i == secTypes.end()) { - char msg[256]; - sprintf(msg,"No supported security type for %d.%d client", - cp.majorVersion, cp.minorVersion); - throwConnFailedException(msg); + throwConnFailedException("No supported security type for %d.%d client", + cp.majorVersion, cp.minorVersion); } os->writeU32(*i); @@ -204,7 +200,7 @@ void SConnection::processSecurityType(int secType) state_ = RFBSTATE_SECURITY; ssecurity = security.GetSSecurity(secType); } catch (rdr::Exception& e) { - throwConnFailedException(e.str()); + throwConnFailedException("%s", e.str()); } processSecurityMsg(); @@ -236,22 +232,31 @@ void SConnection::processInitMsg() reader_->readClientInit(); } -void SConnection::throwConnFailedException(const char* msg) +void SConnection::throwConnFailedException(const char* format, ...) { - vlog.info("%s", msg); + va_list ap; + char str[256]; + + va_start(ap, format); + (void) vsnprintf(str, sizeof(str), format, ap); + va_end(ap); + + vlog.info("Connection failed: %s", str); + if (state_ == RFBSTATE_PROTOCOL_VERSION) { if (cp.majorVersion == 3 && cp.minorVersion == 3) { os->writeU32(0); - os->writeString(msg); + os->writeString(str); os->flush(); } else { os->writeU8(0); - os->writeString(msg); + os->writeString(str); os->flush(); } } + state_ = RFBSTATE_INVALID; - throw ConnFailedException(msg); + throw ConnFailedException(str); } void SConnection::writeConnFailedFromScratch(const char* msg, @@ -301,15 +306,17 @@ void SConnection::approveConnection(bool accept, const char* reason) if (state_ != RFBSTATE_QUERYING) throw Exception("SConnection::approveConnection: invalid state"); - if (!reason) reason = "Authentication failure"; - if (!cp.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) { if (accept) { os->writeU32(secResultOK); } else { os->writeU32(secResultFailed); - if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message - os->writeString(reason); + if (!cp.beforeVersion(3,8)) { // 3.8 onwards have failure message + if (reason) + os->writeString(reason); + else + os->writeString("Authentication failure"); + } } os->flush(); } @@ -321,7 +328,10 @@ void SConnection::approveConnection(bool accept, const char* reason) authSuccess(); } else { state_ = RFBSTATE_INVALID; - throw AuthFailureException(reason); + if (reason) + throw AuthFailureException(reason); + else + throw AuthFailureException(); } } |