Commit 9fc4396e authored by Jochen Becher's avatar Jochen Becher
Browse files

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: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 88bbf4d1
...@@ -137,12 +137,19 @@ void ProjectSerializer::load(const QString &file_name, Project *project) ...@@ -137,12 +137,19 @@ void ProjectSerializer::load(const QString &file_name, Project *project)
#endif #endif
QXmlStreamReader reader(xml_device); QXmlStreamReader reader(xml_device);
qark::QXmlInArchive archive(reader);
archive.beginDocument(); try {
archive >> qark::tag("qmt"); qark::QXmlInArchive archive(reader);
archive >> *project; archive.beginDocument();
archive >> qark::end; archive >> qark::tag("qmt");
archive.endDocument(); 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 #ifdef USE_COMPRESSED_FILES
uncompressor.close(); uncompressor.close();
...@@ -155,12 +162,17 @@ void ProjectSerializer::write(QXmlStreamWriter *writer, const Project *project) ...@@ -155,12 +162,17 @@ void ProjectSerializer::write(QXmlStreamWriter *writer, const Project *project)
writer->setAutoFormatting(true); writer->setAutoFormatting(true);
writer->setAutoFormattingIndent(1); writer->setAutoFormattingIndent(1);
qark::QXmlOutArchive archive(*writer); try {
archive.beginDocument(); qark::QXmlOutArchive archive(*writer);
archive << qark::tag("qmt"); archive.beginDocument();
archive << *project; archive << qark::tag("qmt");
archive << qark::end; archive << *project;
archive.endDocument(); archive << qark::end;
archive.endDocument();
} catch (...) {
throw IOException(QStringLiteral("serialization error"));
}
} }
} }
...@@ -74,9 +74,8 @@ Core::IDocument::OpenResult ModelDocument::open(QString *errorString, const QStr ...@@ -74,9 +74,8 @@ Core::IDocument::OpenResult ModelDocument::open(QString *errorString, const QStr
{ {
Q_UNUSED(fileName); Q_UNUSED(fileName);
if (!load(errorString, realFileName)) OpenResult result = load(errorString, realFileName);
return Core::IDocument::OpenResult::ReadError; return result;
return Core::IDocument::OpenResult::Success;
} }
bool ModelDocument::save(QString *errorString, const QString &name, bool autoSave) bool ModelDocument::save(QString *errorString, const QString &name, bool autoSave)
...@@ -143,7 +142,7 @@ ExtDocumentController *ModelDocument::documentController() const ...@@ -143,7 +142,7 @@ ExtDocumentController *ModelDocument::documentController() const
return d->documentController; 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); d->documentController = ModelEditorPlugin::modelsManager()->createModel(this);
connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed); connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed);
...@@ -151,13 +150,16 @@ bool ModelDocument::load(QString *errorString, const QString &fileName) ...@@ -151,13 +150,16 @@ bool ModelDocument::load(QString *errorString, const QString &fileName)
try { try {
d->documentController->loadProject(fileName); d->documentController->loadProject(fileName);
setFilePath(Utils::FileName::fromString(d->documentController->getProjectController()->getProject()->getFileName())); setFilePath(Utils::FileName::fromString(d->documentController->getProjectController()->getProject()->getFileName()));
} catch (const qmt::Exception &ex) { } catch (const qmt::FileNotFoundException &ex) {
*errorString = ex.getErrorMsg(); *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(); emit contentSet();
return true; return OpenResult::Success;
} }
} // namespace Internal } // namespace Internal
......
...@@ -54,7 +54,7 @@ signals: ...@@ -54,7 +54,7 @@ signals:
void contentSet(); void contentSet();
public: public:
IDocument::OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const QString &fileName,
const QString &realFileName) override; const QString &realFileName) override;
bool save(QString *errorString, const QString &fileName, bool autoSave) override; bool save(QString *errorString, const QString &fileName, bool autoSave) override;
QString defaultPath() const override; QString defaultPath() const override;
...@@ -65,7 +65,7 @@ public: ...@@ -65,7 +65,7 @@ public:
ExtDocumentController *documentController() const; ExtDocumentController *documentController() const;
bool load(QString *errorString, const QString &fileName); OpenResult load(QString *errorString, const QString &fileName);
private: private:
ModelDocumentPrivate *d; ModelDocumentPrivate *d;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment