Skip to content

Commit

Permalink
QTypeRevision: fix support for 8-bit signed segments
Browse files Browse the repository at this point in the history
The logic in the isValidSegment() function was failing for 8-bit signed
because for them Integer(SegmentUnknown) is -1, so
  (std::numeric_limits<Integer>::max)() < Integer(SegmentUnknown)
was always false (127 < -1) and the function would only return true on
the impossible condition of
  segment >= 0 && segment < -1

Fixes: QTBUG-128848
Pick-to: 6.5 6.8
Change-Id: I8d17b93afd6c2982a099fffdcaeccf126b7a9d02
Reviewed-by: Ulf Hermann <[email protected]>
  • Loading branch information
thiagomacieira committed Sep 14, 2024
1 parent a572b4b commit ddfcc07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/corelib/tools/qtyperevision.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ class QTypeRevision
static constexpr bool isValidSegment(Integer segment)
{
// using extra parentheses around max to avoid expanding it if it is a macro
// and adding zero to cause it to be promoted
constexpr auto Max = (std::numeric_limits<Integer>::max)() + 0;
constexpr bool HasSufficientRange = Max >= SegmentUnknown;
return segment >= Integer(0)
&& ((std::numeric_limits<Integer>::max)() < Integer(SegmentUnknown)
|| segment < Integer(SegmentUnknown));
&& (!HasSufficientRange || segment < Integer(SegmentUnknown));
}

template<typename Major, typename Minor,
Expand Down
4 changes: 2 additions & 2 deletions tests/auto/corelib/tools/qtyperevision/tst_qtyperevision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void tst_QTypeRevision::qTypeRevision()
void tst_QTypeRevision::qTypeRevisionTypes()
{
compileTestRevision<quint8>();
// compileTestRevision<qint8>();
compileTestRevision<qint8>();
compileTestRevision<quint16>();
compileTestRevision<qint16>();
compileTestRevision<quint32>();
Expand All @@ -104,7 +104,7 @@ void tst_QTypeRevision::qTypeRevisionTypes()
compileTestRevision<qint64>();
compileTestRevision<long>();
compileTestRevision<ulong>();
// compileTestRevision<char>();
compileTestRevision<char>();

QVERIFY(!QTypeRevision::isValidSegment(0xff));
QVERIFY(!QTypeRevision::isValidSegment(-1));
Expand Down

0 comments on commit ddfcc07

Please sign in to comment.