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 <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2025-03-03 11:52:54 +01:00
parent bcd29a9eab
commit 43874ec9f8

View File

@ -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