From f98ff0d26af6f21c2493308351255c43870c0418 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Fri, 6 Oct 2023 14:01:36 -0600 Subject: [PATCH] Assert against division by zero in public APIs Yet another class of UB we can eliminate in debug builds. --- include/SFML/System/Angle.inl | 5 +++++ include/SFML/System/Time.hpp | 1 + include/SFML/System/Time.inl | 7 +++++++ include/SFML/System/Vector2.inl | 2 ++ include/SFML/System/Vector3.inl | 2 ++ 5 files changed, 17 insertions(+) diff --git a/include/SFML/System/Angle.inl b/include/SFML/System/Angle.inl index 4d4ea3974..283170826 100644 --- a/include/SFML/System/Angle.inl +++ b/include/SFML/System/Angle.inl @@ -191,6 +191,7 @@ constexpr Angle& operator*=(Angle& left, float right) //////////////////////////////////////////////////////////// constexpr Angle operator/(Angle left, float right) { + assert(right != 0 && "Angle::operator/ cannot divide by 0"); return degrees(left.asDegrees() / right); } @@ -198,6 +199,7 @@ constexpr Angle operator/(Angle left, float right) //////////////////////////////////////////////////////////// constexpr Angle& operator/=(Angle& left, float right) { + assert(right != 0 && "Angle::operator/= cannot divide by 0"); return left = left / right; } @@ -205,6 +207,7 @@ constexpr Angle& operator/=(Angle& left, float right) //////////////////////////////////////////////////////////// constexpr float operator/(Angle left, Angle right) { + assert(right.asDegrees() != 0 && "Angle::operator/ cannot divide by 0"); return left.asDegrees() / right.asDegrees(); } @@ -212,6 +215,7 @@ constexpr float operator/(Angle left, Angle right) //////////////////////////////////////////////////////////// constexpr Angle operator%(Angle left, Angle right) { + assert(right.asDegrees() != 0 && "Angle::operator% cannot divide by 0"); return degrees(priv::positiveRemainder(left.asDegrees(), right.asDegrees())); } @@ -219,6 +223,7 @@ constexpr Angle operator%(Angle left, Angle right) //////////////////////////////////////////////////////////// constexpr Angle& operator%=(Angle& left, Angle right) { + assert(right.asDegrees() != 0 && "Angle::operator%= cannot modulus by 0"); return left = left % right; } diff --git a/include/SFML/System/Time.hpp b/include/SFML/System/Time.hpp index c806fb28f..e53b1f3c1 100644 --- a/include/SFML/System/Time.hpp +++ b/include/SFML/System/Time.hpp @@ -32,6 +32,7 @@ #include #include +#include #include diff --git a/include/SFML/System/Time.inl b/include/SFML/System/Time.inl index eedbb353a..65dab49f6 100644 --- a/include/SFML/System/Time.inl +++ b/include/SFML/System/Time.inl @@ -213,6 +213,7 @@ constexpr Time& operator*=(Time& left, std::int64_t right) //////////////////////////////////////////////////////////// constexpr Time operator/(Time left, float right) { + assert(right != 0 && "Time::operator/ cannot divide by 0"); return seconds(left.asSeconds() / right); } @@ -220,6 +221,7 @@ constexpr Time operator/(Time left, float right) //////////////////////////////////////////////////////////// constexpr Time operator/(Time left, std::int64_t right) { + assert(right != 0 && "Time::operator/ cannot divide by 0"); return microseconds(left.asMicroseconds() / right); } @@ -227,6 +229,7 @@ constexpr Time operator/(Time left, std::int64_t right) //////////////////////////////////////////////////////////// constexpr Time& operator/=(Time& left, float right) { + assert(right != 0 && "Time::operator/= cannot divide by 0"); return left = left / right; } @@ -234,6 +237,7 @@ constexpr Time& operator/=(Time& left, float right) //////////////////////////////////////////////////////////// constexpr Time& operator/=(Time& left, std::int64_t right) { + assert(right != 0 && "Time::operator/= cannot divide by 0"); return left = left / right; } @@ -241,6 +245,7 @@ constexpr Time& operator/=(Time& left, std::int64_t right) //////////////////////////////////////////////////////////// constexpr float operator/(Time left, Time right) { + assert(right.asMicroseconds() != 0 && "Time::operator/ cannot divide by 0"); return left.asSeconds() / right.asSeconds(); } @@ -248,6 +253,7 @@ constexpr float operator/(Time left, Time right) //////////////////////////////////////////////////////////// constexpr Time operator%(Time left, Time right) { + assert(right.asMicroseconds() != 0 && "Time::operator% cannot modulus by 0"); return microseconds(left.asMicroseconds() % right.asMicroseconds()); } @@ -255,6 +261,7 @@ constexpr Time operator%(Time left, Time right) //////////////////////////////////////////////////////////// constexpr Time& operator%=(Time& left, Time right) { + assert(right.asMicroseconds() != 0 && "Time::operator%= cannot modulus by 0"); return left = left % right; } diff --git a/include/SFML/System/Vector2.inl b/include/SFML/System/Vector2.inl index 73ad1bab6..42681648d 100644 --- a/include/SFML/System/Vector2.inl +++ b/include/SFML/System/Vector2.inl @@ -177,6 +177,7 @@ constexpr Vector2& operator*=(Vector2& left, T right) template constexpr Vector2 operator/(const Vector2& left, T right) { + assert(right != 0 && "Vector2::operator/ cannot divide by 0"); return Vector2(left.x / right, left.y / right); } @@ -185,6 +186,7 @@ constexpr Vector2 operator/(const Vector2& left, T right) template constexpr Vector2& operator/=(Vector2& left, T right) { + assert(right != 0 && "Vector2::operator/= cannot divide by 0"); left.x /= right; left.y /= right; diff --git a/include/SFML/System/Vector3.inl b/include/SFML/System/Vector3.inl index 9387ed4b9..c760f1dff 100644 --- a/include/SFML/System/Vector3.inl +++ b/include/SFML/System/Vector3.inl @@ -176,6 +176,7 @@ constexpr Vector3& operator*=(Vector3& left, T right) template constexpr Vector3 operator/(const Vector3& left, T right) { + assert(right != 0 && "Vector3::operator/ cannot divide by 0"); return Vector3(left.x / right, left.y / right, left.z / right); } @@ -184,6 +185,7 @@ constexpr Vector3 operator/(const Vector3& left, T right) template constexpr Vector3& operator/=(Vector3& left, T right) { + assert(right != 0 && "Vector3::operator/= cannot divide by 0"); left.x /= right; left.y /= right; left.z /= right;