diff options
| author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2025-01-28 02:33:43 +0100 |
|---|---|---|
| committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2025-01-29 06:36:48 +0100 |
| commit | 83c812e1322a2b004bc604be3e6f61fb83246eb0 (patch) | |
| tree | 99d18b1b753d0c35dd182a45bbf7dd7c57c07e2e /src | |
| parent | 57aabff91eed41fb7ba0cf67df4fe7767c6e0e43 (diff) | |
QNumeric: add a private qUnsignedAbs
Checking qAbs preconditions reveals that several places into Qt are
calling it with minimal values, causing UB. Those places do not actually
need that the absolute value is of the same type as the parameter.
Therefore, introduce qUnsignedAbs, which is like qAbs() but whose return
type is always unsigned, and therefore can accept everything.
This function is private; I don't want to encourage users to rely on our
extension.
Aggressively cherry picking because this will be needed in subsequent
bugfixes.
Change-Id: I2047768a5fd35f12bf898ca8c2008813434edd8d
Pick-to: 6.9 6.8 6.5 6.2
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/global/qnumeric.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/corelib/global/qnumeric.h b/src/corelib/global/qnumeric.h index 5a25b1c2392..2e13da4b850 100644 --- a/src/corelib/global/qnumeric.h +++ b/src/corelib/global/qnumeric.h @@ -336,6 +336,16 @@ template <auto V2, typename T> bool qMulOverflow(T v1, T *r) template <typename T> constexpr inline T qAbs(const T &t) { return t >= 0 ? t : -t; } +namespace QtPrivate { +template <typename T, + typename std::enable_if_t<std::is_integral_v<T>, bool> = true> +constexpr inline auto qUnsignedAbs(T t) +{ + using U = std::make_unsigned_t<T>; + return (t >= 0) ? U(t) : U(~U(t) + U(1)); +} +} // namespace QtPrivate + // gcc < 10 doesn't have __has_builtin #if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG) // ARM64 has a single instruction that can do C++ rounding with conversion to integer. |
