Skip to content

Commit 47c9e00

Browse files
committed
Fix check of the return code from std::system to correctly determine if the
command crashed.
1 parent 1310f58 commit 47c9e00

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

tools/not.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ int main(int argc, const char **argv) {
5252
for (int i = 1; i < argc; ++i)
5353
ss << " " << argv[i];
5454
std::string cmd = ss.str();
55-
int result = system(cmd.c_str());
55+
56+
int result = std::system(cmd.c_str());
5657

5758
#ifdef _WIN32
5859
// Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
@@ -63,14 +64,23 @@ int main(int argc, const char **argv) {
6364
result = -3;
6465
#endif
6566

66-
if (result < 0) {
67+
// On POSIX systems, result is a composite value of the exit status and,
68+
// potentially, the signal that caused termination of the command.
69+
int retcode = WEXITSTATUS(result);
70+
int signal = WTERMSIG(result);
71+
72+
// If signal is non-zero, then the command caused a crash, usually SIGABRT.
73+
if (signal) {
6774
if (expectCrash)
6875
return 0;
6976
return 1;
7077
}
7178

79+
// The command exited normally, but if we expected a crash, return an error
80+
// code.
7281
if (expectCrash)
7382
return 1;
7483

75-
return result == 0;
84+
// Otherwise, invert the success code.
85+
return retcode == EXIT_SUCCESS;
7686
}

0 commit comments

Comments
 (0)