From 26b8818481c7e99684f734261f7281070fb0f386 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Thu, 22 Aug 2024 12:38:53 +0100 Subject: cid#1608296 silence Overflowed integer argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and cid#1606815 Overflowed integer argument cid#1606617 Overflowed integer argument Change-Id: I4569190edd9b8d65e9b080a7ad0fac391f4a657e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172348 Tested-by: Jenkins Reviewed-by: Caolán McNamara --- include/o3tl/safeint.hxx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'include/o3tl') diff --git a/include/o3tl/safeint.hxx b/include/o3tl/safeint.hxx index a32c6beea142..80f8b45c4042 100644 --- a/include/o3tl/safeint.hxx +++ b/include/o3tl/safeint.hxx @@ -231,6 +231,42 @@ template [[nodiscard]] inline T sanitizing_min(T a, T b) return std::min(a, b); } +// To sanitize in/de-crementing value where the result is known by the caller to be guaranteed to fit in +// the source type range without over/under-flow +[[nodiscard]] inline unsigned short sanitizing_inc(unsigned short value) +{ + int res = value + 1; + assert(res <= std::numeric_limits::max() && + "nValue was supposed to be incrementable without overflow"); + return static_cast(res); +} + +[[nodiscard]] inline unsigned short sanitizing_dec(unsigned short value) +{ + int res = value - 1; + assert(res >= 0 && + "nValue was supposed to be decrementable without underflow"); + return static_cast(res); +} + +[[nodiscard]] inline short sanitizing_inc(short value) +{ + int res = value + 1; + assert(res >= std::numeric_limits::min() && + res <= std::numeric_limits::max() && + "nValue was supposed to be incrementable without overflow"); + return static_cast(res); +} + +[[nodiscard]] inline short sanitizing_dec(short value) +{ + int res = value - 1; + assert(res >= std::numeric_limits::min() && + res <= std::numeric_limits::max() && + "nValue was supposed to be decrementable without underflow"); + return static_cast(res); +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit