wolfcrypt/test/test.c: improve strerror_r() flavor sensing (simpler gate randomly provoked cppcheck-all-async-quic).

This commit is contained in:
Daniel Pouzzner
2023-03-01 17:50:00 -06:00
parent 63b049814b
commit 6a7674a146

View File

@@ -643,22 +643,35 @@ static void render_error_message(const char* msg, int es)
break;
case WC_TEST_RET_TAG_ERRNO:
{
#if defined(_GNU_SOURCE) || \
(defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L))
/* strerror_r() comes in two mutually incompatible flavors, a native glibc
* flavor that always returns a non-null char pointer that must be used
* directly, and a POSIX flavor that returns an error int, and iff success,
* stores an error string in the supplied buffer. this is all most
* infelicitous...
*/
#if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
(defined(_GNU_SOURCE) || defined(__USE_GNU) || \
(defined(__USE_XOPEN2K) && \
defined(_POSIX_C_SOURCE) && \
(_POSIX_C_SOURCE >= 200112L)))
char errno_buf[64], *errno_string;
#if defined(_GNU_SOURCE)
errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
errno_buf, sizeof(errno_buf));
#else
/* precisely mirror the gate used in glibc string.h */
#if defined __USE_XOPEN2K && !defined __USE_GNU
if (strerror_r(WC_TEST_RET_DEC_I(es),
errno_buf, sizeof(errno_buf)) != 0)
XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
errno_string = errno_buf;
#else
errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
errno_buf, sizeof(errno_buf));
#endif
err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
errno_string);
#else
#else /* can't figure out how to strerror_r(), or don't want error strings */
err_sys_printf("%s error L=%d errno=%d\n", msg,
WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
#endif