From 8d21fa56a1133529273d57df06888e42bb63fde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 28 Jan 2021 12:49:28 +0100 Subject: [PATCH] UnitTests: Use MathUtil::SaturatingCast to avoid UB [conv.fpint]/1: > A prvalue of a floating-point type can be converted to a prvalue of > an integer type. The conversion truncates; that is, the fractional > part is discarded. The behavior is undefined if the truncated value > cannot be represented in the destination type. --- .../VideoCommon/VertexLoaderTest.cpp | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp b/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp index 4bad4a35a7..7f9dbdec84 100644 --- a/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp +++ b/Source/UnitTests/VideoCommon/VertexLoaderTest.cpp @@ -12,6 +12,7 @@ #include "Common/BitUtils.h" #include "Common/Common.h" +#include "Common/MathUtil.h" #include "VideoCommon/CPMemory.h" #include "VideoCommon/DataReader.h" #include "VideoCommon/OpcodeDecoding.h" @@ -148,7 +149,7 @@ TEST_P(VertexLoaderParamTest, PositionAll) -0x8000, -0x80, -1, - -0, + -0.0, 0, 1, 123, @@ -180,16 +181,16 @@ TEST_P(VertexLoaderParamTest, PositionAll) switch (format) { case ComponentFormat::UByte: - Input((u8)value); + Input(MathUtil::SaturatingCast(value)); break; case ComponentFormat::Byte: - Input((s8)value); + Input(MathUtil::SaturatingCast(value)); break; case ComponentFormat::UShort: - Input((u16)value); + Input(MathUtil::SaturatingCast(value)); break; case ComponentFormat::Short: - Input((s16)value); + Input(MathUtil::SaturatingCast(value)); break; case ComponentFormat::Float: Input(value); @@ -206,20 +207,20 @@ TEST_P(VertexLoaderParamTest, PositionAll) switch (format) { case ComponentFormat::UByte: - f = (u8)*iter++; - g = (u8)*iter++; + f = MathUtil::SaturatingCast(*iter++); + g = MathUtil::SaturatingCast(*iter++); break; case ComponentFormat::Byte: - f = (s8)*iter++; - g = (s8)*iter++; + f = MathUtil::SaturatingCast(*iter++); + g = MathUtil::SaturatingCast(*iter++); break; case ComponentFormat::UShort: - f = (u16)*iter++; - g = (u16)*iter++; + f = MathUtil::SaturatingCast(*iter++); + g = MathUtil::SaturatingCast(*iter++); break; case ComponentFormat::Short: - f = (s16)*iter++; - g = (s16)*iter++; + f = MathUtil::SaturatingCast(*iter++); + g = MathUtil::SaturatingCast(*iter++); break; case ComponentFormat::Float: f = *iter++;