Strerror_r does not affect

Here is a simple program that 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 result.

 strerror_r(0): strerror(0): Success 

Why is there nothing in buf ?

(Compiled on Ubuntu with gcc.)

+4
source share
2 answers

From man strerror_r :

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

and further down:

The GNU-specific strerror_r () returns a pointer to a string containing an error message. This can be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in this case, buf is not used). If the function stores the string in buf, then no more than byte bytes are stored (the string can be truncated if buflen is too small and errnum is unknown). The string always includes a terminating null byte.

The program should use a version specific to GNU and not populate buf . I reproduced the behavior of the hosted program, but saved the return value of strerror_r() , and it was not the same address as buf .

+5
source
 Here is the right way to use strerror_r in cout, ON GNU ---------------- strerror_r(3) - Linux man page Name strerror, strerror_r - return string describing error number Synopsis #include <string.h> char *strerror(int errnum); int strerror_r(int errnum, char *buf, size_t buflen); /* XSI-compliant */ char *strerror_r(int errnum, char *buf, size_t buflen); /* GNU-specific */ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): 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. ----------------------------------- ON POXIS compiler(RHEL-6) ------------------ Example Program: ------------------ cat testStrerr_r.cpp #include <string.h> #include <iostream> int main () { char buf [30]; //strerror_r (0, buf, sizeof (buf)); std :: cout << "Ravi::"<<strerror_r (0, buf, sizeof (buf))<<std :: endl; std :: cout << "Ravi::"<<strerror_r (1, buf, sizeof (buf))<<std :: endl; std :: cout << "Ravi::"<<strerror_r (2, buf, sizeof (buf))<<std :: endl; std :: cout << "Ravi::"<<strerror_r (11, buf, sizeof (buf))<<std :: endl; std :: cout << "Ravi::"<<strerror_r (12, buf, sizeof (buf))<<std :: endl; std :: cout << "Ravi::"<<strerror_r (13, buf, sizeof (buf))<<std :: endl; std :: cout << "Ravi::"<<strerror_r (14, buf, sizeof (buf))<<std :: endl; return 0; } ./testStrerr_r Ravi::Success Ravi::Operation not permitted Ravi::No such file or directory Ravi::Resource temporarily unavailable Ravi::Cannot allocate memory Ravi::Permission denied Ravi::Bad address 
0
source

Source: https://habr.com/ru/post/1479233/


All Articles