From c214c000ccebda30ed867934a9d56af29cb34264 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 3 Aug 2016 16:04:22 -0700 Subject: qEnvironmentVariableIntValue: fix the case of a non-numeric value The documentation says that it's equivalent to qgetenv(varName).toInt() But the implementation wasn't. QByteArray::toInt() verifies that the entire string was consumed, so QByteArray("1a").toInt() == 0, but qstrtoll alone doesn't. That is, qstrtoll("1a", ...) == 1. The implementation also detected the base, a behavior I kept. Instead, I updated the documentation. Change-Id: I0031aa609e714ae983c3fffd14676ea6061a9268 Reviewed-by: Marc Mutz --- src/corelib/global/qglobal.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/corelib/global/qglobal.cpp') diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 22fc20d47e..14853b3687 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -3259,20 +3259,26 @@ bool qEnvironmentVariableIsEmpty(const char *varName) Q_DECL_NOEXCEPT Equivalent to \code - qgetenv(varName).toInt() + qgetenv(varName).toInt(ok, 0) \endcode except that it's much faster, and can't throw exceptions. + \note there's a limit on the length of the value, which is sufficient for + all valid values of int, not counting leading zeroes or spaces. Values that + are too long will either be truncated or this function will set \a ok to \c + false. + \sa qgetenv(), qEnvironmentVariableIsSet() */ int qEnvironmentVariableIntValue(const char *varName, bool *ok) Q_DECL_NOEXCEPT { - QMutexLocker locker(&environmentMutex); -#if defined(_MSC_VER) && _MSC_VER >= 1400 - // we provide a buffer that can hold any int value: static const int NumBinaryDigitsPerOctalDigit = 3; static const int MaxDigitsForOctalInt = (std::numeric_limits::digits + NumBinaryDigitsPerOctalDigit - 1) / NumBinaryDigitsPerOctalDigit; + + QMutexLocker locker(&environmentMutex); +#if defined(_MSC_VER) && _MSC_VER >= 1400 + // we provide a buffer that can hold any int value: char buffer[MaxDigitsForOctalInt + 2]; // +1 for NUL +1 for optional '-' size_t dummy; if (getenv_s(&dummy, buffer, sizeof buffer, varName) != 0) { @@ -3282,15 +3288,16 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) Q_DECL_NOEXCEPT } #else const char * const buffer = ::getenv(varName); - if (!buffer || !*buffer) { + if (!buffer || strlen(buffer) > MaxDigitsForOctalInt + 2) { if (ok) *ok = false; return 0; } #endif bool ok_ = true; - const qlonglong value = qstrtoll(buffer, Q_NULLPTR, 0, &ok_); - if (int(value) != value) { // this is the check in QByteArray::toInt(), keep it in sync + 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 if (ok) *ok = false; return 0; -- cgit v1.2.3 From d56c6cf7a4fe2b7e5543d58a786efc768b7370c2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 2 Jun 2017 15:51:27 -0700 Subject: Work around uname(2) on Apple mobile OSes not returning the proper arch Task-number: QTBUG-61205 Change-Id: Ia3e896da908f42939148fffd14c46fc991650f6f Reviewed-by: Jake Petroules --- src/corelib/global/qglobal.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/corelib/global/qglobal.cpp') diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 14853b3687..2176bd8d4a 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -86,6 +86,12 @@ # include #endif +#if defined(Q_OS_DARWIN) +# include +# include +# include +#endif + #ifdef Q_OS_UNIX #include #include @@ -2437,6 +2443,20 @@ QString QSysInfo::currentCpuArchitecture() case PROCESSOR_ARCHITECTURE_IA64: return QStringLiteral("ia64"); } +#elif defined(Q_OS_DARWIN) + cpu_type_t type; + size_t size = sizeof(type); + sysctlbyname("hw.cputype", &type, &size, NULL, 0); + switch (type) { + case CPU_TYPE_X86: + return QStringLiteral("i386"); + case CPU_TYPE_X86_64: + return QStringLiteral("x86_64"); + case CPU_TYPE_ARM: + return QStringLiteral("arm"); + case CPU_TYPE_ARM64: + return QStringLiteral("arm64"); + } #elif defined(Q_OS_UNIX) long ret = -1; struct utsname u; -- cgit v1.2.3