From b64e918c85ab4febdd5ef81196a9b1433a9d1810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= Date: Wed, 24 Jul 2024 18:54:21 +0200 Subject: [PATCH] Call std::addressof instead of operator& in QDebug::toString QDebug::toString intends to pass the input object's address to its nested implementation, but was calling operator& which does not work with types that has a custom overload. Calling std::addressof fixes this problem. Fixes: QTBUG-127510 Change-Id: Ie608f7b1a63c4032246b6ff98a3651695f0536ca Reviewed-by: Thiago Macieira Reviewed-by: Fabian Kosmale (cherry picked from commit 6cd6c3d6d70f8e76059153dd58ed2c61af2889b3) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qdebug.h | 3 ++- tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index e554e92c60f..a2c270f5cd0 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -235,7 +236,7 @@ public: template static QString toString(const T &object) { - return toStringImpl(&streamTypeErased, &object); + return toStringImpl(&streamTypeErased, std::addressof(object)); } }; diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 15da0758d0a..7ee2ff42cc6 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -1343,6 +1343,19 @@ void tst_QDebug::toString() const stream.nospace() << &qobject; QCOMPARE(QDebug::toString(&qobject), expectedString); } + + // Overloaded operator& + { + struct TypeWithAddressOf + { + int* operator&() const { return nullptr; } + operator QByteArray() const { return "test"; } + }; + + TypeWithAddressOf object; + QString expectedString {"\"test\""}; + QCOMPARE(QDebug::toString(object), expectedString); + } } void tst_QDebug::noQVariantEndlessRecursion() const