2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-14 19:50:20 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 07:41:39 +00:00
|
|
|
* 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/. */
|
2012-05-14 19:50:20 +00:00
|
|
|
|
|
|
|
/* Provides checked integers, detecting integer overflow and divide-by-0. */
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#ifndef mozilla_CheckedInt_h
|
|
|
|
#define mozilla_CheckedInt_h
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2013-07-30 14:25:31 +00:00
|
|
|
#include <stdint.h>
|
2014-04-01 13:38:42 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-12-18 20:27:05 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-01 13:38:42 +00:00
|
|
|
#include "mozilla/IntegerTypeTraits.h"
|
2020-01-08 16:30:40 +00:00
|
|
|
#include <limits>
|
2020-02-20 14:16:03 +00:00
|
|
|
#include <type_traits>
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2019-12-16 10:22:57 +00:00
|
|
|
#define MOZILLA_CHECKEDINT_COMPARABLE_VERSION(major, minor, patch) \
|
|
|
|
(major << 16 | minor << 8 | patch)
|
|
|
|
|
2017-09-21 16:24:10 +00:00
|
|
|
// Probe for builtin math overflow support. Disabled for 32-bit builds for now
|
|
|
|
// since "gcc -m32" claims to support these but its implementation is buggy.
|
|
|
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82274
|
2019-12-16 10:22:57 +00:00
|
|
|
// Also disabled for clang before version 7 (resp. Xcode clang 10.0.1): while
|
|
|
|
// clang 5 and 6 have a working __builtin_add_overflow, it is not constexpr.
|
2017-09-21 16:24:10 +00:00
|
|
|
#if defined(HAVE_64BIT_BUILD)
|
2019-12-16 10:22:57 +00:00
|
|
|
# if defined(__has_builtin) && \
|
|
|
|
(!defined(__clang_major__) || \
|
|
|
|
(!defined(__apple_build_version__) && __clang_major__ >= 7) || \
|
|
|
|
(defined(__apple_build_version__) && \
|
|
|
|
MOZILLA_CHECKEDINT_COMPARABLE_VERSION( \
|
|
|
|
__clang_major__, __clang_minor__, __clang_patchlevel__) >= \
|
|
|
|
MOZILLA_CHECKEDINT_COMPARABLE_VERSION(10, 0, 1)))
|
2017-09-21 16:24:10 +00:00
|
|
|
# define MOZ_HAS_BUILTIN_OP_OVERFLOW (__has_builtin(__builtin_add_overflow))
|
|
|
|
# elif defined(__GNUC__)
|
|
|
|
// (clang also defines __GNUC__ but it supports __has_builtin since at least
|
|
|
|
// v3.1 (released in 2012) so it won't get here.)
|
|
|
|
# define MOZ_HAS_BUILTIN_OP_OVERFLOW (__GNUC__ >= 5)
|
2019-01-18 09:16:18 +00:00
|
|
|
# else
|
2017-09-21 16:24:10 +00:00
|
|
|
# define MOZ_HAS_BUILTIN_OP_OVERFLOW (0)
|
2019-01-18 09:16:18 +00:00
|
|
|
# endif
|
2017-09-21 16:24:10 +00:00
|
|
|
#else
|
|
|
|
# define MOZ_HAS_BUILTIN_OP_OVERFLOW (0)
|
|
|
|
#endif
|
|
|
|
|
2019-12-16 10:22:57 +00:00
|
|
|
#undef MOZILLA_CHECKEDINT_COMPARABLE_VERSION
|
|
|
|
|
2012-05-14 19:50:20 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-12 01:45:22 +00:00
|
|
|
template <typename T>
|
|
|
|
class CheckedInt;
|
|
|
|
|
2012-05-14 19:50:20 +00:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 1: manually record supported types
|
|
|
|
*
|
|
|
|
* What's nontrivial here is that there are different families of integer
|
|
|
|
* types: basic integer types and stdint types. It is merrily undefined which
|
|
|
|
* types from one family may be just typedefs for a type from another family.
|
|
|
|
*
|
|
|
|
* For example, on GCC 4.6, aside from the basic integer types, the only other
|
|
|
|
* type that isn't just a typedef for some of them, is int8_t.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct UnsupportedType {};
|
|
|
|
|
|
|
|
template <typename IntegerType>
|
|
|
|
struct IsSupportedPass2 {
|
2014-05-30 05:40:33 +00:00
|
|
|
static const bool value = false;
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename IntegerType>
|
|
|
|
struct IsSupported {
|
2014-05-30 05:40:33 +00:00
|
|
|
static const bool value = IsSupportedPass2<IntegerType>::value;
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<int8_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<uint8_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<int16_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<uint16_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<int32_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<uint32_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupported<int64_t> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2015-01-06 23:35:02 +00:00
|
|
|
template <>
|
2012-05-14 19:50:20 +00:00
|
|
|
struct IsSupported<uint64_t> {
|
|
|
|
static const bool value = true;
|
2018-11-30 10:46:48 +00:00
|
|
|
};
|
2012-05-14 19:50:20 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<char> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2013-03-02 05:49:25 +00:00
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<signed char> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2012-05-14 19:50:20 +00:00
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<unsigned char> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<short> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<unsigned short> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<int> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<unsigned int> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<long> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<unsigned long> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2013-05-08 21:25:03 +00:00
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<long long> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct IsSupportedPass2<unsigned long long> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
2012-05-14 19:50:20 +00:00
|
|
|
|
|
|
|
/*
|
2014-04-01 13:38:42 +00:00
|
|
|
* Step 2: Implement the actual validity checks.
|
|
|
|
*
|
|
|
|
* Ideas taken from IntegerLib, code different.
|
2012-05-14 19:50:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename IntegerType, size_t Size = sizeof(IntegerType)>
|
|
|
|
struct TwiceBiggerType {
|
2014-05-30 05:40:33 +00:00
|
|
|
typedef typename detail::StdintTypeForSizeAndSignedness<
|
2020-03-28 13:57:15 +00:00
|
|
|
sizeof(IntegerType) * 2, std::is_signed_v<IntegerType>>::Type Type;
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename IntegerType>
|
|
|
|
struct TwiceBiggerType<IntegerType, 8> {
|
2014-05-30 05:40:33 +00:00
|
|
|
typedef UnsupportedType Type;
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool HasSignBit(T aX) {
|
2012-05-14 19:50:20 +00:00
|
|
|
// In C++, right bit shifts on negative values is undefined by the standard.
|
|
|
|
// Notice that signed-to-unsigned conversions are always well-defined in the
|
|
|
|
// standard, as the value congruent modulo 2**n as expected. By contrast,
|
|
|
|
// unsigned-to-signed is only well-defined if the value is representable.
|
2020-03-28 13:57:12 +00:00
|
|
|
return bool(std::make_unsigned_t<T>(aX) >> PositionOfSignBit<T>::value);
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-08 15:44:22 +00:00
|
|
|
// Bitwise ops may return a larger type, so it's good to use this inline
|
|
|
|
// helper guaranteeing that the result is really of type T.
|
2012-05-14 19:50:20 +00:00
|
|
|
template <typename T>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr T BinaryComplement(T aX) {
|
2014-05-30 05:40:33 +00:00
|
|
|
return ~aX;
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 13:57:15 +00:00
|
|
|
template <typename T, typename U, bool IsTSigned = std::is_signed_v<T>,
|
|
|
|
bool IsUSigned = std::is_signed_v<U>>
|
2012-07-05 14:13:31 +00:00
|
|
|
struct DoesRangeContainRange {};
|
|
|
|
|
|
|
|
template <typename T, typename U, bool Signedness>
|
|
|
|
struct DoesRangeContainRange<T, U, Signedness, Signedness> {
|
2014-05-30 05:40:33 +00:00
|
|
|
static const bool value = sizeof(T) >= sizeof(U);
|
2012-07-05 14:13:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
struct DoesRangeContainRange<T, U, true, false> {
|
2014-05-30 05:40:33 +00:00
|
|
|
static const bool value = sizeof(T) > sizeof(U);
|
2012-07-05 14:13:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
struct DoesRangeContainRange<T, U, false, true> {
|
2014-05-30 05:40:33 +00:00
|
|
|
static const bool value = false;
|
2012-07-05 14:13:31 +00:00
|
|
|
};
|
|
|
|
|
2020-03-28 13:57:15 +00:00
|
|
|
template <typename T, typename U, bool IsTSigned = std::is_signed_v<T>,
|
|
|
|
bool IsUSigned = std::is_signed_v<U>,
|
2012-07-05 14:13:31 +00:00
|
|
|
bool DoesTRangeContainURange = DoesRangeContainRange<T, U>::value>
|
2012-05-14 19:50:20 +00:00
|
|
|
struct IsInRangeImpl {};
|
|
|
|
|
2012-07-05 14:13:31 +00:00
|
|
|
template <typename T, typename U, bool IsTSigned, bool IsUSigned>
|
|
|
|
struct IsInRangeImpl<T, U, IsTSigned, IsUSigned, true> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(U) { return true; }
|
2012-07-05 14:13:31 +00:00
|
|
|
};
|
|
|
|
|
2012-05-14 19:50:20 +00:00
|
|
|
template <typename T, typename U>
|
2012-07-05 14:13:31 +00:00
|
|
|
struct IsInRangeImpl<T, U, true, true, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(U aX) {
|
2020-01-08 16:30:40 +00:00
|
|
|
return aX <= std::numeric_limits<T>::max() &&
|
|
|
|
aX >= std::numeric_limits<T>::min();
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2012-07-05 14:13:31 +00:00
|
|
|
struct IsInRangeImpl<T, U, false, false, false> {
|
2020-01-08 16:30:40 +00:00
|
|
|
static constexpr bool run(U aX) {
|
|
|
|
return aX <= std::numeric_limits<T>::max();
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2012-07-05 14:13:31 +00:00
|
|
|
struct IsInRangeImpl<T, U, true, false, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(U aX) {
|
2020-01-08 16:30:40 +00:00
|
|
|
return sizeof(T) > sizeof(U) || aX <= U(std::numeric_limits<T>::max());
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2012-07-05 14:13:31 +00:00
|
|
|
struct IsInRangeImpl<T, U, false, true, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(U aX) {
|
2020-01-08 16:30:40 +00:00
|
|
|
return sizeof(T) >= sizeof(U)
|
|
|
|
? aX >= 0
|
|
|
|
: aX >= 0 && aX <= U(std::numeric_limits<T>::max());
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool IsInRange(U aX) {
|
2014-05-30 05:40:33 +00:00
|
|
|
return IsInRangeImpl<T, U>::run(aX);
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool IsAddValid(T aX, T aY) {
|
2017-09-21 16:24:10 +00:00
|
|
|
#if MOZ_HAS_BUILTIN_OP_OVERFLOW
|
|
|
|
T dummy;
|
|
|
|
return !__builtin_add_overflow(aX, aY, &dummy);
|
|
|
|
#else
|
2014-05-30 05:40:33 +00:00
|
|
|
// Addition is valid if the sign of aX+aY is equal to either that of aX or
|
|
|
|
// that of aY. Since the value of aX+aY is undefined if we have a signed
|
|
|
|
// type, we compute it using the unsigned type of the same size. Beware!
|
|
|
|
// These bitwise operations can return a larger integer type, if T was a
|
|
|
|
// small type like int8_t, so we explicitly cast to T.
|
2012-05-29 16:48:26 +00:00
|
|
|
|
2020-03-28 13:57:12 +00:00
|
|
|
std::make_unsigned_t<T> ux = aX;
|
|
|
|
std::make_unsigned_t<T> uy = aY;
|
|
|
|
std::make_unsigned_t<T> result = ux + uy;
|
2020-03-28 13:57:15 +00:00
|
|
|
return std::is_signed_v<T>
|
2014-05-30 05:40:33 +00:00
|
|
|
? HasSignBit(BinaryComplement(T((result ^ aX) & (result ^ aY))))
|
|
|
|
: BinaryComplement(aX) >= aY;
|
2017-09-21 16:24:10 +00:00
|
|
|
#endif
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool IsSubValid(T aX, T aY) {
|
2017-09-21 16:24:10 +00:00
|
|
|
#if MOZ_HAS_BUILTIN_OP_OVERFLOW
|
|
|
|
T dummy;
|
|
|
|
return !__builtin_sub_overflow(aX, aY, &dummy);
|
|
|
|
#else
|
2014-05-30 05:40:33 +00:00
|
|
|
// Subtraction is valid if either aX and aY have same sign, or aX-aY and aX
|
|
|
|
// have same sign. Since the value of aX-aY is undefined if we have a signed
|
|
|
|
// type, we compute it using the unsigned type of the same size.
|
2020-03-28 13:57:12 +00:00
|
|
|
std::make_unsigned_t<T> ux = aX;
|
|
|
|
std::make_unsigned_t<T> uy = aY;
|
|
|
|
std::make_unsigned_t<T> result = ux - uy;
|
2012-05-29 16:48:26 +00:00
|
|
|
|
2020-03-28 13:57:15 +00:00
|
|
|
return std::is_signed_v<T>
|
2014-05-30 05:40:33 +00:00
|
|
|
? HasSignBit(BinaryComplement(T((result ^ aX) & (aX ^ aY))))
|
|
|
|
: aX >= aY;
|
2017-09-21 16:24:10 +00:00
|
|
|
#endif
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 13:57:15 +00:00
|
|
|
template <typename T, bool IsTSigned = std::is_signed_v<T>,
|
2012-05-14 19:50:20 +00:00
|
|
|
bool TwiceBiggerTypeIsSupported =
|
|
|
|
IsSupported<typename TwiceBiggerType<T>::Type>::value>
|
|
|
|
struct IsMulValidImpl {};
|
|
|
|
|
2012-11-02 15:30:40 +00:00
|
|
|
template <typename T, bool IsTSigned>
|
|
|
|
struct IsMulValidImpl<T, IsTSigned, true> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(T aX, T aY) {
|
2014-05-30 05:40:33 +00:00
|
|
|
typedef typename TwiceBiggerType<T>::Type TwiceBiggerType;
|
|
|
|
TwiceBiggerType product = TwiceBiggerType(aX) * TwiceBiggerType(aY);
|
|
|
|
return IsInRange<T>(product);
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct IsMulValidImpl<T, true, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(T aX, T aY) {
|
2020-01-08 16:30:40 +00:00
|
|
|
const T max = std::numeric_limits<T>::max();
|
|
|
|
const T min = std::numeric_limits<T>::min();
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
if (aX == 0 || aY == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (aX > 0) {
|
|
|
|
return aY > 0 ? aX <= max / aY : aY >= min / aX;
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
2014-05-30 05:40:33 +00:00
|
|
|
|
|
|
|
// If we reach this point, we know that aX < 0.
|
|
|
|
return aY > 0 ? aX >= min / aY : aY >= max / aX;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct IsMulValidImpl<T, false, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(T aX, T aY) {
|
2020-01-08 16:30:40 +00:00
|
|
|
return aY == 0 || aX <= std::numeric_limits<T>::max() / aY;
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2021-11-26 19:18:49 +00:00
|
|
|
constexpr bool IsMulValid(T aX, T aY) {
|
2017-09-21 16:24:10 +00:00
|
|
|
#if MOZ_HAS_BUILTIN_OP_OVERFLOW
|
|
|
|
T dummy;
|
|
|
|
return !__builtin_mul_overflow(aX, aY, &dummy);
|
|
|
|
#else
|
2014-05-30 05:40:33 +00:00
|
|
|
return IsMulValidImpl<T>::run(aX, aY);
|
2017-09-21 16:24:10 +00:00
|
|
|
#endif
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool IsDivValid(T aX, T aY) {
|
2014-05-30 05:40:33 +00:00
|
|
|
// Keep in mind that in the signed case, min/-1 is invalid because
|
|
|
|
// abs(min)>max.
|
2020-03-28 13:57:15 +00:00
|
|
|
return aY != 0 && !(std::is_signed_v<T> &&
|
2020-01-08 16:30:40 +00:00
|
|
|
aX == std::numeric_limits<T>::min() && aY == T(-1));
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 13:57:15 +00:00
|
|
|
template <typename T, bool IsTSigned = std::is_signed_v<T>>
|
2013-06-14 22:19:30 +00:00
|
|
|
struct IsModValidImpl;
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool IsModValid(T aX, T aY) {
|
2014-05-30 05:40:33 +00:00
|
|
|
return IsModValidImpl<T>::run(aX, aY);
|
2013-06-14 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mod is pretty simple.
|
|
|
|
* For now, let's just use the ANSI C definition:
|
2014-05-30 05:40:33 +00:00
|
|
|
* If aX or aY are negative, the results are implementation defined.
|
2013-06-14 22:19:30 +00:00
|
|
|
* Consider these invalid.
|
2014-05-30 05:40:33 +00:00
|
|
|
* Undefined for aY=0.
|
|
|
|
* The result will never exceed either aX or aY.
|
2013-06-14 22:19:30 +00:00
|
|
|
*
|
2014-05-30 05:40:33 +00:00
|
|
|
* Checking that aX>=0 is a warning when T is unsigned.
|
2013-06-14 22:19:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
2014-05-30 05:40:33 +00:00
|
|
|
struct IsModValidImpl<T, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(T aX, T aY) { return aY >= 1; }
|
2013-06-14 22:19:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2014-05-30 05:40:33 +00:00
|
|
|
struct IsModValidImpl<T, true> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr bool run(T aX, T aY) {
|
2014-05-30 05:40:33 +00:00
|
|
|
if (aX < 0) {
|
2013-06-14 22:19:30 +00:00
|
|
|
return false;
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
|
|
|
return aY >= 1;
|
2013-06-14 22:19:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-28 13:57:15 +00:00
|
|
|
template <typename T, bool IsSigned = std::is_signed_v<T>>
|
2013-03-12 01:45:22 +00:00
|
|
|
struct NegateImpl;
|
|
|
|
|
2012-05-14 19:50:20 +00:00
|
|
|
template <typename T>
|
2013-03-12 01:45:22 +00:00
|
|
|
struct NegateImpl<T, false> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr CheckedInt<T> negate(const CheckedInt<T>& aVal) {
|
2014-05-30 05:40:33 +00:00
|
|
|
// Handle negation separately for signed/unsigned, for simpler code and to
|
|
|
|
// avoid an MSVC warning negating an unsigned value.
|
2020-02-20 14:16:03 +00:00
|
|
|
static_assert(detail::IsInRange<T>(0), "Integer type can't represent 0");
|
|
|
|
return CheckedInt<T>(T(0), aVal.isValid() && aVal.mValue == 0);
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
2013-03-12 01:45:22 +00:00
|
|
|
|
2012-05-14 19:50:20 +00:00
|
|
|
template <typename T>
|
2013-03-12 01:45:22 +00:00
|
|
|
struct NegateImpl<T, true> {
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr CheckedInt<T> negate(const CheckedInt<T>& aVal) {
|
2014-05-30 05:40:33 +00:00
|
|
|
// Watch out for the min-value, which (with twos-complement) can't be
|
|
|
|
// negated as -min-value is then (max-value + 1).
|
2020-01-08 16:30:40 +00:00
|
|
|
if (!aVal.isValid() || aVal.mValue == std::numeric_limits<T>::min()) {
|
2014-05-30 05:40:33 +00:00
|
|
|
return CheckedInt<T>(aVal.mValue, false);
|
2013-03-12 01:45:22 +00:00
|
|
|
}
|
2020-02-20 14:16:03 +00:00
|
|
|
/* For some T, arithmetic ops automatically promote to a wider type, so
|
|
|
|
* explitly do the narrowing cast here. The narrowing cast is valid because
|
|
|
|
* we did the check for min value above. */
|
|
|
|
return CheckedInt<T>(T(-aVal.mValue), true);
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2013-03-12 01:45:22 +00:00
|
|
|
};
|
2012-05-14 19:50:20 +00:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/*
|
2014-04-01 13:38:42 +00:00
|
|
|
* Step 3: Now define the CheckedInt class.
|
2012-05-14 19:50:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class CheckedInt
|
|
|
|
* @brief Integer wrapper class checking for integer overflow and other errors
|
|
|
|
* @param T the integer type to wrap. Can be any type among the following:
|
|
|
|
* - any basic integer type such as |int|
|
|
|
|
* - any stdint type such as |int8_t|
|
|
|
|
*
|
|
|
|
* This class implements guarded integer arithmetic. Do a computation, check
|
|
|
|
* that isValid() returns true, you then have a guarantee that no problem, such
|
|
|
|
* as integer overflow, happened during this computation, and you can call
|
|
|
|
* value() to get the plain integer value.
|
|
|
|
*
|
|
|
|
* The arithmetic operators in this class are guaranteed not to raise a signal
|
|
|
|
* (e.g. in case of a division by zero).
|
|
|
|
*
|
|
|
|
* For example, suppose that you want to implement a function that computes
|
2014-05-30 05:40:33 +00:00
|
|
|
* (aX+aY)/aZ, that doesn't crash if aZ==0, and that reports on error (divide by
|
2012-05-14 19:50:20 +00:00
|
|
|
* zero or integer overflow). You could code it as follows:
|
|
|
|
@code
|
2014-07-11 02:10:17 +00:00
|
|
|
bool computeXPlusYOverZ(int aX, int aY, int aZ, int* aResult)
|
2012-05-14 19:50:20 +00:00
|
|
|
{
|
2014-05-30 05:40:33 +00:00
|
|
|
CheckedInt<int> checkedResult = (CheckedInt<int>(aX) + aY) / aZ;
|
|
|
|
if (checkedResult.isValid()) {
|
|
|
|
*aResult = checkedResult.value();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
*
|
|
|
|
* Implicit conversion from plain integers to checked integers is allowed. The
|
|
|
|
* plain integer is checked to be in range before being casted to the
|
|
|
|
* destination type. This means that the following lines all compile, and the
|
|
|
|
* resulting CheckedInts are correctly detected as valid or invalid:
|
|
|
|
* @code
|
|
|
|
// 1 is of type int, is found to be in range for uint8_t, x is valid
|
|
|
|
CheckedInt<uint8_t> x(1);
|
|
|
|
// -1 is of type int, is found not to be in range for uint8_t, x is invalid
|
|
|
|
CheckedInt<uint8_t> x(-1);
|
|
|
|
// -1 is of type int, is found to be in range for int8_t, x is valid
|
|
|
|
CheckedInt<int8_t> x(-1);
|
|
|
|
// 1000 is of type int16_t, is found not to be in range for int8_t,
|
|
|
|
// x is invalid
|
2013-06-14 22:19:40 +00:00
|
|
|
CheckedInt<int8_t> x(int16_t(1000));
|
2012-05-14 19:50:20 +00:00
|
|
|
// 3123456789 is of type uint32_t, is found not to be in range for int32_t,
|
|
|
|
// x is invalid
|
|
|
|
CheckedInt<int32_t> x(uint32_t(3123456789));
|
|
|
|
* @endcode
|
|
|
|
* Implicit conversion from
|
|
|
|
* checked integers to plain integers is not allowed. As shown in the
|
|
|
|
* above example, to get the value of a checked integer as a normal integer,
|
|
|
|
* call value().
|
|
|
|
*
|
|
|
|
* Arithmetic operations between checked and plain integers is allowed; the
|
|
|
|
* result type is the type of the checked integer.
|
|
|
|
*
|
|
|
|
* Checked integers of different types cannot be used in the same arithmetic
|
|
|
|
* expression.
|
|
|
|
*
|
|
|
|
* There are convenience typedefs for all stdint types, of the following form
|
|
|
|
* (these are just 2 examples):
|
|
|
|
@code
|
|
|
|
typedef CheckedInt<int32_t> CheckedInt32;
|
|
|
|
typedef CheckedInt<uint16_t> CheckedUint16;
|
|
|
|
@endcode
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class CheckedInt {
|
2014-05-30 05:40:33 +00:00
|
|
|
protected:
|
|
|
|
T mValue;
|
|
|
|
bool mIsValid;
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt(U aValue, bool aIsValid)
|
|
|
|
: mValue(aValue), mIsValid(aIsValid) {
|
2020-02-20 14:16:03 +00:00
|
|
|
static_assert(std::is_same_v<T, U>,
|
|
|
|
"this constructor must accept only T values");
|
|
|
|
static_assert(detail::IsSupported<T>::value,
|
|
|
|
"This type is not supported by CheckedInt");
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
friend struct detail::NegateImpl<T>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructs a checked integer with given @a value. The checked integer is
|
|
|
|
* initialized as valid or invalid depending on whether the @a value
|
|
|
|
* is in range.
|
|
|
|
*
|
|
|
|
* This constructor is not explicit. Instead, the type of its argument is a
|
|
|
|
* separate template parameter, ensuring that no conversion is performed
|
|
|
|
* before this constructor is actually called. As explained in the above
|
|
|
|
* documentation for class CheckedInt, this constructor checks that its
|
|
|
|
* argument is valid.
|
|
|
|
*/
|
|
|
|
template <typename U>
|
2018-07-21 01:36:20 +00:00
|
|
|
MOZ_IMPLICIT MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT constexpr CheckedInt(U aValue)
|
2014-05-30 05:40:33 +00:00
|
|
|
: mValue(T(aValue)), mIsValid(detail::IsInRange<T>(aValue)) {
|
|
|
|
static_assert(
|
|
|
|
detail::IsSupported<T>::value && detail::IsSupported<U>::value,
|
|
|
|
"This type is not supported by CheckedInt");
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
|
|
|
friend class CheckedInt;
|
2013-06-14 22:19:40 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt<U> toChecked() const {
|
2014-05-30 05:40:33 +00:00
|
|
|
CheckedInt<U> ret(mValue);
|
|
|
|
ret.mIsValid = ret.mIsValid && mIsValid;
|
|
|
|
return ret;
|
|
|
|
}
|
2013-06-14 22:19:40 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/** Constructs a valid checked integer with initial value 0 */
|
2020-02-20 14:16:03 +00:00
|
|
|
constexpr CheckedInt() : mValue(T(0)), mIsValid(true) {
|
2014-05-30 05:40:33 +00:00
|
|
|
static_assert(detail::IsSupported<T>::value,
|
|
|
|
"This type is not supported by CheckedInt");
|
2020-02-20 14:16:03 +00:00
|
|
|
static_assert(detail::IsInRange<T>(0), "Integer type can't represent 0");
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/** @returns the actual value */
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr T value() const {
|
2019-05-02 13:24:31 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
2014-05-30 05:40:33 +00:00
|
|
|
mIsValid,
|
|
|
|
"Invalid checked integer (division by zero or integer overflow)");
|
|
|
|
return mValue;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/**
|
|
|
|
* @returns true if the checked integer is valid, i.e. is not the result
|
|
|
|
* of an invalid operation or of an operation involving an invalid checked
|
|
|
|
* integer
|
|
|
|
*/
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool isValid() const { return mIsValid; }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
friend constexpr CheckedInt<U> operator+(const CheckedInt<U>& aLhs,
|
|
|
|
const CheckedInt<U>& aRhs);
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator+=(U aRhs);
|
|
|
|
constexpr CheckedInt& operator+=(const CheckedInt<T>& aRhs);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
friend constexpr CheckedInt<U> operator-(const CheckedInt<U>& aLhs,
|
|
|
|
const CheckedInt<U>& aRhs);
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator-=(U aRhs);
|
|
|
|
constexpr CheckedInt& operator-=(const CheckedInt<T>& aRhs);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
friend constexpr CheckedInt<U> operator*(const CheckedInt<U>& aLhs,
|
|
|
|
const CheckedInt<U>& aRhs);
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator*=(U aRhs);
|
|
|
|
constexpr CheckedInt& operator*=(const CheckedInt<T>& aRhs);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
friend constexpr CheckedInt<U> operator/(const CheckedInt<U>& aLhs,
|
|
|
|
const CheckedInt<U>& aRhs);
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator/=(U aRhs);
|
|
|
|
constexpr CheckedInt& operator/=(const CheckedInt<T>& aRhs);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
friend constexpr CheckedInt<U> operator%(const CheckedInt<U>& aLhs,
|
|
|
|
const CheckedInt<U>& aRhs);
|
2014-05-30 05:40:33 +00:00
|
|
|
template <typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator%=(U aRhs);
|
|
|
|
constexpr CheckedInt& operator%=(const CheckedInt<T>& aRhs);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt operator-() const {
|
|
|
|
return detail::NegateImpl<T>::negate(*this);
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/**
|
|
|
|
* @returns true if the left and right hand sides are valid
|
|
|
|
* and have the same value.
|
|
|
|
*
|
|
|
|
* Note that these semantics are the reason why we don't offer
|
|
|
|
* a operator!=. Indeed, we'd want to have a!=b be equivalent to !(a==b)
|
|
|
|
* but that would mean that whenever a or b is invalid, a!=b
|
|
|
|
* is always true, which would be very confusing.
|
|
|
|
*
|
|
|
|
* For similar reasons, operators <, >, <=, >= would be very tricky to
|
|
|
|
* specify, so we just avoid offering them.
|
|
|
|
*
|
|
|
|
* Notice that these == semantics are made more reasonable by these facts:
|
|
|
|
* 1. a==b implies equality at the raw data level
|
|
|
|
* (the converse is false, as a==b is never true among invalids)
|
|
|
|
* 2. This is similar to the behavior of IEEE floats, where a==b
|
|
|
|
* means that a and b have the same value *and* neither is NaN.
|
|
|
|
*/
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr bool operator==(const CheckedInt& aOther) const {
|
2014-05-30 05:40:33 +00:00
|
|
|
return mIsValid && aOther.mIsValid && mValue == aOther.mValue;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/** prefix ++ */
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator++() {
|
2014-05-30 05:40:33 +00:00
|
|
|
*this += 1;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/** postfix ++ */
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt operator++(int) {
|
2014-05-30 05:40:33 +00:00
|
|
|
CheckedInt tmp = *this;
|
|
|
|
*this += 1;
|
|
|
|
return tmp;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/** prefix -- */
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt& operator--() {
|
2014-05-30 05:40:33 +00:00
|
|
|
*this -= 1;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/** postfix -- */
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr CheckedInt operator--(int) {
|
2014-05-30 05:40:33 +00:00
|
|
|
CheckedInt tmp = *this;
|
|
|
|
*this -= 1;
|
|
|
|
return tmp;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* The !=, <, <=, >, >= operators are disabled:
|
|
|
|
* see the comment on operator==.
|
|
|
|
*/
|
2015-01-06 23:35:02 +00:00
|
|
|
template <typename U>
|
|
|
|
bool operator!=(U aOther) const = delete;
|
|
|
|
template <typename U>
|
|
|
|
bool operator<(U aOther) const = delete;
|
|
|
|
template <typename U>
|
|
|
|
bool operator<=(U aOther) const = delete;
|
|
|
|
template <typename U>
|
|
|
|
bool operator>(U aOther) const = delete;
|
|
|
|
template <typename U>
|
|
|
|
bool operator>=(U aOther) const = delete;
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2020-02-20 14:16:03 +00:00
|
|
|
#define MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(NAME, OP) \
|
|
|
|
template <typename T> \
|
|
|
|
constexpr CheckedInt<T> operator OP(const CheckedInt<T>& aLhs, \
|
|
|
|
const CheckedInt<T>& aRhs) { \
|
|
|
|
if (!detail::Is##NAME##Valid(aLhs.mValue, aRhs.mValue)) { \
|
|
|
|
static_assert(detail::IsInRange<T>(0), \
|
|
|
|
"Integer type can't represent 0"); \
|
|
|
|
return CheckedInt<T>(T(0), false); \
|
|
|
|
} \
|
|
|
|
/* For some T, arithmetic ops automatically promote to a wider type, so \
|
|
|
|
* explitly do the narrowing cast here. The narrowing cast is valid \
|
|
|
|
* because we did the "Is##NAME##Valid" check above. */ \
|
|
|
|
return CheckedInt<T>(T(aLhs.mValue OP aRhs.mValue), \
|
|
|
|
aLhs.mIsValid && aRhs.mIsValid); \
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
2017-09-21 16:24:10 +00:00
|
|
|
#if MOZ_HAS_BUILTIN_OP_OVERFLOW
|
2019-12-16 10:22:57 +00:00
|
|
|
# define MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR2(NAME, OP, FUN) \
|
|
|
|
template <typename T> \
|
|
|
|
constexpr CheckedInt<T> operator OP(const CheckedInt<T>& aLhs, \
|
|
|
|
const CheckedInt<T>& aRhs) { \
|
|
|
|
auto result = T{}; \
|
|
|
|
if (FUN(aLhs.mValue, aRhs.mValue, &result)) { \
|
2020-02-20 14:16:03 +00:00
|
|
|
static_assert(detail::IsInRange<T>(0), \
|
|
|
|
"Integer type can't represent 0"); \
|
|
|
|
return CheckedInt<T>(T(0), false); \
|
2019-12-16 10:22:57 +00:00
|
|
|
} \
|
|
|
|
return CheckedInt<T>(result, aLhs.mIsValid && aRhs.mIsValid); \
|
2017-09-21 16:24:10 +00:00
|
|
|
}
|
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR2(Add, +, __builtin_add_overflow)
|
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR2(Sub, -, __builtin_sub_overflow)
|
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR2(Mul, *, __builtin_mul_overflow)
|
|
|
|
# undef MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR2
|
|
|
|
#else
|
2012-05-14 19:50:20 +00:00
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Add, +)
|
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Sub, -)
|
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Mul, *)
|
2017-09-21 16:24:10 +00:00
|
|
|
#endif
|
|
|
|
|
2012-07-08 15:25:24 +00:00
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Div, /)
|
2013-06-14 22:19:30 +00:00
|
|
|
MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Mod, %)
|
2012-05-14 19:50:20 +00:00
|
|
|
#undef MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR
|
|
|
|
|
|
|
|
// Implement castToCheckedInt<T>(x), making sure that
|
|
|
|
// - it allows x to be either a CheckedInt<T> or any integer type
|
|
|
|
// that can be casted to T
|
|
|
|
// - if x is already a CheckedInt<T>, we just return a reference to it,
|
|
|
|
// instead of copying it (optimization)
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
struct CastToCheckedIntImpl {
|
2014-05-30 05:40:33 +00:00
|
|
|
typedef CheckedInt<T> ReturnType;
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr CheckedInt<T> run(U aU) { return aU; }
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct CastToCheckedIntImpl<T, CheckedInt<T>> {
|
2014-05-30 05:40:33 +00:00
|
|
|
typedef const CheckedInt<T>& ReturnType;
|
2019-12-16 10:22:57 +00:00
|
|
|
static constexpr const CheckedInt<T>& run(const CheckedInt<T>& aU) {
|
|
|
|
return aU;
|
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2019-12-16 10:22:57 +00:00
|
|
|
constexpr typename detail::CastToCheckedIntImpl<T, U>::ReturnType
|
|
|
|
castToCheckedInt(U aU) {
|
2013-07-18 17:59:53 +00:00
|
|
|
static_assert(detail::IsSupported<T>::value && detail::IsSupported<U>::value,
|
|
|
|
"This type is not supported by CheckedInt");
|
2014-05-30 05:40:33 +00:00
|
|
|
return detail::CastToCheckedIntImpl<T, U>::run(aU);
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 10:22:57 +00:00
|
|
|
#define MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(OP, COMPOUND_OP) \
|
|
|
|
template <typename T> \
|
|
|
|
template <typename U> \
|
|
|
|
constexpr CheckedInt<T>& CheckedInt<T>::operator COMPOUND_OP(U aRhs) { \
|
|
|
|
*this = *this OP castToCheckedInt<T>(aRhs); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
template <typename T> \
|
|
|
|
constexpr CheckedInt<T>& CheckedInt<T>::operator COMPOUND_OP( \
|
|
|
|
const CheckedInt<T>& aRhs) { \
|
|
|
|
*this = *this OP aRhs; \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
template <typename T, typename U> \
|
|
|
|
constexpr CheckedInt<T> operator OP(const CheckedInt<T>& aLhs, U aRhs) { \
|
|
|
|
return aLhs OP castToCheckedInt<T>(aRhs); \
|
|
|
|
} \
|
|
|
|
template <typename T, typename U> \
|
|
|
|
constexpr CheckedInt<T> operator OP(U aLhs, const CheckedInt<T>& aRhs) { \
|
|
|
|
return castToCheckedInt<T>(aLhs) OP aRhs; \
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-05-14 19:50:20 +00:00
|
|
|
|
|
|
|
MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(+, +=)
|
|
|
|
MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(*, *=)
|
|
|
|
MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(-, -=)
|
|
|
|
MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(/, /=)
|
2013-06-14 22:19:30 +00:00
|
|
|
MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(%, %=)
|
2012-05-14 19:50:20 +00:00
|
|
|
|
|
|
|
#undef MOZ_CHECKEDINT_CONVENIENCE_BINARY_OPERATORS
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2021-11-26 19:18:49 +00:00
|
|
|
constexpr bool operator==(const CheckedInt<T>& aLhs, U aRhs) {
|
2014-05-30 05:40:33 +00:00
|
|
|
return aLhs == castToCheckedInt<T>(aRhs);
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2021-11-26 19:18:49 +00:00
|
|
|
constexpr bool operator==(U aLhs, const CheckedInt<T>& aRhs) {
|
2014-05-30 05:40:33 +00:00
|
|
|
return castToCheckedInt<T>(aLhs) == aRhs;
|
2012-05-14 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience typedefs.
|
|
|
|
typedef CheckedInt<int8_t> CheckedInt8;
|
|
|
|
typedef CheckedInt<uint8_t> CheckedUint8;
|
|
|
|
typedef CheckedInt<int16_t> CheckedInt16;
|
|
|
|
typedef CheckedInt<uint16_t> CheckedUint16;
|
|
|
|
typedef CheckedInt<int32_t> CheckedInt32;
|
|
|
|
typedef CheckedInt<uint32_t> CheckedUint32;
|
|
|
|
typedef CheckedInt<int64_t> CheckedInt64;
|
|
|
|
typedef CheckedInt<uint64_t> CheckedUint64;
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#endif /* mozilla_CheckedInt_h */
|