Fix possible socket leak.

This would occur if a client was using `mosquitto_loop_start()`, then if
the connection failed due to the remote server being inaccessible they
called `mosquitto_loop_stop(, true)` and recreated the mosquitto object.

See: https://www.eclipse.org/forums/index.php?t=rview&goto=1839865#msg_1839865
This commit is contained in:
Roger Light 2021-04-02 11:02:13 +01:00
parent 117e59b7cf
commit 6ebbb4d654
2 changed files with 7 additions and 4 deletions

View File

@ -10,6 +10,12 @@ Clients:
and mosquitto_rr, to avoid potentially lost messages. Closes #2134.
- Fix TLS-PSK mode not working with port 8883. Closes #2152.
Client library:
- Fix possible socket leak. This would occur if a client was using
`mosquitto_loop_start()`, then if the connection failed due to the remote
server being inaccessible they called `mosquitto_loop_stop(, true)` and
recreated the mosquitto object.
Build:
- A variety of minor build related fixes, like functions not having previous
declarations.

View File

@ -917,16 +917,13 @@ int net__socket_connect_step3(struct mosquitto *mosq, const char *host)
/* Create a socket and connect it to 'ip' on port 'port'. */
int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port, const char *bind_address, bool blocking)
{
mosq_sock_t sock = INVALID_SOCKET;
int rc, rc2;
if(!mosq || !host) return MOSQ_ERR_INVAL;
rc = net__try_connect(host, port, &sock, bind_address, blocking);
rc = net__try_connect(host, port, &mosq->sock, bind_address, blocking);
if(rc > 0) return rc;
mosq->sock = sock;
if(mosq->tcp_nodelay){
int flag = 1;
if(setsockopt(mosq->sock, IPPROTO_TCP, TCP_NODELAY, (const void*)&flag, sizeof(int)) != 0){