QString::asprintf: fix handling of a present-but-empty precision

%.f should be handled like %.0f. You probably don't want it for strings,
though.

Fixes: QTBUG-107991
Pick-to: 6.2 6.4
Change-Id: I07ec23f3cb174fb197c3fffd1721a941fbcf15e1
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Thiago Macieira 2022-10-26 08:50:37 -07:00
parent df1935df0e
commit bdee2c9760
2 changed files with 38 additions and 0 deletions

View File

@ -6933,6 +6933,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
int precision = -1; // -1 means unspecified
if (*c == '.') {
++c;
precision = 0;
if (qIsDigit(*c)) {
precision = parse_field_width(c, formatEnd - c);
} else if (*c == '*') { // can't parse this in another function, not portably, at least

View File

@ -1395,6 +1395,43 @@ void tst_QString::asprintf()
double d = -514.25683;
QCOMPARE(QString::asprintf("%f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%.f", d), QLatin1String("-514"));
QCOMPARE(QString::asprintf("%.0f", d), QLatin1String("-514"));
QCOMPARE(QString::asprintf("%1f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%1.f", d), QLatin1String("-514"));
QCOMPARE(QString::asprintf("%1.0f", d), QLatin1String("-514"));
QCOMPARE(QString::asprintf("%1.6f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%1.10f", d), QLatin1String("-514.2568300000"));
QCOMPARE(QString::asprintf("%-1f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%-1.f", d), QLatin1String("-514"));
QCOMPARE(QString::asprintf("%-1.0f", d), QLatin1String("-514"));
QCOMPARE(QString::asprintf("%-1.6f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%-1.10f", d), QLatin1String("-514.2568300000"));
QCOMPARE(QString::asprintf("%10f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%10.f", d), QLatin1String(" -514"));
QCOMPARE(QString::asprintf("%10.0f", d), QLatin1String(" -514"));
QCOMPARE(QString::asprintf("%-10f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%-10.f", d), QLatin1String("-514 "));
QCOMPARE(QString::asprintf("%-10.0f", d), QLatin1String("-514 "));
QCOMPARE(QString::asprintf("%010f", d), QLatin1String("-514.256830"));
QCOMPARE(QString::asprintf("%010.f", d), QLatin1String("-000000514"));
QCOMPARE(QString::asprintf("%010.0f", d), QLatin1String("-000000514"));
QCOMPARE(QString::asprintf("%15f", d), QLatin1String(" -514.256830"));
QCOMPARE(QString::asprintf("%15.6f", d), QLatin1String(" -514.256830"));
QCOMPARE(QString::asprintf("%15.10f", d), QLatin1String("-514.2568300000"));
QCOMPARE(QString::asprintf("%-15f", d), QLatin1String("-514.256830 "));
QCOMPARE(QString::asprintf("%-15.6f", d), QLatin1String("-514.256830 "));
QCOMPARE(QString::asprintf("%-15.10f", d), QLatin1String("-514.2568300000"));
QCOMPARE(QString::asprintf("%015f", d), QLatin1String("-0000514.256830"));
QCOMPARE(QString::asprintf("%015.6f", d), QLatin1String("-0000514.256830"));
QCOMPARE(QString::asprintf("%015.10f", d), QLatin1String("-514.2568300000"));
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wformat")
QT_WARNING_DISABLE_CLANG("-Wformat") // Flag '0' ignored when flag '-' is present
QCOMPARE(QString::asprintf("%-015f", d), QLatin1String("-514.256830 "));
QCOMPARE(QString::asprintf("%-015.6f", d), QLatin1String("-514.256830 "));
QCOMPARE(QString::asprintf("%-015.10f", d), QLatin1String("-514.2568300000"));
QT_WARNING_POP
{
/* This code crashed. I don't know how to reduce it further. In other words,