QString::vasprintf: fix 't' modifier for integers on 64-bit

‘t’
     Specifies that the argument is a ‘ptrdiff_t’.
     This modifier was introduced in ISO C99.

We use qsizetype, which is the same width as ptrdiff_t, so it makes no
difference in va_arg().

Change-Id: Iea47e0f8fc8b40378df7fffd16255730109413a5
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2020-07-26 08:31:46 -07:00
parent a6b3a1e459
commit fe9e6b2ceb
2 changed files with 40 additions and 27 deletions

View File

@ -6383,8 +6383,10 @@ QString QString::vasprintf(const char *cformat, va_list ap)
case lm_l: i = va_arg(ap, long int); break;
case lm_ll: i = va_arg(ap, qint64); break;
case lm_j: i = va_arg(ap, long int); break;
case lm_z: i = va_arg(ap, size_t); break;
case lm_t: i = va_arg(ap, int); break;
/* ptrdiff_t actually, but it should be the same for us */
case lm_z: i = va_arg(ap, qsizetype); break;
case lm_t: i = va_arg(ap, qsizetype); break;
default: i = 0; break;
}
subst = QLocaleData::c()->longLongToString(i, precision, 10, width, flags);
@ -6402,6 +6404,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
case lm_h: u = va_arg(ap, uint); break;
case lm_l: u = va_arg(ap, ulong); break;
case lm_ll: u = va_arg(ap, quint64); break;
case lm_t: u = va_arg(ap, size_t); break;
case lm_z: u = va_arg(ap, size_t); break;
default: u = 0; break;
}

View File

@ -580,7 +580,6 @@ private slots:
void truncateWithNegative() const;
void QCharRefMutableUnicode() const;
void QCharRefDetaching() const;
void sprintfZU() const;
void repeatedSignature() const;
void repeated() const;
void repeated_data() const;
@ -1257,6 +1256,33 @@ void tst_QString::asprintf()
QCOMPARE(QString::asprintf("%lf", 1.23456789), QLatin1String("1.234568"));
QCOMPARE(QString::asprintf("%p", ptrValue(0xbfffd350)), QLatin1String("0xbfffd350"));
QCOMPARE(QString::asprintf("%p", ptrValue(0)), QLatin1String("0x0"));
QCOMPARE(QString::asprintf("%td", ptrdiff_t(6)), QString::fromLatin1("6"));
QCOMPARE(QString::asprintf("%td", ptrdiff_t(-6)), QString::fromLatin1("-6"));
QCOMPARE(QString::asprintf("%zu", size_t(6)), QString::fromLatin1("6"));
QCOMPARE(QString::asprintf("%zu", size_t(1) << 31), QString::fromLatin1("2147483648"));
// cross z and t
using ssize_t = std::make_signed<size_t>::type; // should be ptrdiff_t
using uptrdiff_t = std::make_unsigned<ptrdiff_t>::type; // should be size_t
QCOMPARE(QString::asprintf("%tu", uptrdiff_t(6)), QString::fromLatin1("6"));
QCOMPARE(QString::asprintf("%tu", uptrdiff_t(1) << 31), QString::fromLatin1("2147483648"));
QCOMPARE(QString::asprintf("%zd", ssize_t(-6)), QString::fromLatin1("-6"));
if (sizeof(qsizetype) > sizeof(int)) {
// 64-bit test
QCOMPARE(QString::asprintf("%zu", SIZE_MAX), QString::fromLatin1("18446744073709551615"));
QCOMPARE(QString::asprintf("%td", PTRDIFF_MAX), QString::fromLatin1("9223372036854775807"));
QCOMPARE(QString::asprintf("%td", PTRDIFF_MIN), QString::fromLatin1("-9223372036854775808"));
// sign extension is easy, make sure we can get something middle-ground
// (24 + 8 = 32; addition used to avoid warning about shifting more
// than size type on 32-bit systems)
size_t ubig = size_t(1) << (24 + sizeof(size_t));
ptrdiff_t sbig = ptrdiff_t(1) << (24 + sizeof(ptrdiff_t));
QCOMPARE(QString::asprintf("%zu", ubig), QString::fromLatin1("4294967296"));
QCOMPARE(QString::asprintf("%td", sbig), QString::fromLatin1("4294967296"));
QCOMPARE(QString::asprintf("%td", -sbig), QString::fromLatin1("-4294967296"));
}
int i = 6;
long l = -2;
@ -1265,6 +1291,14 @@ void tst_QString::asprintf()
double d = -514.25683;
QCOMPARE(QString::asprintf("%f", d), QLatin1String("-514.256830"));
{
/* This code crashed. I don't know how to reduce it further. In other words,
* both %zu and %s needs to be present. */
size_t s = 6;
QCOMPARE(QString::asprintf("%zu%s", s, "foo"), QString::fromLatin1("6foo"));
QCOMPARE(QString::asprintf("%zu %s\n", s, "foo"), QString::fromLatin1("6 foo\n"));
}
}
void tst_QString::asprintfS()
@ -6368,30 +6402,6 @@ void tst_QString::QCharRefDetaching() const
}
}
void tst_QString::sprintfZU() const
{
{
size_t s = 6;
QCOMPARE(QString::asprintf("%zu", s), QString::fromLatin1("6"));
}
{
QCOMPARE(QString::asprintf("%s\n", "foo"), QString::fromLatin1("foo\n"));
}
{
/* This code crashed. I don't know how to reduce it further. In other words,
* both %zu and %s needs to be present. */
size_t s = 6;
QCOMPARE(QString::asprintf("%zu%s", s, "foo"), QString::fromLatin1("6foo"));
}
{
size_t s = 6;
QCOMPARE(QString::asprintf("%zu %s\n", s, "foo"), QString::fromLatin1("6 foo\n"));
}
}
void tst_QString::repeatedSignature() const
{
/* repated() should be a const member. */