This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
strerror_r -- GNU version
- From: Chris Hall <glibc at gmch dot uk>
- To: libc-help at sourceware dot org
- Date: Sat, 4 Jan 2020 13:59:07 +0000
- Subject: strerror_r -- GNU version
I note that for all known error numbers, the current implementation of
GNU strerror_r() returns a pointer to a constant string (and not the
supplied buffer).
So I can use GNU strerror_r() to distinguish known error numbers from
unknown (or invalid) ones.
But to do so I am depending on an undocumented feature of the
implementation (of a non-standard function) -- which is a worry :-(
I note that the POSIX (XSI) strerror_r() may return EINVAL for invalid
error numbers. And invalid appears to include unknown numbers. But
that cannot be relied on, either.
Is there a good standard/portable way to distinguish known error numbers ?
Inspection of the (recent) glibc implementation of POSIX (XSI)
strerror_r() tells me that (a) it does return EINVAL for unknown errors,
and (b) it uses GNU strerror_r() and checks the return address to detect
known/unknown errors.
On a you-know-and-I-know-it-works basis I could depend on the same
trick, if _GNU_SOURCE... But I guess I have to expect to be bitten in
the backside at some time in the future ?
FWIW: I wish there was a portable way to map error numbers to their
macro names (or at least a preferred macro name), which could double as
a way to discover whether an error number is known or not.
Chris