From ca8fefca858e34bf09a7d691413fb9994e745b22 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 5 Sep 2023 09:52:31 +0200 Subject: [PATCH] QCoreApplication::requestPermission: refactor "dead" code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An assert that checks in debug mode is morally equivalent to an assumption/contract in release mode. Rechecking that an assumption is true is pointless (it's true by definition), so the code for the case the check fails is effectively "dead". Right now Q_ASSERT(x) doesn't turn into Q_ASSUME(x) because some compilers still have poor codegen (and basically emit a check, even in release mode). With C++23's [[assume(x)]] we may reconsider that. As soon as we do it, this code is no longer theoretically dead but practically dead. Refactor the code so that the check is done unconditionally, and if it fails, we assert (in debug mode). In release, we protect users from a broken backend (which may cause an endless loop of user code re-requesting the same permission, so it's worth avoiding it). Change-Id: If0e70e7d88a585ce16ec4838ba7be747652d8155 Reviewed-by: Tor Arne Vestbø --- src/corelib/kernel/qcoreapplication.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 203f74a6666..2844da771f1 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -2921,10 +2921,11 @@ void QCoreApplication::requestPermission(const QPermission &requestedPermission, PermissionReceiver *receiver = new PermissionReceiver(std::move(slotObj), context); QPermissions::Private::requestPermission(requestedPermission, [=](Qt::PermissionStatus status) { - Q_ASSERT_X(status != Qt::PermissionStatus::Undetermined, "QPermission", - "QCoreApplication::requestPermission() should never return Undetermined"); - if (status == Qt::PermissionStatus::Undetermined) + if (status == Qt::PermissionStatus::Undetermined) { + Q_ASSERT_X(false, "QPermission", + "Internal error: requestPermission() should never return Undetermined"); status = Qt::PermissionStatus::Denied; + } if (QCoreApplication::self) { QPermission permission = requestedPermission;