From dfd33e40c18140a1407b17e00009e12f16cbf72d Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 4 Nov 2018 22:03:35 +0100 Subject: [PATCH] Detect emulated driver before casting to avoid crashs --- src/blackcore/application.cpp | 2 +- src/blackcore/application.h | 2 +- src/blackcore/context/contextsimulatorimpl.cpp | 4 ++-- src/blackcore/context/contextsimulatorimpl.h | 2 +- src/blackcore/simulator.cpp | 6 ++++++ src/blackcore/simulator.h | 3 +++ src/plugins/simulator/fsxcommon/fsxsettingscomponent.cpp | 3 ++- 7 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 49837ff23..0228ac331 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -1392,7 +1392,7 @@ namespace BlackCore return args.join(' '); } - ISimulator *CApplication::getISimulator() const + QPointer CApplication::getISimulator() const { if (!this->getCoreFacade()) { return nullptr; } if (!this->getCoreFacade()->getCContextSimulator()) { return nullptr; } diff --git a/src/blackcore/application.h b/src/blackcore/application.h index e952639fa..35802fd84 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -323,7 +323,7 @@ namespace BlackCore // ----------------------- simulator ---------------------------------------- //! The simulator plugin, if available - ISimulator *getISimulator() const; + QPointer getISimulator() const; // ----------------------- contexts ---------------------------------------- diff --git a/src/blackcore/context/contextsimulatorimpl.cpp b/src/blackcore/context/contextsimulatorimpl.cpp index be261dc63..1938a71ac 100644 --- a/src/blackcore/context/contextsimulatorimpl.cpp +++ b/src/blackcore/context/contextsimulatorimpl.cpp @@ -854,9 +854,9 @@ namespace BlackCore return false; } - ISimulator *CContextSimulator::simulator() const + QPointer CContextSimulator::simulator() const { - if (!this->isSimulatorAvailable()) { return nullptr; } + if (!this->isSimulatorAvailable() || !m_simulatorPlugin.second) { return nullptr; } return m_simulatorPlugin.second; } diff --git a/src/blackcore/context/contextsimulatorimpl.h b/src/blackcore/context/contextsimulatorimpl.h index 477d8fe10..da3f4fa20 100644 --- a/src/blackcore/context/contextsimulatorimpl.h +++ b/src/blackcore/context/contextsimulatorimpl.h @@ -138,7 +138,7 @@ namespace BlackCore void gracefulShutdown(); //! Access to simulator (i.e. the plugin) - ISimulator *simulator() const; + QPointer simulator() const; //! Simulator available? bool hasSimulator() const { return this->simulator(); } diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index 2f6144406..8bb7c14c8 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -333,6 +333,12 @@ namespace BlackCore ISimulationEnvironmentProvider::resetSimulationEnvironmentStatistics(); } + bool ISimulator::isEmulatedDriver() const + { + const QString className = this->metaObject()->className(); + return className.contains("emulated", Qt::CaseInsensitive); + } + bool ISimulator::parseCommandLine(const QString &commandLine, const CIdentifier &originator) { if (this->isMyIdentifier(originator)) { return false; } diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index b2acf7e72..65f175e51 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -243,6 +243,9 @@ namespace BlackCore //! \copydoc BlackMisc::IProvider::asQObject virtual QObject *asQObject() override { return this; } + //! Is this the emulated driver just pretending to be P3D, FSX, or XPlane + bool isEmulatedDriver() const; + //! \addtogroup swiftdotcommands //! @{ //!
diff --git a/src/plugins/simulator/fsxcommon/fsxsettingscomponent.cpp b/src/plugins/simulator/fsxcommon/fsxsettingscomponent.cpp
index 6652b9019..18bef8e80 100644
--- a/src/plugins/simulator/fsxcommon/fsxsettingscomponent.cpp
+++ b/src/plugins/simulator/fsxcommon/fsxsettingscomponent.cpp
@@ -138,7 +138,8 @@ namespace BlackSimPlugin
             const CSimulatorPluginInfo plugin = sGui->getIContextSimulator()->getSimulatorPluginInfo();
             if (plugin.isEmulatedPlugin()) { return nullptr; } // cast would fail
 
-            ISimulator *simulator = sGui->getISimulator();
+            ISimulator *simulator = sGui->getISimulator().data();
+            if (!simulator || simulator->isEmulatedDriver())     { return nullptr; }
             if (!simulator->getSimulatorInfo().isFsxP3DFamily()) { return nullptr; }
             if (simulator->getSimulatorInfo() != m_simulator)    { return nullptr; }