strerror_r returns trash when I manually set errno during testing

Posted by Robert S. Barnes on Stack Overflow See other posts from Stack Overflow or by Robert S. Barnes
Published on 2010-06-16T06:50:58Z Indexed on 2010/06/16 6:52 UTC
Read the original article Hit count: 327

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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about unit-testing