Commit 5e0d22c6 authored by Milian Wolff's avatar Milian Wolff
Browse files

Fix UB in UI controller usage for NoUi tests

Only connect to qApp in init and disconnect in cleanup.
Then don't call cleanup in NoUi tests.

Fixes UBSAN report:
```
/home/milian/projects/kf5/src/extragear/kdevelop/kdevelop/kdevplatform/shell/uicontroller.cpp:241:16: runtime error: downcast of address 0x7f30c868a1c0 which does not point to an object of type 'QApplication'
0x7f30c868a1c0: note: object is of type 'QCoreApplication'
 00 00 00 00  f0 4f 59 de 30 7f 00 00  e0 55 02 00 e0 60 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for 'QCoreApplication'
    #0 0x7f30eeacada3 in KDevelop::UiController::~UiController() /home/milian/projects/kf5/src/extragear/kdevelop/kdevelop/kdevplatform/shell/uicontroller.cpp:241
    #1 0x7f30eeacb874 in KDevelop::UiController::~UiController() /home/milian/projects/kf5/src/extragear/kdevelop/kdevelop/kdevplatform/shell/uicontroller.cpp:242
    #2 0x7f30eea745d3 in KDevelop::CorePrivate::~CorePrivate() /home/milian/projects/kf5/src/extragear/kdevelop/kdevelop/kdevplatform/shell/core.cpp:279
    #3 0x7f30eea7f4be in KDevelop::Core::~Core() /home/milian/projects/kf5/src/extragear/kdevelop/kdevelop/kdevplatform/shell/core.cpp:330
    #4 0x7f30f1250435 in KDevelop::TestCore::~TestCore() kdevplatform/tests/KDevPlatformTests_autogen/EWIEGA46WW/../../../../../../../../src/extragear/kdevelop/kdevelop/kdevplatform/tests/testcore.h:75
    #5 0x7f30f1250435 in KDevelop::TestCore::~TestCore() kdevplatform/tests/KDevPlatformTests_autogen/EWIEGA46WW/../../../../../../../../src/extragear/kdevelop/kdevelop/kdevplatform/tests/testcore.h:75
    #6 0x7f30de32a33f in QObject::event(QEvent*) (/usr/lib/libQt5Core.so.5+0x2e233f)
    #7 0x7f30de2fda4f in QCoreApplication::notifyInternal2(QObject*, QEvent*) (/usr/lib/libQt5Core.so.5+0x2b5a4f)
    #8 0x7f30de300572 in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) (/usr/lib/libQt5Core.so.5+0x2b8572)
    #9 0x7f30de3522dd in QTest::qWait(int) (/usr/lib/libQt5Core.so.5+0x30a2dd)
    #10 0x7f30f1279cef in KDevelop::TestCore::shutdown() /home/milian/projects/kf5/src/extragear/kdevelop/kdevelop/kdevplatform/tests/testcore.cpp:99
```
parent 96e91bcd
......@@ -367,8 +367,11 @@ void Core::cleanup()
d->debugController->cleanup();
d->selectionController->cleanup();
// Save the layout of the ui here, so run it first
d->uiController->cleanup();
if (!(d->m_mode & Core::NoUi)) {
// Save the layout of the ui here, so run it first
d->uiController->cleanup();
}
if (d->workingSetController)
d->workingSetController->cleanup();
......
......@@ -228,18 +228,10 @@ UiController::UiController(Core *core)
if (!defaultMainWindow() || (Core::self()->setupFlags() & Core::NoUi))
return;
connect(qApp, &QApplication::focusChanged,
this, [this] (QWidget* old, QWidget* now) { Q_D(UiController); d->widgetChanged(old, now); } );
setupActions();
}
UiController::~UiController()
{
// disconnect early to prevent UB due to accessing partially destroyed UiController
// in the focusChanged handler above
disconnect(qApp, nullptr, this, nullptr);
}
UiController::~UiController() = default;
void UiController::setupActions()
{
......@@ -448,8 +440,12 @@ MainWindow *UiController::defaultMainWindow()
void UiController::initialize()
{
defaultMainWindow()->initialize();
}
connect(qApp, &QApplication::focusChanged, this, [this](QWidget* old, QWidget* now) {
Q_D(UiController);
d->widgetChanged(old, now);
});
}
void UiController::cleanup()
{
......@@ -457,6 +453,10 @@ void UiController::cleanup()
w->saveSettings();
}
saveAllAreas(KSharedConfig::openConfig());
// disconnect early to prevent UB due to accessing partially destroyed UiController
// in the focusChanged handler above
disconnect(qApp, nullptr, this, nullptr);
}
void UiController::selectNewToolViewToAdd(MainWindow *mw)
......
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