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
|
#endif
|
||||||
|
|
||||||
QXmlStreamReader reader(xml_device);
|
QXmlStreamReader reader(xml_device);
|
||||||
|
|
||||||
|
try {
|
||||||
qark::QXmlInArchive archive(reader);
|
qark::QXmlInArchive archive(reader);
|
||||||
archive.beginDocument();
|
archive.beginDocument();
|
||||||
archive >> qark::tag("qmt");
|
archive >> qark::tag("qmt");
|
||||||
archive >> *project;
|
archive >> *project;
|
||||||
archive >> qark::end;
|
archive >> qark::end;
|
||||||
archive.endDocument();
|
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)
|
|||||||
writer->setAutoFormatting(true);
|
writer->setAutoFormatting(true);
|
||||||
writer->setAutoFormattingIndent(1);
|
writer->setAutoFormattingIndent(1);
|
||||||
|
|
||||||
|
try {
|
||||||
qark::QXmlOutArchive archive(*writer);
|
qark::QXmlOutArchive archive(*writer);
|
||||||
archive.beginDocument();
|
archive.beginDocument();
|
||||||
archive << qark::tag("qmt");
|
archive << qark::tag("qmt");
|
||||||
archive << *project;
|
archive << *project;
|
||||||
archive << qark::end;
|
archive << qark::end;
|
||||||
archive.endDocument();
|
archive.endDocument();
|
||||||
|
} catch (...) {
|
||||||
|
throw IOException(QStringLiteral("serialization error"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|||||||
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)
|
|||||||
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:
|
|||||||
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:
|
|||||||
|
|
||||||
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user