From 40df30d258ebfd24a447fababc649867c24513d8 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 10 Sep 2024 16:55:32 +0200 Subject: Split SystemException to handle Windows Windows has (at least) two error namespaces, both errno and GetLastResult(). These overlap, so it is important we keep track of which one we are dealing with. To make things extra problematic, the BSD socket API normally uses errno, but on Windows it has been mapped in to the GetLastResult() namespace. Try to keep better control of this by using separate classes for the namespaces. --- vncviewer/ServerDialog.cxx | 6 +++--- vncviewer/Surface_Win32.cxx | 22 +++++++++++----------- vncviewer/UserDialog.cxx | 2 +- vncviewer/parameters.cxx | 32 ++++++++++++++++---------------- 4 files changed, 31 insertions(+), 31 deletions(-) (limited to 'vncviewer') diff --git a/vncviewer/ServerDialog.cxx b/vncviewer/ServerDialog.cxx index 67082a9e..8622fff1 100644 --- a/vncviewer/ServerDialog.cxx +++ b/vncviewer/ServerDialog.cxx @@ -359,7 +359,7 @@ void ServerDialog::loadServerHistory() return; } std::string msg = format(_("Could not open \"%s\""), filepath); - throw rdr::SystemException(msg.c_str(), errno); + throw rdr::PosixException(msg.c_str(), errno); } int lineNr = 0; @@ -375,7 +375,7 @@ void ServerDialog::loadServerHistory() fclose(f); std::string msg = format(_("Failed to read line %d in " "file \"%s\""), lineNr, filepath); - throw rdr::SystemException(msg.c_str(), errno); + throw rdr::PosixException(msg.c_str(), errno); } int len = strlen(line); @@ -431,7 +431,7 @@ void ServerDialog::saveServerHistory() FILE* f = fopen(filepath, "w+"); if (!f) { std::string msg = format(_("Could not open \"%s\""), filepath); - throw rdr::SystemException(msg.c_str(), errno); + throw rdr::PosixException(msg.c_str(), errno); } // Save the last X elements to the config file. diff --git a/vncviewer/Surface_Win32.cxx b/vncviewer/Surface_Win32.cxx index 46a2b055..67ba15c5 100644 --- a/vncviewer/Surface_Win32.cxx +++ b/vncviewer/Surface_Win32.cxx @@ -57,10 +57,10 @@ void Surface::draw(int src_x, int src_y, int dst_x, int dst_y, dc = CreateCompatibleDC(fl_gc); if (!dc) - throw rdr::SystemException("CreateCompatibleDC", GetLastError()); + throw rdr::Win32Exception("CreateCompatibleDC", GetLastError()); if (!SelectObject(dc, bitmap)) - throw rdr::SystemException("SelectObject", GetLastError()); + throw rdr::Win32Exception("SelectObject", GetLastError()); if (!BitBlt(fl_gc, dst_x, dst_y, dst_w, dst_h, dc, src_x, src_y, SRCCOPY)) { @@ -70,7 +70,7 @@ void Surface::draw(int src_x, int src_y, int dst_x, int dst_y, // with it. For now, we've only seen this error and for this function // so only ignore this combination. if (GetLastError() != ERROR_INVALID_HANDLE) - throw rdr::SystemException("BitBlt", GetLastError()); + throw rdr::Win32Exception("BitBlt", GetLastError()); } DeleteDC(dc); @@ -83,10 +83,10 @@ void Surface::draw(Surface* dst, int src_x, int src_y, dstdc = CreateCompatibleDC(nullptr); if (!dstdc) - throw rdr::SystemException("CreateCompatibleDC", GetLastError()); + throw rdr::Win32Exception("CreateCompatibleDC", GetLastError()); if (!SelectObject(dstdc, dst->bitmap)) - throw rdr::SystemException("SelectObject", GetLastError()); + throw rdr::Win32Exception("SelectObject", GetLastError()); origdc = fl_gc; fl_gc = dstdc; @@ -113,15 +113,15 @@ void Surface::blend(Surface* dst, int src_x, int src_y, dstdc = CreateCompatibleDC(nullptr); if (!dstdc) - throw rdr::SystemException("CreateCompatibleDC", GetLastError()); + throw rdr::Win32Exception("CreateCompatibleDC", GetLastError()); srcdc = CreateCompatibleDC(nullptr); if (!srcdc) - throw rdr::SystemException("CreateCompatibleDC", GetLastError()); + throw rdr::Win32Exception("CreateCompatibleDC", GetLastError()); if (!SelectObject(dstdc, dst->bitmap)) - throw rdr::SystemException("SelectObject", GetLastError()); + throw rdr::Win32Exception("SelectObject", GetLastError()); if (!SelectObject(srcdc, bitmap)) - throw rdr::SystemException("SelectObject", GetLastError()); + throw rdr::Win32Exception("SelectObject", GetLastError()); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; @@ -136,7 +136,7 @@ void Surface::blend(Surface* dst, int src_x, int src_y, // with it. For now, we've only seen this error and for this function // so only ignore this combination. if (GetLastError() != ERROR_INVALID_HANDLE) - throw rdr::SystemException("BitBlt", GetLastError()); + throw rdr::Win32Exception("BitBlt", GetLastError()); } DeleteDC(srcdc); @@ -161,7 +161,7 @@ void Surface::alloc() bitmap = CreateDIBSection(nullptr, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&data, nullptr, 0); if (!bitmap) - throw rdr::SystemException("CreateDIBSection", GetLastError()); + throw rdr::Win32Exception("CreateDIBSection", GetLastError()); } void Surface::dealloc() diff --git a/vncviewer/UserDialog.cxx b/vncviewer/UserDialog.cxx index 6ea67d6d..b3840d62 100644 --- a/vncviewer/UserDialog.cxx +++ b/vncviewer/UserDialog.cxx @@ -115,7 +115,7 @@ void UserDialog::getUserPasswd(bool secure_, std::string* user, fp = fopen(passwordFileName, "rb"); if (!fp) - throw rdr::SystemException(_("Opening password file failed"), errno); + throw rdr::PosixException(_("Opening password file failed"), errno); obfPwd.resize(fread(obfPwd.data(), 1, obfPwd.size(), fp)); fclose(fp); diff --git a/vncviewer/parameters.cxx b/vncviewer/parameters.cxx index 4bbf7a7f..c75cad8b 100644 --- a/vncviewer/parameters.cxx +++ b/vncviewer/parameters.cxx @@ -318,7 +318,7 @@ static void setKeyString(const char *_name, const char *_value, HKEY* hKey) { LONG res = RegSetValueExW(*hKey, name, 0, REG_SZ, (BYTE*)&value, (wcslen(value)+1)*2); if (res != ERROR_SUCCESS) - throw rdr::SystemException("RegSetValueExW", res); + throw rdr::Win32Exception("RegSetValueExW", res); } @@ -334,7 +334,7 @@ static void setKeyInt(const char *_name, const int _value, HKEY* hKey) { LONG res = RegSetValueExW(*hKey, name, 0, REG_DWORD, (BYTE*)&value, sizeof(DWORD)); if (res != ERROR_SUCCESS) - throw rdr::SystemException("RegSetValueExW", res); + throw rdr::Win32Exception("RegSetValueExW", res); } @@ -355,7 +355,7 @@ static bool getKeyString(const char* _name, char* dest, size_t destSize, HKEY* h if (res != ERROR_SUCCESS){ delete [] value; if (res != ERROR_FILE_NOT_FOUND) - throw rdr::SystemException("RegQueryValueExW", res); + throw rdr::Win32Exception("RegQueryValueExW", res); // The value does not exist, defaults will be used. return false; } @@ -392,7 +392,7 @@ static bool getKeyInt(const char* _name, int* dest, HKEY* hKey) { LONG res = RegQueryValueExW(*hKey, name, nullptr, nullptr, (LPBYTE)&value, &dwordsize); if (res != ERROR_SUCCESS){ if (res != ERROR_FILE_NOT_FOUND) - throw rdr::SystemException("RegQueryValueExW", res); + throw rdr::Win32Exception("RegQueryValueExW", res); // The value does not exist, defaults will be used. return false; } @@ -412,7 +412,7 @@ static void removeValue(const char* _name, HKEY* hKey) { LONG res = RegDeleteValueW(*hKey, name); if (res != ERROR_SUCCESS) { if (res != ERROR_FILE_NOT_FOUND) - throw rdr::SystemException("RegDeleteValueW", res); + throw rdr::Win32Exception("RegDeleteValueW", res); // The value does not exist, no need to remove it. return; } @@ -426,7 +426,7 @@ void saveHistoryToRegKey(const list& serverHistory) { &hKey, nullptr); if (res != ERROR_SUCCESS) - throw rdr::SystemException(_("Failed to create registry key"), res); + throw rdr::Win32Exception(_("Failed to create registry key"), res); unsigned index = 0; assert(SERVER_HISTORY_SIZE < 100); @@ -447,7 +447,7 @@ void saveHistoryToRegKey(const list& serverHistory) { res = RegCloseKey(hKey); if (res != ERROR_SUCCESS) - throw rdr::SystemException(_("Failed to close registry key"), res); + throw rdr::Win32Exception(_("Failed to close registry key"), res); } static void saveToReg(const char* servername) { @@ -459,7 +459,7 @@ static void saveToReg(const char* servername) { REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &hKey, nullptr); if (res != ERROR_SUCCESS) - throw rdr::SystemException(_("Failed to create registry key"), res); + throw rdr::Win32Exception(_("Failed to create registry key"), res); try { setKeyString("ServerName", servername, &hKey); @@ -502,7 +502,7 @@ static void saveToReg(const char* servername) { res = RegCloseKey(hKey); if (res != ERROR_SUCCESS) - throw rdr::SystemException(_("Failed to close registry key"), res); + throw rdr::Win32Exception(_("Failed to close registry key"), res); } list loadHistoryFromRegKey() { @@ -518,7 +518,7 @@ list loadHistoryFromRegKey() { return serverHistory; } - throw rdr::SystemException(_("Failed to open registry key"), res); + throw rdr::Win32Exception(_("Failed to open registry key"), res); } unsigned index; @@ -545,7 +545,7 @@ list loadHistoryFromRegKey() { res = RegCloseKey(hKey); if (res != ERROR_SUCCESS) - throw rdr::SystemException(_("Failed to close registry key"), res); + throw rdr::Win32Exception(_("Failed to close registry key"), res); return serverHistory; } @@ -592,7 +592,7 @@ static char* loadFromReg() { return nullptr; } - throw rdr::SystemException(_("Failed to open registry key"), res); + throw rdr::Win32Exception(_("Failed to open registry key"), res); } const size_t buffersize = 256; @@ -614,7 +614,7 @@ static char* loadFromReg() { res = RegCloseKey(hKey); if (res != ERROR_SUCCESS) - throw rdr::SystemException(_("Failed to close registry key"), res); + throw rdr::Win32Exception(_("Failed to close registry key"), res); return servername; } @@ -648,7 +648,7 @@ void saveViewerParameters(const char *filename, const char *servername) { FILE* f = fopen(filepath, "w+"); if (!f) { std::string msg = format(_("Could not open \"%s\""), filepath); - throw rdr::SystemException(msg.c_str(), errno); + throw rdr::PosixException(msg.c_str(), errno); } fprintf(f, "%s\n", IDENTIFIER_STRING); @@ -755,7 +755,7 @@ char* loadViewerParameters(const char *filename) { if (!filename) return nullptr; // Use defaults. std::string msg = format(_("Could not open \"%s\""), filepath); - throw rdr::SystemException(msg.c_str(), errno); + throw rdr::PosixException(msg.c_str(), errno); } int lineNr = 0; @@ -770,7 +770,7 @@ char* loadViewerParameters(const char *filename) { fclose(f); std::string msg = format(_("Failed to read line %d in " "file \"%s\""), lineNr, filepath); - throw rdr::SystemException(msg.c_str(), errno); + throw rdr::PosixException(msg.c_str(), errno); } if (strlen(line) == (sizeof(line) - 1)) { -- cgit v1.2.3