Use strerror_r Correctly: XSI vs GNU Versions
strerror_r has two incompatible signatures in glibc. Learn which one you get, how to write code that works with both, and how strerror_l avoids the problem.

Two functions called strerror_r exist, they have the same name, and they return different types. One returns an int and always fills your buffer. The other returns a char * that may point somewhere other than your buffer. Code that calls the wrong one for its feature-test macros compiles with a warning or none at all, then prints garbage or crashes the first time an error occurs. This post sorts out which version you get on Linux with glibc 2.41, how to write a call that is correct under both, and why strerror_l from POSIX.1-2008 is often the cleaner answer. The two forms are described in the strerror_r(3) man page, and the POSIX one in the standard's page for strerror. Earlier posts in this cluster covered plain strerror and errno.
Why strerror_r exists
Plain strerror returns a pointer to a string that the library may overwrite on the next call. In a multi-threaded program, two threads calling it at once can see each other's text, and for unknown error numbers glibc builds the text in a shared buffer. strerror_r fixes that by letting the caller supply the buffer, so the result is private to the caller and the function is thread-safe.
The trouble is that two standards disagreed about its signature before they were reconciled, and glibc ships both.
The two signatures
| Version | Signature | Returns | Buffer use |
|---|---|---|---|
| XSI-compliant (POSIX) | int strerror_r(int errnum, char *buf, size_t buflen) | 0 on success, or an error number | Always writes the message into buf |
| GNU-specific | char *strerror_r(int errnum, char *buf, size_t buflen) | pointer to the message | May return a pointer to a static string and leave buf untouched |
The XSI version returns 0 for success and sets errno or returns a positive value such as EINVAL for an unknown number and ERANGE when the buffer is too small. The GNU version never reports failure. For a known error it returns a pointer to an immutable static string, ignoring your buffer. For an unknown error it writes Unknown error N into your buffer and returns that.
Which one do you get
glibc selects the version from feature-test macros. The rule in the man page is that the XSI version is used if (_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE, and the GNU version otherwise. The practical consequences:
A plain
cc file.ccompiles with default settings, which on glibc defines_DEFAULT_SOURCE, and gives the GNU version.cc -std=c11defines__STRICT_ANSI__, and thenstrerror_ris not declared unless you define a feature-test macro yourself.cc -D_POSIX_C_SOURCE=200809Lwithout_GNU_SOURCEgives the XSI version.A C++ compiler such as
g++predefines_GNU_SOURCE, so C++ code on glibc gets the GNU version unless it undefines it.
That last point catches people. A header-only library that works in a C test program breaks under g++, because the same call now returns a pointer and not an integer.
The bug in action
Here is code written for the XSI version.
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char buf[64];
int rc = strerror_r(ENOENT, buf, sizeof buf);
if (rc != 0) {
fprintf(stderr, "strerror_r failed: %d\n", rc);
return 1;
}
printf("%s\n", buf);
return 0;
}Built with cc -std=c11 -Wall wrong_version.c, it uses the XSI version because the macro is defined before any header and _GNU_SOURCE is not, and it prints No such file or directory. Now take the same body, but compile it as cc -D_GNU_SOURCE -Wall with #define _POSIX_C_SOURCE removed. The call returns a char *, the compiler warns about assigning a pointer to an int without a cast, and rc holds the low bits of a pointer. The rc != 0 branch runs, the message says strerror_r failed with a nonsense number, and buf is never written because glibc returned its static string and left your buffer alone. The warning is the only help the compiler gives, so build with -Wall and read it.
A wrapper that is correct under both
The portable fix is a wrapper that picks the right interpretation at compile time. glibc defines the macro _GNU_SOURCE for the GNU variant, and the __USE_GNU internal condition is what <string.h> tests. User code should not rely on internal macros, so test the documented pair.
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdio.h>
#include <string.h>
#if (_POSIX_C_SOURCE >= 200112L) && !defined(_GNU_SOURCE)
#define STRERROR_R_XSI 1
#endif
/* Fill buf with the message for errnum. Always NUL-terminates. */
const char *safe_strerror(int errnum, char *buf, size_t buflen)
{
#ifdef STRERROR_R_XSI
if (strerror_r(errnum, buf, buflen) != 0)
snprintf(buf, buflen, "Unknown error %d", errnum);
return buf;
#else
return strerror_r(errnum, buf, buflen); /* GNU: use the return value */
#endif
}
int main(void)
{
char buf[64];
printf("%s\n", safe_strerror(EACCES, buf, sizeof buf));
printf("%s\n", safe_strerror(9999, buf, sizeof buf));
return 0;
}Permission denied
Unknown error 9999Two rules make this correct. In the GNU branch, use the return value and never read buf directly, because for known errors the message is not in it. In the XSI branch, check the return code, because ERANGE means the text was truncated and the buffer holds a truncated, still terminated string on glibc. A buffer of 128 bytes holds every message glibc produces for English, but translated messages in other locales can be longer, so handle truncation as a normal case.
strerror_l: no buffer, no version split
POSIX.1-2008 added strerror_l(int errnum, locale_t locale). It returns a char *, takes a locale object, and is thread-safe because the strings come from per-locale storage. glibc provides it from version 2.6. It removes the buffer and the two signatures at once, at the cost of managing a locale_t.
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
locale_t loc = newlocale(LC_MESSAGES_MASK, "C", (locale_t)0);
if (loc == (locale_t)0) {
perror("newlocale");
return 1;
}
printf("%s\n", strerror_l(ENOENT, loc));
freelocale(loc);
return 0;
}No such file or directoryUsing the C locale also fixes the log-searching problem from the earlier post: the text is always English whatever the user's LANG says, so a grep for a message works on every machine. If you want the user's locale instead, pass uselocale((locale_t)0) or create the locale object from "".
Deciding which to use
Use strerror_l when you target POSIX.1-2008 systems and want one clean call. Use a wrapper like safe_strerror when the code builds as both C and C++, or on older systems without strerror_l. Use plain strerror only in single-threaded tools where the string is printed at once. musl implements the XSI form of strerror_r and has no GNU form, so a wrapper that tests the macro also keeps your code working there.
Checking which version your build uses
Rather than reason about macros, make the compiler tell you. A one-line static check fails the build when the version is not the one your code expects, which turns a runtime surprise into a compile-time error.
#define _POSIX_C_SOURCE 200809L
#include <string.h>
_Static_assert(
_Generic(strerror_r(0, (char *)0, 0), int: 1, default: 0),
"this file expects the XSI strerror_r that returns int");The _Generic selection looks at the type of the call expression and gives 1 only for int. Compiled with -D_GNU_SOURCE, the expression has type char *, the assertion fires and the compiler prints the message. The expression is never evaluated, because _Generic and _Static_assert work on types, so the null buffer does no harm. Put the check in the one file that wraps the call, and the rest of the program never has to think about it.


