diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 03e3f0611a..32e10b9512 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) diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index e9d0cf8236..0db139b518 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -425,18 +425,17 @@ void tst_QByteArray::setNum() QCOMPARE(a.setNum(37, 2), QByteArray("100101")); QCOMPARE(a.setNum(37, 36), QByteArray("11")); - // Negative numbers are only properly supported for base 10. - QCOMPARE(a.setNum(short(-1), 16), QByteArray("ffff")); - QCOMPARE(a.setNum(int(-1), 16), QByteArray("ffffffff")); - QCOMPARE(a.setNum(qlonglong(-1), 16), QByteArray("ffffffffffffffff")); + QCOMPARE(a.setNum(short(-1), 16), QByteArray("-1")); + QCOMPARE(a.setNum(int(-1), 16), QByteArray("-1")); + QCOMPARE(a.setNum(qlonglong(-1), 16), QByteArray("-1")); QCOMPARE(a.setNum(short(-1), 10), QByteArray("-1")); QCOMPARE(a.setNum(int(-1), 10), QByteArray("-1")); QCOMPARE(a.setNum(qlonglong(-1), 10), QByteArray("-1")); QCOMPARE(a.setNum(-123), QByteArray("-123")); - QCOMPARE(a.setNum(0x123,16), QByteArray("123")); - QCOMPARE(a.setNum((short)123), QByteArray("123")); + QCOMPARE(a.setNum(0x123, 16), QByteArray("123")); + QCOMPARE(a.setNum(short(123)), QByteArray("123")); QCOMPARE(a.setNum(1.23), QByteArray("1.23")); QCOMPARE(a.setNum(1.234567), QByteArray("1.23457"));