From bdeaeebd48ad39898b804e0083ed1456cc64f24b Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 2 Sep 2024 16:37:36 +0200 Subject: [PATCH] Don't modify exception description dynamically In preparation for using the built-in exception types, where the string can only be set via the constructor. --- common/rdr/Exception.cxx | 102 ++++++++++++++++++--------------------- common/rdr/Exception.h | 10 +++- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/common/rdr/Exception.cxx b/common/rdr/Exception.cxx index 277da533..53637cac 100644 --- a/common/rdr/Exception.cxx +++ b/common/rdr/Exception.cxx @@ -28,6 +28,8 @@ #include #include +#include + #ifdef _WIN32 #include #include @@ -38,88 +40,80 @@ #include -#ifdef HAVE_GNUTLS -#include -#endif - using namespace rdr; Exception::Exception(const char *message) { - snprintf(str_, len, "%s", message); + snprintf(str_, sizeof(str_), "%s", message); } Exception::Exception(const std::string& message) { - snprintf(str_, len, "%s", message.c_str()); + snprintf(str_, sizeof(str_), "%s", message.c_str()); } GAIException::GAIException(const char* s, int err_) - : Exception(s), err(err_) + : Exception(rfb::format("%s: %s (%d)", s, strerror(err_).c_str(), err_)), + err(err_) +{ +} + +std::string GAIException::strerror(int err_) const { - strncat(str_, ": ", len-1-strlen(str_)); #ifdef _WIN32 - wchar_t *currStr = new wchar_t[len-strlen(str_)]; - wcsncpy(currStr, gai_strerrorW(err), len-1-strlen(str_)); - WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_), - len-1-strlen(str_), nullptr, nullptr); - delete [] currStr; + char str[256]; + + WideCharToMultiByte(CP_UTF8, 0, gai_strerrorW(err_), -1, str, + sizeof(str), nullptr, nullptr); + + return str; #else - strncat(str_, gai_strerror(err), len-1-strlen(str_)); + return gai_strerror(err_); #endif - strncat(str_, " (", len-1-strlen(str_)); - char buf[20]; -#ifdef WIN32 - if (err < 0) - sprintf(buf, "%x", err); - else -#endif - sprintf(buf,"%d",err); - strncat(str_, buf, len-1-strlen(str_)); - strncat(str_, ")", len-1-strlen(str_)); } PosixException::PosixException(const char* s, int err_) - : Exception(s), err(err_) + : Exception(rfb::format("%s: %s (%d)", s, strerror(err_).c_str(), err_)), + err(err_) +{ +} + +std::string PosixException::strerror(int err_) const { - strncat(str_, ": ", len-1-strlen(str_)); #ifdef _WIN32 - wchar_t *currStr = new wchar_t[len-strlen(str_)]; - wcsncpy(currStr, _wcserror(err), len-1-strlen(str_)); - WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_), - len-1-strlen(str_), nullptr, nullptr); - delete [] currStr; + char str[256]; + + WideCharToMultiByte(CP_UTF8, 0, _wcserror(err_), -1, str, + sizeof(str), nullptr, nullptr); + + return str; #else - strncat(str_, strerror(err), len-1-strlen(str_)); + return ::strerror(err_); #endif - strncat(str_, " (", len-1-strlen(str_)); - char buf[20]; - sprintf(buf,"%d",err); - strncat(str_, buf, len-1-strlen(str_)); - strncat(str_, ")", len-1-strlen(str_)); } #ifdef WIN32 Win32Exception::Win32Exception(const char* s, unsigned err_) - : Exception(s), err(err_) + : Exception(rfb::format("%s: %s (%d)", s, strerror(err_).c_str(), err_)), + err(err_) +{ +} + +std::string Win32Exception::strerror(unsigned err_) const { - strncat(str_, ": ", len-1-strlen(str_)); - wchar_t *currStr = new wchar_t[len-strlen(str_)]; + wchar_t wstr[256]; + char str[256]; + FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, err, 0, currStr, len-1-strlen(str_), nullptr); - WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_), - len-1-strlen(str_), nullptr, nullptr); - delete [] currStr; - - int l = strlen(str_); - if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n')) - str_[l-2] = 0; - - strncat(str_, " (", len-1-strlen(str_)); - char buf[20]; - sprintf(buf,"%d",err); - strncat(str_, buf, len-1-strlen(str_)); - strncat(str_, ")", len-1-strlen(str_)); + nullptr, err_, 0, wstr, sizeof(wstr), nullptr); + WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, + sizeof(str), nullptr, nullptr); + + int l = strlen(str); + if ((l >= 2) && (str[l-2] == '\r') && (str[l-1] == '\n')) + str[l-2] = 0; + + return str; } #endif diff --git a/common/rdr/Exception.h b/common/rdr/Exception.h index 65d3d90a..c6be8fcc 100644 --- a/common/rdr/Exception.h +++ b/common/rdr/Exception.h @@ -26,23 +26,27 @@ namespace rdr { struct Exception { - enum { len = 256 }; - char str_[len]; Exception(const char* message); Exception(const std::string& message); virtual ~Exception() {} virtual const char* str() const { return str_; } + private: + char str_[256]; }; struct PosixException : public Exception { int err; PosixException(const char* s, int err_); + private: + std::string strerror(int err_) const; }; #ifdef WIN32 struct Win32Exception : public Exception { unsigned err; Win32Exception(const char* s, unsigned err_); + private: + std::string strerror(unsigned err_) const; }; #endif @@ -59,6 +63,8 @@ namespace rdr { struct GAIException : public Exception { int err; GAIException(const char* s, int err_); + private: + std::string strerror(int err_) const; }; struct EndOfStream : public Exception { -- 2.39.5