From 0e272178d1887b0ef0ccac61876c6f5e30fea22f Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 3 Sep 2024 09:10:14 +0200 Subject: [PATCH] Add more usage of SystemException Prefer this exception for failures involving errno as it gives a better error description. --- common/rfb/SSecurityRSAAES.cxx | 5 +++-- vncviewer/ServerDialog.cxx | 17 ++++++++++------- vncviewer/UserDialog.cxx | 3 ++- vncviewer/parameters.cxx | 17 ++++++++++------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/common/rfb/SSecurityRSAAES.cxx b/common/rfb/SSecurityRSAAES.cxx index 5b3a04d2..92b332d6 100644 --- a/common/rfb/SSecurityRSAAES.cxx +++ b/common/rfb/SSecurityRSAAES.cxx @@ -24,6 +24,7 @@ #error "This source should not be compiled without HAVE_NETTLE defined" #endif +#include #include #include #include @@ -155,7 +156,7 @@ void SSecurityRSAAES::loadPrivateKey() { FILE* file = fopen(keyFile, "rb"); if (!file) - throw Exception("failed to open key file"); + throw rdr::SystemException("failed to open key file", errno); fseek(file, 0, SEEK_END); size_t size = ftell(file); if (size == 0 || size > MaxKeyFileSize) { @@ -166,7 +167,7 @@ void SSecurityRSAAES::loadPrivateKey() std::vector data(size); if (fread(data.data(), 1, data.size(), file) != size) { fclose(file); - throw Exception("failed to read key"); + throw rdr::SystemException("failed to read key", errno); } fclose(file); diff --git a/vncviewer/ServerDialog.cxx b/vncviewer/ServerDialog.cxx index d51b8713..f9e3ffb3 100644 --- a/vncviewer/ServerDialog.cxx +++ b/vncviewer/ServerDialog.cxx @@ -38,6 +38,7 @@ #include #include #include +#include #include "fltk/layout.h" #include "ServerDialog.h" @@ -329,8 +330,8 @@ void ServerDialog::loadServerHistory() // no history file return; } - throw Exception(_("Could not open \"%s\": %s"), - filepath, strerror(errno)); + std::string msg = format(_("Could not open \"%s\""), filepath); + throw rdr::SystemException(msg.c_str(), errno); } int lineNr = 0; @@ -344,8 +345,9 @@ void ServerDialog::loadServerHistory() break; fclose(f); - throw Exception(_("Failed to read line %d in file %s: %s"), - lineNr, filepath, strerror(errno)); + std::string msg = format(_("Failed to read line %d in " + "file \"%s\""), lineNr, filepath); + throw rdr::SystemException(msg.c_str(), errno); } int len = strlen(line); @@ -390,9 +392,10 @@ void ServerDialog::saveServerHistory() /* Write server history to file */ FILE* f = fopen(filepath, "w+"); - if (!f) - throw Exception(_("Could not open \"%s\": %s"), - filepath, strerror(errno)); + if (!f) { + std::string msg = format(_("Could not open \"%s\""), filepath); + throw rdr::SystemException(msg.c_str(), errno); + } // Save the last X elements to the config file. for(size_t idx=0; idx < serverHistory.size() && idx <= SERVER_HISTORY_SIZE; idx++) diff --git a/vncviewer/UserDialog.cxx b/vncviewer/UserDialog.cxx index 13821a9c..6ea67d6d 100644 --- a/vncviewer/UserDialog.cxx +++ b/vncviewer/UserDialog.cxx @@ -21,6 +21,7 @@ #endif #include +#include #include #include @@ -114,7 +115,7 @@ void UserDialog::getUserPasswd(bool secure_, std::string* user, fp = fopen(passwordFileName, "rb"); if (!fp) - throw rfb::Exception(_("Opening password file failed")); + throw rdr::SystemException(_("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 2e8ad7a1..e40391a2 100644 --- a/vncviewer/parameters.cxx +++ b/vncviewer/parameters.cxx @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -640,9 +641,10 @@ void saveViewerParameters(const char *filename, const char *servername) { /* Write parameters to file */ FILE* f = fopen(filepath, "w+"); - if (!f) - throw Exception(_("Could not open \"%s\": %s"), - filepath, strerror(errno)); + if (!f) { + std::string msg = format(_("Could not open \"%s\""), filepath); + throw rdr::SystemException(msg.c_str(), errno); + } fprintf(f, "%s\n", IDENTIFIER_STRING); fprintf(f, "\n"); @@ -747,8 +749,8 @@ char* loadViewerParameters(const char *filename) { if (!f) { if (!filename) return nullptr; // Use defaults. - throw Exception(_("Could not open \"%s\": %s"), - filepath, strerror(errno)); + std::string msg = format(_("Could not open \"%s\""), filepath); + throw rdr::SystemException(msg.c_str(), errno); } int lineNr = 0; @@ -761,8 +763,9 @@ char* loadViewerParameters(const char *filename) { break; fclose(f); - throw Exception(_("Failed to read line %d in file %s: %s"), - lineNr, filepath, strerror(errno)); + std::string msg = format(_("Failed to read line %d in " + "file \"%s\""), lineNr, filepath); + throw rdr::SystemException(msg.c_str(), errno); } if (strlen(line) == (sizeof(line) - 1)) { -- 2.39.5