Skip to main content
3 of 7
added 102 characters in body

A way not to forget to check for errors in C

As everybody knows, C allows us to write anything we want. There comes one big problem — we are the only who responsible to handle errors and, therefore, write reliable code.

The goal is to make writing reliable code easier.

I have an idea to wrap standard functions so that we can’t hide errors. If we want to ignore errors, we’ll need to explicitly do that.

This code shows my solution:

void
safe_access
        (bool *ok, const char *path, int amode)
{
        // reset error
        *ok = true;
        errno = 0;

        int ret = access(path, amode);

        // indicate error
        if (ret) {
                *ok = false;
        }
}

This functuon wraps access(3) from unistd.h. It solves the followind three problems:

  1. It resets errno, so we won’t be able to check for old errno:
// this function sets errno
access(...);

// we forget to reset errno and everything seems to be OK
// now, we want to check for errno
// but, unfortunately, we’re prone to check old errno that comes from access()
long a = strtol(...);
if (... && errno) {
}
  1. It disallows us to forget handling errors. Even when we want to ignore errors, we need to explicitly do that:
bool ok;
safe_access(&ok, ...);

// we need something to do with `ok`

Also, we can tune warnings to disallow us to keep variables unused.

  1. Everybody will check errors the same way. This is not regular but sometimes we need to check errors differently than just if-not-zero, if-negative, or if-NULL. For example, we need to use ferror(3) after fread(3). My solution is to write safe_fread() that will do it.

Any critics, advices?