Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using errno for application / library error reporting

Tags:

c

errno

I'm writing a C library for a software project. I need to do some error reporting, but I'm a little bit too lazy to implement my own complex set of error-codes, variables and functions. Is it acceptable to use the errno facility provided by the libc for custom error reporting? All my errors fit into the categories given by the E... macros.

For instance, let's say my code includes a function that reads a SHA256 hash in hexdecimal notation and converts it into some sort of internal format. I want to use errnoto report errors:

#include <errno.h>

int hash_fromstr(hash_t *out, const char *in) {
  /* ... */

  if (strlen(in) != 65) {
    errno = EINVAL;
    return -1;
  }

  /* ... */
}

Of course this example is ridiculously simplified, in reality much more errors may happen in other functions.

like image 620
fuz Avatar asked May 14 '12 20:05

fuz


People also ask

How does errno work in C?

Global Variable errno: When a function is called in C, a variable named as errno is automatically assigned a code (value) which can be used to identify the type of error that has been encountered. Its a global variable indicating the error occurred during any function call and defined in the header file errno. h.

How do I check my errno value?

Viewing and Printing the Errno Value Your program can use the strerror() and perror() functions to print the value of errno. The strerror() function returns a pointer to an error message string that is associated with errno. The perror() function prints a message to stderr.

Do I need to set errno to 0?

To detect an error, an application must set errno to 0 before calling the function and check whether it is nonzero after the call. Affected functions include strcoll() , strxfrm() , strerror() , wcscoll() , wcsxfrm() , and fwide() . The C Standard allows these functions to set errno to a nonzero value on success.

Is errno set on success?

errno is set on an error in a system-level call. Because errno holds the value for the last call that set it, this value may be changed by succeeding calls. Run-time library calls that set errno on an error do not clear errno on success.


Video Answer


2 Answers

You can modify the value of errno as you please, just be sure that your library code checks that errno isn't set before doing so to ensure your library code can still properly detect internal standard failures that cause errno to be set. You might also check out "should I set errno" for more information.

like image 56
Chimera Avatar answered Oct 19 '22 18:10

Chimera


Yes, you can modify it and it has thread scope, which is very desirable in that kind of error handling.

Using errno error family (E...) and maybe extending it can be a very powerful yet simple error handling pattern. It may be considered bad tasted approach by others, but IMHO it produces cleaner code and a standardized pattern for error handling.

like image 1
Felipe Lavratti Avatar answered Oct 19 '22 18:10

Felipe Lavratti