|
19 | 19 | #include <sstream>
|
20 | 20 |
|
21 | 21 | #ifdef _WIN32
|
| 22 | +#define WIN32_LEAN_AND_MEAN |
| 23 | +#define NOMINMAX |
22 | 24 | #include <windows.h>
|
23 | 25 | #endif
|
24 | 26 |
|
@@ -54,33 +56,43 @@ int main(int argc, const char **argv) {
|
54 | 56 | std::string cmd = ss.str();
|
55 | 57 |
|
56 | 58 | int result = std::system(cmd.c_str());
|
| 59 | + int retcode = 0; |
| 60 | + int signal = 0; |
57 | 61 |
|
58 | 62 | #ifdef _WIN32
|
59 | 63 | // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
|
60 | 64 | // unreachable, should be recognized as a crash. However, some binaries use
|
61 | 65 | // exit code 3 on non-crash failure paths, so only do this if we expect a
|
62 | 66 | // 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, |
68 | 77 | // 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 |
71 | 81 |
|
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. |
73 | 83 | if (signal) {
|
74 | 84 | if (expectCrash)
|
75 |
| - return 0; |
76 |
| - return 1; |
| 85 | + return EXIT_SUCCESS; |
| 86 | + return EXIT_FAILURE; |
77 | 87 | }
|
78 | 88 |
|
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. |
81 | 92 | 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; |
86 | 98 | }
|
0 commit comments