diff options
author | Caolán McNamara <caolan.mcnamara@collabora.com> | 2024-08-22 12:38:53 +0100 |
---|---|---|
committer | Caolán McNamara <caolan.mcnamara@collabora.com> | 2024-08-25 17:36:28 +0200 |
commit | 26b8818481c7e99684f734261f7281070fb0f386 (patch) | |
tree | 457d6e1a16e19d38e97e99cc954796f85d21ed26 /include/o3tl | |
parent | 472644f5a30b47f7855ee988a207bab8960e954d (diff) |
cid#1608296 silence Overflowed integer argument
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 <caolan.mcnamara@collabora.com>
Diffstat (limited to 'include/o3tl')
-rw-r--r-- | include/o3tl/safeint.hxx | 36 |
1 files changed, 36 insertions, 0 deletions
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<typename T> [[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<unsigned short>::max() && + "nValue was supposed to be incrementable without overflow"); + return static_cast<unsigned short>(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<unsigned short>(res); +} + +[[nodiscard]] inline short sanitizing_inc(short value) +{ + int res = value + 1; + assert(res >= std::numeric_limits<short>::min() && + res <= std::numeric_limits<short>::max() && + "nValue was supposed to be incrementable without overflow"); + return static_cast<short>(res); +} + +[[nodiscard]] inline short sanitizing_dec(short value) +{ + int res = value - 1; + assert(res >= std::numeric_limits<short>::min() && + res <= std::numeric_limits<short>::max() && + "nValue was supposed to be decrementable without underflow"); + return static_cast<short>(res); +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |