From 43874ec9f8e05798212d4f72f9bcbe23b8f30cac Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 3 Mar 2025 11:52:54 +0100 Subject: [PATCH] QGridLayout: make QHVContainer's preconditions machine-readable Coverity complains that other() computes an address that's out of m_data's bounds, and that's correct, if the argument isn't either Qt::Horizontal or Qt::Vertical (they're flags, so Coverity assumes that 0 and Vertical|Horizonal are valid arguments, too), but as the doc block above the class suggests, this is not supported. So add Q_ASSERTs to map() and mapOther(), and drop the noexcept of functions that call these now. Not picking to older branches in case Coverity is right and I am wrong. Amends 46a1cf915096ab056ad3bfd1d42fd504c04763cd. Coverity-Id: 427374 Change-Id: I47a2821e99dd99666a170f2bb2859c1d6351b50e Reviewed-by: Volker Hilsheimer --- src/gui/util/qgridlayoutengine_p.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/gui/util/qgridlayoutengine_p.h b/src/gui/util/qgridlayoutengine_p.h index e00c2c5e483..159e4a5a25a 100644 --- a/src/gui/util/qgridlayoutengine_p.h +++ b/src/gui/util/qgridlayoutengine_p.h @@ -72,12 +72,14 @@ class QHVContainer { static_assert(Qt::Horizontal == 0x1); static_assert(Qt::Vertical == 0x2); - static constexpr int map(Qt::Orientation o) noexcept + static constexpr int map(Qt::Orientation o) { + Q_ASSERT(o == Qt::Horizontal || o == Qt::Vertical); // Q_PRE return int(o) - 1; } - static constexpr int mapOther(Qt::Orientation o) noexcept + static constexpr int mapOther(Qt::Orientation o) { + Q_ASSERT(o == Qt::Horizontal || o == Qt::Vertical); // Q_PRE return 2 - int(o); } public: @@ -86,11 +88,11 @@ public: : m_data{h, v} {} QHVContainer() = default; - constexpr T &operator[](Qt::Orientation o) noexcept { return m_data[map(o)]; } - constexpr const T &operator[](Qt::Orientation o) const noexcept { return m_data[map(o)]; } + constexpr T &operator[](Qt::Orientation o) { return m_data[map(o)]; } + constexpr const T &operator[](Qt::Orientation o) const { return m_data[map(o)]; } - constexpr T &other(Qt::Orientation o) noexcept { return m_data[mapOther(o)]; } - constexpr const T &other(Qt::Orientation o) const noexcept { return m_data[mapOther(o)]; } + constexpr T &other(Qt::Orientation o) { return m_data[mapOther(o)]; } + constexpr const T &other(Qt::Orientation o) const { return m_data[mapOther(o)]; } constexpr void transpose() noexcept { qSwap(m_data[0], m_data[1]); } constexpr QHVContainer transposed() const