During testing I have a mock object which sets errno = ETIMEDOUT; The object I’m testing sees the error and calls strerror_r to get back an error string:
if (ret) {
if (ret == EAI_SYSTEM) {
char err[128];
strerror_r(errno, err, 128);
err_string.assign(err);
} else {
err_string.assign(gai_strerror(ret));
}
return ret;
}
I don’t understand why strerror_r is returning trash. I even tried calling
strerror_r(ETIMEDOUT, err, 128)
directly and still got trash. I must be missing something. It seems I’m getting the gnu version of the function not the posix one, but that shouldn’t make any difference in this case.
Edit
I’m on Ubuntu 8.04. glibc version looks like 2.7 in features.h.
According to this page, http://linux.die.net/man/3/strerror_r, if you’re using the GNU version of
strerror_r(), the function might decide to not store anything at all into the buffer you provide; you’d need to use the string returned from the function (seems like a rather bizarre interface):So if you happen to be using the GNU version you can’t rely on your
errbuffer to have anything useful put into it, unlessstrerror_r()returns the address oferr.