Fix crash saving user brushes in unknown situations (fix #3728)

We've received crash reports where save_xml() throws an Exception()
because the user brushes file cannot be saved (open_file(..., "wb")
returns nlulptr). We're not sure why (probably privileges?) but at
least we fixed the exception.
This commit is contained in:
David Capello 2023-05-01 10:53:44 -03:00
parent 4d3575f4ce
commit 10ea91cb52
2 changed files with 22 additions and 9 deletions

View File

@ -179,20 +179,31 @@ AppBrushes::AppBrushes()
m_standard.push_back(BrushRef(new Brush(kSquareBrushType, 7, 0)));
m_standard.push_back(BrushRef(new Brush(kLineBrushType, 7, 44)));
try {
std::string fn = m_userBrushesFilename = userBrushesFilename();
if (base::is_file(fn))
std::string fn = userBrushesFilename();
if (base::is_file(fn)) {
try {
load(fn);
}
catch (const std::exception& ex) {
LOG(ERROR, "BRSH: Error loading user brushes from '%s': %s\n",
fn.c_str(), ex.what());
}
}
catch (const std::exception& ex) {
LOG(ERROR, "BRSH: Error loading user brushes: %s\n", ex.what());
}
m_userBrushesFilename = fn;
}
AppBrushes::~AppBrushes()
{
if (!m_userBrushesFilename.empty())
save(m_userBrushesFilename);
if (!m_userBrushesFilename.empty()) {
try {
save(m_userBrushesFilename);
}
// We cannot throw exceptions from a destructor
catch (const std::exception& ex) {
LOG(ERROR, "BRSH: Error saving user brushes to '%s': %s\n",
m_userBrushesFilename.c_str(), ex.what());
}
}
}
AppBrushes::slot_id AppBrushes::addBrushSlot(const BrushSlot& brush)

View File

@ -38,8 +38,10 @@ XmlDocumentRef open_xml(const std::string& filename)
void save_xml(XmlDocumentRef doc, const std::string& filename)
{
FileHandle file(open_file(filename, "wb"));
if (!file)
if (!file) {
// TODO add information about why the file couldn't be opened (errno?, win32?)
throw Exception("Error loading file: " + filename);
}
if (!doc->SaveFile(file.get()))
throw XmlException(doc.get());