tint: fix UB const-eval div by zero Bug: oss-fuzz:56904 Change-Id: I2f47fcfa238b28e1148b0b36ccbe2166445964a1 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/125380 Reviewed-by: Ben Clayton <bclayton@google.com> Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Antonio Maiorano <amaiorano@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt index 59776f9..084e54a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -335,8 +335,8 @@ target_compile_options(${TARGET} PUBLIC -fsanitize=thread) target_link_options(${TARGET} PUBLIC -fsanitize=thread) elseif (${DAWN_ENABLE_UBSAN}) - target_compile_options(${TARGET} PUBLIC -fsanitize=undefined) - target_link_options(${TARGET} PUBLIC -fsanitize=undefined) + target_compile_options(${TARGET} PUBLIC -fsanitize=undefined -fsanitize=float-divide-by-zero) + target_link_options(${TARGET} PUBLIC -fsanitize=undefined -fsanitize=float-divide-by-zero) endif() endif(COMPILER_IS_LIKE_GNU)
diff --git a/src/tint/number.h b/src/tint/number.h index 69df284..7bc1eea 100644 --- a/src/tint/number.h +++ b/src/tint/number.h
@@ -539,6 +539,9 @@ /// @returns a / b, or an empty optional if the resulting value overflowed the float value template <typename FloatingPointT, typename = traits::EnableIf<IsFloatingPoint<FloatingPointT>>> inline std::optional<FloatingPointT> CheckedDiv(FloatingPointT a, FloatingPointT b) { + if (b == FloatingPointT{0.0} || b == FloatingPointT{-0.0}) { + return {}; + } auto result = FloatingPointT{a.value / b.value}; if (!std::isfinite(result.value)) { return {}; @@ -576,6 +579,9 @@ /// float value template <typename FloatingPointT, typename = traits::EnableIf<IsFloatingPoint<FloatingPointT>>> inline std::optional<FloatingPointT> CheckedMod(FloatingPointT a, FloatingPointT b) { + if (b == FloatingPointT{0.0} || b == FloatingPointT{-0.0}) { + return {}; + } auto result = FloatingPointT{detail::Mod(a.value, b.value)}; if (!std::isfinite(result.value)) { return {};