summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2021-12-16 10:34:38 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-12-17 02:00:08 +0000
commitf1eac1f0f11b03b4b50b9d66821d193e349c8af7 (patch)
treea5f8e0bf136b035867949504a94767f8b60048e5 /tests
parent12b5f4be13d5edf72c0243a79e641c743aec6705 (diff)
tst_QByteArray: fix UB (signed overflow)
The first case is simple, as it's a static overflow. Theoretically, the compiler would be allowed to just remove the complete function as dead code. This is an error left from the port from int to qsizetype: Qt 5.15 there has uint(MaxAllocSize) + 1, so use quint here again, qint64 is wrong. In the second case, we _may_ reach alloc == MaxAllocSize. Check that, if we do, we don't then add 1 to it. Change-Id: I93044ed6f1b77559642fa1e4e8f313cf59eeeb79 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit b1938e5787425c39d6e9f6a41605338d1822c86b) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp5
1 files changed, 3 insertions, 2 deletions
diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
index 48a2a69e48..3940c6e8a1 100644
--- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
@@ -1768,7 +1768,7 @@ void tst_QByteArray::blockSizeCalculations()
QCOMPARE(qCalculateGrowingBlockSize(MaxAllocSize/2, 2, 1).elementCount, qsizetype(MaxAllocSize)/2);
// error conditions
- QCOMPARE(qCalculateBlockSize(qint64(MaxAllocSize) + 1, 1), qsizetype(-1));
+ QCOMPARE(qCalculateBlockSize(quint64(MaxAllocSize) + 1, 1), qsizetype(-1));
QCOMPARE(qCalculateBlockSize(qsizetype(-1), 1), qsizetype(-1));
QCOMPARE(qCalculateBlockSize(MaxAllocSize, 1, 1), qsizetype(-1));
QCOMPARE(qCalculateBlockSize(MaxAllocSize/2 + 1, 2), qsizetype(-1));
@@ -1825,7 +1825,8 @@ void tst_QByteArray::blockSizeCalculations()
QVERIFY(checkSize(alloc, qsizetype(MaxAllocSize) / elementSize));
// the next allocation should be invalid
- QCOMPARE(qCalculateGrowingBlockSize(alloc + 1, elementSize).size, qsizetype(-1));
+ if (alloc < MaxAllocSize) // lest alloc + 1 overflows (= UB)
+ QCOMPARE(qCalculateGrowingBlockSize(alloc + 1, elementSize).size, qsizetype(-1));
}
}