-1

Im currently using QProcess to run Linux commands from a C++ app and I'm having a strange behavior when using QProcess::start function.

This minimal example illustrates that even if the process fails, the process exit status is considered to be successful by QProcess::exitStatus() even if QProcess::exitCode() returns the good exit code.

int main(int argc, char *argv[])
{
    QProcess process;

    process.start("sleep a");
    process.waitForFinished();
    process.close();

    if(QProcess::NormalExit == process.exitStatus())
    {
        qCritical() << "SUCCESS : " + QString::number(process.exitCode());
    }
    else
    {
        qCritical() << "ERROR : " + QString::number(process.exitCode());
    }
}

Output

$ ./test 
"SUCCESS : 1"

I found out that using Process::execute instead of Process::start doesn't change anything about Process::exitStatus, it's even worse since Process::exitCode doesn't returns 1 anymore.

int main(int argc, char *argv[])
{
    QProcess process;

    qCritical() << process.execute("sleep a");

    if(QProcess::NormalExit == process.exitStatus())
    {
        qCritical() << "SUCCESS : " + QString::number(process.exitCode());
    }
    else
    {
        qCritical() << "ERROR : " + QString::number(process.exitCode());
    }
}

Output

$ ./test 
/bin/sleep: invalid time interval ‘a’
Try '/bin/sleep --help' for more information.
1
"SUCCESS : 0"

Both Process::exitStatus() and Process::exitCode() seems to fail to work correctly.

Does anybody knows why those function are not working ? Am I misusing them ?

Arkaik
  • 852
  • 2
  • 19
  • 39

1 Answers1

3

The output is correct. "sleep" never crashed but it validates the input and if it is not adequate then prints an error message, in addition to that, it returns an exit code that indicates whether the program was used correctly or not, that is, it returns a 0 or another number, respectively.

The crash happens when the program is not designed to handle certain tasks, such as accessing unreserved memory.

So if you want to know if sleep ran as expected then you have to evaluate both cases at the same time:

if(process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0)
{
    qCritical() << "SUCCESS";
}
else
{
    qCritical() << "ERROR";
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241