summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-06-29 21:18:04 -0700
committerThiago Macieira <thiago.macieira@intel.com>2017-07-18 15:51:22 +0000
commitf3205a49498d3084f46bc2c2abcd085c8436553b (patch)
tree74556aab75224d973fc8a1e2e77135fb0793229f /src/corelib/kernel
parentc0bd7ade1aa49ed6877e5f13a74bcb72ff662062 (diff)
Merge qt_error_string and QSystemError
This removes a lot of duplicated code that existed in both qglobal.cpp and qsystemerror.cpp, including the hack to get the correct strerror_r signature. This removes the incorrect use of EACCES, EMFILE, ENOENT, and ENOSPC from qt_error_string on Windows. qt_error_string is supposed to be used only with Win32 error codes from GetLastError(), despite there being a lot of uses in cross-platform and even Windows-specific code that pass errno constants. It may or may not work: that depends on whether the constants happen to match. ENOENT matches ERROR_FILE_NOT_FOUND and one could argue that ENOSPC matching ERROR_OUT_OF_PAPER is acceptable, but EMFILE isn't the same as ERROR_BAD_LENGTH nor is EACCES, ERROR_INVALID_DATA. Change-Id: I1eba2b016de74620bfc8fffd14cccb7f77f4b510 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> Reviewed-by: Kai Koehne <kai.koehne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r--src/corelib/kernel/qsystemerror.cpp29
-rw-r--r--src/corelib/kernel/qsystemerror_p.h14
2 files changed, 38 insertions, 5 deletions
diff --git a/src/corelib/kernel/qsystemerror.cpp b/src/corelib/kernel/qsystemerror.cpp
index 3899f24d3b..6583838a9f 100644
--- a/src/corelib/kernel/qsystemerror.cpp
+++ b/src/corelib/kernel/qsystemerror.cpp
@@ -101,6 +101,8 @@ static QString windowsErrorString(int errorCode)
if (ret.isEmpty() && errorCode == ERROR_MOD_NOT_FOUND)
ret = QString::fromLatin1("The specified module could not be found.");
+ if (ret.endsWith(QLatin1String("\r\n")))
+ ret.chop(2);
return ret;
}
#endif
@@ -126,7 +128,7 @@ static QString standardLibraryErrorString(int errorCode)
break;
default: {
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX)
- QByteArray buf(1024, '\0');
+ QByteArray buf(1024, Qt::Uninitialized);
ret = fromstrerror_helper(strerror_r(errorCode, buf.data(), buf.size()), buf);
#else
ret = QString::fromLocal8Bit(strerror(errorCode));
@@ -141,7 +143,7 @@ static QString standardLibraryErrorString(int errorCode)
return ret.trimmed();
}
-QString QSystemError::toString() const
+QString QSystemError::string(ErrorScope errorScope, int errorCode)
{
switch(errorScope) {
case NativeError:
@@ -161,5 +163,26 @@ QString QSystemError::toString() const
}
}
-QT_END_NAMESPACE
+QString QSystemError::stdString(int errorCode)
+{
+ return standardLibraryErrorString(errorCode == -1 ? errno : errorCode);
+}
+
+#ifdef Q_OS_WIN
+QString QSystemError::windowsString(int errorCode)
+{
+ return windowsErrorString(errorCode == -1 ? GetLastError() : errorCode);
+}
+QString qt_error_string(int code)
+{
+ return windowsErrorString(code == -1 ? GetLastError() : code);
+}
+#else
+QString qt_error_string(int code)
+{
+ return standardLibraryErrorString(code == -1 ? errno : code);
+}
+#endif
+
+QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qsystemerror_p.h b/src/corelib/kernel/qsystemerror_p.h
index 440b763149..1d8c253f53 100644
--- a/src/corelib/kernel/qsystemerror_p.h
+++ b/src/corelib/kernel/qsystemerror_p.h
@@ -69,10 +69,16 @@ public:
inline QSystemError(int error, ErrorScope scope);
inline QSystemError();
- QString toString() const;
+ inline QString toString() const;
inline ErrorScope scope() const;
inline int error() const;
+ static QString string(ErrorScope errorScope, int errorCode);
+ static QString stdString(int errorCode = -1);
+#ifdef Q_OS_WIN
+ static QString windowsString(int errorCode = -1);
+#endif
+
//data members
int errorCode;
ErrorScope errorScope;
@@ -90,6 +96,11 @@ QSystemError::QSystemError()
}
+QString QSystemError::toString() const
+{
+ return string(errorScope, errorCode);
+}
+
QSystemError::ErrorScope QSystemError::scope() const
{
return errorScope;
@@ -100,7 +111,6 @@ int QSystemError::error() const
return errorCode;
}
-
QT_END_NAMESPACE
#endif // QSYSTEMERROR_P_H