14

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.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • As long as your library can emit the standard error codes without breaking the proper functioning of `errno` in other parts of a users application that links against your library I don't see a problem with it. Perhaps you might use the `errno` variable but not alter it and return a copy of it? – Chimera May 14 '12 at 21:00
  • @Jim I want to use `errno` for errors that occur inside the library itself - not necessarily in the function of the libc called by the library. – fuz May 14 '12 at 21:07
  • 1
    So are you asking if it's ok to set the value of `errno` to report errors to the caller of your library? If so you might want to check out http://stackoverflow.com/questions/9856822/should-i-set-errno – Chimera May 14 '12 at 21:13
  • @Jim Yeah. That's exactly what I'm trying to do. – fuz May 14 '12 at 21:15

2 Answers2

8

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.

Community
  • 1
  • 1
Chimera
  • 5,884
  • 7
  • 49
  • 81
1

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.

Felipe Lavratti
  • 2,887
  • 16
  • 34
  • How can you expand `errno`? As far as I know there is no portable way to find out which `errno` values are unallocated and even if you did, you get a conflict with other libraries that try to do the same. – fuz Oct 28 '14 at 19:06
  • @FUZxxl It is true that is not possible to know which values are unallocated, but the int type range is big enough to accommodate safe margin. About the conflicts, I see no conflicts happening, considering that the errno value has only the scope of a single function call, so, depending the library, the error shall be processed against the library error list only, avoiding conflicts. Do you agree? – Felipe Lavratti Oct 29 '14 at 22:43
  • @fani I disagree, because using `errno` for custom things breaks things like `strerror`—the custom `errno` value remains after a failed library call and potentially messes up outside processing. – fuz Oct 29 '14 at 23:12