From 1e439979f7e359543fdb0c86238d51f64e161625 Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Mon, 19 Jul 2021 12:41:31 +0100 Subject: Fix: OverflowSafeInt negation not handling INT64_MIN INT64_MIN negated is above INT64_MAX, and would overflow. Instead, when negating INT64_MIN make it INT64_MAX. This does mean that -(-(INT64_MIN)) != INT64_MIN. --- src/core/overflowsafe_type.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/overflowsafe_type.hpp b/src/core/overflowsafe_type.hpp index 2b1edeede..0a9df5920 100644 --- a/src/core/overflowsafe_type.hpp +++ b/src/core/overflowsafe_type.hpp @@ -34,7 +34,7 @@ public: inline OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } - inline OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); } + inline OverflowSafeInt operator - () const { return OverflowSafeInt(this->m_value == T_MIN ? T_MAX : -this->m_value); } /** * Safe implementation of addition. -- cgit v1.2.3