From: Viktor Szakats Date: Sat, 4 Oct 2025 01:10:37 +0000 (+0200) Subject: curlx: move Curl_strerror, use in src and tests, ban `strerror` globally X-Git-Tag: rc-8_17_0-1~128 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=34ad78da89c614aafb21033bac456a1c0f54921a;p=thirdparty%2Fcurl.git curlx: move Curl_strerror, use in src and tests, ban `strerror` globally Also: - tests/server: replace local `sstrerror()` with `curlx_strerror()`. - tests/server: show the error code next to the string, where missing. - curlx: use `curl_msnprintf()` when building for src and tests. (units was already using it.) - lib: drop unused includes found along the way. - curlx_strerror(): avoid compiler warning (and another similar one): ``` In file included from servers.c:14: ../../lib/../../lib/curlx/strerr.c: In function ‘curlx_strerror’: ../../lib/../../lib/curlx/strerr.c:328:32: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=] 328 | SNPRINTF(buf, buflen, "%s", msg); | ^ ../../lib/../../lib/curlx/strerr.c:47:18: note: ‘snprintf’ output 1 or more bytes (assuming 2) into a destination of size 1 47 | #define SNPRINTF snprintf | ^ ../../lib/../../lib/curlx/strerr.c:328:7: note: in expansion of macro ‘SNPRINTF’ 328 | SNPRINTF(buf, buflen, "%s", msg); | ^~~~~~~~ ``` Follow-up to 45438c8d6f8e70385d66c029568524e9e803c539 #18823 Closes #18840 --- diff --git a/docs/examples/.checksrc b/docs/examples/.checksrc index a83f0edb9f..336e1928d2 100644 --- a/docs/examples/.checksrc +++ b/docs/examples/.checksrc @@ -7,6 +7,7 @@ allowfunc open allowfunc snprintf allowfunc socket allowfunc sscanf +allowfunc strerror banfunc curl_maprintf banfunc curl_mfprintf banfunc curl_mprintf diff --git a/lib/.checksrc b/lib/.checksrc index 9b8d799ea2..e69de29bb2 100644 --- a/lib/.checksrc +++ b/lib/.checksrc @@ -1 +0,0 @@ -banfunc strerror diff --git a/lib/Makefile.inc b/lib/Makefile.inc index ff8144fb5a..500c690561 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -31,6 +31,7 @@ LIB_CURLX_CFILES = \ curlx/inet_pton.c \ curlx/multibyte.c \ curlx/nonblock.c \ + curlx/strerr.c \ curlx/strparse.c \ curlx/timediff.c \ curlx/timeval.c \ @@ -49,6 +50,7 @@ LIB_CURLX_HFILES = \ curlx/inet_pton.h \ curlx/multibyte.h \ curlx/nonblock.h \ + curlx/strerr.h \ curlx/strparse.h \ curlx/timediff.h \ curlx/timeval.h \ diff --git a/lib/cf-socket.c b/lib/cf-socket.c index 8ed1116c07..34bcda68d7 100644 --- a/lib/cf-socket.c +++ b/lib/cf-socket.c @@ -62,7 +62,6 @@ #include "bufq.h" #include "sendf.h" #include "if2ip.h" -#include "strerror.h" #include "cfilters.h" #include "cf-socket.h" #include "connect.h" @@ -80,6 +79,7 @@ #include "strdup.h" #include "system_win32.h" #include "curlx/version_win32.h" +#include "curlx/strerr.h" #include "curlx/strparse.h" /* The last 3 #include files should be in this order */ @@ -114,7 +114,7 @@ static void tcpnodelay(struct Curl_easy *data, curl_socket_t sockfd) if(setsockopt(sockfd, level, TCP_NODELAY, (void *)&onoff, sizeof(onoff)) < 0) infof(data, "Could not set TCP_NODELAY: %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); #else (void)data; (void)sockfd; @@ -136,7 +136,7 @@ static void nosigpipe(struct Curl_easy *data, #ifndef CURL_DISABLE_VERBOSE_STRINGS char buffer[STRERROR_LEN]; infof(data, "Could not set SO_NOSIGPIPE: %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); #endif } } @@ -644,7 +644,7 @@ static CURLcode bindlocal(struct Curl_easy *data, struct connectdata *conn, char buffer[STRERROR_LEN]; data->state.os_errno = error = SOCKERRNO; failf(data, "Couldn't bind to interface '%s' with errno %d: %s", - iface, error, Curl_strerror(error, buffer, sizeof(buffer))); + iface, error, curlx_strerror(error, buffer, sizeof(buffer))); return CURLE_INTERFACE_FAILED; } break; @@ -747,8 +747,8 @@ static CURLcode bindlocal(struct Curl_easy *data, struct connectdata *conn, char buffer[STRERROR_LEN]; data->state.errorbuf = FALSE; data->state.os_errno = error = SOCKERRNO; - failf(data, "Couldn't bind to '%s' with errno %d: %s", - host, error, Curl_strerror(error, buffer, sizeof(buffer))); + failf(data, "Couldn't bind to '%s' with errno %d: %s", host, + error, curlx_strerror(error, buffer, sizeof(buffer))); return CURLE_INTERFACE_FAILED; } } @@ -799,7 +799,7 @@ static CURLcode bindlocal(struct Curl_easy *data, struct connectdata *conn, char buffer[STRERROR_LEN]; data->state.os_errno = error = SOCKERRNO; failf(data, "bind failed with errno %d: %s", - error, Curl_strerror(error, buffer, sizeof(buffer))); + error, curlx_strerror(error, buffer, sizeof(buffer))); } return CURLE_INTERFACE_FAILED; @@ -900,8 +900,8 @@ static CURLcode socket_connect_result(struct Curl_easy *data, #else { char buffer[STRERROR_LEN]; - infof(data, "Immediate connect fail for %s: %s", - ipaddress, Curl_strerror(error, buffer, sizeof(buffer))); + infof(data, "Immediate connect fail for %s: %s", ipaddress, + curlx_strerror(error, buffer, sizeof(buffer))); } #endif data->state.os_errno = error; @@ -1050,13 +1050,13 @@ static CURLcode set_local_ip(struct Curl_cfilter *cf, if(getsockname(ctx->sock, (struct sockaddr*) &ssloc, &slen)) { int error = SOCKERRNO; failf(data, "getsockname() failed with errno %d: %s", - error, Curl_strerror(error, buffer, sizeof(buffer))); + error, curlx_strerror(error, buffer, sizeof(buffer))); return CURLE_FAILED_INIT; } if(!Curl_addr2string((struct sockaddr*)&ssloc, slen, ctx->ip.local_ip, &ctx->ip.local_port)) { failf(data, "ssloc inet_ntop() failed with errno %d: %s", - errno, Curl_strerror(errno, buffer, sizeof(buffer))); + errno, curlx_strerror(errno, buffer, sizeof(buffer))); return CURLE_FAILED_INIT; } } @@ -1082,7 +1082,7 @@ static CURLcode set_remote_ip(struct Curl_cfilter *cf, ctx->error = errno; /* malformed address or bug in inet_ntop, try next address */ failf(data, "curl_sa_addr inet_ntop() failed with errno %d: %s", - errno, Curl_strerror(errno, buffer, sizeof(buffer))); + errno, curlx_strerror(errno, buffer, sizeof(buffer))); return CURLE_FAILED_INIT; } return CURLE_OK; @@ -1360,7 +1360,7 @@ out: infof(data, "connect to %s port %u from %s port %d failed: %s", ctx->ip.remote_ip, ctx->ip.remote_port, ctx->ip.local_ip, ctx->ip.local_port, - Curl_strerror(ctx->error, buffer, sizeof(buffer))); + curlx_strerror(ctx->error, buffer, sizeof(buffer))); } #endif } @@ -1498,7 +1498,7 @@ static CURLcode cf_socket_send(struct Curl_cfilter *cf, struct Curl_easy *data, else { char buffer[STRERROR_LEN]; failf(data, "Send failure: %s", - Curl_strerror(sockerr, buffer, sizeof(buffer))); + curlx_strerror(sockerr, buffer, sizeof(buffer))); data->state.os_errno = sockerr; result = CURLE_SEND_ERROR; } @@ -1566,7 +1566,7 @@ static CURLcode cf_socket_recv(struct Curl_cfilter *cf, struct Curl_easy *data, else { char buffer[STRERROR_LEN]; failf(data, "Recv failure: %s", - Curl_strerror(sockerr, buffer, sizeof(buffer))); + curlx_strerror(sockerr, buffer, sizeof(buffer))); data->state.os_errno = sockerr; result = CURLE_RECV_ERROR; } @@ -2037,13 +2037,13 @@ static void cf_tcp_set_accepted_remote_ip(struct Curl_cfilter *cf, if(getpeername(ctx->sock, (struct sockaddr*) &ssrem, &plen)) { int error = SOCKERRNO; failf(data, "getpeername() failed with errno %d: %s", - error, Curl_strerror(error, buffer, sizeof(buffer))); + error, curlx_strerror(error, buffer, sizeof(buffer))); return; } if(!Curl_addr2string((struct sockaddr*)&ssrem, plen, ctx->ip.remote_ip, &ctx->ip.remote_port)) { failf(data, "ssrem inet_ntop() failed with errno %d: %s", - errno, Curl_strerror(errno, buffer, sizeof(buffer))); + errno, curlx_strerror(errno, buffer, sizeof(buffer))); return; } #else diff --git a/lib/curl_setup.h b/lib/curl_setup.h index a741abde03..f6fe6535f4 100644 --- a/lib/curl_setup.h +++ b/lib/curl_setup.h @@ -950,6 +950,10 @@ endings either CRLF or LF so 't' is appropriate. #define CURL_ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) +/* Buffer size for error messages retrieved via + curlx_strerror() and Curl_sspi_strerror() */ +#define STRERROR_LEN 256 + #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */ /* * The following memory function replacement typedef's are COPIED from diff --git a/lib/curlx/.checksrc b/lib/curlx/.checksrc index 9b8d799ea2..e69de29bb2 100644 --- a/lib/curlx/.checksrc +++ b/lib/curlx/.checksrc @@ -1 +0,0 @@ -banfunc strerror diff --git a/lib/curlx/curlx.h b/lib/curlx/curlx.h index 33ac72e8e1..480e91950d 100644 --- a/lib/curlx/curlx.h +++ b/lib/curlx/curlx.h @@ -58,6 +58,9 @@ #include "version_win32.h" /* provides curlx_verify_windows_version() */ +#include "strerr.h" +/* The curlx_strerror() function */ + #include "strparse.h" /* The curlx_str_* parsing functions */ diff --git a/lib/curlx/strerr.c b/lib/curlx/strerr.c new file mode 100644 index 0000000000..96dc9c8355 --- /dev/null +++ b/lib/curlx/strerr.c @@ -0,0 +1,361 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + ***************************************************************************/ + +#include "../curl_setup.h" + +#ifdef HAVE_STRERROR_R +# if (!defined(HAVE_POSIX_STRERROR_R) && \ + !defined(HAVE_GLIBC_STRERROR_R)) || \ + (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)) +# error "strerror_r MUST be either POSIX, glibc style" +# endif +#endif + +#include + +#ifndef WITHOUT_LIBCURL +#include +#define SNPRINTF curl_msnprintf +#else +/* when built for the test servers */ + +/* adjust for old MSVC */ +#if defined(_MSC_VER) && (_MSC_VER < 1900) +#define SNPRINTF _snprintf +#else +#define SNPRINTF snprintf +#endif +#endif /* !WITHOUT_LIBCURL */ + +#include "winapi.h" +#include "strerr.h" +/* The last 2 #include files should be in this order */ +#include "../curl_memory.h" +#include "../memdebug.h" + +#ifdef USE_WINSOCK +/* This is a helper function for curlx_strerror that converts Winsock error + * codes (WSAGetLastError) to error messages. + * Returns NULL if no error message was found for error code. + */ +static const char * +get_winsock_error(int err, char *buf, size_t len) +{ +#ifndef CURL_DISABLE_VERBOSE_STRINGS + const char *p; + size_t alen; +#endif + + if(!len) + return NULL; + + *buf = '\0'; + +#ifdef CURL_DISABLE_VERBOSE_STRINGS + (void)err; + return NULL; +#else + switch(err) { + case WSAEINTR: + p = "Call interrupted"; + break; + case WSAEBADF: + p = "Bad file"; + break; + case WSAEACCES: + p = "Bad access"; + break; + case WSAEFAULT: + p = "Bad argument"; + break; + case WSAEINVAL: + p = "Invalid arguments"; + break; + case WSAEMFILE: + p = "Out of file descriptors"; + break; + case WSAEWOULDBLOCK: + p = "Call would block"; + break; + case WSAEINPROGRESS: + case WSAEALREADY: + p = "Blocking call in progress"; + break; + case WSAENOTSOCK: + p = "Descriptor is not a socket"; + break; + case WSAEDESTADDRREQ: + p = "Need destination address"; + break; + case WSAEMSGSIZE: + p = "Bad message size"; + break; + case WSAEPROTOTYPE: + p = "Bad protocol"; + break; + case WSAENOPROTOOPT: + p = "Protocol option is unsupported"; + break; + case WSAEPROTONOSUPPORT: + p = "Protocol is unsupported"; + break; + case WSAESOCKTNOSUPPORT: + p = "Socket is unsupported"; + break; + case WSAEOPNOTSUPP: + p = "Operation not supported"; + break; + case WSAEAFNOSUPPORT: + p = "Address family not supported"; + break; + case WSAEPFNOSUPPORT: + p = "Protocol family not supported"; + break; + case WSAEADDRINUSE: + p = "Address already in use"; + break; + case WSAEADDRNOTAVAIL: + p = "Address not available"; + break; + case WSAENETDOWN: + p = "Network down"; + break; + case WSAENETUNREACH: + p = "Network unreachable"; + break; + case WSAENETRESET: + p = "Network has been reset"; + break; + case WSAECONNABORTED: + p = "Connection was aborted"; + break; + case WSAECONNRESET: + p = "Connection was reset"; + break; + case WSAENOBUFS: + p = "No buffer space"; + break; + case WSAEISCONN: + p = "Socket is already connected"; + break; + case WSAENOTCONN: + p = "Socket is not connected"; + break; + case WSAESHUTDOWN: + p = "Socket has been shut down"; + break; + case WSAETOOMANYREFS: + p = "Too many references"; + break; + case WSAETIMEDOUT: + p = "Timed out"; + break; + case WSAECONNREFUSED: + p = "Connection refused"; + break; + case WSAELOOP: + p = "Loop??"; + break; + case WSAENAMETOOLONG: + p = "Name too long"; + break; + case WSAEHOSTDOWN: + p = "Host down"; + break; + case WSAEHOSTUNREACH: + p = "Host unreachable"; + break; + case WSAENOTEMPTY: + p = "Not empty"; + break; + case WSAEPROCLIM: + p = "Process limit reached"; + break; + case WSAEUSERS: + p = "Too many users"; + break; + case WSAEDQUOT: + p = "Bad quota"; + break; + case WSAESTALE: + p = "Something is stale"; + break; + case WSAEREMOTE: + p = "Remote error"; + break; + case WSAEDISCON: + p = "Disconnected"; + break; + /* Extended Winsock errors */ + case WSASYSNOTREADY: + p = "Winsock library is not ready"; + break; + case WSANOTINITIALISED: + p = "Winsock library not initialised"; + break; + case WSAVERNOTSUPPORTED: + p = "Winsock version not supported"; + break; + + /* getXbyY() errors (already handled in herrmsg): + * Authoritative Answer: Host not found */ + case WSAHOST_NOT_FOUND: + p = "Host not found"; + break; + + /* Non-Authoritative: Host not found, or SERVERFAIL */ + case WSATRY_AGAIN: + p = "Host not found, try again"; + break; + + /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */ + case WSANO_RECOVERY: + p = "Unrecoverable error in call to nameserver"; + break; + + /* Valid name, no data record of requested type */ + case WSANO_DATA: + p = "No data record of requested type"; + break; + + default: + return NULL; + } + alen = strlen(p); + if(alen < len) + strcpy(buf, p); + return buf; +#endif +} +#endif /* USE_WINSOCK */ + +/* + * Our thread-safe and smart strerror() replacement. + * + * The 'err' argument passed in to this function MUST be a true errno number + * as reported on this system. We do no range checking on the number before + * we pass it to the "number-to-message" conversion function and there might + * be systems that do not do proper range checking in there themselves. + * + * We do not do range checking (on systems other than Windows) since there is + * no good reliable and portable way to do it. + * + * On Windows different types of error codes overlap. This function has an + * order of preference when trying to match error codes: + * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError). + * + * It may be more correct to call one of the variant functions instead: + * Call Curl_sspi_strerror if the error code is definitely Windows SSPI. + * Call curlx_winapi_strerror if the error code is definitely Windows API. + */ +const char *curlx_strerror(int err, char *buf, size_t buflen) +{ +#ifdef _WIN32 + DWORD old_win_err = GetLastError(); +#endif + int old_errno = errno; + char *p; + + if(!buflen) + return NULL; + +#ifndef _WIN32 + DEBUGASSERT(err >= 0); +#endif + + *buf = '\0'; + +#ifdef _WIN32 +#ifndef UNDER_CE + /* 'sys_nerr' is the maximum errno number, it is not widely portable */ + if(err >= 0 && err < sys_nerr) + SNPRINTF(buf, buflen, "%s", sys_errlist[err]); + else +#endif + { + if( +#ifdef USE_WINSOCK + !get_winsock_error(err, buf, buflen) && +#endif + !curlx_get_winapi_error(err, buf, buflen)) + SNPRINTF(buf, buflen, "Unknown error %d (%#x)", err, err); + } +#else /* !_WIN32 */ + +#if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R) + /* + * The POSIX-style strerror_r() may set errno to ERANGE if insufficient + * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated + * message string, or EINVAL if 'errnum' is not a valid error number. + */ + if(strerror_r(err, buf, buflen) && + buflen > sizeof("Unknown error ") + 20) { + if(buf[0] == '\0') + SNPRINTF(buf, buflen, "Unknown error %d", err); + } +#elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R) + /* + * The glibc-style strerror_r() only *might* use the buffer we pass to + * the function, but it always returns the error message as a pointer, + * so we must copy that string unconditionally (if non-NULL). + */ + { + char buffer[256]; + char *msg = strerror_r(err, buffer, sizeof(buffer)); + if(msg && buflen > 1) + SNPRINTF(buf, buflen, "%s", msg); + else if(buflen > sizeof("Unknown error ") + 20) + SNPRINTF(buf, buflen, "Unknown error %d", err); + } +#else + { + /* !checksrc! disable BANNEDFUNC 1 */ + const char *msg = strerror(err); + if(msg && buflen > 1) + SNPRINTF(buf, buflen, "%s", msg); + else if(buflen > sizeof("Unknown error ") + 20) + SNPRINTF(buf, buflen, "Unknown error %d", err); + } +#endif + +#endif /* _WIN32 */ + + /* strip trailing '\r\n' or '\n'. */ + p = strrchr(buf, '\n'); + if(p && (p - buf) >= 2) + *p = '\0'; + p = strrchr(buf, '\r'); + if(p && (p - buf) >= 1) + *p = '\0'; + + if(errno != old_errno) + CURL_SETERRNO(old_errno); + +#ifdef _WIN32 + if(old_win_err != GetLastError()) + SetLastError(old_win_err); +#endif + + return buf; +} diff --git a/lib/curlx/strerr.h b/lib/curlx/strerr.h new file mode 100644 index 0000000000..4413e6738c --- /dev/null +++ b/lib/curlx/strerr.h @@ -0,0 +1,29 @@ +#ifndef HEADER_CURL_STRERR_H +#define HEADER_CURL_STRERR_H +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + ***************************************************************************/ + +const char *curlx_strerror(int err, char *buf, size_t buflen); + +#endif /* HEADER_CURL_STRERR_H */ diff --git a/lib/curlx/winapi.c b/lib/curlx/winapi.c index 7e3d1aa9ca..cc008e3087 100644 --- a/lib/curlx/winapi.c +++ b/lib/curlx/winapi.c @@ -25,12 +25,12 @@ /* * curlx_winapi_strerror: - * Variant of Curl_strerror if the error code is definitely Windows API. + * Variant of curlx_strerror if the error code is definitely Windows API. */ #ifdef _WIN32 #include "winapi.h" -#ifdef BUILDING_LIBCURL +#ifndef WITHOUT_LIBCURL #include #define SNPRINTF curl_msnprintf #else @@ -42,10 +42,9 @@ #else #define SNPRINTF snprintf #endif +#endif /* !WITHOUT_LIBCURL */ -#endif /* !BUILDING_LIBCURL */ - -/* This is a helper function for Curl_strerror that converts Windows API error +/* This is a helper function for curlx_strerror that converts Windows API error * codes (GetLastError) to error messages. * Returns NULL if no error message was found for error code. */ diff --git a/lib/ftp.c b/lib/ftp.c index 13b613bc1e..a3194c2a7b 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -58,7 +58,6 @@ #include "cfilters.h" #include "cf-socket.h" #include "connect.h" -#include "strerror.h" #include "curlx/inet_ntop.h" #include "curlx/inet_pton.h" #include "select.h" @@ -71,6 +70,7 @@ #include "http_proxy.h" #include "socks.h" #include "strdup.h" +#include "curlx/strerr.h" #include "curlx/strparse.h" /* The last 3 #include files should be in this order */ #include "curl_printf.h" @@ -1007,7 +1007,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data, sslen = sizeof(ss); if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) { failf(data, "getsockname() failed: %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); goto out; } switch(sa->sa_family) { @@ -1054,7 +1054,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data, } if(!ai) { failf(data, "socket failure: %s", - Curl_strerror(error, buffer, sizeof(buffer))); + curlx_strerror(error, buffer, sizeof(buffer))); goto out; } CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), opened socket", @@ -1081,12 +1081,12 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data, * the control connection instead and restart the port loop */ infof(data, "bind(port=%hu) on non-local address failed: %s", port, - Curl_strerror(error, buffer, sizeof(buffer))); + curlx_strerror(error, buffer, sizeof(buffer))); sslen = sizeof(ss); if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) { failf(data, "getsockname() failed: %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); goto out; } port = port_min; @@ -1095,7 +1095,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data, } if(error != SOCKEADDRINUSE && error != SOCKEACCES) { failf(data, "bind(port=%hu) failed: %s", port, - Curl_strerror(error, buffer, sizeof(buffer))); + curlx_strerror(error, buffer, sizeof(buffer))); goto out; } } @@ -1118,7 +1118,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data, sslen = sizeof(ss); if(getsockname(portsock, sa, &sslen)) { failf(data, "getsockname() failed: %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); goto out; } CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), socket bound to port %d", @@ -1128,7 +1128,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data, if(listen(portsock, 1)) { failf(data, "socket failure: %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); goto out; } CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), listening on %d", diff --git a/lib/strerror.c b/lib/strerror.c index 47c6572114..c4545af888 100644 --- a/lib/strerror.c +++ b/lib/strerror.c @@ -24,15 +24,8 @@ #include "curl_setup.h" -#ifdef HAVE_STRERROR_R -# if (!defined(HAVE_POSIX_STRERROR_R) && \ - !defined(HAVE_GLIBC_STRERROR_R)) || \ - (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)) -# error "strerror_r MUST be either POSIX, glibc style" -# endif -#endif - #include +#include #ifdef USE_WINDOWS_SSPI #include "curl_sspi.h" @@ -40,8 +33,8 @@ #include "curlx/winapi.h" #include "strerror.h" -/* The last 3 #include files should be in this order */ -#include "curl_printf.h" + +/* The last 2 #include files should be in this order */ #include "curl_memory.h" #include "memdebug.h" @@ -548,315 +541,10 @@ curl_url_strerror(CURLUcode error) #endif } -#ifdef USE_WINSOCK -/* This is a helper function for Curl_strerror that converts Winsock error - * codes (WSAGetLastError) to error messages. - * Returns NULL if no error message was found for error code. - */ -static const char * -get_winsock_error(int err, char *buf, size_t len) -{ -#ifndef CURL_DISABLE_VERBOSE_STRINGS - const char *p; - size_t alen; -#endif - - if(!len) - return NULL; - - *buf = '\0'; - -#ifdef CURL_DISABLE_VERBOSE_STRINGS - (void)err; - return NULL; -#else - switch(err) { - case WSAEINTR: - p = "Call interrupted"; - break; - case WSAEBADF: - p = "Bad file"; - break; - case WSAEACCES: - p = "Bad access"; - break; - case WSAEFAULT: - p = "Bad argument"; - break; - case WSAEINVAL: - p = "Invalid arguments"; - break; - case WSAEMFILE: - p = "Out of file descriptors"; - break; - case WSAEWOULDBLOCK: - p = "Call would block"; - break; - case WSAEINPROGRESS: - case WSAEALREADY: - p = "Blocking call in progress"; - break; - case WSAENOTSOCK: - p = "Descriptor is not a socket"; - break; - case WSAEDESTADDRREQ: - p = "Need destination address"; - break; - case WSAEMSGSIZE: - p = "Bad message size"; - break; - case WSAEPROTOTYPE: - p = "Bad protocol"; - break; - case WSAENOPROTOOPT: - p = "Protocol option is unsupported"; - break; - case WSAEPROTONOSUPPORT: - p = "Protocol is unsupported"; - break; - case WSAESOCKTNOSUPPORT: - p = "Socket is unsupported"; - break; - case WSAEOPNOTSUPP: - p = "Operation not supported"; - break; - case WSAEAFNOSUPPORT: - p = "Address family not supported"; - break; - case WSAEPFNOSUPPORT: - p = "Protocol family not supported"; - break; - case WSAEADDRINUSE: - p = "Address already in use"; - break; - case WSAEADDRNOTAVAIL: - p = "Address not available"; - break; - case WSAENETDOWN: - p = "Network down"; - break; - case WSAENETUNREACH: - p = "Network unreachable"; - break; - case WSAENETRESET: - p = "Network has been reset"; - break; - case WSAECONNABORTED: - p = "Connection was aborted"; - break; - case WSAECONNRESET: - p = "Connection was reset"; - break; - case WSAENOBUFS: - p = "No buffer space"; - break; - case WSAEISCONN: - p = "Socket is already connected"; - break; - case WSAENOTCONN: - p = "Socket is not connected"; - break; - case WSAESHUTDOWN: - p = "Socket has been shut down"; - break; - case WSAETOOMANYREFS: - p = "Too many references"; - break; - case WSAETIMEDOUT: - p = "Timed out"; - break; - case WSAECONNREFUSED: - p = "Connection refused"; - break; - case WSAELOOP: - p = "Loop??"; - break; - case WSAENAMETOOLONG: - p = "Name too long"; - break; - case WSAEHOSTDOWN: - p = "Host down"; - break; - case WSAEHOSTUNREACH: - p = "Host unreachable"; - break; - case WSAENOTEMPTY: - p = "Not empty"; - break; - case WSAEPROCLIM: - p = "Process limit reached"; - break; - case WSAEUSERS: - p = "Too many users"; - break; - case WSAEDQUOT: - p = "Bad quota"; - break; - case WSAESTALE: - p = "Something is stale"; - break; - case WSAEREMOTE: - p = "Remote error"; - break; - case WSAEDISCON: - p = "Disconnected"; - break; - /* Extended Winsock errors */ - case WSASYSNOTREADY: - p = "Winsock library is not ready"; - break; - case WSANOTINITIALISED: - p = "Winsock library not initialised"; - break; - case WSAVERNOTSUPPORTED: - p = "Winsock version not supported"; - break; - - /* getXbyY() errors (already handled in herrmsg): - * Authoritative Answer: Host not found */ - case WSAHOST_NOT_FOUND: - p = "Host not found"; - break; - - /* Non-Authoritative: Host not found, or SERVERFAIL */ - case WSATRY_AGAIN: - p = "Host not found, try again"; - break; - - /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */ - case WSANO_RECOVERY: - p = "Unrecoverable error in call to nameserver"; - break; - - /* Valid name, no data record of requested type */ - case WSANO_DATA: - p = "No data record of requested type"; - break; - - default: - return NULL; - } - alen = strlen(p); - if(alen < len) - strcpy(buf, p); - return buf; -#endif -} -#endif /* USE_WINSOCK */ - -/* - * Our thread-safe and smart strerror() replacement. - * - * The 'err' argument passed in to this function MUST be a true errno number - * as reported on this system. We do no range checking on the number before - * we pass it to the "number-to-message" conversion function and there might - * be systems that do not do proper range checking in there themselves. - * - * We do not do range checking (on systems other than Windows) since there is - * no good reliable and portable way to do it. - * - * On Windows different types of error codes overlap. This function has an - * order of preference when trying to match error codes: - * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError). - * - * It may be more correct to call one of the variant functions instead: - * Call Curl_sspi_strerror if the error code is definitely Windows SSPI. - * Call curlx_winapi_strerror if the error code is definitely Windows API. - */ -const char *Curl_strerror(int err, char *buf, size_t buflen) -{ -#ifdef _WIN32 - DWORD old_win_err = GetLastError(); -#endif - int old_errno = errno; - char *p; - - if(!buflen) - return NULL; - -#ifndef _WIN32 - DEBUGASSERT(err >= 0); -#endif - - *buf = '\0'; - -#ifdef _WIN32 -#ifndef UNDER_CE - /* 'sys_nerr' is the maximum errno number, it is not widely portable */ - if(err >= 0 && err < sys_nerr) - curl_msnprintf(buf, buflen, "%s", sys_errlist[err]); - else -#endif - { - if( -#ifdef USE_WINSOCK - !get_winsock_error(err, buf, buflen) && -#endif - !curlx_get_winapi_error(err, buf, buflen)) - curl_msnprintf(buf, buflen, "Unknown error %d (%#x)", err, err); - } -#else /* not Windows coming up */ - -#if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R) - /* - * The POSIX-style strerror_r() may set errno to ERANGE if insufficient - * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated - * message string, or EINVAL if 'errnum' is not a valid error number. - */ - if(strerror_r(err, buf, buflen)) { - if('\0' == buf[0]) - curl_msnprintf(buf, buflen, "Unknown error %d", err); - } -#elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R) - /* - * The glibc-style strerror_r() only *might* use the buffer we pass to - * the function, but it always returns the error message as a pointer, - * so we must copy that string unconditionally (if non-NULL). - */ - { - char buffer[256]; - char *msg = strerror_r(err, buffer, sizeof(buffer)); - if(msg) - curl_msnprintf(buf, buflen, "%s", msg); - else - curl_msnprintf(buf, buflen, "Unknown error %d", err); - } -#else - { - /* !checksrc! disable BANNEDFUNC 1 */ - const char *msg = strerror(err); - if(msg) - curl_msnprintf(buf, buflen, "%s", msg); - else - curl_msnprintf(buf, buflen, "Unknown error %d", err); - } -#endif - -#endif /* end of not Windows */ - - /* strip trailing '\r\n' or '\n'. */ - p = strrchr(buf, '\n'); - if(p && (p - buf) >= 2) - *p = '\0'; - p = strrchr(buf, '\r'); - if(p && (p - buf) >= 1) - *p = '\0'; - - if(errno != old_errno) - CURL_SETERRNO(old_errno); - -#ifdef _WIN32 - if(old_win_err != GetLastError()) - SetLastError(old_win_err); -#endif - - return buf; -} - #ifdef USE_WINDOWS_SSPI /* * Curl_sspi_strerror: - * Variant of Curl_strerror if the error code is definitely Windows SSPI. + * Variant of curlx_strerror if the error code is definitely Windows SSPI. */ const char *Curl_sspi_strerror(int err, char *buf, size_t buflen) { diff --git a/lib/strerror.h b/lib/strerror.h index 424fb5b7b5..2120726c9f 100644 --- a/lib/strerror.h +++ b/lib/strerror.h @@ -24,11 +24,6 @@ * ***************************************************************************/ -#include "urldata.h" - -#define STRERROR_LEN 256 /* a suitable length */ - -const char *Curl_strerror(int err, char *buf, size_t buflen); #ifdef USE_WINDOWS_SSPI const char *Curl_sspi_strerror(int err, char *buf, size_t buflen); #endif diff --git a/lib/tftp.c b/lib/tftp.c index ad2c84e660..b6bc5e9bdc 100644 --- a/lib/tftp.c +++ b/lib/tftp.c @@ -55,7 +55,6 @@ #include "tftp.h" #include "progress.h" #include "connect.h" -#include "strerror.h" #include "sockaddr.h" /* required for Curl_sockaddr_storage */ #include "multiif.h" #include "url.h" @@ -63,6 +62,7 @@ #include "speedcheck.h" #include "select.h" #include "escape.h" +#include "curlx/strerr.h" #include "curlx/strparse.h" /* The last 3 #include files should be in this order */ @@ -539,7 +539,7 @@ static CURLcode tftp_send_first(struct tftp_conn *state, free(filename); if(senddata != (ssize_t)sbytes) { char buffer[STRERROR_LEN]; - failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } break; @@ -622,7 +622,7 @@ static CURLcode tftp_rx(struct tftp_conn *state, tftp_event_t event) (struct sockaddr *)&state->remote_addr, state->remote_addrlen); if(sbytes < 0) { - failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } @@ -647,7 +647,7 @@ static CURLcode tftp_rx(struct tftp_conn *state, tftp_event_t event) (struct sockaddr *)&state->remote_addr, state->remote_addrlen); if(sbytes < 0) { - failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } @@ -673,7 +673,7 @@ static CURLcode tftp_rx(struct tftp_conn *state, tftp_event_t event) (struct sockaddr *)&state->remote_addr, state->remote_addrlen); if(sbytes < 0) { - failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } } @@ -750,8 +750,8 @@ static CURLcode tftp_tx(struct tftp_conn *state, tftp_event_t event) state->remote_addrlen); /* Check all sbytes were sent */ if(sbytes < 0) { - failf(data, "%s", Curl_strerror(SOCKERRNO, - buffer, sizeof(buffer))); + failf(data, "%s", + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); result = CURLE_SEND_ERROR; } } @@ -795,7 +795,7 @@ static CURLcode tftp_tx(struct tftp_conn *state, tftp_event_t event) state->remote_addrlen); /* Check all sbytes were sent */ if(sbytes < 0) { - failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } /* Update the progress meter */ @@ -821,7 +821,7 @@ static CURLcode tftp_tx(struct tftp_conn *state, tftp_event_t event) state->remote_addrlen); /* Check all sbytes were sent */ if(sbytes < 0) { - failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } /* since this was a re-send, we remain at the still byte position */ @@ -1039,7 +1039,7 @@ static CURLcode tftp_connect(struct Curl_easy *data, bool *done) if(rc) { char buffer[STRERROR_LEN]; failf(data, "bind() failed; %s", - Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); + curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); return CURLE_COULDNT_CONNECT; } conn->bits.bound = TRUE; @@ -1257,7 +1257,7 @@ static CURLcode tftp_multi_statemach(struct Curl_easy *data, bool *done) /* bail out */ int error = SOCKERRNO; char buffer[STRERROR_LEN]; - failf(data, "%s", Curl_strerror(error, buffer, sizeof(buffer))); + failf(data, "%s", curlx_strerror(error, buffer, sizeof(buffer))); state->event = TFTP_EVENT_ERROR; } else if(rc) { diff --git a/lib/url.c b/lib/url.c index ae4a6f6502..2a46354c61 100644 --- a/lib/url.c +++ b/lib/url.c @@ -80,7 +80,6 @@ #include "progress.h" #include "cookie.h" #include "strcase.h" -#include "strerror.h" #include "escape.h" #include "share.h" #include "content_encoding.h" @@ -125,6 +124,7 @@ #include "altsvc.h" #include "curlx/dynbuf.h" #include "headers.h" +#include "curlx/strerr.h" #include "curlx/strparse.h" /* The last 3 #include files should be in this order */ #include "curl_printf.h" @@ -1743,7 +1743,7 @@ static void zonefrom_url(CURLU *uh, struct Curl_easy *data, #ifndef CURL_DISABLE_VERBOSE_STRINGS char buffer[STRERROR_LEN]; infof(data, "Invalid zoneid: %s; %s", zoneid, - Curl_strerror(errno, buffer, sizeof(buffer))); + curlx_strerror(errno, buffer, sizeof(buffer))); #endif } else diff --git a/lib/vauth/.checksrc b/lib/vauth/.checksrc index 9b8d799ea2..e69de29bb2 100644 --- a/lib/vauth/.checksrc +++ b/lib/vauth/.checksrc @@ -1 +0,0 @@ -banfunc strerror diff --git a/lib/vquic/.checksrc b/lib/vquic/.checksrc index 9b8d799ea2..e69de29bb2 100644 --- a/lib/vquic/.checksrc +++ b/lib/vquic/.checksrc @@ -1 +0,0 @@ -banfunc strerror diff --git a/lib/vquic/curl_ngtcp2.c b/lib/vquic/curl_ngtcp2.c index 4a45c1f6db..a85c9d7609 100644 --- a/lib/vquic/curl_ngtcp2.c +++ b/lib/vquic/curl_ngtcp2.c @@ -57,7 +57,6 @@ #include "../cf-socket.h" #include "../connect.h" #include "../progress.h" -#include "../strerror.h" #include "../curlx/dynbuf.h" #include "../http1.h" #include "../select.h" diff --git a/lib/vquic/curl_osslq.c b/lib/vquic/curl_osslq.c index aea03c038f..1d7c3bce99 100644 --- a/lib/vquic/curl_osslq.c +++ b/lib/vquic/curl_osslq.c @@ -42,7 +42,6 @@ #include "../cf-socket.h" #include "../connect.h" #include "../progress.h" -#include "../strerror.h" #include "../curlx/dynbuf.h" #include "../http1.h" #include "../select.h" @@ -57,6 +56,7 @@ #include "curl_osslq.h" #include "../url.h" #include "../curlx/warnless.h" +#include "../curlx/strerr.h" /* The last 3 #include files should be in this order */ #include "../curl_printf.h" @@ -552,7 +552,7 @@ static CURLcode cf_osslq_ssl_err(struct Curl_cfilter *cf, Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip); if(sockerr && detail == SSL_ERROR_SYSCALL) - Curl_strerror(sockerr, extramsg, sizeof(extramsg)); + curlx_strerror(sockerr, extramsg, sizeof(extramsg)); failf(data, "QUIC connect: %s in connection to %s:%d (%s)", extramsg[0] ? extramsg : osslq_SSL_ERROR_to_str(detail), ctx->peer.dispname, ip.remote_port, ip.remote_ip); diff --git a/lib/vquic/curl_quiche.c b/lib/vquic/curl_quiche.c index 5fb67f69f4..36691d09db 100644 --- a/lib/vquic/curl_quiche.c +++ b/lib/vquic/curl_quiche.c @@ -39,7 +39,6 @@ #include "../multiif.h" #include "../connect.h" #include "../progress.h" -#include "../strerror.h" #include "../select.h" #include "../http1.h" #include "vquic.h" diff --git a/lib/vquic/vquic.c b/lib/vquic/vquic.c index 275ea8bccc..91680915f3 100644 --- a/lib/vquic/vquic.c +++ b/lib/vquic/vquic.c @@ -43,7 +43,7 @@ #include "../rand.h" #include "vquic.h" #include "vquic_int.h" -#include "../strerror.h" +#include "../curlx/strerr.h" #include "../curlx/strparse.h" /* The last 3 #include files should be in this order */ @@ -434,7 +434,7 @@ static CURLcode recvmmsg_packets(struct Curl_cfilter *cf, result = CURLE_COULDNT_CONNECT; goto out; } - Curl_strerror(SOCKERRNO, errstr, sizeof(errstr)); + curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); failf(data, "QUIC: recvmmsg() unexpectedly returned %d (errno=%d; %s)", mcount, SOCKERRNO, errstr); result = CURLE_RECV_ERROR; @@ -527,7 +527,7 @@ static CURLcode recvmsg_packets(struct Curl_cfilter *cf, result = CURLE_COULDNT_CONNECT; goto out; } - Curl_strerror(SOCKERRNO, errstr, sizeof(errstr)); + curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); failf(data, "QUIC: recvmsg() unexpectedly returned %zd (errno=%d; %s)", rc, SOCKERRNO, errstr); result = CURLE_RECV_ERROR; @@ -602,7 +602,7 @@ static CURLcode recvfrom_packets(struct Curl_cfilter *cf, result = CURLE_COULDNT_CONNECT; goto out; } - Curl_strerror(SOCKERRNO, errstr, sizeof(errstr)); + curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); failf(data, "QUIC: recvfrom() unexpectedly returned %zd (errno=%d; %s)", nread, SOCKERRNO, errstr); result = CURLE_RECV_ERROR; diff --git a/lib/vssh/.checksrc b/lib/vssh/.checksrc index 9b8d799ea2..e69de29bb2 100644 --- a/lib/vssh/.checksrc +++ b/lib/vssh/.checksrc @@ -1 +0,0 @@ -banfunc strerror diff --git a/lib/vtls/.checksrc b/lib/vtls/.checksrc index 9b8d799ea2..e69de29bb2 100644 --- a/lib/vtls/.checksrc +++ b/lib/vtls/.checksrc @@ -1 +0,0 @@ -banfunc strerror diff --git a/lib/vtls/openssl.c b/lib/vtls/openssl.c index fd5529f5ab..0714ce7c6a 100644 --- a/lib/vtls/openssl.c +++ b/lib/vtls/openssl.c @@ -63,9 +63,9 @@ #include "hostcheck.h" #include "../transfer.h" #include "../multiif.h" +#include "../curlx/strerr.h" #include "../curlx/strparse.h" #include "../strdup.h" -#include "../strerror.h" #include "../curl_printf.h" #include "apple.h" @@ -4668,7 +4668,7 @@ static CURLcode ossl_connect_step2(struct Curl_cfilter *cf, int sockerr = SOCKERRNO; if(sockerr && detail == SSL_ERROR_SYSCALL) - Curl_strerror(sockerr, extramsg, sizeof(extramsg)); + curlx_strerror(sockerr, extramsg, sizeof(extramsg)); failf(data, OSSL_PACKAGE " SSL_connect: %s in connection to %s:%d ", extramsg[0] ? extramsg : SSL_ERROR_to_str(detail), connssl->peer.hostname, connssl->peer.port); @@ -5274,7 +5274,7 @@ static CURLcode ossl_send_earlydata(struct Curl_cfilter *cf, if(sslerror) ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)); else if(sockerr) - Curl_strerror(sockerr, error_buffer, sizeof(error_buffer)); + curlx_strerror(sockerr, error_buffer, sizeof(error_buffer)); else msnprintf(error_buffer, sizeof(error_buffer), "%s", SSL_ERROR_to_str(err)); @@ -5460,7 +5460,7 @@ static CURLcode ossl_send(struct Curl_cfilter *cf, if(sslerror) ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)); else if(sockerr) - Curl_strerror(sockerr, error_buffer, sizeof(error_buffer)); + curlx_strerror(sockerr, error_buffer, sizeof(error_buffer)); else msnprintf(error_buffer, sizeof(error_buffer), "%s", SSL_ERROR_to_str(err)); @@ -5557,7 +5557,7 @@ static CURLcode ossl_recv(struct Curl_cfilter *cf, if(sslerror) ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)); else if(sockerr && err == SSL_ERROR_SYSCALL) - Curl_strerror(sockerr, error_buffer, sizeof(error_buffer)); + curlx_strerror(sockerr, error_buffer, sizeof(error_buffer)); else msnprintf(error_buffer, sizeof(error_buffer), "%s", SSL_ERROR_to_str(err)); @@ -5580,7 +5580,7 @@ static CURLcode ossl_recv(struct Curl_cfilter *cf, * the error in case of some weirdness in the OSSL stack */ int sockerr = SOCKERRNO; if(sockerr) - Curl_strerror(sockerr, error_buffer, sizeof(error_buffer)); + curlx_strerror(sockerr, error_buffer, sizeof(error_buffer)); else { msnprintf(error_buffer, sizeof(error_buffer), "Connection closed abruptly"); diff --git a/lib/vtls/rustls.c b/lib/vtls/rustls.c index 70e109212d..ce7a4d5c9d 100644 --- a/lib/vtls/rustls.c +++ b/lib/vtls/rustls.c @@ -34,13 +34,13 @@ #include "../curlx/fopen.h" #include "../curlx/inet_pton.h" +#include "../curlx/strerr.h" #include "../urldata.h" #include "../sendf.h" #include "vtls.h" #include "vtls_int.h" #include "rustls.h" #include "keylog.h" -#include "../strerror.h" #include "cipher_suite.h" #include "x509asn1.h" @@ -171,7 +171,7 @@ static ssize_t tls_recv_more(struct Curl_cfilter *cf, else if(io_error) { char buffer[STRERROR_LEN]; failf(data, "reading from socket: %s", - Curl_strerror(io_error, buffer, sizeof(buffer))); + curlx_strerror(io_error, buffer, sizeof(buffer))); *err = CURLE_RECV_ERROR; return -1; } @@ -283,7 +283,7 @@ static CURLcode cr_flush_out(struct Curl_cfilter *cf, struct Curl_easy *data, else if(io_error) { char buffer[STRERROR_LEN]; failf(data, "writing to socket: %s", - Curl_strerror(io_error, buffer, sizeof(buffer))); + curlx_strerror(io_error, buffer, sizeof(buffer))); return CURLE_SEND_ERROR; } if(tlswritten == 0) { diff --git a/scripts/checksrc.pl b/scripts/checksrc.pl index 54800ce12d..017738ecdf 100755 --- a/scripts/checksrc.pl +++ b/scripts/checksrc.pl @@ -58,6 +58,7 @@ my %banfunc = ( "vsnprintf" => 1, "sscanf" => 1, "strcat" => 1, + "strerror" => 1, "strncat" => 1, "strncpy" => 1, "strtok_r" => 1, diff --git a/src/Makefile.inc b/src/Makefile.inc index 35f8e6fdee..d57d6c9125 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -38,12 +38,14 @@ CURLX_CFILES = \ ../lib/curlx/dynbuf.c \ ../lib/curlx/fopen.c \ ../lib/curlx/nonblock.c \ + ../lib/curlx/strerr.c \ ../lib/curlx/strparse.c \ ../lib/curlx/timediff.c \ ../lib/curlx/timeval.c \ ../lib/curlx/version_win32.c \ ../lib/curlx/wait.c \ - ../lib/curlx/warnless.c + ../lib/curlx/warnless.c \ + ../lib/curlx/winapi.c CURLX_HFILES = \ ../lib/curlx/binmode.h \ @@ -52,12 +54,14 @@ CURLX_HFILES = \ ../lib/curlx/dynbuf.h \ ../lib/curlx/fopen.h \ ../lib/curlx/nonblock.h \ + ../lib/curlx/strerr.h \ ../lib/curlx/strparse.h \ ../lib/curlx/timediff.h \ ../lib/curlx/timeval.h \ ../lib/curlx/version_win32.h \ ../lib/curlx/wait.h \ - ../lib/curlx/warnless.h + ../lib/curlx/warnless.h \ + ../lib/curlx/winapi.h CURL_CFILES = \ config2setopts.c \ diff --git a/src/config2setopts.c b/src/config2setopts.c index 533fe992d3..2a58b75772 100644 --- a/src/config2setopts.c +++ b/src/config2setopts.c @@ -83,9 +83,10 @@ static int sockopt_callback(void *clientp, curl_socket_t curlfd, #endif } if(result < 0) { + char buffer[STRERROR_LEN]; int error = errno; - warnf("Setting type of service to %d failed with errno %d: %s", - tos, error, strerror(error)); + warnf("Setting type of service to %d failed with errno %d: %s", tos, + error, curlx_strerror(error, buffer, sizeof(buffer))); } } #endif @@ -94,9 +95,10 @@ static int sockopt_callback(void *clientp, curl_socket_t curlfd, int priority = (int)config->vlan_priority; if(setsockopt(curlfd, SOL_SOCKET, SO_PRIORITY, (void *)&priority, sizeof(priority)) != 0) { + char buffer[STRERROR_LEN]; int error = errno; - warnf("VLAN priority %d failed with errno %d: %s", - priority, error, strerror(error)); + warnf("VLAN priority %d failed with errno %d: %s", priority, + error, curlx_strerror(error, buffer, sizeof(buffer))); } } #endif diff --git a/src/tool_cb_wrt.c b/src/tool_cb_wrt.c index 12e4417da4..724706b7a1 100644 --- a/src/tool_cb_wrt.c +++ b/src/tool_cb_wrt.c @@ -96,7 +96,9 @@ bool tool_create_output_file(struct OutStruct *outs, } if(!file) { - warnf("Failed to open the file %s: %s", fname, strerror(errno)); + char errbuf[STRERROR_LEN]; + warnf("Failed to open the file %s: %s", fname, + curlx_strerror(errno, errbuf, sizeof(errbuf))); return FALSE; } outs->s_isreg = TRUE; diff --git a/src/tool_filetime.c b/src/tool_filetime.c index b442fc2014..afd84c0138 100644 --- a/src/tool_filetime.c +++ b/src/tool_filetime.c @@ -77,8 +77,11 @@ int getfiletime(const char *filename, curl_off_t *stamp) *stamp = (curl_off_t)statbuf.st_mtime; rc = 0; } - else - warnf("Failed to get filetime: %s", strerror(errno)); + else { + char errbuf[STRERROR_LEN]; + warnf("Failed to get filetime: %s", + curlx_strerror(errno, errbuf, sizeof(errbuf))); + } #endif return rc; } @@ -131,8 +134,10 @@ void setfiletime(curl_off_t filetime, const char *filename) times[0].tv_sec = times[1].tv_sec = (time_t)filetime; times[0].tv_usec = times[1].tv_usec = 0; if(utimes(filename, times)) { + char errbuf[STRERROR_LEN]; warnf("Failed to set filetime %" CURL_FORMAT_CURL_OFF_T - " on '%s': %s", filetime, filename, strerror(errno)); + " on '%s': %s", filetime, filename, + curlx_strerror(errno, errbuf, sizeof(errbuf))); } #elif defined(HAVE_UTIME) @@ -140,8 +145,10 @@ void setfiletime(curl_off_t filetime, const char *filename) times.actime = (time_t)filetime; times.modtime = (time_t)filetime; if(utime(filename, ×)) { + char errbuf[STRERROR_LEN]; warnf("Failed to set filetime %" CURL_FORMAT_CURL_OFF_T - " on '%s': %s", filetime, filename, strerror(errno)); + " on '%s': %s", filetime, filename, + curlx_strerror(errno, errbuf, sizeof(errbuf))); } #endif } diff --git a/src/tool_formparse.c b/src/tool_formparse.c index f1f2f5b7e5..cd3cf52e3e 100644 --- a/src/tool_formparse.c +++ b/src/tool_formparse.c @@ -222,8 +222,9 @@ size_t tool_mime_stdin_read(char *buffer, /* Read from stdin. */ nitems = fread(buffer, 1, nitems, stdin); if(ferror(stdin)) { + char errbuf[STRERROR_LEN]; /* Show error only once. */ - warnf("stdin: %s", strerror(errno)); + warnf("stdin: %s", curlx_strerror(errno, errbuf, sizeof(errbuf))); return CURL_READFUNC_ABORT; } } @@ -444,8 +445,9 @@ static int read_field_headers(const char *filename, FILE *fp, switch(c) { case EOF: if(ferror(fp)) { + char errbuf[STRERROR_LEN]; errorf("Header file %s read error: %s", filename, - strerror(errno)); + curlx_strerror(errno, errbuf, sizeof(errbuf))); return -1; } return 0; /* Done. */ @@ -564,9 +566,11 @@ static int get_param_part(char endchar, sep = *p; *endpos = '\0'; fp = curlx_fopen(hdrfile, FOPEN_READTEXT); - if(!fp) + if(!fp) { + char errbuf[STRERROR_LEN]; warnf("Cannot read from %s: %s", hdrfile, - strerror(errno)); + curlx_strerror(errno, errbuf, sizeof(errbuf))); + } else { int i = read_field_headers(hdrfile, fp, &headers); diff --git a/src/tool_operate.c b/src/tool_operate.c index d4a6d4db42..2d539de7b5 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -604,9 +604,11 @@ static CURLcode post_per_transfer(struct per_transfer *per, /* Set file extended attributes */ if(!result && config->xattr && outs->fopened && outs->stream) { rc = fwrite_xattr(curl, per->url, fileno(outs->stream)); - if(rc) - warnf("Error setting extended attributes on '%s': %s", - outs->filename, strerror(errno)); + if(rc) { + char errbuf[STRERROR_LEN]; + warnf("Error setting extended attributes on '%s': %s", outs->filename, + curlx_strerror(errno, errbuf, sizeof(errbuf))); + } } if(!result && !outs->stream && !outs->bytes) { @@ -796,9 +798,11 @@ static CURLcode etag_compare(struct OperationConfig *config) /* open file for reading: */ FILE *file = curlx_fopen(config->etag_compare_file, FOPEN_READTEXT); - if(!file) + if(!file) { + char errbuf[STRERROR_LEN]; warnf("Failed to open %s: %s", config->etag_compare_file, - strerror(errno)); + curlx_strerror(errno, errbuf, sizeof(errbuf))); + } if((PARAM_OK == file2string(&etag_from_file, file)) && etag_from_file) { @@ -1068,8 +1072,11 @@ static void check_stdin_upload(struct OperationConfig *config, else per->infd = (int)f; #endif - if(curlx_nonblock((curl_socket_t)per->infd, TRUE) < 0) - warnf("fcntl failed on fd=%d: %s", per->infd, strerror(errno)); + if(curlx_nonblock((curl_socket_t)per->infd, TRUE) < 0) { + char errbuf[STRERROR_LEN]; + warnf("fcntl failed on fd=%d: %s", per->infd, + curlx_strerror(errno, errbuf, sizeof(errbuf))); + } } } diff --git a/src/var.c b/src/var.c index d279fdeb6e..79c8307248 100644 --- a/src/var.c +++ b/src/var.c @@ -457,7 +457,9 @@ ParameterError setvariable(const char *input) else { file = curlx_fopen(line, "rb"); if(!file) { - errorf("Failed to open %s: %s", line, strerror(errno)); + char errbuf[STRERROR_LEN]; + errorf("Failed to open %s: %s", line, + curlx_strerror(errno, errbuf, sizeof(errbuf))); err = PARAM_READ_ERROR; } } diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc index c316a05269..d8735a44e1 100644 --- a/tests/libtest/Makefile.inc +++ b/tests/libtest/Makefile.inc @@ -34,14 +34,16 @@ UTILS_C = memptr.c testutil.c testtrace.c UTILS_H = testutil.h testtrace.h unitcheck.h CURLX_C = \ + ../../lib/curl_threads.c \ ../../lib/curlx/fopen.c \ - ../../lib/curlx/warnless.c \ ../../lib/curlx/multibyte.c \ + ../../lib/curlx/strerr.c \ ../../lib/curlx/timediff.c \ ../../lib/curlx/timeval.c \ - ../../lib/curl_threads.c \ ../../lib/curlx/version_win32.c \ - ../../lib/curlx/wait.c + ../../lib/curlx/wait.c \ + ../../lib/curlx/warnless.c \ + ../../lib/curlx/winapi.c # All libtest programs TESTS_C = \ diff --git a/tests/libtest/first.h b/tests/libtest/first.h index a902bdb8f0..52a6395ad1 100644 --- a/tests/libtest/first.h +++ b/tests/libtest/first.h @@ -427,15 +427,17 @@ void ws_close(CURL *curl); /* ---------------------------------------------------------------- */ -#define exe_select_test(A, B, C, D, E, Y, Z) do { \ - int ec; \ - if(select_wrapper((A), (B), (C), (D), (E)) == -1) { \ - ec = SOCKERRNO; \ - curl_mfprintf(stderr, "%s:%d select() failed, with " \ - "errno %d (%s)\n", \ - (Y), (Z), ec, strerror(ec)); \ - res = TEST_ERR_SELECT; \ - } \ +#define exe_select_test(A, B, C, D, E, Y, Z) do { \ + int ec; \ + if(select_wrapper((A), (B), (C), (D), (E)) == -1) { \ + char ecbuf[STRERROR_LEN]; \ + ec = SOCKERRNO; \ + curl_mfprintf(stderr, "%s:%d select() failed, with " \ + "errno %d (%s)\n", \ + (Y), (Z), \ + ec, curlx_strerror(ec, ecbuf, sizeof(ecbuf))); \ + res = TEST_ERR_SELECT; \ + } \ } while(0) #define res_select_test(A, B, C, D, E) \ diff --git a/tests/libtest/lib505.c b/tests/libtest/lib505.c index 71b79b2a24..b4f7702851 100644 --- a/tests/libtest/lib505.c +++ b/tests/libtest/lib505.c @@ -36,6 +36,7 @@ static CURLcode test_lib505(const char *URL) { CURL *curl; CURLcode res = CURLE_OK; + char errbuf[STRERROR_LEN]; FILE *hd_src; int hd; struct_stat file_info; @@ -54,7 +55,7 @@ static CURLcode test_lib505(const char *URL) hd_src = curlx_fopen(libtest_arg2, "rb"); if(!hd_src) { curl_mfprintf(stderr, "fopen failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */ } @@ -69,7 +70,7 @@ static CURLcode test_lib505(const char *URL) if(hd == -1) { /* can't open file, bail out */ curl_mfprintf(stderr, "fstat() failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); curlx_fclose(hd_src); return TEST_ERR_MAJOR_BAD; diff --git a/tests/libtest/lib518.c b/tests/libtest/lib518.c index 007d261870..a2cb3471bf 100644 --- a/tests/libtest/lib518.c +++ b/tests/libtest/lib518.c @@ -51,9 +51,11 @@ static void t518_store_errmsg(const char *msg, int err) { if(!err) curl_msnprintf(t518_msgbuff, sizeof(t518_msgbuff), "%s", msg); - else + else { + char errbuf[STRERROR_LEN]; curl_msnprintf(t518_msgbuff, sizeof(t518_msgbuff), "%s, errno %d, %s", msg, - err, strerror(err)); + err, curlx_strerror(err, errbuf, sizeof(errbuf))); + } } static void t518_close_file_descriptors(void) diff --git a/tests/libtest/lib525.c b/tests/libtest/lib525.c index b34cd261af..55224eb76e 100644 --- a/tests/libtest/lib525.c +++ b/tests/libtest/lib525.c @@ -29,6 +29,7 @@ static CURLcode test_lib525(const char *URL) { CURLcode res = CURLE_OK; CURL *curl = NULL; + char errbuf[STRERROR_LEN]; FILE *hd_src = NULL; int hd; struct_stat file_info; @@ -45,7 +46,7 @@ static CURLcode test_lib525(const char *URL) hd_src = curlx_fopen(libtest_arg2, "rb"); if(!hd_src) { curl_mfprintf(stderr, "fopen failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); return TEST_ERR_FOPEN; } @@ -60,7 +61,7 @@ static CURLcode test_lib525(const char *URL) if(hd == -1) { /* can't open file, bail out */ curl_mfprintf(stderr, "fstat() failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); curlx_fclose(hd_src); return TEST_ERR_FSTAT; diff --git a/tests/libtest/lib537.c b/tests/libtest/lib537.c index b8bbfb7536..05d50cba7f 100644 --- a/tests/libtest/lib537.c +++ b/tests/libtest/lib537.c @@ -48,9 +48,11 @@ static void t537_store_errmsg(const char *msg, int err) { if(!err) curl_msnprintf(t537_msgbuff, sizeof(t537_msgbuff), "%s", msg); - else + else { + char errbuf[STRERROR_LEN]; curl_msnprintf(t537_msgbuff, sizeof(t537_msgbuff), "%s, errno %d, %s", msg, - err, strerror(err)); + err, curlx_strerror(err, errbuf, sizeof(errbuf))); + } } static void t537_close_file_descriptors(void) diff --git a/tests/libtest/lib541.c b/tests/libtest/lib541.c index 5e6e3c2f33..dfe585d9da 100644 --- a/tests/libtest/lib541.c +++ b/tests/libtest/lib541.c @@ -33,6 +33,7 @@ static CURLcode test_lib541(const char *URL) { CURL *curl; CURLcode res = CURLE_OK; + char errbuf[STRERROR_LEN]; FILE *hd_src; int hd; struct_stat file_info; @@ -45,7 +46,7 @@ static CURLcode test_lib541(const char *URL) hd_src = curlx_fopen(libtest_arg2, "rb"); if(!hd_src) { curl_mfprintf(stderr, "fopen failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */ } @@ -60,7 +61,7 @@ static CURLcode test_lib541(const char *URL) if(hd == -1) { /* can't open file, bail out */ curl_mfprintf(stderr, "fstat() failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); curlx_fclose(hd_src); return TEST_ERR_MAJOR_BAD; diff --git a/tests/libtest/lib556.c b/tests/libtest/lib556.c index 7b155a2528..8239d26034 100644 --- a/tests/libtest/lib556.c +++ b/tests/libtest/lib556.c @@ -83,8 +83,9 @@ again: #else if((size_t)write(STDOUT_FILENO, buf, nread) != nread) { #endif + char errbuf[STRERROR_LEN]; curl_mfprintf(stderr, "write() failed: errno %d (%s)\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); res = TEST_ERR_FAILURE; break; } diff --git a/tests/libtest/lib582.c b/tests/libtest/lib582.c index 9671c4b52f..187432ac0c 100644 --- a/tests/libtest/lib582.c +++ b/tests/libtest/lib582.c @@ -225,6 +225,7 @@ static CURLcode test_lib582(const char *URL) { CURLcode res = CURLE_OK; CURL *curl = NULL; + char errbuf[STRERROR_LEN]; FILE *hd_src = NULL; int hd; struct_stat file_info; @@ -246,7 +247,7 @@ static CURLcode test_lib582(const char *URL) hd_src = curlx_fopen(libtest_arg2, "rb"); if(!hd_src) { curl_mfprintf(stderr, "fopen() failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); return TEST_ERR_FOPEN; } @@ -261,7 +262,7 @@ static CURLcode test_lib582(const char *URL) if(hd == -1) { /* can't open file, bail out */ curl_mfprintf(stderr, "fstat() failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); curlx_fclose(hd_src); return TEST_ERR_FSTAT; diff --git a/tests/libtest/lib591.c b/tests/libtest/lib591.c index c85c42b0c2..c0d5b1a436 100644 --- a/tests/libtest/lib591.c +++ b/tests/libtest/lib591.c @@ -41,8 +41,9 @@ static CURLcode test_lib591(const char *URL) upload = curlx_fopen(libtest_arg3, "rb"); if(!upload) { + char errbuf[STRERROR_LEN]; curl_mfprintf(stderr, "fopen() failed with error (%d) %s\n", - errno, strerror(errno)); + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg3); return TEST_ERR_FOPEN; } diff --git a/tests/server/Makefile.inc b/tests/server/Makefile.inc index be35fe7c4f..bbdacc1a17 100644 --- a/tests/server/Makefile.inc +++ b/tests/server/Makefile.inc @@ -36,10 +36,11 @@ UTILS_H = CURLX_C = \ ../../lib/curlx/base64.c \ ../../lib/curlx/fopen.c \ - ../../lib/curlx/inet_pton.c \ ../../lib/curlx/inet_ntop.c \ + ../../lib/curlx/inet_pton.c \ ../../lib/curlx/multibyte.c \ ../../lib/curlx/nonblock.c \ + ../../lib/curlx/strerr.c \ ../../lib/curlx/strparse.c \ ../../lib/curlx/timediff.c \ ../../lib/curlx/timeval.c \ diff --git a/tests/server/dnsd.c b/tests/server/dnsd.c index 2d0ce35e6f..3f8f5b37a7 100644 --- a/tests/server/dnsd.c +++ b/tests/server/dnsd.c @@ -104,8 +104,10 @@ static int store_incoming(const unsigned char *data, size_t size, /* Open request dump file. */ server = fopen(dumpfile, "ab"); if(!server) { + char errbuf[STRERROR_LEN]; int error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Error opening file '%s'", dumpfile); return -1; } @@ -383,6 +385,7 @@ static int test_dnsd(int argc, char **argv) int flag; int rc; int error; + char errbuf[STRERROR_LEN]; int result = 0; pidname = ".dnsd.pid"; @@ -480,7 +483,8 @@ static int test_dnsd(int argc, char **argv) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; - logmsg("Error creating socket (%d) %s", error, sstrerror(error)); + logmsg("Error creating socket (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); result = 1; goto dnsd_cleanup; } @@ -490,7 +494,7 @@ static int test_dnsd(int argc, char **argv) (void *)&flag, sizeof(flag))) { error = SOCKERRNO; logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); result = 1; goto dnsd_cleanup; } @@ -515,8 +519,8 @@ static int test_dnsd(int argc, char **argv) #endif /* USE_IPV6 */ if(rc) { error = SOCKERRNO; - logmsg("Error binding socket on port %hu (%d) %s", port, error, - sstrerror(error)); + logmsg("Error binding socket on port %hu (%d) %s", port, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); result = 1; goto dnsd_cleanup; } @@ -538,7 +542,7 @@ static int test_dnsd(int argc, char **argv) if(getsockname(sock, &localaddr.sa, &la_size) < 0) { error = SOCKERRNO; logmsg("getsockname() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); goto dnsd_cleanup; } diff --git a/tests/server/first.h b/tests/server/first.h index 4d3ae61de7..8e69a2b976 100644 --- a/tests/server/first.h +++ b/tests/server/first.h @@ -130,7 +130,6 @@ extern void logmsg(const char *msg, ...); extern void loghex(unsigned char *buffer, ssize_t len); extern unsigned char byteval(char *value); extern int win32_init(void); -extern const char *sstrerror(int err); extern FILE *test2fopen(long testno, const char *logdir2); extern curl_off_t our_getpid(void); extern int write_pidfile(const char *filename); diff --git a/tests/server/mqttd.c b/tests/server/mqttd.c index 9414eef504..acb427a37c 100644 --- a/tests/server/mqttd.c +++ b/tests/server/mqttd.c @@ -557,8 +557,10 @@ static curl_socket_t mqttit(curl_socket_t fd) logmsg("SUBSCRIBE to '%s' [%d]", topic, packet_id); stream = test2fopen(testno, logdir); if(!stream) { + char errbuf[STRERROR_LEN]; error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Couldn't open test file %ld", testno); goto end; } @@ -658,6 +660,7 @@ static bool mqttd_incoming(curl_socket_t listenfd) do { ssize_t rc; int error = 0; + char errbuf[STRERROR_LEN]; curl_socket_t sockfd = listenfd; int maxfd = (int)sockfd; @@ -686,7 +689,7 @@ static bool mqttd_incoming(curl_socket_t listenfd) if(rc < 0) { logmsg("select() failed with error (%d) %s", - error, strerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return FALSE; } @@ -694,7 +697,8 @@ static bool mqttd_incoming(curl_socket_t listenfd) curl_socket_t newfd = accept(sockfd, NULL, NULL); if(CURL_SOCKET_BAD == newfd) { error = SOCKERRNO; - logmsg("accept() failed with error (%d) %s", error, sstrerror(error)); + logmsg("accept() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); } else { logmsg("====> Client connect, fd %ld. " @@ -720,6 +724,7 @@ static int test_mqttd(int argc, char *argv[]) int wroteportfile = 0; bool juggle_again; int error; + char errbuf[STRERROR_LEN]; int arg = 1; pidname = ".mqttd.pid"; @@ -825,7 +830,8 @@ static int test_mqttd(int argc, char *argv[]) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; - logmsg("Error creating socket (%d) %s", error, sstrerror(error)); + logmsg("Error creating socket (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto mqttd_cleanup; } diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 0a4f9159c0..5e8bdc15ce 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -225,8 +225,10 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req) stream = test2fopen(req->testno, logdir); if(!stream) { + char errbuf[STRERROR_LEN]; int error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Couldn't open test file %ld", req->testno); req->open = FALSE; /* closes connection */ return 1; /* done */ @@ -527,6 +529,7 @@ static void rtspd_storerequest(char *reqbuf, size_t totalsize) { int res; int error = 0; + char errbuf[STRERROR_LEN]; size_t written; size_t writeleft; FILE *dump; @@ -544,8 +547,8 @@ static void rtspd_storerequest(char *reqbuf, size_t totalsize) /* !checksrc! disable ERRNOVAR 1 */ } while(!dump && ((error = errno) == EINTR)); if(!dump) { - logmsg("Error opening file %s error (%d) %s", - dumpfile, error, strerror(error)); + logmsg("Error opening file %s error (%d) %s", dumpfile, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Failed to write request input to %s", dumpfile); return; } @@ -564,8 +567,8 @@ static void rtspd_storerequest(char *reqbuf, size_t totalsize) if(writeleft == 0) logmsg("Wrote request (%zu bytes) input to %s", totalsize, dumpfile); else if(writeleft > 0) { - logmsg("Error writing file %s error (%d) %s", - dumpfile, error, strerror(error)); + logmsg("Error writing file %s error (%d) %s", dumpfile, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s", totalsize-writeleft, totalsize, dumpfile); } @@ -574,14 +577,15 @@ storerequest_cleanup: res = fclose(dump); if(res) - logmsg("Error closing file %s error (%d) %s", - dumpfile, errno, strerror(errno)); + logmsg("Error closing file %s error (%d) %s", dumpfile, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); } /* return 0 on success, non-zero on failure */ static int rtspd_get_request(curl_socket_t sock, struct rtspd_httprequest *req) { int error; + char errbuf[STRERROR_LEN]; int fail = 0; int done_processing = 0; char *reqbuf = req->reqbuf; @@ -642,7 +646,8 @@ static int rtspd_get_request(curl_socket_t sock, struct rtspd_httprequest *req) } else if(got < 0) { error = SOCKERRNO; - logmsg("recv() returned error (%d) %s", error, sstrerror(error)); + logmsg("recv() returned error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); fail = 1; } if(fail) { @@ -704,6 +709,7 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) bool sendfailure = FALSE; size_t responsesize; int error = 0; + char errbuf[STRERROR_LEN]; int res; static char weare[256]; char responsedump[256]; @@ -788,7 +794,8 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) snprintf(partbuf, sizeof(partbuf), "data%ld", req->partno); if(!stream) { error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Couldn't open test file"); return 0; } @@ -811,7 +818,8 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) stream = test2fopen(req->testno, logdir); if(!stream) { error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Couldn't open test file"); free(ptr); return 0; @@ -851,7 +859,8 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) dump = fopen(responsedump, "ab"); if(!dump) { error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Error opening file '%s'", responsedump); logmsg("couldn't create logfile '%s'", responsedump); free(ptr); @@ -907,8 +916,8 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) res = fclose(dump); if(res) - logmsg("Error closing file %s error (%d) %s", - responsedump, errno, strerror(errno)); + logmsg("Error closing file %s error (%d) %s", responsedump, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); if(got_exit_signal) { free(ptr); @@ -948,7 +957,7 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) /* should not happen */ error = SOCKERRNO; logmsg("curlx_wait_ms() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); break; } } @@ -987,6 +996,7 @@ static int test_rtspd(int argc, char *argv[]) struct rtspd_httprequest req; int rc; int error; + char errbuf[STRERROR_LEN]; int arg = 1; memset(&req, 0, sizeof(req)); @@ -1092,7 +1102,8 @@ static int test_rtspd(int argc, char *argv[]) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; - logmsg("Error creating socket (%d) %s", error, sstrerror(error)); + logmsg("Error creating socket (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto server_cleanup; } @@ -1101,7 +1112,7 @@ static int test_rtspd(int argc, char *argv[]) (void *)&flag, sizeof(flag))) { error = SOCKERRNO; logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto server_cleanup; } @@ -1125,8 +1136,8 @@ static int test_rtspd(int argc, char *argv[]) #endif /* USE_IPV6 */ if(rc) { error = SOCKERRNO; - logmsg("Error binding socket on port %hu (%d) %s", - port, error, sstrerror(error)); + logmsg("Error binding socket on port %hu (%d) %s", port, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto server_cleanup; } @@ -1147,7 +1158,7 @@ static int test_rtspd(int argc, char *argv[]) if(getsockname(sock, &localaddr.sa, &la_size) < 0) { error = SOCKERRNO; logmsg("getsockname() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); goto server_cleanup; } @@ -1180,7 +1191,7 @@ static int test_rtspd(int argc, char *argv[]) if(rc) { error = SOCKERRNO; logmsg("listen() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto server_cleanup; } @@ -1207,7 +1218,7 @@ static int test_rtspd(int argc, char *argv[]) if(CURL_SOCKET_BAD == msgsock) { error = SOCKERRNO; logmsg("MAJOR ERROR, accept() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); break; } diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 2ae681072e..3f4c9ef8b0 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -200,6 +200,7 @@ static ssize_t fullread(int filedes, void *buffer, size_t nbytes) } if(rc < 0) { + char errbuf[STRERROR_LEN]; error = errno; /* !checksrc! disable ERRNOVAR 1 */ if((error == EINTR) || (error == EAGAIN)) @@ -211,7 +212,7 @@ static ssize_t fullread(int filedes, void *buffer, size_t nbytes) } logmsg("reading from file descriptor: %d,", filedes); logmsg("unrecoverable read() failure (%d) %s", - error, strerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return -1; } @@ -253,13 +254,14 @@ static ssize_t fullwrite(int filedes, const void *buffer, size_t nbytes) } if(wc < 0) { + char errbuf[STRERROR_LEN]; error = errno; /* !checksrc! disable ERRNOVAR 1 */ if((error == EINTR) || (error == EAGAIN)) continue; logmsg("writing to file descriptor: %d,", filedes); logmsg("unrecoverable write() failure (%d) %s", - error, strerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return -1; } @@ -947,6 +949,7 @@ static bool juggle(curl_socket_t *sockfdp, int maxfd = -99; ssize_t rc; int error = 0; + char errbuf[STRERROR_LEN]; unsigned char buffer[BUFFER_SIZE]; char data[16]; @@ -1067,7 +1070,7 @@ static bool juggle(curl_socket_t *sockfdp, if(rc < 0) { logmsg("select() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return FALSE; } @@ -1172,7 +1175,8 @@ static bool juggle(curl_socket_t *sockfdp, curl_socket_t newfd = accept(sockfd, NULL, NULL); if(CURL_SOCKET_BAD == newfd) { error = SOCKERRNO; - logmsg("accept() failed with error (%d) %s", error, sstrerror(error)); + logmsg("accept() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); } else { logmsg("====> Client connect"); @@ -1226,6 +1230,7 @@ static int test_sockfilt(int argc, char *argv[]) bool juggle_again; int rc; int error; + char errbuf[STRERROR_LEN]; int arg = 1; enum sockmode mode = PASSIVE_LISTEN; /* default */ const char *addr = NULL; @@ -1345,7 +1350,8 @@ static int test_sockfilt(int argc, char *argv[]) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; - logmsg("Error creating socket (%d) %s", error, sstrerror(error)); + logmsg("Error creating socket (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); write_stdout("FAIL\n", 5); goto sockfilt_cleanup; } @@ -1382,8 +1388,8 @@ static int test_sockfilt(int argc, char *argv[]) } if(rc) { error = SOCKERRNO; - logmsg("Error connecting to port %hu (%d) %s", - server_connectport, error, sstrerror(error)); + logmsg("Error connecting to port %hu (%d) %s", server_connectport, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); write_stdout("FAIL\n", 5); goto sockfilt_cleanup; } diff --git a/tests/server/socksd.c b/tests/server/socksd.c index 06283e0424..f16b1d79ed 100644 --- a/tests/server/socksd.c +++ b/tests/server/socksd.c @@ -209,9 +209,10 @@ static curl_socket_t socksconnect(unsigned short connectport, rc = connect(sock, &me.sa, sizeof(me.sa4)); if(rc) { + char errbuf[STRERROR_LEN]; int error = SOCKERRNO; - logmsg("Failed connecting to %s:%hu (%d) %s", - connectaddr, connectport, error, sstrerror(error)); + logmsg("Failed connecting to %s:%hu (%d) %s", connectaddr, connectport, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return CURL_SOCKET_BAD; } logmsg("Connected fine to %s:%d", connectaddr, connectport); @@ -621,6 +622,7 @@ static bool socksd_incoming(curl_socket_t listenfd) int i; ssize_t rc; int error = 0; + char errbuf[STRERROR_LEN]; curl_socket_t sockfd = listenfd; int maxfd = (int)sockfd; @@ -676,7 +678,7 @@ static bool socksd_incoming(curl_socket_t listenfd) if(rc < 0) { logmsg("select() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return FALSE; } @@ -685,7 +687,7 @@ static bool socksd_incoming(curl_socket_t listenfd) if(CURL_SOCKET_BAD == newfd) { error = SOCKERRNO; logmsg("accept() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); } else { curl_socket_t remotefd; @@ -738,6 +740,7 @@ static int test_socksd(int argc, char *argv[]) int wroteportfile = 0; bool juggle_again; int error; + char errbuf[STRERROR_LEN]; int arg = 1; const char *unix_socket = NULL; @@ -870,7 +873,7 @@ static int test_socksd(int argc, char *argv[]) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; logmsg("Error creating socket (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto socks5_cleanup; } @@ -922,7 +925,8 @@ socks5_cleanup: #ifdef USE_UNIX_SOCKETS if(unlink_socket && socket_domain == AF_UNIX && unix_socket) { error = unlink(unix_socket); - logmsg("unlink(%s) = %d (%s)", unix_socket, error, strerror(error)); + logmsg("unlink(%s) = %d (%s)", unix_socket, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); } #endif diff --git a/tests/server/sws.c b/tests/server/sws.c index 32e82891fe..1fbbb3c247 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -204,8 +204,10 @@ static int sws_parse_servercmd(struct sws_httprequest *req) req->connmon = FALSE; if(!stream) { + char errbuf[STRERROR_LEN]; error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg(" Couldn't open test file %ld", req->testno); req->open = FALSE; /* closes connection */ return 1; /* done */ @@ -706,6 +708,7 @@ static void sws_storerequest(const char *reqbuf, size_t totalsize) { int res; int error = 0; + char errbuf[STRERROR_LEN]; size_t written; size_t writeleft; FILE *dump; @@ -724,8 +727,8 @@ static void sws_storerequest(const char *reqbuf, size_t totalsize) /* !checksrc! disable ERRNOVAR 1 */ } while(!dump && ((error = errno) == EINTR)); if(!dump) { - logmsg("[2] Error opening file %s error (%d) %s", - dumpfile, error, strerror(error)); + logmsg("[2] Error opening file %s error (%d) %s", dumpfile, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Failed to write request input "); return; } @@ -744,8 +747,8 @@ static void sws_storerequest(const char *reqbuf, size_t totalsize) if(writeleft == 0) logmsg("Wrote request (%zu bytes) input to %s", totalsize, dumpfile); else if(writeleft > 0) { - logmsg("Error writing file %s error (%d) %s", - dumpfile, error, strerror(error)); + logmsg("Error writing file %s error (%d) %s", dumpfile, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s", totalsize-writeleft, totalsize, dumpfile); } @@ -754,8 +757,8 @@ storerequest_cleanup: res = fclose(dump); if(res) - logmsg("Error closing file %s error (%d) %s", - dumpfile, errno, strerror(errno)); + logmsg("Error closing file %s error (%d) %s", dumpfile, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); } static void init_httprequest(struct sws_httprequest *req) @@ -883,12 +886,14 @@ static int sws_get_request(curl_socket_t sock, struct sws_httprequest *req) fail = 1; } else if(got < 0) { + char errbuf[STRERROR_LEN]; int error = SOCKERRNO; if(EAGAIN == error || SOCKEWOULDBLOCK == error) { /* nothing to read at the moment */ return 0; } - logmsg("recv() returned error (%d) %s", error, sstrerror(error)); + logmsg("recv() returned error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); fail = 1; } if(fail) { @@ -947,6 +952,7 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req) bool sendfailure = FALSE; size_t responsesize; int error = 0; + char errbuf[STRERROR_LEN]; int res; static char weare[256]; char responsedump[256]; @@ -1027,7 +1033,8 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req) stream = test2fopen(req->testno, logdir); if(!stream) { error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return 0; } else { @@ -1049,7 +1056,8 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req) stream = test2fopen(req->testno, logdir); if(!stream) { error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); free(ptr); return 0; } @@ -1088,7 +1096,8 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req) dump = fopen(responsedump, "ab"); if(!dump) { error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg(" [5] Error opening file '%s'", responsedump); free(ptr); free(cmd); @@ -1140,8 +1149,8 @@ retry: res = fclose(dump); if(res) - logmsg("Error closing file %s error (%d) %s", - responsedump, errno, strerror(errno)); + logmsg("Error closing file %s error (%d) %s", responsedump, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); if(got_exit_signal) { free(ptr); @@ -1181,7 +1190,7 @@ retry: /* should not happen */ error = SOCKERRNO; logmsg("curlx_wait_ms() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); break; } } @@ -1212,6 +1221,7 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port) srvr_sockaddr_union_t serveraddr; curl_socket_t serverfd; int error; + char errbuf[STRERROR_LEN]; int rc = 0; const char *op_br = ""; const char *cl_br = ""; @@ -1234,7 +1244,7 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port) if(CURL_SOCKET_BAD == serverfd) { error = SOCKERRNO; logmsg("Error creating socket for server connection (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return CURL_SOCKET_BAD; } @@ -1254,7 +1264,7 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port) if(curlx_nonblock(serverfd, TRUE)) { error = SOCKERRNO; logmsg("curlx_nonblock(TRUE) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(serverfd); return CURL_SOCKET_BAD; } @@ -1336,8 +1346,8 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port) } } error: - logmsg("Error connecting to server port %hu (%d) %s", - port, error, sstrerror(error)); + logmsg("Error connecting to server port %hu (%d) %s", port, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(serverfd); return CURL_SOCKET_BAD; } @@ -1348,7 +1358,7 @@ success: if(curlx_nonblock(serverfd, FALSE)) { error = SOCKERRNO; logmsg("curlx_nonblock(FALSE) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(serverfd); return CURL_SOCKET_BAD; } @@ -1812,6 +1822,7 @@ static curl_socket_t accept_connection(curl_socket_t sock) { curl_socket_t msgsock = CURL_SOCKET_BAD; int error; + char errbuf[STRERROR_LEN]; int flag = 1; if(MAX_SOCKETS == num_sockets) { @@ -1834,14 +1845,14 @@ static curl_socket_t accept_connection(curl_socket_t sock) return 0; } logmsg("MAJOR ERROR, accept() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return CURL_SOCKET_BAD; } if(curlx_nonblock(msgsock, TRUE)) { error = SOCKERRNO; logmsg("curlx_nonblock failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(msgsock); return CURL_SOCKET_BAD; } @@ -1850,7 +1861,7 @@ static curl_socket_t accept_connection(curl_socket_t sock) (void *)&flag, sizeof(flag))) { error = SOCKERRNO; logmsg("setsockopt(SO_KEEPALIVE) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(msgsock); return CURL_SOCKET_BAD; } @@ -1977,6 +1988,7 @@ static int test_sws(int argc, char *argv[]) struct sws_httprequest *req = NULL; int rc = 0; int error; + char errbuf[STRERROR_LEN]; int arg = 1; const char *connecthost = "127.0.0.1"; char port_str[11]; @@ -2152,7 +2164,8 @@ static int test_sws(int argc, char *argv[]) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; - logmsg("Error creating socket (%d) %s", error, sstrerror(error)); + logmsg("Error creating socket (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto sws_cleanup; } @@ -2161,13 +2174,13 @@ static int test_sws(int argc, char *argv[]) (void *)&flag, sizeof(flag))) { error = SOCKERRNO; logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto sws_cleanup; } if(curlx_nonblock(sock, TRUE)) { error = SOCKERRNO; logmsg("curlx_nonblock failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto sws_cleanup; } @@ -2197,12 +2210,12 @@ static int test_sws(int argc, char *argv[]) error = SOCKERRNO; #ifdef USE_UNIX_SOCKETS if(socket_domain == AF_UNIX) - logmsg("Error binding socket on path %s (%d) %s", - unix_socket, error, sstrerror(error)); + logmsg("Error binding socket on path %s (%d) %s", unix_socket, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); else #endif - logmsg("Error binding socket on port %hu (%d) %s", - port, error, sstrerror(error)); + logmsg("Error binding socket on port %hu (%d) %s", port, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto sws_cleanup; } @@ -2223,7 +2236,7 @@ static int test_sws(int argc, char *argv[]) if(getsockname(sock, &localaddr.sa, &la_size) < 0) { error = SOCKERRNO; logmsg("getsockname() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); goto sws_cleanup; } @@ -2261,7 +2274,8 @@ static int test_sws(int argc, char *argv[]) rc = listen(sock, 50); if(rc) { error = SOCKERRNO; - logmsg("listen() failed with error (%d) %s", error, sstrerror(error)); + logmsg("listen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto sws_cleanup; } @@ -2341,7 +2355,8 @@ static int test_sws(int argc, char *argv[]) if(rc < 0) { error = SOCKERRNO; - logmsg("select() failed with error (%d) %s", error, sstrerror(error)); + logmsg("select() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); goto sws_cleanup; } @@ -2446,7 +2461,8 @@ sws_cleanup: #ifdef USE_UNIX_SOCKETS if(unlink_socket && socket_domain == AF_UNIX && unix_socket) { rc = unlink(unix_socket); - logmsg("unlink(%s) = %d (%s)", unix_socket, rc, strerror(rc)); + logmsg("unlink(%s) = %d (%s)", unix_socket, + rc, curlx_strerror(rc, errbuf, sizeof(errbuf))); } #endif diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c index 4c37685ab7..64f005dfe5 100644 --- a/tests/server/tftpd.c +++ b/tests/server/tftpd.c @@ -130,7 +130,7 @@ struct formats { struct errmsg { int e_code; - const char *e_msg; + char e_msg[STRERROR_LEN]; }; typedef union { @@ -174,7 +174,7 @@ static struct errmsg errmsgs[] = { { TFTP_EBADID, "Unknown transfer ID" }, { TFTP_EEXISTS, "File already exists" }, { TFTP_ENOUSER, "No such user" }, - { -1, 0 } + { -1, "" } }; static const struct formats formata[] = { @@ -548,6 +548,7 @@ static int test_tftpd(int argc, char **argv) int flag; int rc; int error; + char errbuf[STRERROR_LEN]; struct testcase test; int result = 0; srvr_sockaddr_union_t from; @@ -654,7 +655,8 @@ static int test_tftpd(int argc, char **argv) if(CURL_SOCKET_BAD == sock) { error = SOCKERRNO; - logmsg("Error creating socket (%d) %s", error, sstrerror(error)); + logmsg("Error creating socket (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); result = 1; goto tftpd_cleanup; } @@ -664,7 +666,7 @@ static int test_tftpd(int argc, char **argv) (void *)&flag, sizeof(flag))) { error = SOCKERRNO; logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); result = 1; goto tftpd_cleanup; } @@ -689,8 +691,8 @@ static int test_tftpd(int argc, char **argv) #endif /* USE_IPV6 */ if(rc) { error = SOCKERRNO; - logmsg("Error binding socket on port %hu (%d) %s", port, error, - sstrerror(error)); + logmsg("Error binding socket on port %hu (%d) %s", port, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); result = 1; goto tftpd_cleanup; } @@ -712,7 +714,7 @@ static int test_tftpd(int argc, char **argv) if(getsockname(sock, &localaddr.sa, &la_size) < 0) { error = SOCKERRNO; logmsg("getsockname() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); goto tftpd_cleanup; } @@ -898,8 +900,10 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size) /* Open request dump file. */ server = fopen(dumpfile, "ab"); if(!server) { + char errbuf[STRERROR_LEN]; int error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Error opening file '%s'", dumpfile); return -1; } @@ -1005,8 +1009,10 @@ static int tftpd_parse_servercmd(struct testcase *req) stream = test2fopen(req->testno, logdir); if(!stream) { + char errbuf[STRERROR_LEN]; error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg(" Couldn't open test file %ld", req->testno); return 1; /* done */ } @@ -1107,7 +1113,6 @@ static int validate_access(struct testcase *test, else partno = 0; - logmsg("requested test number %ld part %ld", testno, partno); test->testno = testno; @@ -1120,8 +1125,10 @@ static int validate_access(struct testcase *test, snprintf(partbuf, sizeof(partbuf), "data%ld", partno); if(!stream) { + char errbuf[STRERROR_LEN]; int error = errno; - logmsg("fopen() failed with error (%d) %s", error, strerror(error)); + logmsg("fopen() failed with error (%d) %s", + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Couldn't open test file for test: %ld", testno); return TFTP_EACCESS; } @@ -1356,7 +1363,7 @@ static void nak(int error) if(pe->e_code == error) break; if(pe->e_code < 0) { - pe->e_msg = strerror(error - 100); + curlx_strerror(error - 100, pe->e_msg, sizeof(pe->e_msg)); tp->th_code = TFTP_EUNDEF; /* set 'undef' errorcode */ } length = (int)strlen(pe->e_msg); diff --git a/tests/server/util.c b/tests/server/util.c index f4802745c8..5abd308738 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -123,9 +123,10 @@ void logmsg(const char *msg, ...) fclose(logfp); } else { + char errbuf[STRERROR_LEN]; int error = errno; fprintf(stderr, "fopen() failed with error (%d) %s\n", - error, strerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); fprintf(stderr, "Error opening file '%s'\n", serverlogfile); fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer); } @@ -189,15 +190,6 @@ int win32_init(void) atexit(win32_cleanup); return 0; } - -/* socket-safe strerror (works on Winsock errors, too) */ -const char *sstrerror(int err) -{ - static char buf[512]; - return curlx_winapi_strerror(err, buf, sizeof(buf)); -} -#else -#define sstrerror(e) strerror(e) #endif /* _WIN32 */ /* fopens the test case file */ @@ -246,7 +238,9 @@ int write_pidfile(const char *filename) pid = our_getpid(); pidfile = fopen(filename, "wb"); if(!pidfile) { - logmsg("Couldn't write pid file: %s %s", filename, strerror(errno)); + char errbuf[STRERROR_LEN]; + logmsg("Couldn't write pid file: %s (%d) %s", filename, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); return 0; /* fail */ } fprintf(pidfile, "%ld\n", (long)pid); @@ -260,7 +254,9 @@ int write_portfile(const char *filename, int port) { FILE *portfile = fopen(filename, "wb"); if(!portfile) { - logmsg("Couldn't write port file: %s %s", filename, strerror(errno)); + char errbuf[STRERROR_LEN]; + logmsg("Couldn't write port file: %s (%d) %s", filename, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); return 0; /* fail */ } fprintf(portfile, "%d\n", port); @@ -273,6 +269,7 @@ void set_advisor_read_lock(const char *filename) { FILE *lockfile; int error = 0; + char errbuf[STRERROR_LEN]; int res; do { @@ -280,15 +277,15 @@ void set_advisor_read_lock(const char *filename) /* !checksrc! disable ERRNOVAR 1 */ } while(!lockfile && ((error = errno) == EINTR)); if(!lockfile) { - logmsg("Error creating lock file %s error (%d) %s", - filename, error, strerror(error)); + logmsg("Error creating lock file %s error (%d) %s", filename, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return; } res = fclose(lockfile); if(res) - logmsg("Error closing lock file %s error (%d) %s", - filename, errno, strerror(errno)); + logmsg("Error closing lock file %s error (%d) %s", filename, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); } void clear_advisor_read_lock(const char *filename) @@ -306,9 +303,11 @@ void clear_advisor_read_lock(const char *filename) res = unlink(filename); /* !checksrc! disable ERRNOVAR 1 */ } while(res && ((error = errno) == EINTR)); - if(res) - logmsg("Error removing lock file %s error (%d) %s", - filename, error, strerror(error)); + if(res) { + char errbuf[STRERROR_LEN]; + logmsg("Error removing lock file %s error (%d) %s", filename, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); + } } /* vars used to keep around previous signal handlers */ @@ -566,6 +565,8 @@ static SIGHANDLER_T set_signal(int signum, SIGHANDLER_T handler, void install_signal_handlers(bool keep_sigalrm) { + char errbuf[STRERROR_LEN]; + (void)errbuf; #ifdef _WIN32 /* setup Windows exit event before any signal can trigger */ exit_event = CreateEvent(NULL, TRUE, FALSE, NULL); @@ -576,20 +577,23 @@ void install_signal_handlers(bool keep_sigalrm) /* ignore SIGHUP signal */ old_sighup_handler = set_signal(SIGHUP, SIG_IGN, FALSE); if(old_sighup_handler == SIG_ERR) - logmsg("cannot install SIGHUP handler: %s", strerror(errno)); + logmsg("cannot install SIGHUP handler: (%d) %s", + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); #endif #ifdef SIGPIPE /* ignore SIGPIPE signal */ old_sigpipe_handler = set_signal(SIGPIPE, SIG_IGN, FALSE); if(old_sigpipe_handler == SIG_ERR) - logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); + logmsg("cannot install SIGPIPE handler: (%d) %s", + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); #endif #ifdef SIGALRM if(!keep_sigalrm) { /* ignore SIGALRM signal */ old_sigalrm_handler = set_signal(SIGALRM, SIG_IGN, FALSE); if(old_sigalrm_handler == SIG_ERR) - logmsg("cannot install SIGALRM handler: %s", strerror(errno)); + logmsg("cannot install SIGALRM handler: (%d) %s", + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); } #else (void)keep_sigalrm; @@ -598,19 +602,22 @@ void install_signal_handlers(bool keep_sigalrm) /* handle SIGINT signal with our exit_signal_handler */ old_sigint_handler = set_signal(SIGINT, exit_signal_handler, TRUE); if(old_sigint_handler == SIG_ERR) - logmsg("cannot install SIGINT handler: %s", strerror(errno)); + logmsg("cannot install SIGINT handler: (%d) %s", + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); #endif #ifdef SIGTERM /* handle SIGTERM signal with our exit_signal_handler */ old_sigterm_handler = set_signal(SIGTERM, exit_signal_handler, TRUE); if(old_sigterm_handler == SIG_ERR) - logmsg("cannot install SIGTERM handler: %s", strerror(errno)); + logmsg("cannot install SIGTERM handler: (%d) %s", + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); #endif #if defined(SIGBREAK) && defined(_WIN32) /* handle SIGBREAK signal with our exit_signal_handler */ old_sigbreak_handler = set_signal(SIGBREAK, exit_signal_handler, TRUE); if(old_sigbreak_handler == SIG_ERR) - logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); + logmsg("cannot install SIGBREAK handler: (%d) %s", + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); #endif #ifdef _WIN32 #ifndef UNDER_CE @@ -687,6 +694,7 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket, struct sockaddr_un *sau) { int error; + char errbuf[STRERROR_LEN]; int rc; size_t len = strlen(unix_socket); @@ -703,8 +711,8 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket, /* socket already exists. Perhaps it is stale? */ curl_socket_t unixfd = socket(AF_UNIX, SOCK_STREAM, 0); if(CURL_SOCKET_BAD == unixfd) { - logmsg("Failed to create socket at %s (%d) %s", - unix_socket, SOCKERRNO, sstrerror(SOCKERRNO)); + logmsg("Failed to create socket at %s (%d) %s", unix_socket, + SOCKERRNO, curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); return -1; } /* check whether the server is alive */ @@ -712,8 +720,8 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket, error = SOCKERRNO; sclose(unixfd); if(rc && error != SOCKECONNREFUSED) { - logmsg("Failed to connect to %s (%d) %s", - unix_socket, error, sstrerror(error)); + logmsg("Failed to connect to %s (%d) %s", unix_socket, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); return rc; } /* socket server is not alive, now check if it was actually a socket. */ @@ -724,8 +732,8 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket, rc = lstat(unix_socket, &statbuf); #endif if(rc) { - logmsg("Error binding socket, failed to stat %s (%d) %s", - unix_socket, errno, strerror(errno)); + logmsg("Error binding socket, failed to stat %s (%d) %s", unix_socket, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); return rc; } #ifdef S_IFSOCK @@ -737,8 +745,8 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket, /* dead socket, cleanup and retry bind */ rc = unlink(unix_socket); if(rc) { - logmsg("Error binding socket, failed to unlink %s (%d) %s", - unix_socket, errno, strerror(errno)); + logmsg("Error binding socket, failed to unlink %s (%d) %s", unix_socket, + errno, curlx_strerror(errno, errbuf, sizeof(errbuf))); return rc; } /* stale socket is gone, retry bind */ @@ -762,6 +770,7 @@ curl_socket_t sockdaemon(curl_socket_t sock, int delay = 20; int attempt = 0; int error = 0; + char errbuf[STRERROR_LEN]; #ifndef USE_UNIX_SOCKETS (void)unix_socket; @@ -775,14 +784,14 @@ curl_socket_t sockdaemon(curl_socket_t sock, if(rc) { error = SOCKERRNO; logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); if(maxretr) { rc = curlx_wait_ms(delay); if(rc) { /* should not happen */ error = SOCKERRNO; logmsg("curlx_wait_ms() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); return CURL_SOCKET_BAD; } @@ -799,7 +808,8 @@ curl_socket_t sockdaemon(curl_socket_t sock, if(rc) { logmsg("setsockopt(SO_REUSEADDR) failed %d times in %d ms. Error (%d) %s", - attempt, totdelay, error, strerror(error)); + attempt, totdelay, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); logmsg("Continuing anyway..."); } @@ -836,12 +846,12 @@ curl_socket_t sockdaemon(curl_socket_t sock, error = SOCKERRNO; #ifdef USE_UNIX_SOCKETS if(socket_domain == AF_UNIX) - logmsg("Error binding socket on path %s (%d) %s", - unix_socket, error, sstrerror(error)); + logmsg("Error binding socket on path %s (%d) %s", unix_socket, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); else #endif - logmsg("Error binding socket on port %hu (%d) %s", - *listenport, error, sstrerror(error)); + logmsg("Error binding socket on port %hu (%d) %s", *listenport, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); return CURL_SOCKET_BAD; } @@ -865,7 +875,7 @@ curl_socket_t sockdaemon(curl_socket_t sock, if(getsockname(sock, &localaddr.sa, &la_size) < 0) { error = SOCKERRNO; logmsg("getsockname() failed with error (%d) %s", - error, sstrerror(error)); + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); return CURL_SOCKET_BAD; } @@ -902,8 +912,8 @@ curl_socket_t sockdaemon(curl_socket_t sock, rc = listen(sock, 5); if(rc) { error = SOCKERRNO; - logmsg("listen(%ld, 5) failed with error (%d) %s", - (long)sock, error, sstrerror(error)); + logmsg("listen(%ld, 5) failed with error (%d) %s", (long)sock, + error, curlx_strerror(error, errbuf, sizeof(errbuf))); sclose(sock); return CURL_SOCKET_BAD; }