1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

Reused address info struct incorrectly

Wrongly assumed that `ai` was done being used before binding, it's not,
so create a separate address info for binding...
This commit is contained in:
llmII 2021-09-06 10:44:23 -05:00
parent b270d88427
commit 470e8f6fc7
No known key found for this signature in database
GPG Key ID: E3AD2E259F58A9A0

View File

@ -420,17 +420,18 @@ JANET_CORE_FN(cfun_net_connect,
}
}
ai = NULL;
/* Check if we're binding address */
struct addrinfo *binding = NULL;
if (argc >= 3 && is_unix == 0) {
if (argc == 4 || !janet_checktype(argv[2], JANET_KEYWORD)) {
ai = janet_get_addrinfo(argv, 0, socktype, 0, &is_unix, 1);
binding = janet_get_addrinfo(argv, 0, socktype, 0, &is_unix, 1);
}
}
if (ai != NULL) {
if (binding != NULL) {
/* Check all addrinfos in a loop for the first that we can bind to. */
struct addrinfo *rp = NULL;
for (rp = ai; rp != NULL; rp = rp->ai_next) {
for (rp = binding; rp != NULL; rp = rp->ai_next) {
#ifdef JANET_WINDOWS
sock = WSASocketW(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
#else
@ -442,7 +443,7 @@ JANET_CORE_FN(cfun_net_connect,
if (bind(sock, rp->ai_addr, (int) rp->ai_addrlen) == 0) break;
JSOCKCLOSE(sock);
}
freeaddrinfo(ai);
freeaddrinfo(binding);
if (NULL == rp) {
janet_panic("could not bind outgoing address");
}