Skip to content

Commit f64b9ec

Browse files
rimruldscho
authored andcommitted
compat/mingw: handle WSA errors in strerror
We map WSAGetLastError() errors to errno errors in winsock_error_to_errno(), but the MSVC strerror() implementation only produces "Unknown error" for most of them. Produce some more meaningful error messages in these cases. Our builds for ARM64 link against the newer UCRT strerror() that does know these errors, so we won't change the strerror() used there. The wording of the messages is copied from glibc strerror() messages. Reported-by: M Hickford <mirth.hickford@gmail.com> Signed-off-by: Matthias Aßhauer <mha1993@live.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 2a8b254 commit f64b9ec

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
13391339

13401340
CLAR_TEST_SUITES += u-ctype
13411341
CLAR_TEST_SUITES += u-strvec
1342+
CLAR_TEST_SUITES += u-mingw
13421343
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13431344
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
13441345
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o

compat/mingw.c

+85
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,91 @@ static inline int winsock_return(int ret)
22402240

22412241
#define WINSOCK_RETURN(x) do { return winsock_return(x); } while (0)
22422242

2243+
#undef strerror
2244+
char *mingw_strerror(int errnum)
2245+
{
2246+
static char buf[41] ="";
2247+
switch (errnum) {
2248+
case EWOULDBLOCK:
2249+
xsnprintf(buf, 41, "%s", "Operation would block");
2250+
break;
2251+
case EINPROGRESS:
2252+
xsnprintf(buf, 41, "%s", "Operation now in progress");
2253+
break;
2254+
case EALREADY:
2255+
xsnprintf(buf, 41, "%s", "Operation already in progress");
2256+
break;
2257+
case ENOTSOCK:
2258+
xsnprintf(buf, 41, "%s", "Socket operation on non-socket");
2259+
break;
2260+
case EDESTADDRREQ:
2261+
xsnprintf(buf, 41, "%s", "Destination address required");
2262+
break;
2263+
case EMSGSIZE:
2264+
xsnprintf(buf, 41, "%s", "Message too long");
2265+
break;
2266+
case EPROTOTYPE:
2267+
xsnprintf(buf, 41, "%s", "Protocol wrong type for socket");
2268+
break;
2269+
case ENOPROTOOPT:
2270+
xsnprintf(buf, 41, "%s", "Protocol not available");
2271+
break;
2272+
case EPROTONOSUPPORT:
2273+
xsnprintf(buf, 41, "%s", "Protocol not supported");
2274+
break;
2275+
case EOPNOTSUPP:
2276+
xsnprintf(buf, 41, "%s", "Operation not supported");
2277+
break;
2278+
case EAFNOSUPPORT:
2279+
xsnprintf(buf, 41, "%s", "Address family not supported by protocol");
2280+
break;
2281+
case EADDRINUSE:
2282+
xsnprintf(buf, 41, "%s", "Address already in use");
2283+
break;
2284+
case EADDRNOTAVAIL:
2285+
xsnprintf(buf, 41, "%s", "Cannot assign requested address");
2286+
break;
2287+
case ENETDOWN:
2288+
xsnprintf(buf, 41, "%s", "Network is down");
2289+
break;
2290+
case ENETUNREACH:
2291+
xsnprintf(buf, 41, "%s", "Network is unreachable");
2292+
break;
2293+
case ENETRESET:
2294+
xsnprintf(buf, 41, "%s", "Network dropped connection on reset");
2295+
break;
2296+
case ECONNABORTED:
2297+
xsnprintf(buf, 41, "%s", "Software caused connection abort");
2298+
break;
2299+
case ECONNRESET:
2300+
xsnprintf(buf, 41, "%s", "Connection reset by peer");
2301+
break;
2302+
case ENOBUFS:
2303+
xsnprintf(buf, 41, "%s", "No buffer space available");
2304+
break;
2305+
case EISCONN:
2306+
xsnprintf(buf, 41, "%s", "Transport endpoint is already connected");
2307+
break;
2308+
case ENOTCONN:
2309+
xsnprintf(buf, 41, "%s", "Transport endpoint is not connected");
2310+
break;
2311+
case ETIMEDOUT:
2312+
xsnprintf(buf, 41, "%s", "Connection timed out");
2313+
break;
2314+
case ECONNREFUSED:
2315+
xsnprintf(buf, 41, "%s", "Connection refused");
2316+
break;
2317+
case ELOOP:
2318+
xsnprintf(buf, 41, "%s", "Too many levels of symbolic links");
2319+
break;
2320+
case EHOSTUNREACH:
2321+
xsnprintf(buf, 41, "%s", "No route to host");
2322+
break;
2323+
default: return strerror(errnum);
2324+
}
2325+
return buf;
2326+
}
2327+
22432328
#undef gethostname
22442329
int mingw_gethostname(char *name, int namelen)
22452330
{

compat/mingw.h

+5
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ int mingw_socket(int domain, int type, int protocol);
311311
int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
312312
#define connect mingw_connect
313313

314+
char *mingw_strerror(int errnum);
315+
#ifndef _UCRT
316+
#define strerror mingw_strerror
317+
#endif
318+
314319
int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);
315320
#define bind mingw_bind
316321

t/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
clar_test_suites = [
22
'unit-tests/u-ctype.c',
3+
'unit-tests/u-mingw.c',
34
'unit-tests/u-strvec.c',
45
]
56

t/unit-tests/u-mingw.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "unit-test.h"
2+
3+
#if defined(GIT_WINDOWS_NATIVE) && !defined(_UCRT)
4+
#undef strerror
5+
int errnos_contains(int);
6+
static int errnos [53]={
7+
/* errnos in err_win_to_posix */
8+
EACCES, EBUSY, EEXIST, ERANGE, EIO, ENODEV, ENXIO, ENOEXEC, EINVAL, ENOENT,
9+
EPIPE, ENAMETOOLONG, ENOSYS, ENOTEMPTY, ENOSPC, EFAULT, EBADF, EPERM, EINTR,
10+
E2BIG, ESPIPE, ENOMEM, EXDEV, EAGAIN, ENFILE, EMFILE, ECHILD, EROFS,
11+
/* errnos only in winsock_error_to_errno */
12+
EWOULDBLOCK, EINPROGRESS, EALREADY, ENOTSOCK, EDESTADDRREQ, EMSGSIZE,
13+
EPROTOTYPE, ENOPROTOOPT, EPROTONOSUPPORT, EOPNOTSUPP, EAFNOSUPPORT,
14+
EADDRINUSE, EADDRNOTAVAIL, ENETDOWN, ENETUNREACH, ENETRESET, ECONNABORTED,
15+
ECONNRESET, ENOBUFS, EISCONN, ENOTCONN, ETIMEDOUT, ECONNREFUSED, ELOOP,
16+
EHOSTUNREACH
17+
};
18+
19+
int errnos_contains(int errnum)
20+
{
21+
for(int i=0;i<53;i++)
22+
if(errnos[i]==errnum)
23+
return 1;
24+
return 0;
25+
}
26+
#endif
27+
28+
void test_mingw__no_strerror_shim_on_ucrt(void)
29+
{
30+
#if defined(GIT_WINDOWS_NATIVE) && defined(_UCRT)
31+
cl_assert_(strerror != mingw_strerror,
32+
"mingw_strerror is unnescessary when building against UCRT");
33+
#else
34+
cl_skip();
35+
#endif
36+
}
37+
38+
void test_mingw__strerror(void)
39+
{
40+
#if defined(GIT_WINDOWS_NATIVE) && !defined(_UCRT)
41+
for(int i=0;i<53;i++)
42+
{
43+
char *crt;
44+
char *mingw;
45+
mingw = mingw_strerror(errnos[i]);
46+
crt = strerror(errnos[i]);
47+
cl_assert_(!strcasestr(mingw, "unknown error"),
48+
"mingw_strerror should know all errno values we care about");
49+
if(!strcasestr(crt, "unknown error"))
50+
cl_assert_equal_s(crt,mingw);
51+
}
52+
#else
53+
cl_skip();
54+
#endif
55+
}
56+
57+
void test_mingw__errno_translation(void)
58+
{
59+
#if defined(GIT_WINDOWS_NATIVE) && !defined(_UCRT)
60+
/* GetLastError() return values are currently defined from 0 to 15841,
61+
testing up to 20000 covers some room for future expansion */
62+
for (int i=0;i<20000;i++)
63+
{
64+
if(i!=ERROR_SUCCESS)
65+
cl_assert_(errnos_contains(err_win_to_posix(i)),
66+
"all err_win_to_posix return values should be tested against mingw_strerror");
67+
/* ideally we'd test the same for winsock_error_to_errno, but it's static */
68+
}
69+
#else
70+
cl_skip();
71+
#endif
72+
}

0 commit comments

Comments
 (0)