From e4a4b6f260a3fa4530a888f7730447d3dfbf99af Mon Sep 17 00:00:00 2001
From: Alexander Alashkin <alexander.alashkin@cesanta.com>
Date: Wed, 16 Nov 2016 14:08:22 +0200
Subject: [PATCH] Fix coredump in mg_tun_destroy_client

PUBLISHED_FROM=56ff5afe805e4680e02183a8c3887ea60ee5ebfe
---
 mongoose.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 91a76d345..fef0251b1 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -11342,10 +11342,24 @@ static struct mg_tun_client *mg_tun_create_client(struct mg_mgr *mgr,
 }
 
 void mg_tun_destroy_client(struct mg_tun_client *client) {
-  /* the dispatcher connection handler will in turn close all tunnels */
-  client->disp->flags |= MG_F_CLOSE_IMMEDIATELY;
-  /* this is used as a signal to other tun handlers that the party is over */
-  client->disp->user_data = client->iface->data = NULL;
+  /*
+   *  NOTE:
+   * `client` is NULL in case of OOM
+   * `client->disp` is NULL if connection failed
+   * `client->iface is NULL is `mg_find_iface` failed
+   */
+
+  if (client != NULL && client->disp != NULL) {
+    /* the dispatcher connection handler will in turn close all tunnels */
+    client->disp->flags |= MG_F_CLOSE_IMMEDIATELY;
+    /* this is used as a signal to other tun handlers that the party is over */
+    client->disp->user_data = NULL;
+  }
+
+  if (client != NULL && client->iface != NULL) {
+    client->iface->data = NULL;
+  }
+
   MG_FREE(client);
 }
 
-- 
GitLab