Error Handling#

RCSW functions report failure through their return value and, where it helps, errno. There are no exceptions, callbacks or global error state beyond errno.

Return values#

  • Functions that can fail and return nothing else return status_t: OK (0) or ERROR (-1).

  • Functions that return a pointer return NULL on failure.

  • Functions that return an index return -1 on failure.

  • Query functions (xx_size(), xx_isempty(), …) return 0, false or NULL when given an invalid handle. These values can’t be told apart from a valid answer, so validate handles before relying on them.

errno is set on many failure paths, but not all of them; treat it as a hint about why something failed, and the return value as the fact that it did. Error Codes lists the values the data structures use.

Function preconditions#

Public functions check their arguments with the FPC (function precondition) macros in rcsw/core/fpc.h. What a failed check does is chosen at build time with LIBRA_FPC:

Mode

On a failed precondition

RETURN (default)

The function sets errno = EINVAL and returns its error value (ERROR, NULL, -1, 0 or false, per the rules above).

ABORT

The check is an assert(). In a build with NDEBUG defined, nothing is checked.

NONE

Nothing is checked. Invalid arguments are undefined behaviour.

The checks are short-circuited: the first failed condition decides the result.

Cleanup on failure#

Functions that acquire resources step by step use a single error: label for cleanup. The checking macros jump to it:

Macro

Jumps to error when

RCSW_CHECK (cond)

cond is false.

RCSW_CHECK_PTR (ptr)

ptr is NULL.

RCSW_CHECK_FD (fd)

fd is negative.

ER_CHECK (cond, msg, ...)

cond is false, after reporting msg at ERROR level.

ER_SENTINEL (msg, ...)

Always, after reporting msg at ERROR level. Marks code that should be unreachable.

status_t widget_init(struct widget* w) {
  w->buf = malloc(64);
  RCSW_CHECK_PTR(w->buf);
  ER_CHECK(OK == bus_attach(w), "Failed to attach widget");
  return OK;

error:
  free(w->buf);
  return ERROR;
}

For conditions that must never fail, ER_ASSERT reports a FATAL event and then calls assert(), and ER_FATAL_SENTINEL reports a FATAL event and calls abort(). With NDEBUG defined, ER_ASSERT still evaluates the condition and reports the event, but the assert() doesn’t stop the program.