tst_qfloat16: don't cause UB in converting from float to ints

The test data is provided as float, so we must obey [conv.fpint]/1 when
converting from float to integer types. There are values in the rows
that are outside of the range of short and shorter integers, so just
check the range.

Fixes: QTBUG-125889
Pick-to: 6.7
Change-Id: If3345151ddf84c43a4f1fffd17d405ee0cd00d44
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Thiago Macieira 2024-05-29 14:31:17 -03:00
parent 626831db34
commit 3c9bb0daaf

View File

@ -155,6 +155,7 @@ void tst_qfloat16::ordering_data()
row(2.0f, -inf);
row(-2.0f, nan);
row(-inf, -2.0f);
// testing with values outside qfloat16 range
row(0.0f, 13e5f);
// generateRow(inf, 13e5f); // fails qfloat16 vs qfloat16 and qfloat16 vs int (QTBUG-118193)
@ -187,11 +188,21 @@ void tst_qfloat16::ordering()
#undef CHECK_FP
auto check_int = [=](auto rhs) {
// check that we're in range before converting, otherwise
// [conv.fpint]/1 says it would be UB
using RHS = decltype(rhs);
if (right > double(std::numeric_limits<RHS>::max()))
return;
if (auto min = std::numeric_limits<RHS>::min(); min != 0 && right < double(min))
return;
rhs = RHS(right);
const auto expectedRes = Qt::compareThreeWay(left, rhs);
QTestPrivate::testAllComparisonOperators(lhs, rhs, expectedRes);
};
#define CHECK_INT(RHS) \
do { \
const auto rhs = static_cast<RHS>(right); \
const auto expectedRes = Qt::compareThreeWay(left, rhs); \
QTestPrivate::testAllComparisonOperators(lhs, rhs, expectedRes); \
check_int(static_cast<RHS>(0)); \
POSTCHECK("qfloat16 vs " #RHS " comparison failed") \
} while (false) \
/* END */