From 5b73897f9892918f6148ea97bfa6ea998f196927 Mon Sep 17 00:00:00 2001 From: Zard1096 Date: Tue, 11 Jul 2017 13:08:38 +0800 Subject: [PATCH] Fix iOS crash issues Relate to issues #327 and #63. mosq->sock may be closed before FD_SET(mosq->sock, &writefds) and FD_ISSET(mosq->sock, &writefds) but after judgement in line 947 if(mosq->sock != INVALID_SOCKET). FD_SET(-1, ...) and FD_ISSET(-1, ...) would certainly crash. Signed-off-by: Zard1096 --- lib/mosquitto.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/mosquitto.c b/lib/mosquitto.c index be8e62e5..61ffdd87 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -971,9 +971,10 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) /* Fake write possible, to stimulate output write even though * we didn't ask for it, because at that point the publish or * other command wasn't present. */ - FD_SET(mosq->sock, &writefds); + if(mosq->sock != INVALID_SOCKET) + FD_SET(mosq->sock, &writefds); } - if(FD_ISSET(mosq->sock, &writefds)){ + if(mosq->sock != INVALID_SOCKET && FD_ISSET(mosq->sock, &writefds)){ #ifdef WITH_TLS if(mosq->want_connect){ rc = mosquitto__socket_connect_tls(mosq);