From 79126ece739c8a97b20a2d399a505ab0a6bd9494 Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Wed, 26 Dec 2018 11:32:32 -0800 Subject: [PATCH] [XLA] Avoid fp division by 0 in literal_comparison.cc. Found by running hlo_evaluator_test with ubsan. PiperOrigin-RevId: 226937627 --- tensorflow/compiler/xla/literal_comparison.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tensorflow/compiler/xla/literal_comparison.cc b/tensorflow/compiler/xla/literal_comparison.cc index f4376d66af8..258bc966b1a 100644 --- a/tensorflow/compiler/xla/literal_comparison.cc +++ b/tensorflow/compiler/xla/literal_comparison.cc @@ -387,7 +387,14 @@ class NearComparator { rel_error = std::numeric_limits::infinity(); } else { abs_error = FpAbsoluteValue(actual - expected); - rel_error = abs_error / FpAbsoluteValue(expected); + + // Avoid division by 0 even though it's well-defined because ubsan can be + // configured to treat this as a fatal error. + if (expected != T{0}) { + rel_error = abs_error / FpAbsoluteValue(expected); + } else { + rel_error = std::numeric_limits::infinity(); + } } const bool is_abs_mismatch = abs_error > error_.abs; const bool is_rel_mismatch = rel_error > error_.rel;