Skip to content

Commit 7adfb27

Browse files
Check if we are dividing by zero (#2016)
Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>
1 parent c51d995 commit 7adfb27

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/opentime/rationalTime.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class OPENTIME_API_TYPE RationalTime
6969
/// @brief Returns the time value converted to a new rate.
7070
constexpr double value_rescaled_to(double new_rate) const noexcept
7171
{
72-
return new_rate == _rate ? _value : (_value * new_rate) / _rate;
72+
return new_rate == _rate
73+
? _value
74+
: (_rate > 0 ? (_value * new_rate) / _rate : 0);
7375
}
7476

7577
/// @brief Returns the time value converted to a new rate.

tests/test_opentime.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ main(int argc, char** argv)
3232
assertFalse(t2.is_invalid_time());
3333
});
3434

35+
tests.add_test("test_rescale", [] {
36+
RationalTime t1(1.0, 1.0);
37+
RationalTime t2 = t1.rescaled_to(24);
38+
assertEqual(t2.value(), 24);
39+
assertEqual(t2.rate(), 24);
40+
41+
// Try rescaling an invalid time:
42+
t2 = RationalTime(1.0, 0.0).rescaled_to(24);
43+
assertEqual(t2.value(), 0);
44+
assertEqual(t2.rate(), 24);
45+
});
46+
3547
tests.add_test("test_equality", [] {
3648
RationalTime t1(30.2);
3749
assertEqual(t1, t1);

0 commit comments

Comments
 (0)