void TestError_s( void ) { char error[ 256 ]; size_t errlen; int violations = NumViolations; errlen = strerrorlen_s( EBADF ); VERIFY( errlen != 0 ); VERIFY( strerror_s( error, sizeof( error ), EBADF ) == 0 ); /* get an error string */ VERIFY( strlen(error) != 0 ); VERIFY( strerror_s( error, errlen - 1, EBADF ) != 0 ); /* truncated error string */ VERIFY( strcmp( error + errlen - 5, "..." ) == 0 ); /* really truncated? */ VERIFY( NumViolations == violations ); //rt constraints VERIFY( strerror_s( NULL, errlen - 1, EBADF ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( strerror_s( error, 0, EBADF ) != 0 ); VERIFY( NumViolations == ++violations ); #if RSIZE_MAX != SIZE_MAX VERIFY( strerror_s( error, ~0, EBADF ) != 0 ); VERIFY( NumViolations == ++violations ); #endif }
/** * threadsafe, if __STDC_LIB_EXT1__ is available. TODO: UNTESTED! */ inline std::string strerror(int errnum) { #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE // XSI-compliant strerror_r() function // int strerror_r(int errnum, char* buf, size_t buflen); char buf[1024]; int ret = strerror_r(errnum, buf, sizeof(buf)); return buf; // #else // GNU-specific strerror_r() function // char* strerror_r(int errnum, char* buf, size_t buflen); return strerror_r(errnum, nullptr, 0); #elif defined(__STDC_LIB_EXT1__) int length = strerrorlen_s(errnum) + 1; return_string.resize(length); strerror_s((char*)return_string.data(), length, errnum); return return_string; #else // #warning "Not threadsafe!" return strerror(errnum); #endif }