Avoid undefined behavior in qjsonwriter.cpp

See comment in qnumeric_p.h:convertDoubleTo for details.

Change-Id: Ifcd13f7f67995af6a60e50ccabe843a855be04ae
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ulf Hermann 2019-06-19 13:42:01 +02:00
parent b6ded193ee
commit 084e17c4e1
1 changed files with 4 additions and 2 deletions

View File

@ -43,6 +43,7 @@
#include "qjsonwriter_p.h"
#include "qjson_p.h"
#include "private/qutfcodec_p.h"
#include <private/qnumeric_p.h>
QT_BEGIN_NAMESPACE
@ -131,8 +132,9 @@ static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &
case QJsonValue::Double: {
const double d = v.toDouble(b);
if (qIsFinite(d)) { // +2 to format to ensure the expected precision
const double abs = std::abs(d);
json += QByteArray::number(d, abs == static_cast<quint64>(abs) ? 'f' : 'g', QLocale::FloatingPointShortest);
quint64 absInt;
json += QByteArray::number(d, convertDoubleTo(std::abs(d), &absInt) ? 'f' : 'g',
QLocale::FloatingPointShortest);
} else {
json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
}