Can a process exit code overflow for small values?

I am writing a testing framework, and in main () I return the number of failed tests. For instance:

int main() { int failedTests = runTests(); return failedTests; } 

So can this overflow "int"? Can I get %ERROR_LEVEL% as == 0 when I really returned something other than 0? Does it depend on the host operating system? What are the usual maximum values? Can I be sure that an integer <32768 (2 ^ 16 is short) will always match?

EDIT:

I had problems with python sys.exit , which always uses the range 0-127 (almost), so now I'm careful.

+6
source share
2 answers

It depends on the operating system. On Linux, it is limited to 8 bits, so it can be 0 (for success) or any positive integer no more than 255. See _ exit (2) and wait (2) .

Only two values ​​are standardized (by name): EXIT_SUCCESS (usually 0) and EXIT_FAILURE (often like 1). Note that the standard defines the name (and probably also the behavior of exit(0); ...), but not their meaning.

You can write the number (of unsuccessful tests) to stdout or to some file specified through some program argument.

FreeBSD defined in its sysexits (3) a set of names and exit codes, but they are not standardized, for example. in POSIX. See the POSIX documentation on exit .

+4
source

Yes, it can overflow like any other int .

The standard describes int with a width of at least 16 bits. Returning anything that does not match int is undefined behavior.

What the environment in which a C ++ program works, does with value, is an implementation defined and not described by the standard.

Three values ​​are mentioned by the C ++ standard: EXIT_SUCCESS , EXIT_FAILURE and 0 . Both 0 and EXIT_SUCCESS indicate successful execution of the program, while the other indicates failure. EXIT_SUCCES not required to be zero.

Citation of the C ++ 11 standard:

If the status is zero or EXIT_SUCCESS, a status completion form is returned. If the status is EXIT_- FAILURE, the form for executing a status failure is returned. Otherwise, the return status is determined by the implementation .225

Supported regular values ​​vary from implementation to implementation, Windows uses full 32-bit integers as return values, and full 32 bits can be obtained using various means. POSIX only defines how the low-order 8 bits can be obtained, and thus, shells often truncate return values.

+3
source

All Articles