From f6d878d5ce4d6e41c088698bd337cf7f4438433e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 24 Mar 2025 12:11:04 +0100 Subject: [PATCH] qcompare.h: standardize on Coverity-friendly ctors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Coverity complained that the Qt::strong_ordering(std::strong_ordering) ctor may leave m_order uninitialized, which is true if you assume that 'stdorder' could be anything else but {less, greater, equal}, which, however, should not happen™. Standardize on the pattern that QPartialOrdering(Qt::partial_ordering) was using: init m_order to equivalent, and then check for the other possible states. I would have preferred adding 'else Q_UNREACHABLE()', but these are constexpr functions, so we'd need the GCC 8 protection, and then the else would have a body longer than one line, and I don't know whether violating the coding style and adding {} only on the else would fly with reviewers, so that's done in a follow-up. Amends several changes (this code has seen a lot of churn over time). Pick-to: 6.9 6.8 Coverity-Id: 475148 Change-Id: I3d88cdaaffbdfb8720161470b5f89046a3a15088 Reviewed-by: Ivan Solovev Reviewed-by: Thiago Macieira --- src/corelib/global/qcompare.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/corelib/global/qcompare.h b/src/corelib/global/qcompare.h index 41f2b2fd5a5..d82cf5ab4a4 100644 --- a/src/corelib/global/qcompare.h +++ b/src/corelib/global/qcompare.h @@ -155,11 +155,10 @@ public: #ifdef __cpp_lib_three_way_comparison constexpr Q_IMPLICIT partial_ordering(std::partial_ordering stdorder) noexcept + : m_order{} // == equivalent { if (stdorder == std::partial_ordering::less) m_order = static_cast(QtPrivate::Ordering::Less); - else if (stdorder == std::partial_ordering::equivalent) - m_order = static_cast(QtPrivate::Ordering::Equivalent); else if (stdorder == std::partial_ordering::greater) m_order = static_cast(QtPrivate::Ordering::Greater); else if (stdorder == std::partial_ordering::unordered) @@ -349,11 +348,10 @@ public: #ifdef __cpp_lib_three_way_comparison constexpr Q_IMPLICIT weak_ordering(std::weak_ordering stdorder) noexcept + : m_order{} // == equivalent { if (stdorder == std::weak_ordering::less) m_order = static_cast(QtPrivate::Ordering::Less); - else if (stdorder == std::weak_ordering::equivalent) - m_order = static_cast(QtPrivate::Ordering::Equivalent); else if (stdorder == std::weak_ordering::greater) m_order = static_cast(QtPrivate::Ordering::Greater); } @@ -545,13 +543,10 @@ public: #ifdef __cpp_lib_three_way_comparison constexpr Q_IMPLICIT strong_ordering(std::strong_ordering stdorder) noexcept + : m_order{} // == equivalent { if (stdorder == std::strong_ordering::less) m_order = static_cast(QtPrivate::Ordering::Less); - else if (stdorder == std::strong_ordering::equivalent) - m_order = static_cast(QtPrivate::Ordering::Equivalent); - else if (stdorder == std::strong_ordering::equal) - m_order = static_cast(QtPrivate::Ordering::Equal); else if (stdorder == std::strong_ordering::greater) m_order = static_cast(QtPrivate::Ordering::Greater); } @@ -814,11 +809,10 @@ public: #ifdef __cpp_lib_three_way_comparison constexpr Q_IMPLICIT QPartialOrdering(std::partial_ordering stdorder) noexcept + : m_order{} // == equivalent { if (stdorder == std::partial_ordering::less) m_order = static_cast(QtPrivate::Ordering::Less); - else if (stdorder == std::partial_ordering::equivalent) - m_order = static_cast(QtPrivate::Ordering::Equivalent); else if (stdorder == std::partial_ordering::greater) m_order = static_cast(QtPrivate::Ordering::Greater); else if (stdorder == std::partial_ordering::unordered)