The Hackerlab at `regexps.com`

## Handling Bugs

up: libhackerlab next: Machine-Specific Definitions prev: A Bigger Picture

         Captain Mission did not fear Panic, the sudden
         intolerable knowing that everything is alive.
         He was himself an emissary of Panic, of the
         knowledge that man fears above all else: the
         truth of his origin.  It's so close.  Just wipe
         away the words and look.

                                 William S. Burroughs
                                 Ghost of a Chance

The Hackerlab C library defines a bug as a condition within a running program which is not expected to arise, and which can not be recovered from if it does arise, but which can not be ruled out with certainty.

When a bug occurs, the only sane alternative is for the process to exit with a non-0 status. By convention, a process which exits due to a bug should print an error message on its standard output stream giving some indication of what bug occurred.

When a fatal condition arises, call `panic` . In tricky code, play it safe and test for conditions which should always be true by using the macro `invariant` . See Panic.

If your program needs to quit in some way besides `_exit(1)` , you might want to replace the function `panic_exit` . See Exiting Due to Panic.

- Panic
- Exiting Due to Panic

### Panic

up: Handling Bugs next: Exiting Due to Panic

    #include <hackerlab/bugs/panic.h>

Function `panic`

    int panic (char * str);

Print an error message containing `str` on descriptor `2` and exit the process by calling `panic_exit` .

This function uses `write` to print `str` .

This function does not return.

Function `panic_msg`

    int panic_msg (char * str);

Print an error message containing `str` on descriptor `2` .

This function uses `write` to print `str` .

This function does return.

Macro `invariant`

    void invariant(CONDITION);

Defined as:

      #define invariant(X) invariant_test(X, #X, __FILE__, __LINE__)

If `CONDITION` evaluates to `0` , write a message to the standard error output (descriptor `2` ) and exit by calling `panic_exit` . See panic.

Function `invariant_test`

    void invariant_test (int condition, char * str, char * file, int line);

If `condition` is `0` , write a message to stderr (fd `2` ) and exit. See invariant.

### Exiting Due to Panic

up: Handling Bugs prev: Panic

    #include <hackerlab/bugs/panic-exit.h>

Function `panic_exit`

    void panic_exit (void);

This function is called by `panic` to terminate the process with status `1` . (It does not return. It calls `_exit` .)

For your convenience, this function is defined in its own object file. You can define your own version and still link against this library. If you do define your own version, it must not return.

libhackerlab: The Hackerlab C Library
The Hackerlab at `regexps.com`