Skip to content

Commit bc7debe

Browse files
authored
Merge pull request #10558 from Icinga/fix-posix-error-double-free
Fix double-free error in posix_error::what()
2 parents 3674af7 + db4e984 commit bc7debe

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

lib/base/exception.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -335,40 +335,40 @@ void ScriptError::SetHandledByDebugger(bool handled)
335335
m_HandledByDebugger = handled;
336336
}
337337

338-
posix_error::~posix_error() throw()
338+
const char* posix_error::what() const noexcept
339339
{
340-
free(m_Message);
341-
}
342-
343-
const char *posix_error::what() const throw()
344-
{
345-
if (!m_Message) {
346-
std::ostringstream msgbuf;
347-
348-
const char * const *func = boost::get_error_info<boost::errinfo_api_function>(*this);
349-
350-
if (func)
351-
msgbuf << "Function call '" << *func << "'";
352-
else
353-
msgbuf << "Function call";
354-
355-
const std::string *fname = boost::get_error_info<boost::errinfo_file_name>(*this);
340+
if (m_Message.IsEmpty()) {
341+
try {
342+
const char* const* func = boost::get_error_info<boost::errinfo_api_function>(*this);
343+
if (func) {
344+
m_Message += "Function call '" + std::string(*func) + "'";
345+
} else {
346+
m_Message += "Function call";
347+
}
356348

357-
if (fname)
358-
msgbuf << " for file '" << *fname << "'";
349+
const std::string* fname = boost::get_error_info<boost::errinfo_file_name>(*this);
359350

360-
msgbuf << " failed";
351+
if (fname) {
352+
m_Message += " for file '" + *fname + "'";
353+
}
361354

362-
const int *errnum = boost::get_error_info<boost::errinfo_errno>(*this);
355+
m_Message += " failed";
363356

364-
if (errnum)
365-
msgbuf << " with error code " << *errnum << ", '" << strerror(*errnum) << "'";
357+
const int* errnum = boost::get_error_info<boost::errinfo_errno>(*this);
366358

367-
String str = msgbuf.str();
368-
m_Message = strdup(str.CStr());
359+
if (errnum) {
360+
m_Message += " with error code " + std::to_string(*errnum) + ", '" + strerror(*errnum) + "'";
361+
}
362+
} catch (const std::bad_alloc&) {
363+
m_Message.Clear();
364+
return "Exception in 'posix_error::what()': bad_alloc";
365+
} catch (...) {
366+
m_Message.Clear();
367+
return "Exception in 'posix_error::what()'";
368+
}
369369
}
370370

371-
return m_Message;
371+
return m_Message.CStr();
372372
}
373373

374374
ValidationError::ValidationError(const ConfigObject::Ptr& object, const std::vector<String>& attributePath, const String& message)

lib/base/exception.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,10 @@ String DiagnosticInformation(const boost::exception_ptr& eptr, bool verbose = tr
118118

119119
class posix_error : virtual public std::exception, virtual public boost::exception {
120120
public:
121-
~posix_error() throw() override;
122-
123-
const char *what(void) const throw() final;
121+
const char* what() const noexcept final;
124122

125123
private:
126-
mutable char *m_Message{nullptr};
124+
mutable String m_Message;
127125
};
128126

129127
#ifdef _WIN32

0 commit comments

Comments
 (0)