From 940b4d1848e8c70ab7642901a68594e8016caffc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 18:51:28 +0200 Subject: Adding upstream version 1:7.0.4. Signed-off-by: Daniel Baumann --- include/o3tl/safeint.hxx | 241 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 include/o3tl/safeint.hxx (limited to 'include/o3tl/safeint.hxx') diff --git a/include/o3tl/safeint.hxx b/include/o3tl/safeint.hxx new file mode 100644 index 000000000..6d8d1304f --- /dev/null +++ b/include/o3tl/safeint.hxx @@ -0,0 +1,241 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_SAFEINT_HXX +#define INCLUDED_O3TL_SAFEINT_HXX + +#include + +#include +#include +#include + +#if defined(_MSC_VER) +#include +#else +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif +#endif + +namespace o3tl +{ + +template inline +typename std::enable_if::value, T>::type saturating_add( + T a, T b) +{ + if (b >= 0) { + if (a <= std::numeric_limits::max() - b) { + return a + b; + } else { + return std::numeric_limits::max(); + } + } else { + if (a >= std::numeric_limits::min() - b) { + return a + b; + } else { + return std::numeric_limits::min(); + } + } +} + +template inline +typename std::enable_if::value, T>::type saturating_add( + T a, T b) +{ + if (a <= std::numeric_limits::max() - b) { + return a + b; + } else { + return std::numeric_limits::max(); + } +} + +template inline +typename std::enable_if::value, T>::type saturating_sub( + T a, T b) +{ + if (b >= 0) { + if (a >= std::numeric_limits::min() + b) { + return a - b; + } else { + return std::numeric_limits::min(); + } + } else { + if (a <= std::numeric_limits::max() + b) { + return a - b; + } else { + return std::numeric_limits::max(); + } + } +} + +template inline +typename std::enable_if::value, T>::type saturating_sub( + T a, T b) +{ + if (a >= std::numeric_limits::min() + b) { + return a - b; + } else { + return std::numeric_limits::min(); + } +} + +template inline +typename std::enable_if::value, T>::type saturating_toggle_sign( + T a) +{ + if (a == std::numeric_limits::min()) + return std::numeric_limits::max(); + return a * -1; +} + +#if defined(_MSC_VER) + +template inline bool checked_multiply(T a, T b, T& result) +{ + return !msl::utilities::SafeMultiply(a, b, result); +} + +template inline bool checked_add(T a, T b, T& result) +{ + return !msl::utilities::SafeAdd(a, b, result); +} + +template inline bool checked_sub(T a, T b, T& result) +{ + return !msl::utilities::SafeSubtract(a, b, result); +} + +#elif (defined __GNUC__ && !defined __clang__) || (__has_builtin(__builtin_mul_overflow) && !(defined ANDROID && defined __clang__) && !(defined(__clang__) && defined(__i386__))) +// 32-bit clang fails with undefined reference to `__mulodi4' + +template inline bool checked_multiply(T a, T b, T& result) +{ + return __builtin_mul_overflow(a, b, &result); +} + +template inline bool checked_add(T a, T b, T& result) +{ + return __builtin_add_overflow(a, b, &result); +} + +template inline bool checked_sub(T a, T b, T& result) +{ + return __builtin_sub_overflow(a, b, &result); +} + +#else + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template inline typename std::enable_if::value, bool>::type checked_multiply(T a, T b, T& result) +{ + if (a > 0) { /* a is positive */ + if (b > 0) { /* a and b are positive */ + if (a > (std::numeric_limits::max() / b)) { + return true; /* Handle error */ + } + } else { /* a positive, b nonpositive */ + if (b < (std::numeric_limits::min() / a)) { + return true; /* Handle error */ + } + } /* a positive, b nonpositive */ + } else { /* a is nonpositive */ + if (b > 0) { /* a is nonpositive, b is positive */ + if (a < (std::numeric_limits::min() / b)) { + return true; /* Handle error */ + } + } else { /* a and b are nonpositive */ + if ( (a != 0) && (b < (std::numeric_limits::max() / a))) { + return true; /* Handle error */ + } + } /* End if a and b are nonpositive */ + } /* End if a is nonpositive */ + + result = a * b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template inline typename std::enable_if::value, bool>::type checked_multiply(T a, T b, T& result) +{ + if (b && a > std::numeric_limits::max() / b) { + return true;/* Handle error */ + } + + result = a * b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template inline typename std::enable_if::value, bool>::type checked_add(T a, T b, T& result) +{ + if (((b > 0) && (a > (std::numeric_limits::max() - b))) || + ((b < 0) && (a < (std::numeric_limits::min() - b)))) { + return true; + } + + result = a + b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template inline typename std::enable_if::value, bool>::type checked_add(T a, T b, T& result) +{ + if (std::numeric_limits::max() - a < b) { + return true;/* Handle error */ + } + + result = a + b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template inline typename std::enable_if::value, bool>::type checked_sub(T a, T b, T& result) +{ + if ((b > 0 && a < std::numeric_limits::min() + b) || + (b < 0 && a > std::numeric_limits::max() + b)) { + return true; + } + + result = a - b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template inline typename std::enable_if::value, bool>::type checked_sub(T a, T b, T& result) +{ + if (a < b) { + return true; + } + + result = a - b; + + return false; +} + +#endif + +template constexpr std::enable_if_t, std::make_unsigned_t> +make_unsigned(T value) +{ + assert(value >= 0); + return value; +} + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3