Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strerror_r has no effect

Tags:

c

gcc

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.)

like image 338
spraff Avatar asked May 05 '13 12:05

spraff


1 Answers

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.

like image 139
hmjd Avatar answered Oct 22 '22 02:10

hmjd