From 73819e524bd0d0b69320cb9299cad5745cd0ad50 Mon Sep 17 00:00:00 2001 From: Darby Johnston Date: Sun, 26 Apr 2026 10:57:12 -0700 Subject: [PATCH] Check if we are dividing by zero Signed-off-by: Darby Johnston --- src/opentime/rationalTime.h | 4 +++- tests/test_opentime.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/opentime/rationalTime.h b/src/opentime/rationalTime.h index c8df84d39..5551ed3fb 100644 --- a/src/opentime/rationalTime.h +++ b/src/opentime/rationalTime.h @@ -69,7 +69,9 @@ class OPENTIME_API_TYPE RationalTime /// @brief Returns the time value converted to a new rate. constexpr double value_rescaled_to(double new_rate) const noexcept { - return new_rate == _rate ? _value : (_value * new_rate) / _rate; + return new_rate == _rate + ? _value + : (_rate > 0 ? (_value * new_rate) / _rate : 0); } /// @brief Returns the time value converted to a new rate. diff --git a/tests/test_opentime.cpp b/tests/test_opentime.cpp index bcb4ff5b0..c752d94a4 100644 --- a/tests/test_opentime.cpp +++ b/tests/test_opentime.cpp @@ -32,6 +32,18 @@ main(int argc, char** argv) assertFalse(t2.is_invalid_time()); }); + tests.add_test("test_rescale", [] { + RationalTime t1(1.0, 1.0); + RationalTime t2 = t1.rescaled_to(24); + assertEqual(t2.value(), 24); + assertEqual(t2.rate(), 24); + + // Try rescaling an invalid time: + t2 = RationalTime(1.0, 0.0).rescaled_to(24); + assertEqual(t2.value(), 0); + assertEqual(t2.rate(), 24); + }); + tests.add_test("test_equality", [] { RationalTime t1(30.2); assertEqual(t1, t1);