summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJøger Hansegård <joger.hansegard@qt.io>2024-03-20 18:17:53 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2024-03-21 21:27:33 +0000
commite19b386c4d49f2e725c21f8db7b69917a7fb22c0 (patch)
tree587f10757e5032598bbf61d3e92fd25cb524f730
parent0ff1f255dd0c798c36057c184c529e83b48b0ea4 (diff)
Use std::addressof to get the address of QMaybe wrapped type
Using QMaybe with types that overloads operator& can have unintended effects because QMaybe::operator-> would call the overload. This could cause problems with types such as WRL::ComPtr which resets its pointer when taking its address. To ensure that we get the address of the instance, we should use std::addressof instead of calling operator& to get the wrapped object's address. This fixes an issue where QMaybe<ComPtr> resets the COM pointer and returns nullptr when attempting to extract the raw interface pointer like this: QMaybe<ComPtr<Interface>, HRESULT> instance = foo(); Interface *rawIf = instance->Get(); // Reset instance and return 0 Pick-to: 6.5 Change-Id: I544dec6dc0cf59c2b3a46fe6d9ebbf08aac80737 Reviewed-by: Tim Blechmann <tim@klingt.org> Reviewed-by: Artem Dyomin <artem.dyomin@qt.io> (cherry picked from commit 3367b137e3aeec76b7edb7d14bde1a6cec7a64f4) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 08b52a310244d299dddf01128493bdadc32325cf)
-rw-r--r--src/multimedia/qmaybe_p.h5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/multimedia/qmaybe_p.h b/src/multimedia/qmaybe_p.h
index f35d2c329..5fe2d4d6c 100644
--- a/src/multimedia/qmaybe_p.h
+++ b/src/multimedia/qmaybe_p.h
@@ -19,6 +19,7 @@
#include <qstring.h>
#include <optional>
#include <utility>
+#include <memory>
QT_BEGIN_NAMESPACE
@@ -77,8 +78,8 @@ public:
return *m_value;
}
- constexpr Value *operator->() noexcept { return &value(); }
- constexpr const Value *operator->() const noexcept { return &value(); }
+ constexpr Value *operator->() noexcept { return std::addressof(value()); }
+ constexpr const Value *operator->() const noexcept { return std::addressof(value()); }
constexpr Value &operator*() &noexcept { return value(); }
constexpr const Value &operator*() const &noexcept { return value(); }