Skip to content

Commit 6dde874

Browse files
author
Alex Trotsenko
committed
QProcess: discard unwanted output from the child process
Drop process output to nullDevice(), if an application does not request forwarding, redirecting or reading from the device channel. This prevents from accumulation of unnecessary data which can not be read. Change-Id: Ia311a8c658a46cf580ffa9484c5369f3fc5f98a7 Reviewed-by: Thiago Macieira <[email protected]> Reviewed-by: Oswald Buddenhagen <[email protected]>
1 parent f029468 commit 6dde874

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/corelib/io/qprocess.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,16 @@ void QProcessPrivate::start(QIODevice::OpenMode mode)
22062206
mode &= ~QIODevice::ReadOnly; // not open for reading
22072207
if (mode == 0)
22082208
mode = QIODevice::Unbuffered;
2209+
#ifndef Q_OS_WINCE
2210+
if ((mode & QIODevice::ReadOnly) == 0) {
2211+
if (stdoutChannel.type == QProcessPrivate::Channel::Normal)
2212+
q->setStandardOutputFile(q->nullDevice());
2213+
if (stderrChannel.type == QProcessPrivate::Channel::Normal
2214+
&& processChannelMode != QProcess::MergedChannels)
2215+
q->setStandardErrorFile(q->nullDevice());
2216+
}
2217+
#endif
2218+
22092219
q->QIODevice::open(mode);
22102220

22112221
stdinChannel.closed = false;

tests/auto/corelib/io/qprocess/tst_qprocess.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private slots:
122122
void removeFileWhileProcessIsRunning();
123123
void fileWriterProcess();
124124
void switchReadChannels();
125+
void discardUnwantedOutput();
125126
void setWorkingDirectory();
126127
void setNonExistentWorkingDirectory();
127128
#endif // not Q_OS_WINCE
@@ -2228,6 +2229,25 @@ void tst_QProcess::switchReadChannels()
22282229
}
22292230
#endif
22302231

2232+
#ifndef Q_OS_WINCE
2233+
// Reading and writing to a process is not supported on Qt/CE
2234+
void tst_QProcess::discardUnwantedOutput()
2235+
{
2236+
QProcess process;
2237+
2238+
process.setProgram("testProcessEcho2/testProcessEcho2");
2239+
process.start(QIODevice::WriteOnly);
2240+
process.write("Hello, World");
2241+
process.closeWriteChannel();
2242+
QVERIFY(process.waitForFinished(5000));
2243+
2244+
process.setReadChannel(QProcess::StandardOutput);
2245+
QCOMPARE(process.bytesAvailable(), Q_INT64_C(0));
2246+
process.setReadChannel(QProcess::StandardError);
2247+
QCOMPARE(process.bytesAvailable(), Q_INT64_C(0));
2248+
}
2249+
#endif
2250+
22312251
//-----------------------------------------------------------------------------
22322252
#ifndef Q_OS_WINCE
22332253
// Q_OS_WIN - setWorkingDirectory will chdir before starting the process on unices

0 commit comments

Comments
 (0)