From b32d566bca446548c07b53a069ce28e6f95282e3 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Thu, 13 Jul 2023 17:17:16 +0100 Subject: [PATCH] conversions: fix undefined behaviour in DataInfoSpec For conversion from integers to float, the DataInfoSpec constructor tries to convert `CL_FLT_MAX` to an integer. The float value cannot be represented as an integer, which is undefined behaviour. Fix by only doing this conversion when `InType` is a floating point value. While at it, use `static_cast` for the conversions. Signed-off-by: Sven van Haastregt --- test_conformance/conversions/conversions_data_info.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test_conformance/conversions/conversions_data_info.h b/test_conformance/conversions/conversions_data_info.h index b02773b1..8440c2c7 100644 --- a/test_conformance/conversions/conversions_data_info.h +++ b/test_conformance/conversions/conversions_data_info.h @@ -150,13 +150,13 @@ DataInfoSpec::DataInfoSpec(const DataInitInfo &agg) else if (std::is_same::value) ranges = std::make_pair(CL_LONG_MIN, CL_LONG_MAX); - InType outMin = ((InType)ranges.first); - InType outMax = ((InType)ranges.second); - // clang-format off // for readability sake keep this section unformatted if (std::is_floating_point::value) { // from float/double + InType outMin = static_cast(ranges.first); + InType outMax = static_cast(ranges.second); + InType eps = std::is_same::value ? (InType) FLT_EPSILON : (InType) DBL_EPSILON; if (std::is_integral::value) { // to char/uchar/short/ushort/int/uint/long/ulong