QProcess/Win: Properly report the reason of a fail-to-start

We were losing the GetLastError() by calling CloseHandle(),
QProcess::tr() or just not adding it.

Change-Id: Ic16faa6d68a32d42d620fffd1a8e72be718bf19d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
This commit is contained in:
Thiago Macieira 2025-01-29 18:12:04 -08:00
parent 3d007ff2e9
commit 23e67332a0
2 changed files with 26 additions and 5 deletions

View File

@ -513,6 +513,12 @@ bool QProcessPrivate::callCreateProcess(QProcess::CreateProcessArguments *cpargs
cpargs->inheritHandles, cpargs->flags, cpargs->environment,
cpargs->currentDirectory, cpargs->startupInfo,
cpargs->processInformation);
if (!success) {
// don't CloseHandle here (we'll do that in cleanup()) so GetLastError()
// remains unmodified
return false;
}
if (stdinChannel.pipe[0] != INVALID_Q_PIPE) {
CloseHandle(stdinChannel.pipe[0]);
stdinChannel.pipe[0] = INVALID_Q_PIPE;
@ -576,9 +582,10 @@ void QProcessPrivate::startProcess()
if (!callCreateProcess(&cpargs)) {
// Capture the error string before we do CloseHandle below
QString errorString = QProcess::tr("Process failed to start: %1").arg(qt_error_string());
QString errorString = qt_error_string();
cleanup();
setErrorAndEmit(QProcess::FailedToStart, errorString);
setErrorAndEmit(QProcess::FailedToStart,
QProcess::tr("Process failed to start: %1").arg(errorString));
return;
}
@ -946,7 +953,9 @@ bool QProcessPrivate::startDetached(qint64 *pid)
if (!success) {
if (pid)
*pid = -1;
setErrorAndEmit(QProcess::FailedToStart);
QString errorString = qt_error_string();
setErrorAndEmit(QProcess::FailedToStart,
QProcess::tr("Process failed to start: %1").arg(errorString));
}
closeChannels();

View File

@ -25,6 +25,9 @@
# include <sys/resource.h>
# include <sys/wait.h>
#endif
#ifdef Q_OS_WIN
# include <winerror.h>
#endif
#include <QtTest/private/qemulationdetector_p.h>
@ -424,8 +427,13 @@ void tst_QProcess::simpleStartFail()
#ifdef Q_OS_UNIX
QVERIFY2(process.errorString().contains(": execve: "), process.errorString().toLocal8Bit());
QVERIFY2(process.errorString().contains(qt_error_string(ENOENT)), process.errorString().toLocal8Bit());
int errorcode = ENOENT;
#else
// value happens to match ENOENT, but that's a coincidence
int errorcode = ERROR_FILE_NOT_FOUND;
#endif
QVERIFY2(process.errorString().contains(qt_error_string(errorcode)),
process.errorString().toLocal8Bit());
}
void tst_QProcess::readFromProcess()
@ -2940,8 +2948,12 @@ void tst_QProcess::setNonExistentWorkingDirectory()
#ifdef Q_OS_UNIX
QVERIFY2(process.errorString().contains(": chdir: "), process.errorString().toLocal8Bit());
QVERIFY2(process.errorString().contains(qt_error_string(ENOENT)), process.errorString().toLocal8Bit());
int errorcode = ENOENT;
#else
int errorcode = ERROR_DIRECTORY;
#endif
QVERIFY2(process.errorString().contains(qt_error_string(errorcode)),
process.errorString().toLocal8Bit());
}
void tst_QProcess::startFinishStartFinish()