forked from qt-creator/qt-creator
ModelEditor: catch all exceptions on loading/saving model files
As reported in QTCREATORBUG-15256 an invalid model file crashes QtC on loading the file. Not all exceptions were caught. Change-Id: Ie2e75ba23d92482e1365664f64728422e2003b32 Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
@@ -137,12 +137,19 @@ void ProjectSerializer::load(const QString &file_name, Project *project)
|
||||
#endif
|
||||
|
||||
QXmlStreamReader reader(xml_device);
|
||||
|
||||
try {
|
||||
qark::QXmlInArchive archive(reader);
|
||||
archive.beginDocument();
|
||||
archive >> qark::tag("qmt");
|
||||
archive >> *project;
|
||||
archive >> qark::end;
|
||||
archive.endDocument();
|
||||
} catch (const qark::QXmlInArchive::FileFormatException &) {
|
||||
throw FileIOException(QStringLiteral("illegal file format"), file_name);
|
||||
} catch (...) {
|
||||
throw FileIOException(QStringLiteral("serialization error"), file_name);
|
||||
}
|
||||
|
||||
#ifdef USE_COMPRESSED_FILES
|
||||
uncompressor.close();
|
||||
@@ -155,12 +162,17 @@ void ProjectSerializer::write(QXmlStreamWriter *writer, const Project *project)
|
||||
writer->setAutoFormatting(true);
|
||||
writer->setAutoFormattingIndent(1);
|
||||
|
||||
try {
|
||||
qark::QXmlOutArchive archive(*writer);
|
||||
archive.beginDocument();
|
||||
archive << qark::tag("qmt");
|
||||
archive << *project;
|
||||
archive << qark::end;
|
||||
archive.endDocument();
|
||||
} catch (...) {
|
||||
throw IOException(QStringLiteral("serialization error"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -74,9 +74,8 @@ Core::IDocument::OpenResult ModelDocument::open(QString *errorString, const QStr
|
||||
{
|
||||
Q_UNUSED(fileName);
|
||||
|
||||
if (!load(errorString, realFileName))
|
||||
return Core::IDocument::OpenResult::ReadError;
|
||||
return Core::IDocument::OpenResult::Success;
|
||||
OpenResult result = load(errorString, realFileName);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ModelDocument::save(QString *errorString, const QString &name, bool autoSave)
|
||||
@@ -143,7 +142,7 @@ ExtDocumentController *ModelDocument::documentController() const
|
||||
return d->documentController;
|
||||
}
|
||||
|
||||
bool ModelDocument::load(QString *errorString, const QString &fileName)
|
||||
Core::IDocument::OpenResult ModelDocument::load(QString *errorString, const QString &fileName)
|
||||
{
|
||||
d->documentController = ModelEditorPlugin::modelsManager()->createModel(this);
|
||||
connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed);
|
||||
@@ -151,13 +150,16 @@ bool ModelDocument::load(QString *errorString, const QString &fileName)
|
||||
try {
|
||||
d->documentController->loadProject(fileName);
|
||||
setFilePath(Utils::FileName::fromString(d->documentController->getProjectController()->getProject()->getFileName()));
|
||||
} catch (const qmt::Exception &ex) {
|
||||
} catch (const qmt::FileNotFoundException &ex) {
|
||||
*errorString = ex.getErrorMsg();
|
||||
return false;
|
||||
return OpenResult::ReadError;
|
||||
} catch (const qmt::Exception &ex) {
|
||||
*errorString = tr("Could not open \"%1\" for reading: %2.").arg(fileName).arg(ex.getErrorMsg());
|
||||
return OpenResult::CannotHandle;
|
||||
}
|
||||
|
||||
emit contentSet();
|
||||
return true;
|
||||
return OpenResult::Success;
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
@@ -54,7 +54,7 @@ signals:
|
||||
void contentSet();
|
||||
|
||||
public:
|
||||
IDocument::OpenResult open(QString *errorString, const QString &fileName,
|
||||
OpenResult open(QString *errorString, const QString &fileName,
|
||||
const QString &realFileName) override;
|
||||
bool save(QString *errorString, const QString &fileName, bool autoSave) override;
|
||||
QString defaultPath() const override;
|
||||
@@ -65,7 +65,7 @@ public:
|
||||
|
||||
ExtDocumentController *documentController() const;
|
||||
|
||||
bool load(QString *errorString, const QString &fileName);
|
||||
OpenResult load(QString *errorString, const QString &fileName);
|
||||
|
||||
private:
|
||||
ModelDocumentPrivate *d;
|
||||
|
||||
Reference in New Issue
Block a user