lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sat, Nov 16, 2013 at 07:32:43AM +0000, Sir Pogsalot wrote:
> errno.  It's at least great that both Windows and Linux have strerror() --
> though it's still weird as hell that the function isn't thread-safe.  I
> don't see a point to strerror_r() existing, would be nice if POSIX made
> more changes..

strerror is defined to return a useful error message even if the errno value
is unknown.

  "Typically, the values for errnum come from errno, but strerror shall map
  any value of type int to a message."

  -- C99 (n1256)

To do this it needs to write into a static buffer. Nothing prevents a system
from making strerror thread-safe--it just needs to use thread-local storage.
But none do it, AFAIK.

I actually use this guarantee by C. All of my projects use a single int to
report errors (not a global one; the return it directly or via an
out-parameter). System errors are positive (as strongly implied by POSIX**)
and my project-specific errors are negative. I then split up the negative
range into namespaces, like so:

  #define JSON_EBASE -(('J' << 24) | ('S' << 16) | ('O' << 8) | 'N')

  #define HXD_EBASE -(('D' << 24) | ('U' << 16) | ('M' << 8) | 'P')

  #define DNS_EBASE -(('d' << 24) | ('n' << 16) | ('s' << 8) | 64)

  #define FASTPCRE_EBASE -(('F' << 24) | ('A' << 16) | ('S' << 8) | ('T' << 0))

This way my error scheme mimics C's and POSIX's (with its plusses and
minuses--simplicitly being the biggest plus). Applications are expected to
use the library-specific message writer (json_strerror, hxd_strerror, etc),
but if for some reason the value gets passed to strerror, the behavior is
still well defined, and the error can be decoded by an engineer.

** POSIX only requires that POSIX-defined errno values are positive. 
Technically esoteric, system specific errno values could be negative. In
practice the only system I've found that uses negative errno values is