spraff
spraff

Reputation: 33385

strerror_r has no effect

Here is a simple program which illustrates the problem.

#include <string.h>
#include <iostream>

int main () {
    char buf [30];
    strerror_r (0, buf, sizeof (buf));

    std :: cout << "strerror_r(0): " << buf << std :: endl;
    std :: cout << "strerror(0): " << strerror (0) << std :: endl;
}

Here is the output.

strerror_r(0): 
strerror(0): Success

Why is there nothing in buf?

(Compiled on Ubuntu with gcc.)

Upvotes: 5

Views: 1735

Answers (1)

hmjd
hmjd

Reputation: 121961

From man strerror_r:

The XSI-compliant version of strerror_r() is provided if: (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE Otherwise, the GNU-specific version is provided.

and further down:

The GNU-specific strerror_r() returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most buflen bytes are stored (the string may be truncated if buflen is too small and errnum is unknown). The string always includes a terminating null byte.

The program must be using the GNU-specific version and not populating buf. I reproduced the behaviour of the posted program but stored the return value of strerror_r() and it was not the same address as buf.

Upvotes: 7

Related Questions