Skip to content

Commit 0071e1c

Browse files
author
Tarun Prabhu
committed
Use EXIT_SUCCESS and EXIT_FAILURE to make it clear in the code that the return
code is being inverted. Handle the return value from std::system on Windows correctly.
1 parent 47c9e00 commit 0071e1c

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

tools/not.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <sstream>
2020

2121
#ifdef _WIN32
22+
#define WIN32_LEAN_AND_MEAN
23+
#define NOMINMAX
2224
#include <windows.h>
2325
#endif
2426

@@ -54,33 +56,43 @@ int main(int argc, const char **argv) {
5456
std::string cmd = ss.str();
5557

5658
int result = std::system(cmd.c_str());
59+
int retcode = 0;
60+
int signal = 0;
5761

5862
#ifdef _WIN32
5963
// Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
6064
// unreachable, should be recognized as a crash. However, some binaries use
6165
// exit code 3 on non-crash failure paths, so only do this if we expect a
6266
// crash.
63-
if (expectCrash && result == 3)
64-
result = -3;
65-
#endif
66-
67-
// On POSIX systems, result is a composite value of the exit status and,
67+
if (expectCrash && result == 3) {
68+
retcode = 3;
69+
signal = 1;
70+
} else {
71+
// On Windows, result is the exit code, except for the special case above.
72+
retcode = result;
73+
signal = 0;
74+
}
75+
#else
76+
// On POSIX systems, result is a composite value of the exit code and,
6877
// potentially, the signal that caused termination of the command.
69-
int retcode = WEXITSTATUS(result);
70-
int signal = WTERMSIG(result);
78+
retcode = WEXITSTATUS(result);
79+
signal = WTERMSIG(result);
80+
#endif
7181

72-
// If signal is non-zero, then the command caused a crash, usually SIGABRT.
82+
// If signal is non-zero, the command caused a crash, usually SIGABRT.
7383
if (signal) {
7484
if (expectCrash)
75-
return 0;
76-
return 1;
85+
return EXIT_SUCCESS;
86+
return EXIT_FAILURE;
7787
}
7888

79-
// The command exited normally, but if we expected a crash, return an error
80-
// code.
89+
// The command exited normally. If the command was expected to crash, return
90+
// EXIT_FAILURE since EXIT_SUCCESS is returned in the event of an expected
91+
// crash. Otherwise, invert the return code.
8192
if (expectCrash)
82-
return 1;
83-
84-
// Otherwise, invert the success code.
85-
return retcode == EXIT_SUCCESS;
93+
return EXIT_FAILURE;
94+
else if (retcode == EXIT_SUCCESS)
95+
return EXIT_FAILURE;
96+
else
97+
return EXIT_SUCCESS;
8698
}

0 commit comments

Comments
 (0)