From 58ba9456592fe67e0dc725075c7f005771e025c1 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 6 Jan 2025 14:42:32 +0100 Subject: [PATCH 1/2] App: Handle possibly raised exception in ZipFile constructor This fixes the issue reported at: https://forum.freecad.org/viewtopic.php?t=93619 --- src/App/ProjectFile.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/App/ProjectFile.cpp b/src/App/ProjectFile.cpp index 601969e02f..cf56233bc5 100644 --- a/src/App/ProjectFile.cpp +++ b/src/App/ProjectFile.cpp @@ -66,6 +66,26 @@ using namespace App; namespace { + +class ZipTools +{ +public: + static std::unique_ptr open(const std::string& file) + { + std::unique_ptr project; + try { + project = std::make_unique(file); + if (!project->isValid()) { + project.reset(); + } + } + catch (const std::exception&) { + } + + return project; + } +}; + class DocumentMetadata { public: @@ -212,11 +232,12 @@ bool ProjectFile::loadDocument() return true; // already loaded } - zipios::ZipFile project(stdFile); - if (!project.isValid()) { + auto project = ZipTools::open(stdFile); + if (!project) { return false; } - std::unique_ptr str(project.getInputStream("Document.xml")); + + std::unique_ptr str(project->getInputStream("Document.xml")); if (str) { std::unique_ptr parser(new XercesDOMParser); parser->setValidationScheme(XercesDOMParser::Val_Auto); From a88623ff240b12c40011511f9773da9b1c71b843 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 2 Mar 2025 13:32:42 +0100 Subject: [PATCH 2/2] Start: Fix possible crash when opening Start page Procedure to trigger the crash: * Create a document * Create a spreadsheet and open it * Close the 3D view * Open the Start page * It may or may not crash but the program is in an undefined state The reason of the crash is caused by an inappropriate use of static_cast. This bug is caused by PR 17152. To fix the crash use dynamic_cast instead. If the cast fails it returns a null pointer instead of a dangling pointer. --- src/Mod/Start/Gui/StartView.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Mod/Start/Gui/StartView.cpp b/src/Mod/Start/Gui/StartView.cpp index 63ce17f892..8ef9dd78f3 100644 --- a/src/Mod/Start/Gui/StartView.cpp +++ b/src/Mod/Start/Gui/StartView.cpp @@ -417,8 +417,7 @@ void StartView::changeEvent(QEvent* event) _openFirstStart->setEnabled(true); Gui::Document* doc = Gui::Application::Instance->activeDocument(); if (doc) { - Gui::View3DInventor* view = static_cast(doc->getActiveView()); - if (view) { + if (auto view = dynamic_cast(doc->getActiveView())) { Gui::View3DInventorViewer* viewer = view->getViewer(); if (viewer->isEditing()) { _openFirstStart->setEnabled(false);