From da68c4580cbb087d928f010468c6165aacd6b299 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 7 May 2021 09:45:54 +0430 Subject: [PATCH] AK: Make Checked check for division overflow as well Signed integer overflow can occur with division when the RHS is -1, as the negative values' range is one larger than the positives. --- AK/Checked.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/AK/Checked.h b/AK/Checked.h index 8d5321d5b6..0a92152162 100644 --- a/AK/Checked.h +++ b/AK/Checked.h @@ -183,6 +183,13 @@ public: constexpr void div(T other) { + if constexpr (IsSigned) { + // Ensure that the resulting value won't be out of range, this can only happen when dividing by -1. + if (other == -1 && m_value == NumericLimits::min()) { + m_overflow = true; + return; + } + } m_value /= other; }