From f4dd7e29a68c4439e3da40db4eb2d67999d316b0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 22 Mar 2025 17:56:55 +0100 Subject: [PATCH] QMessageBox: really fix UB (invalid cast) in Private::canBeNativeDialog() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code comment above the cast is correct, but the code wasn't: While we're receiving the result of the cast in a QDialog pointer, the cast is still to QMessageBox*, and whether that cast is in the Q_Q macro or not doesn't change the fact that it's invalid. Says UBSan: qmessagebox.cpp:2804:31: runtime error: downcast of address 0x7ffebfd87140 which does not point to an object of type 'QMessageBox' 0x7ffebfd87140: note: object is of type 'QDialog' 2b 7f 00 00 30 94 57 b9 2b 7f 00 00 80 8c 00 00 90 61 00 00 08 96 57 b9 2b 7f 00 00 00 00 d8 bf ^~~~~~~~~~~~~~~~~~~~~~~ vptr for 'QDialog' The trivial fix is to cast only to QDialog. Amends 29b2506e8cf0c792821a3ddb28e62080cd66ae28. Pick-to: 6.9 6.8 Change-Id: Ia3f6c08b62f6bed274f43baab881a0d802bd986b Reviewed-by: Thorbjørn Lund Martsum --- src/widgets/dialogs/qmessagebox.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index b2a7ad274b2..df17399ca7e 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -2800,7 +2800,7 @@ bool QMessageBoxPrivate::canBeNativeDialog() const { // Don't use Q_Q here! This function is called from ~QDialog, // so Q_Q calling q_func() invokes undefined behavior (invalid cast in q_func()). - const QDialog * const q = static_cast(q_ptr); + const QDialog * const q = static_cast(q_ptr); if (nativeDialogInUse) return true; if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)