Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcheckedint_impl.h
Go to the documentation of this file.
1// Copyright (C) 2025 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QCHECKEDINT_H
6#define QCHECKEDINT_H
7
8#include <QtCore/qassert.h>
9#include <QtCore/qcompare.h>
10#include <QtCore/qhashfunctions.h>
11#include <QtCore/qnumeric.h>
12
13#include <type_traits>
14
15QT_BEGIN_NAMESPACE
16
17namespace QtPrivate {
19
20template <typename Int>
22{
23 static_assert(std::is_integral_v<Int>, "Only integer types are supported");
24 static_assert(std::is_signed_v<Int>, "Only signed types are supported");
25 static_assert(std::is_same_v<Int, decltype(+Int{})>, "Only fully promoted types are supported");
26};
27
28// Implements wraparound semantics, and checks for overflow.
29// Never causes UB on any operation.
30template <typename Int>
31struct SafeCheckImpl : private CheckIntTypeHelper<Int>
32{
33 static inline constexpr Int MinInt = (std::numeric_limits<Int>::min)();
34 static inline constexpr Int MaxInt = (std::numeric_limits<Int>::max)();
35
36 [[nodiscard]]
37 static constexpr bool add(Int a, Int b, Int *result) noexcept
38 {
39 return !qAddOverflow(a, b, result);
40 }
41
42 [[nodiscard]]
43 static constexpr bool sub(Int a, Int b, Int *result) noexcept
44 {
45 return !qSubOverflow(a, b, result);
46 }
47
48 [[nodiscard]]
49 static constexpr bool mul(Int a, Int b, Int *result) noexcept
50 {
51 return !qMulOverflow(a, b, result);
52 }
53
54 template <Int B> [[nodiscard]]
55 static constexpr bool mul(Int a, std::integral_constant<Int, B> b, Int *result)
56 {
57 return !qMulOverflow(a, b, result);
58 }
59
60 [[nodiscard]]
61 static constexpr bool div(Int a, Int b, Int *result) noexcept
62 {
63 if (Q_UNLIKELY(b == 0))
64 return false;
65 if (b == -1 && a == MinInt)
66 return false;
67
68 *result = a / b;
69 return true;
70 }
71};
72
73// Reports failed checks through Q_ASSERT.
75{
76 static constexpr void check(bool ok, const char *where, const char *description)
77 {
78 Q_ASSERT_X(ok, where, description);
79 }
80};
81
82template <typename Int,
83 typename Impl = SafeCheckImpl<Int>,
84 typename FailureReportPolicy = AssertReportPolicy>
85class QCheckedInt : private CheckIntTypeHelper<Int>
86{
87 Int m_i;
88
89#define Q_CHECKEDINT_POLICY_CHECK(cond, what)
90 FailureReportPolicy::check(cond, Q_FUNC_INFO, what)
91
92public:
93 template <typename AInt>
95
96 QCheckedInt() = default;
97
98 explicit constexpr QCheckedInt(Int i) noexcept
99 : m_i(i)
100 {}
101
102 // Conversions
103 explicit constexpr operator Int() const noexcept
104 {
105 return m_i;
106 }
107
108 // Accessors
109 constexpr Int value() const noexcept { return m_i; }
110 template <typename AInt, if_is_same_int<AInt> = true>
111 constexpr void setValue(AInt i) noexcept { m_i = i; }
112
113 constexpr Int &as_underlying() & noexcept { return m_i; }
114 constexpr const Int &as_underlying() const & noexcept { return m_i; }
115 constexpr Int &&as_underlying() && noexcept { return std::move(m_i); }
116 constexpr const Int &&as_underlying() const && noexcept { return std::move(m_i); }
117
118 // Unary ops
119 constexpr QCheckedInt operator+() const noexcept { return *this; }
120 constexpr QCheckedInt operator-() const
121 {
122 QCheckedInt result{};
123 const bool ok = Impl::sub(Int(0), m_i, &result.m_i);
124 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in unary negation");
125 return result;
126 }
127
128 constexpr QCheckedInt &operator++()
129 {
130 const bool ok = Impl::add(m_i, Int(1), &m_i);
131 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in operator++");
132 return *this;
133 }
134
135 constexpr QCheckedInt operator++(int)
136 {
137 QCheckedInt result = *this;
138 ++*this;
139 return result;
140 }
141
142 constexpr QCheckedInt &operator--()
143 {
144 const bool ok = Impl::sub(m_i, Int(1), &m_i);
145 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in operator--");
146 return *this;
147 }
148
149 constexpr QCheckedInt operator--(int)
150 {
151 QCheckedInt result = *this;
152 --*this;
153 return result;
154 }
155
156 // Addition
157 friend constexpr QCheckedInt operator+(QCheckedInt lhs, QCheckedInt rhs)
158 {
159 QCheckedInt result{};
160 const bool ok = Impl::add(lhs.m_i, rhs.m_i, &result.m_i);
161 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in operator+");
162 return result;
163 }
164
165 template <typename AInt, if_is_same_int<AInt> = true>
166 friend constexpr QCheckedInt operator+(QCheckedInt lhs, AInt rhs)
167 {
168 return lhs + QCheckedInt(rhs);
169 }
170
171 template <typename AInt, if_is_same_int<AInt> = true>
172 friend constexpr QCheckedInt operator+(AInt lhs, QCheckedInt rhs)
173 {
174 return QCheckedInt(lhs) + rhs;
175 }
176
177 constexpr QCheckedInt &operator+=(QCheckedInt other)
178 {
179 return *this = *this + other;
180 }
181
182 template <typename AInt, if_is_same_int<AInt> = true>
183 constexpr QCheckedInt &operator+=(AInt other)
184 {
185 return *this = *this + QCheckedInt(other);
186 }
187
188 // Subtraction
189 friend constexpr QCheckedInt operator-(QCheckedInt lhs, QCheckedInt rhs)
190 {
191 QCheckedInt result{};
192 const bool ok = Impl::sub(lhs.m_i, rhs.m_i, &result.m_i);
193 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in operator-");
194 return result;
195 }
196
197 template <typename AInt, if_is_same_int<AInt> = true>
198 friend constexpr QCheckedInt operator-(QCheckedInt lhs, AInt rhs)
199 {
200 return lhs - QCheckedInt(rhs);
201 }
202
203 template <typename AInt, if_is_same_int<AInt> = true>
204 friend constexpr QCheckedInt operator-(AInt lhs, QCheckedInt rhs)
205 {
206 return QCheckedInt(lhs) - rhs;
207 }
208
209 constexpr QCheckedInt &operator-=(QCheckedInt other)
210 {
211 return *this = *this - other;
212 }
213
214 template <typename AInt, if_is_same_int<AInt> = true>
215 constexpr QCheckedInt &operator-=(AInt other)
216 {
217 return *this = *this - QCheckedInt(other);
218 }
219
220 // Multiplication
221 friend constexpr QCheckedInt operator*(QCheckedInt lhs, QCheckedInt rhs)
222 {
223 QCheckedInt result{};
224 const bool ok = Impl::mul(lhs.m_i, rhs.m_i, &result.m_i);
225 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in operator*");
226 return result;
227 }
228
229 template <typename AInt, if_is_same_int<AInt> = true>
230 friend constexpr QCheckedInt operator*(QCheckedInt lhs, AInt rhs)
231 {
232 return lhs * QCheckedInt(rhs);
233 }
234
235 template <typename AInt, if_is_same_int<AInt> = true>
236 friend constexpr QCheckedInt operator*(AInt lhs, QCheckedInt rhs)
237 {
238 return QCheckedInt(lhs) * rhs;
239 }
240
241 constexpr QCheckedInt &operator*=(QCheckedInt other)
242 {
243 return *this = *this * other;
244 }
245
246 template <typename AInt, if_is_same_int<AInt> = true>
247 constexpr QCheckedInt &operator*=(AInt other)
248 {
249 return *this = *this * QCheckedInt(other);
250 }
251
252 template <Int RHS>
253 friend constexpr QCheckedInt operator*(QCheckedInt lhs, std::integral_constant<Int, RHS> rhs)
254 {
255 QCheckedInt result{};
256 const bool ok = Impl::mul(lhs.m_i, rhs, &result.m_i);
257 Q_CHECKEDINT_POLICY_CHECK(ok, "Overflow in operator*");
258 return result;
259 }
260
261 template <Int LHS>
262 friend constexpr QCheckedInt operator*(std::integral_constant<Int, LHS> lhs, QCheckedInt rhs)
263 {
264 return rhs * lhs;
265 }
266
267 template <Int O>
268 constexpr QCheckedInt &operator*=(std::integral_constant<Int, O> other)
269 {
270 return *this = *this * other;
271 }
272
273 // Division
274 friend constexpr QCheckedInt operator/(QCheckedInt lhs, QCheckedInt rhs)
275 {
276 QCheckedInt result{};
277 const bool ok = Impl::div(lhs.m_i, rhs.m_i, &result.m_i);
278 Q_CHECKEDINT_POLICY_CHECK(ok, "Division by zero");
279 return result;
280 }
281
282 template <typename AInt, if_is_same_int<AInt> = true>
283 friend constexpr QCheckedInt operator/(QCheckedInt lhs, AInt rhs)
284 {
285 return lhs / QCheckedInt(rhs);
286 }
287
288 template <typename AInt, if_is_same_int<AInt> = true>
289 friend constexpr QCheckedInt operator/(AInt lhs, QCheckedInt rhs)
290 {
291 return QCheckedInt(lhs) / rhs;
292 }
293
294 constexpr QCheckedInt &operator/=(QCheckedInt other)
295 {
296 return *this = *this / other;
297 }
298
299 template <typename AInt, if_is_same_int<AInt> = true>
300 constexpr QCheckedInt &operator/=(AInt other)
301 {
302 return *this = *this / QCheckedInt(other);
303 }
304
305#undef Q_CHECKEDINT_POLICY_CHECK
306
307 // Comparisons
308 friend constexpr bool comparesEqual(QCheckedInt lhs, QCheckedInt rhs) noexcept
309 {
310 return lhs.m_i == rhs.m_i;
311 }
312
313 template <typename AInt, if_is_same_int<AInt> = true>
314 friend constexpr bool comparesEqual(QCheckedInt lhs, AInt rhs) noexcept
315 {
316 return lhs.m_i == rhs;
317 }
318
319 friend constexpr Qt::strong_ordering compareThreeWay(QCheckedInt lhs, QCheckedInt rhs) noexcept
320 {
321 return Qt::compareThreeWay(lhs.m_i, rhs.m_i);
322 }
323
324 template <typename AInt, if_is_same_int<AInt> = true>
325 friend constexpr Qt::strong_ordering compareThreeWay(QCheckedInt lhs, AInt rhs) noexcept
326 {
327 return Qt::compareThreeWay(lhs.m_i, rhs);
328 }
329
333 AInt,
334 template <typename AInt, if_is_same_int<AInt> = true>)
335
336private:
337 friend size_t constexpr qHash(QCheckedInt key, size_t seed = 0) noexcept
338 {
339 using QT_PREPEND_NAMESPACE(qHash); // ### needed?
340 return qHash(key.value(), seed);
341 }
342};
343
344} // namespace QCheckedIntegers
345} // namespace QtPrivate
346
347QT_END_NAMESPACE
348
349#endif // QCHECKEDINT_H
friend constexpr QCheckedInt operator+(AInt lhs, QCheckedInt rhs)
constexpr QCheckedInt & operator-=(AInt other)
friend constexpr QCheckedInt operator/(AInt lhs, QCheckedInt rhs)
friend constexpr QCheckedInt operator/(QCheckedInt lhs, QCheckedInt rhs)
constexpr QCheckedInt operator+() const noexcept
constexpr QCheckedInt & operator*=(QCheckedInt other)
constexpr const Int & as_underlying() const &noexcept
friend constexpr QCheckedInt operator+(QCheckedInt lhs, QCheckedInt rhs)
friend constexpr QCheckedInt operator-(QCheckedInt lhs, QCheckedInt rhs)
friend constexpr Qt::strong_ordering compareThreeWay(QCheckedInt lhs, AInt rhs) noexcept
constexpr QCheckedInt & operator*=(AInt other)
constexpr const Int && as_underlying() const &&noexcept
constexpr void setValue(AInt i) noexcept
constexpr QCheckedInt & operator/=(AInt other)
friend constexpr QCheckedInt operator-(AInt lhs, QCheckedInt rhs)
constexpr QCheckedInt & operator+=(QCheckedInt other)
friend constexpr bool comparesEqual(QCheckedInt lhs, QCheckedInt rhs) noexcept
constexpr Int && as_underlying() &&noexcept
constexpr QCheckedInt & operator/=(QCheckedInt other)
constexpr Int & as_underlying() &noexcept
constexpr Int value() const noexcept
friend constexpr Qt::strong_ordering compareThreeWay(QCheckedInt lhs, QCheckedInt rhs) noexcept
friend constexpr QCheckedInt operator-(QCheckedInt lhs, AInt rhs)
friend constexpr QCheckedInt operator*(AInt lhs, QCheckedInt rhs)
friend constexpr QCheckedInt operator*(QCheckedInt lhs, QCheckedInt rhs)
friend constexpr QCheckedInt operator*(QCheckedInt lhs, AInt rhs)
friend constexpr bool comparesEqual(QCheckedInt lhs, AInt rhs) noexcept
constexpr QCheckedInt operator-() const
friend constexpr QCheckedInt operator+(QCheckedInt lhs, AInt rhs)
constexpr QCheckedInt & operator+=(AInt other)
constexpr QCheckedInt & operator-=(QCheckedInt other)
friend constexpr QCheckedInt operator*(QCheckedInt lhs, std::integral_constant< Int, RHS > rhs)
friend constexpr QCheckedInt operator*(std::integral_constant< Int, LHS > lhs, QCheckedInt rhs)
friend constexpr QCheckedInt operator/(QCheckedInt lhs, AInt rhs)
constexpr QCheckedInt & operator*=(std::integral_constant< Int, O > other)
#define Q_CHECKEDINT_POLICY_CHECK(cond, what)
static constexpr void check(bool ok, const char *where, const char *description)
static constexpr bool mul(Int a, Int b, Int *result) noexcept
static constexpr bool add(Int a, Int b, Int *result) noexcept
static constexpr bool sub(Int a, Int b, Int *result) noexcept
static constexpr bool div(Int a, Int b, Int *result) noexcept
static constexpr bool mul(Int a, std::integral_constant< Int, B > b, Int *result)