Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When assert() fails, what is the program exit code?

Tags:

c

linux

assert

When an assert() call fails, what is the exit code used, and where is it documented?

like image 333
Matt Joiner Avatar asked May 19 '10 03:05

Matt Joiner


People also ask

What if assert fails in Java?

An assertion allows testing the correctness of any assumptions that have been made in the program. An assertion is achieved using the assert statement in Java. While executing assertion, it is believed to be true. If it fails, JVM throws an error named AssertionError.

Does assert end program?

assert() doesn't simply exit the application but calls abort() . This is usually accompanied with (some kind of) core-dump.

What is exit code 139 in C?

exit(139): It indicates Segmentation Fault which means that the program was trying to access a memory location not allocated to it. This mostly occurs while using pointers or trying to access an out-of-bounds array index.

Which is used to disrupt the execution assert asset system abort () exit?

void abort ( void );


2 Answers

The C99 (unchanged in C11) standard states that assert calls abort and the abort stuff states this about the return code:

An implementation-defined form of the status unsuccessful termination is returned to the host environment by means of the function call raise(SIGABRT).

It's documented in section 7.2.1.1 (assert) and 7.20.4.1 (abort) of the C99 standard here.

Many UNIX systems will return 128 plus the signal number (SIGABRT is signal number 6) so you may get 134. Whatever you get, it should be documented by the C implementation.

For example, see here for gcc. Although it's quite silent on what gets returned to the calling environment. From the specific sections here:

Some choices are made by the library and operating system (or other environment when compiling for a freestanding environment); refer to their documentation for details.

And here:

The behavior of most of these points are dependent on the implementation of the C library, and are not defined by GCC itself.

So is the glibc doco here on program termination (specifically the exit status bit). It mentions conventions but no firm rules.

like image 148
paxdiablo Avatar answered Oct 04 '22 16:10

paxdiablo


It's implementation-specific. You could do this:

int main() {     assert(0); } 

Then run it:

./a.out echo $? 

1 (<- or whatever)

This'll at least tell you what to expect for your setup. I'm getting 134 on a couple of Linux boxes with both GCC and g++.

like image 42
Sniggerfardimungus Avatar answered Oct 04 '22 17:10

Sniggerfardimungus