diff options
author | Pierre Ossman <ossman@cendio.se> | 2024-11-07 10:37:53 +0100 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2024-11-07 10:37:53 +0100 |
commit | f7507aea98b1a428d02fe5c41d25ee69dd5436bb (patch) | |
tree | 49f9349a1d7441874d1cb6d4428e2bcb0d63b422 /common/rdr/Exception.cxx | |
parent | 7508e9887de022e127d8fadb9f6a6bd8e9778864 (diff) | |
parent | 2b7857283b834391266e414adcff8c20f8fe3067 (diff) | |
download | tigervnc-f7507aea98b1a428d02fe5c41d25ee69dd5436bb.tar.gz tigervnc-f7507aea98b1a428d02fe5c41d25ee69dd5436bb.zip |
Merge branch 'stdexcept' of github.com:CendioOssman/tigervnc
Diffstat (limited to 'common/rdr/Exception.cxx')
-rw-r--r-- | common/rdr/Exception.cxx | 130 |
1 files changed, 75 insertions, 55 deletions
diff --git a/common/rdr/Exception.cxx b/common/rdr/Exception.cxx index 6a03fa54..694ee359 100644 --- a/common/rdr/Exception.cxx +++ b/common/rdr/Exception.cxx @@ -1,6 +1,7 @@ /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. * Copyright (C) 2004 Red Hat Inc. * Copyright (C) 2010 TigerVNC Team + * Copyright 2014-2024 Pierre Ossman for Cendio AB * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +28,8 @@ #include <rdr/Exception.h> #include <rdr/TLSException.h> +#include <rfb/util.h> + #ifdef _WIN32 #include <winsock2.h> #include <windows.h> @@ -37,77 +40,94 @@ #include <string.h> -#ifdef HAVE_GNUTLS -#include <gnutls/gnutls.h> -#endif - using namespace rdr; -Exception::Exception(const char *format, ...) { - va_list ap; - va_start(ap, format); - (void) vsnprintf(str_, len, format, ap); - va_end(ap); +getaddrinfo_error::getaddrinfo_error(const char* s, int err_) + : std::runtime_error(rfb::format("%s: %s (%d)", s, + strerror(err_).c_str(), err_)), + err(err_) +{ +} + +getaddrinfo_error::getaddrinfo_error(const std::string& s, int err_) + : std::runtime_error(rfb::format("%s: %s (%d)", s.c_str(), + strerror(err_).c_str(), err_)), + err(err_) +{ } -GAIException::GAIException(const char* s, int err_) - : Exception("%s", s), err(err_) +std::string getaddrinfo_error::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_)); -#endif - strncat(str_, " (", len-1-strlen(str_)); - char buf[20]; -#ifdef WIN32 - if (err < 0) - sprintf(buf, "%x", err); - else + return gai_strerror(err_); #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", s), err(err_) +posix_error::posix_error(const char* what_arg, int err_) + : std::runtime_error(rfb::format("%s: %s (%d)", what_arg, + strerror(err_).c_str(), err_)), + err(err_) +{ +} + +posix_error::posix_error(const std::string& what_arg, int err_) + : std::runtime_error(rfb::format("%s: %s (%d)", what_arg.c_str(), + strerror(err_).c_str(), err_)), + err(err_) { - strncat(str_, ": ", len-1-strlen(str_)); - strncat(str_, strerror(err), len-1-strlen(str_)); - 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_)); +} + +std::string posix_error::strerror(int err_) const +{ +#ifdef _WIN32 + char str[256]; + + WideCharToMultiByte(CP_UTF8, 0, _wcserror(err_), -1, str, + sizeof(str), nullptr, nullptr); + + return str; +#else + return ::strerror(err_); +#endif } #ifdef WIN32 -Win32Exception::Win32Exception(const char* s, unsigned err_) - : Exception("%s", s), err(err_) +win32_error::win32_error(const char* what_arg, unsigned err_) + : std::runtime_error(rfb::format("%s: %s (%d)", what_arg, + strerror(err_).c_str(), err_)), + err(err_) { - strncat(str_, ": ", len-1-strlen(str_)); - wchar_t *currStr = new wchar_t[len-strlen(str_)]; +} + +win32_error::win32_error(const std::string& what_arg, unsigned err_) + : std::runtime_error(rfb::format("%s: %s (%d)", what_arg.c_str(), + strerror(err_).c_str(), err_)), + err(err_) +{ +} + +std::string win32_error::strerror(unsigned err_) const +{ + 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 |