Fix a possible crash in process launcher

It may apparently happen that when calling QProcess::start()
we may receive a synchronous signal QProcess::errorOccurred()
from the process we are trying to start. In this case
the handler of the error signal might have removed the
process from m_processes hash, which invalidated the
"Process *& process" reference inside
LauncherSocketHandler::handleStartPacket(). So, using
process reference after calling start() may be dangerous.

Refactor ProcessStartHandler so that it stores the pointer
to the process it handles. The pointer to the handler
should still be valid after calling start(), since
the process itself is being deleted with a delay.

Make ProcessStartHandler a member of ProcessHelper. In this
way it's being reused in QProcessImpl and ProcessLauncher.

Fixes: QTCREATORBUG-26726
Change-Id: I8e3f39953035d76c83bbbb13bd78e3042ba2a14e
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2021-12-16 10:43:58 +01:00
parent 8a14a14aae
commit f25300cadf
4 changed files with 32 additions and 32 deletions

View File

@@ -44,44 +44,40 @@ QIODevice::OpenMode ProcessStartHandler::openMode() const
return QIODevice::ReadWrite; // initial write and then reading (close the write channel)
}
void ProcessStartHandler::handleProcessStart(QProcess *process)
void ProcessStartHandler::handleProcessStart()
{
if (m_processMode == ProcessMode::Writer)
return;
if (m_writeData.isEmpty())
process->closeWriteChannel();
m_process->closeWriteChannel();
}
void ProcessStartHandler::handleProcessStarted(QProcess *process)
void ProcessStartHandler::handleProcessStarted()
{
if (!m_writeData.isEmpty()) {
process->write(m_writeData);
m_process->write(m_writeData);
m_writeData = {};
if (m_processMode == ProcessMode::Reader)
process->closeWriteChannel();
m_process->closeWriteChannel();
}
}
void ProcessStartHandler::setBelowNormalPriority(QProcess *process)
void ProcessStartHandler::setBelowNormalPriority()
{
#ifdef Q_OS_WIN
process->setCreateProcessArgumentsModifier(
m_process->setCreateProcessArgumentsModifier(
[](QProcess::CreateProcessArguments *args) {
args->flags |= BELOW_NORMAL_PRIORITY_CLASS;
});
#else
Q_UNUSED(process)
#endif // Q_OS_WIN
}
void ProcessStartHandler::setNativeArguments(QProcess *process, const QString &arguments)
void ProcessStartHandler::setNativeArguments(const QString &arguments)
{
#ifdef Q_OS_WIN
if (!arguments.isEmpty())
process->setNativeArguments(arguments);
m_process->setNativeArguments(arguments);
#else
Q_UNUSED(process)
Q_UNUSED(arguments)
#endif // Q_OS_WIN
}