summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qlocale.cpp
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-08-24 14:56:53 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2021-08-30 17:46:00 +0200
commit6db5fd5918e9c2fb73d61de13356307248c4f2e9 (patch)
tree4bbea647910160e50e10b2ba6e4ae20acbd3c6b8 /src/corelib/text/qlocale.cpp
parent0564e4afba41667e8e3610ee900562befa44731c (diff)
Avoid UB (and the consequent need to suppress an MSVC warning)
Converting a negative signed value to its absolute value in the matching unsigned type can be done by adding one, negating (which we can now do without the UB), casting and then adding one again. This is cleaner than casting the negative value to the unsigned type in order to then "negate" it within that type, about which MSVC grumbles; we can now avoid the need to suppress that grumble. Change-Id: I9148ead23c928aeb2b90884a2f2e292fdf3af5e3 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/text/qlocale.cpp')
-rw-r--r--src/corelib/text/qlocale.cpp13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index 5abc4d3a09..2999793857 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -3642,20 +3642,17 @@ QString QLocaleData::signPrefix(bool negative, unsigned flags) const
return {};
}
-QString QLocaleData::longLongToString(qlonglong l, int precision,
+QString QLocaleData::longLongToString(qlonglong n, int precision,
int base, int width, unsigned flags) const
{
- bool negative = l < 0;
+ bool negative = n < 0;
-QT_WARNING_PUSH
- /* "unary minus operator applied to unsigned type, result still unsigned" */
-QT_WARNING_DISABLE_MSVC(4146)
/*
Negating std::numeric_limits<qlonglong>::min() hits undefined behavior, so
- taking an absolute value has to cast to unsigned to change sign.
+ taking an absolute value has to take a slight detour.
*/
- QString numStr = qulltoa(negative ? -qulonglong(l) : qulonglong(l), base, zeroDigit());
-QT_WARNING_POP
+ QString numStr = qulltoa(negative ? 1u + qulonglong(-(n + 1)) : qulonglong(n),
+ base, zeroDigit());
return applyIntegerFormatting(std::move(numStr), negative, precision, base, width, flags);
}