fconfigure -sockname fails with "invalid argument" after a failed async connect
This makes socket-14.17 fail intermittently on DragonFly 6.4: if both ::1 and 127.0.0.1 are refused before fconfigure runs, the connect is no longer pending. A loop of socket -async localhost $closedport followed by fconfigure -sockname failed 144 times in 2000 iterations on 8.6 and 9.0 and 120 times on 9.1 in one session, and 1 to 4 times in 2000 in another.
Also, in socket test mode (testsocket testflags $s 1), fconfigure -sockname or -peername on a pending async socket dereferences a NULL errorCodePtr in WaitForConnect() and crashes.
getsockname() on a socket whose non-blocking connect to a closed loopback port was refused:
| System | getsockname() | getpeername() |
|---|---|---|
| Linux 7.0 | local address and port | ENOTCONN |
| FreeBSD 15.1 | local address and port | ENOTCONN |
| Windows 11 | wildcard address and a port | WSAENOTCONN |
| OpenIndiana | wildcard address, port 0 | ENOTCONN |
| NetBSD 10.1 | EINVAL | ENOTCONN |
| OpenBSD 7.9 | EINVAL | ENOTCONN |
| DragonFly 6.4 | ECONNRESET (IPv4), EINVAL (IPv6) | ENOTCONN |
So after a failed async connect, -sockname returns a meaningless address on some systems and raises an error on others, and on the latter plain fconfigure $s fails too. -peername raises an error everywhere.
Possible fixes: raise an error on all systems, as -peername already does and -sockname does on the BSDs, but skip it for plain fconfigure $s; or return "", as while the connect is pending, which no system does now. With the error, socket-14.16 and 14.17 would still need to accept either result. win/tclWinSock.c has the same structure.