diff --git a/lib/base/exception.cpp b/lib/base/exception.cpp index 57b324b4a..d596290f8 100644 --- a/lib/base/exception.cpp +++ b/lib/base/exception.cpp @@ -335,40 +335,40 @@ void ScriptError::SetHandledByDebugger(bool handled) m_HandledByDebugger = handled; } -posix_error::~posix_error() throw() +const char* posix_error::what() const noexcept { - free(m_Message); -} + if (m_Message.IsEmpty()) { + try { + const char* const* func = boost::get_error_info(*this); + if (func) { + m_Message += "Function call '" + std::string(*func) + "'"; + } else { + m_Message += "Function call"; + } -const char *posix_error::what() const throw() -{ - if (!m_Message) { - std::ostringstream msgbuf; + const std::string* fname = boost::get_error_info(*this); - const char * const *func = boost::get_error_info(*this); + if (fname) { + m_Message += " for file '" + *fname + "'"; + } - if (func) - msgbuf << "Function call '" << *func << "'"; - else - msgbuf << "Function call"; + m_Message += " failed"; - const std::string *fname = boost::get_error_info(*this); + const int* errnum = boost::get_error_info(*this); - if (fname) - msgbuf << " for file '" << *fname << "'"; - - msgbuf << " failed"; - - const int *errnum = boost::get_error_info(*this); - - if (errnum) - msgbuf << " with error code " << *errnum << ", '" << strerror(*errnum) << "'"; - - String str = msgbuf.str(); - m_Message = strdup(str.CStr()); + if (errnum) { + m_Message += " with error code " + std::to_string(*errnum) + ", '" + strerror(*errnum) + "'"; + } + } catch (const std::bad_alloc&) { + m_Message.Clear(); + return "Exception in 'posix_error::what()': bad_alloc"; + } catch (...) { + m_Message.Clear(); + return "Exception in 'posix_error::what()'"; + } } - return m_Message; + return m_Message.CStr(); } ValidationError::ValidationError(const ConfigObject::Ptr& object, const std::vector& attributePath, const String& message) diff --git a/lib/base/exception.hpp b/lib/base/exception.hpp index 18dab650e..53b7642c9 100644 --- a/lib/base/exception.hpp +++ b/lib/base/exception.hpp @@ -118,12 +118,10 @@ String DiagnosticInformation(const boost::exception_ptr& eptr, bool verbose = tr class posix_error : virtual public std::exception, virtual public boost::exception { public: - ~posix_error() throw() override; - - const char *what(void) const throw() final; + const char* what() const noexcept final; private: - mutable char *m_Message{nullptr}; + mutable String m_Message; }; #ifdef _WIN32