diff options
author | Li Xi <lixi@uniontech.com> | 2021-11-11 15:23:04 +0800 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-11-15 16:51:14 +0100 |
commit | a0e7fbd4d54ddbea5c2b155b0f828df3ce3c98fb (patch) | |
tree | aadd9b17f486f6fb90a48c49aec6b675546de639 /src/widgets/widgets/qmainwindow.cpp | |
parent | 61aa482241a7e81116224b2a5ae642df49526982 (diff) |
Test result of qobject_cast before dereferencing
Since QMainWindow::setMenuWidget accepts a QWidget (allowing users to
implement their own menu widget), we need to use qobject_cast on the
stored widget to see if it is a QMenuBar before calling QMenuBar APIs.
This qobject_cast may return nullptr.
Pick-to: 6.2
Fixes: QTBUG-98247
Change-Id: Iff1dbd24fa7ca09098fe49c179770356c966251d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/widgets/qmainwindow.cpp')
-rw-r--r-- | src/widgets/widgets/qmainwindow.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/widgets/widgets/qmainwindow.cpp b/src/widgets/widgets/qmainwindow.cpp index 7cffd90aba..5aeea88e4e 100644 --- a/src/widgets/widgets/qmainwindow.cpp +++ b/src/widgets/widgets/qmainwindow.cpp @@ -522,10 +522,10 @@ void QMainWindow::setMenuBar(QMenuBar *menuBar) { QLayout *topLayout = layout(); - if (topLayout->menuBar() && topLayout->menuBar() != menuBar) { + if (QWidget *existingMenuBar = topLayout->menuBar(); existingMenuBar && existingMenuBar != menuBar) { // Reparent corner widgets before we delete the old menu bar. - QMenuBar *oldMenuBar = qobject_cast<QMenuBar *>(topLayout->menuBar()); - if (menuBar) { + QMenuBar *oldMenuBar = qobject_cast<QMenuBar *>(existingMenuBar); + if (oldMenuBar && menuBar) { // TopLeftCorner widget. QWidget *cornerWidget = oldMenuBar->cornerWidget(Qt::TopLeftCorner); if (cornerWidget) @@ -535,9 +535,10 @@ void QMainWindow::setMenuBar(QMenuBar *menuBar) if (cornerWidget) menuBar->setCornerWidget(cornerWidget, Qt::TopRightCorner); } - oldMenuBar->hide(); - oldMenuBar->setParent(nullptr); - oldMenuBar->deleteLater(); + + existingMenuBar->hide(); + existingMenuBar->setParent(nullptr); + existingMenuBar->deleteLater(); } topLayout->setMenuBar(menuBar); } |