summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndre Hartmann <aha_1980@gmx.de>2018-06-17 20:09:39 +0200
committerAndré Hartmann <aha_1980@gmx.de>2018-07-07 19:58:18 +0000
commitf98ee77cd3e9964b995d063345a895e537bc1157 (patch)
tree25b4f05d5bf44a6a74ca2b7092d50df44c55e1f9 /src
parent85472b6b02b42ea624e1c00a5fd38c0d2889a731 (diff)
QByteArray: toInt() and toDouble() ignore surrounding whitespaces
[ChangeLog][QtCore][QByteArray] QByteArray::toInt(), QByteArray::toDouble() and the other number conversion functions now ignore leading and trailing whitespaces, as their QString counterparts already did. For consistency reasons, the same behavior was added to qEnvironmentVariableIntValue() also. Task-number: QTBUG-66187 Change-Id: I8b5e478ea8577b811d969286ea9e269f539c1ea4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/global/qglobal.cpp22
-rw-r--r--src/corelib/tools/qbytearray.cpp3
-rw-r--r--src/corelib/tools/qlocale.cpp18
-rw-r--r--src/corelib/tools/qlocale_tools.cpp16
-rw-r--r--src/corelib/tools/qlocale_tools_p.h7
5 files changed, 54 insertions, 12 deletions
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp
index 3fe91cae65..b52139d5a6 100644
--- a/src/corelib/global/qglobal.cpp
+++ b/src/corelib/global/qglobal.cpp
@@ -3442,7 +3442,27 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) Q_DECL_NOEXCEPT
bool ok_ = true;
const char *endptr;
const qlonglong value = qstrtoll(buffer, &endptr, 0, &ok_);
- if (int(value) != value || *endptr != '\0') { // this is the check in QByteArray::toInt(), keep it in sync
+
+ // Keep the following checks in sync with QByteArray::toInt()
+ if (!ok_) {
+ if (ok)
+ *ok = false;
+ return 0;
+ }
+
+ if (*endptr != '\0') {
+ while (ascii_isspace(*endptr))
+ ++endptr;
+ }
+
+ if (*endptr != '\0') {
+ // we stopped at a non-digit character after converting some digits
+ if (ok)
+ *ok = false;
+ return 0;
+ }
+
+ if (int(value) != value) {
if (ok)
*ok = false;
return 0;
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
index d8d8be7a26..3a24176dc2 100644
--- a/src/corelib/tools/qbytearray.cpp
+++ b/src/corelib/tools/qbytearray.cpp
@@ -4135,7 +4135,8 @@ double QByteArray::toDouble(bool *ok) const
QByteArray nulled = nulTerminated();
bool nonNullOk = false;
int processed = 0;
- double d = asciiToDouble(nulled.constData(), nulled.length(), nonNullOk, processed);
+ double d = asciiToDouble(nulled.constData(), nulled.length(),
+ nonNullOk, processed, WhitespacesAllowed);
if (ok)
*ok = nonNullOk;
return d;
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp
index e70630eb12..3997652437 100644
--- a/src/corelib/tools/qlocale.cpp
+++ b/src/corelib/tools/qlocale.cpp
@@ -3646,6 +3646,11 @@ qlonglong QLocaleData::bytearrayToLongLong(const char *num, int base, bool *ok)
}
if (*endptr != '\0') {
+ while (ascii_isspace(*endptr))
+ ++endptr;
+ }
+
+ if (*endptr != '\0') {
// we stopped at a non-digit character after converting some digits
if (ok != 0)
*ok = false;
@@ -3663,12 +3668,23 @@ qulonglong QLocaleData::bytearrayToUnsLongLong(const char *num, int base, bool *
const char *endptr;
qulonglong l = qstrtoull(num, &endptr, base, &_ok);
- if (!_ok || *endptr != '\0') {
+ if (!_ok) {
if (ok != 0)
*ok = false;
return 0;
}
+ if (*endptr != '\0') {
+ while (ascii_isspace(*endptr))
+ ++endptr;
+ }
+
+ if (*endptr != '\0') {
+ if (ok != nullptr)
+ *ok = false;
+ return 0;
+ }
+
if (ok != 0)
*ok = true;
return l;
diff --git a/src/corelib/tools/qlocale_tools.cpp b/src/corelib/tools/qlocale_tools.cpp
index 4d969a4723..baa4da37f4 100644
--- a/src/corelib/tools/qlocale_tools.cpp
+++ b/src/corelib/tools/qlocale_tools.cpp
@@ -278,7 +278,7 @@ void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *
}
double asciiToDouble(const char *num, int numLen, bool &ok, int &processed,
- TrailingJunkMode trailingJunkMode)
+ StrayCharacterMode strayCharMode)
{
if (*num == '\0') {
ok = false;
@@ -315,9 +315,13 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed,
double d = 0.0;
#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
- int conv_flags = (trailingJunkMode == TrailingJunkAllowed) ?
- double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK :
- double_conversion::StringToDoubleConverter::NO_FLAGS;
+ int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS;
+ if (strayCharMode == TrailingJunkAllowed) {
+ conv_flags = double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK;
+ } else if (strayCharMode == WhitespacesAllowed) {
+ conv_flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES
+ | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES;
+ }
double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_snan(), 0, 0);
d = conv.StringToDouble(num, numLen, &processed);
@@ -336,7 +340,7 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed,
if (qDoubleSscanf(num, QT_CLOCALE, "%lf%n", &d, &processed) < 1)
processed = 0;
- if ((trailingJunkMode == TrailingJunkProhibited && processed != numLen) || qIsNaN(d)) {
+ if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qIsNaN(d)) {
// Implementation defined nan symbol or garbage found. We don't accept it.
processed = 0;
ok = false;
@@ -361,7 +365,7 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed,
#endif // !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
// Otherwise we would have gotten NaN or sorted it out above.
- Q_ASSERT(trailingJunkMode == TrailingJunkAllowed || processed == numLen);
+ Q_ASSERT(strayCharMode == TrailingJunkAllowed || processed == numLen);
// Check if underflow has occurred.
if (isZero(d)) {
diff --git a/src/corelib/tools/qlocale_tools_p.h b/src/corelib/tools/qlocale_tools_p.h
index 742abb4957..c8ea25b504 100644
--- a/src/corelib/tools/qlocale_tools_p.h
+++ b/src/corelib/tools/qlocale_tools_p.h
@@ -72,13 +72,14 @@
QT_BEGIN_NAMESPACE
-enum TrailingJunkMode {
+enum StrayCharacterMode {
TrailingJunkProhibited,
- TrailingJunkAllowed
+ TrailingJunkAllowed,
+ WhitespacesAllowed
};
double asciiToDouble(const char *num, int numLen, bool &ok, int &processed,
- TrailingJunkMode trailingJunkMode = TrailingJunkProhibited);
+ StrayCharacterMode strayCharMode = TrailingJunkProhibited);
void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *buf, int bufSize,
bool &sign, int &length, int &decpt);