summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-07-30 15:55:08 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-08-02 21:41:05 +0000
commit4b3ed8aa32c8f0cae5d6c177bf83f52bb64ab5be (patch)
tree91a05521dc582015f0e59c6b052f356d7cc328f4 /src/corelib/text
parentc6ad7292929c4bfc13c18e5571f01140d3ef746b (diff)
QByteArray: don't coerce negative to unsigned for any base
This follows up on commit 98666c8afc80cccb80ca4426b97ec52916c6e610, which did the same for QString. If someone wants to get formatting suitable to an unsigned value, they can cast the value to that unsigned type and the correct overload shall pick it up. [ChangeLog][Important Behavior Changes] QByteArray's formatting of negative whole numbers to bases other than ten now, like QString's (since Qt 6.0), formats the absolute value and prepends a minus sign. Task-number: QTBUG-53706 Change-Id: I91fee23d25ac0d5d5bcfcbeccbac1386627c004a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 260168d9d7547a2e7586bff6cb42e11d54c9d06e) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qbytearray.cpp5
-rw-r--r--src/corelib/text/qbytearray.h6
2 files changed, 6 insertions, 5 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index d8deb5e8cc..a51e094766 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -3866,7 +3866,7 @@ QByteArray QByteArray::toBase64(Base64Options options) const
Sets the byte array to the printed value of \a n in base \a base (ten by
default) and returns a reference to the byte array. Bases 2 through 36 are
supported, using letters for digits beyond 9; A is ten, B is eleven and so
- on. For bases other than ten, n is treated as an unsigned integer.
+ on.
Example:
\snippet code/src_corelib_text_qbytearray.cpp 40
@@ -3942,7 +3942,8 @@ QByteArray &QByteArray::setNum(qlonglong n, int base)
char buff[buffsize];
char *p;
- if (n < 0 && base == 10) {
+ if (n < 0) {
+ // Take care to avoid overflow on negating min value:
p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
*--p = '-';
} else {
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 91f50d76c3..2d614f1fe0 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -598,15 +598,15 @@ inline const QByteArray operator+(char a1, const QByteArray &a2)
#endif // QT_USE_QSTRINGBUILDER
inline QByteArray &QByteArray::setNum(short n, int base)
-{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); }
+{ return setNum(qlonglong(n), base); }
inline QByteArray &QByteArray::setNum(ushort n, int base)
{ return setNum(qulonglong(n), base); }
inline QByteArray &QByteArray::setNum(int n, int base)
-{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); }
+{ return setNum(qlonglong(n), base); }
inline QByteArray &QByteArray::setNum(uint n, int base)
{ return setNum(qulonglong(n), base); }
inline QByteArray &QByteArray::setNum(long n, int base)
-{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ulong(n)), base); }
+{ return setNum(qlonglong(n), base); }
inline QByteArray &QByteArray::setNum(ulong n, int base)
{ return setNum(qulonglong(n), base); }
inline QByteArray &QByteArray::setNum(float n, char f, int prec)