Skip to content

Commit

Permalink
Fix possible socket leak.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ralight committed Apr 2, 2021
1 parent 117e59b commit 6ebbb4d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.txt
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions lib/net_mosq.c
Expand Up @@ -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){
Expand Down

0 comments on commit 6ebbb4d

Please sign in to comment.