QLocaleData::longLongToString: clean up sign handling

In qlltoa we simply discarded sign, passing absolute value down to
qulltoa; given that it had just one caller and did something only that
caller is apt to want - violating the principle of least surprise for
those who would expect it to handle sign sanely - it is better to just
inline it in the one place it was used.

That one place, QLocaleData::longLongToString(), handles sign
separately and (for reasons I shall argue elsewhere are bogus) ignores
sign for bases other than ten.  When ignoring sign, it passes negative
values directly to qulltoa, which caught the attention of Coverity
(CID 22326).  This deliberately cast (for the same bogus reasons)
negatives to large unsigned.  Inlining base ten's call to qlltoa() as
a call to qulltoa() could now be unified into a simple (and less
obviously perverse) form.  Making the (contentious) cast explicit can
then calm Coverity and we get a simpler implementation all round.

Incorporate also Marc Mutz's fix for unary minus applied to unsigned
and handling of the most negative value, suitably adapted.

Change-Id: Iea02500a5dd7c6d7ac6e73656e1b11f116ae85c6
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2017-09-27 11:14:11 +02:00
parent 196f7af6e2
commit ee84db4c18
3 changed files with 9 additions and 11 deletions

View File

@ -3149,11 +3149,15 @@ QString QLocaleData::longLongToString(const QChar zero, const QChar group,
negative = false; // neither are negative numbers
}
QString num_str;
if (base == 10)
num_str = qlltoa(l, base, zero);
else
num_str = qulltoa(l, base, zero);
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.
*/
QString num_str = qulltoa(negative ? -qulonglong(l) : qulonglong(l), base, zero);
QT_WARNING_POP
uint cnt_thousand_sep = 0;
if (flags & ThousandsGroup && base == 10) {

View File

@ -456,11 +456,6 @@ QString qulltoa(qulonglong l, int base, const QChar _zero)
return QString(reinterpret_cast<QChar *>(p), 65 - (p - buff));
}
QString qlltoa(qlonglong l, int base, const QChar zero)
{
return qulltoa(l < 0 ? -l : l, base, zero);
}
QString &decimalForm(QChar zero, QChar decimal, QChar group,
QString &digits, int decpt, int precision,
PrecisionMode pm,

View File

@ -83,7 +83,6 @@ void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *
bool &sign, int &length, int &decpt);
QString qulltoa(qulonglong l, int base, const QChar _zero);
QString qlltoa(qlonglong l, int base, const QChar zero);
Q_CORE_EXPORT QString qdtoa(qreal d, int *decpt, int *sign);
enum PrecisionMode {