From abb84aa2ca061763bf15f822f918981f888b5c29 Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Mon, 25 Apr 2022 14:26:07 -0700 Subject: [PATCH] rational: assert when limits are used for calculations These will necessarily result in overflows or underflows so I've attempted to weed this behavior out while adding asserts in case there are still usages I haven't found --- app/common/rational.cpp | 8 ++++++++ app/common/rational.h | 2 +- app/common/timerange.cpp | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/common/rational.cpp b/app/common/rational.cpp index 34d627572..e7020f92b 100644 --- a/app/common/rational.cpp +++ b/app/common/rational.cpp @@ -186,6 +186,8 @@ const rational& rational::operator=(const rational &rhs) const rational& rational::operator+=(const rational &rhs) { + Q_ASSERT(*this != RATIONAL_MIN && *this != RATIONAL_MAX && rhs != RATIONAL_MIN && rhs != RATIONAL_MAX); + if (!isNaN()) { if (rhs.isNaN()) { // Set to NaN @@ -209,6 +211,8 @@ const rational& rational::operator+=(const rational &rhs) const rational& rational::operator-=(const rational &rhs) { + Q_ASSERT(*this != RATIONAL_MIN && *this != RATIONAL_MAX && rhs != RATIONAL_MIN && rhs != RATIONAL_MAX); + if (!isNaN()) { if (rhs.isNaN()) { // Set to NaN @@ -232,6 +236,8 @@ const rational& rational::operator-=(const rational &rhs) const rational& rational::operator/=(const rational &rhs) { + Q_ASSERT(*this != RATIONAL_MIN && *this != RATIONAL_MAX && rhs != RATIONAL_MIN && rhs != RATIONAL_MAX); + if (!isNaN()) { if (rhs.isNaN()) { // Set to NaN @@ -250,6 +256,8 @@ const rational& rational::operator/=(const rational &rhs) const rational& rational::operator*=(const rational &rhs) { + Q_ASSERT(*this != RATIONAL_MIN && *this != RATIONAL_MAX && rhs != RATIONAL_MIN && rhs != RATIONAL_MAX); + if (!isNaN()) { if (rhs.isNaN()) { denom_ = 0; diff --git a/app/common/rational.h b/app/common/rational.h index 074f1b864..26f625fc6 100644 --- a/app/common/rational.h +++ b/app/common/rational.h @@ -1,7 +1,7 @@ //Copyright 2015 Adam Quintero //This program is distributed under the terms of the GNU General Public License. -// Adapted by MattKC for the Olive Video Editor (2019) +// Adapted by MattKC for the Olive Video Editor (2019-2022) #ifndef RATIONAL_H #define RATIONAL_H diff --git a/app/common/timerange.cpp b/app/common/timerange.cpp index c352e476d..c4337a7aa 100644 --- a/app/common/timerange.cpp +++ b/app/common/timerange.cpp @@ -44,6 +44,7 @@ const rational &TimeRange::out() const const rational &TimeRange::length() const { + Q_ASSERT(!length_.isNaN()); return length_; } @@ -173,7 +174,11 @@ void TimeRange::normalize() } // Calculate length - length_ = out_ - in_; + if (out_ == RATIONAL_MIN || out_ == RATIONAL_MAX || in_ == RATIONAL_MIN || in_ == RATIONAL_MAX) { + length_ = rational::NaN; + } else { + length_ = out_ - in_; + } } void TimeRangeList::insert(const TimeRangeList &list_to_add)