summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKarsten Heimrich <karsten.heimrich@theqtcompany.com>2016-03-30 12:53:31 +0200
committerFrerich Raabe <raabe@froglogic.com>2016-03-30 21:13:03 +0000
commitba145f6090f481a41e7a476355c83039ed8ff521 (patch)
tree95de65d51e258c65d2ab5adf9d275cb5717bb229 /src
parent6e904b95bd15b542fb7dc846c80ec705c9155d51 (diff)
Make communication via installer.execute() Unicode safe
It was impossible to pass Unicode data safely to a process started via installer.execute(), or to read Unicode data printed by that process safely back in. The reason for this is that the code hardcoded the latin1 codec for converting between strings used in the script interpreter and bytes used by the QProcessWrapper API. Fix this by adding two new optional arguments to installer.execute() which can be used to define the codec to be used for writing to stdin resp. reading from stdout. This defaults to latin1 for backwards compatibility. Change-Id: I290d8d9617b286ef90b2f0a05c6e7a47f6df317f Reviewed-by: Karsten Heimrich <karsten.heimrich@theqtcompany.com> Reviewed-by: Frerich Raabe <raabe@froglogic.com>
Diffstat (limited to 'src')
-rw-r--r--src/libs/installer/packagemanagercore.cpp24
-rw-r--r--src/libs/installer/packagemanagercore.h4
2 files changed, 24 insertions, 4 deletions
diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp
index b0a773671..af517da5b 100644
--- a/src/libs/installer/packagemanagercore.cpp
+++ b/src/libs/installer/packagemanagercore.cpp
@@ -62,6 +62,8 @@
#include <QtCore/QSettings>
#include <QtCore/QTemporaryFile>
#include <QtCore/QTextCodec>
+#include <QtCore/QTextDecoder>
+#include <QtCore/QTextEncoder>
#include <QtCore/QTextStream>
#include <QDesktopServices>
@@ -1842,6 +1844,12 @@ bool PackageManagerCore::localInstallerBinaryUsed()
\a stdIn is sent as standard input to the application.
+ \a stdInCodec is the name of the codec to use for converting the input string
+ into bytes to write to the standard input of the application.
+
+ \a stdOutCodec is the name of the codec to use for converting data written by the
+ application to standard output into a string.
+
Returns an empty array if the program could not be executed, otherwise
the output of command as the first item, and the return code as the second.
@@ -1851,7 +1859,7 @@ bool PackageManagerCore::localInstallerBinaryUsed()
\sa executeDetached()
*/
QList<QVariant> PackageManagerCore::execute(const QString &program, const QStringList &arguments,
- const QString &stdIn) const
+ const QString &stdIn, const QString &stdInCodec, const QString &stdOutCodec) const
{
QProcessWrapper process;
@@ -1868,13 +1876,23 @@ QList<QVariant> PackageManagerCore::execute(const QString &program, const QStrin
return QList< QVariant >();
if (!adjustedStdIn.isNull()) {
- process.write(adjustedStdIn.toLatin1());
+ QTextCodec *codec = QTextCodec::codecForName(qPrintable(stdInCodec));
+ if (!codec)
+ return QList<QVariant>();
+
+ QTextEncoder encoder(codec);
+ process.write(encoder.fromUnicode(adjustedStdIn));
process.closeWriteChannel();
}
process.waitForFinished(-1);
- return QList<QVariant>() << QString::fromLatin1(process.readAllStandardOutput()) << process.exitCode();
+ QTextCodec *codec = QTextCodec::codecForName(qPrintable(stdOutCodec));
+ if (!codec)
+ return QList<QVariant>();
+ return QList<QVariant>()
+ << QTextDecoder(codec).toUnicode(process.readAllStandardOutput())
+ << process.exitCode();
}
/*!
diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h
index f00f33e72..eebc33c4b 100644
--- a/src/libs/installer/packagemanagercore.h
+++ b/src/libs/installer/packagemanagercore.h
@@ -138,7 +138,9 @@ public:
Q_INVOKABLE bool localInstallerBinaryUsed();
Q_INVOKABLE QList<QVariant> execute(const QString &program,
- const QStringList &arguments = QStringList(), const QString &stdIn = QString()) const;
+ const QStringList &arguments = QStringList(), const QString &stdIn = QString(),
+ const QString &stdInCodec = QLatin1String("latin1"),
+ const QString &stdOutCodec = QLatin1String("latin1")) const;
Q_INVOKABLE bool executeDetached(const QString &program,
const QStringList &arguments = QStringList(),
const QString &workingDirectory = QString()) const;