From d02ffcd15482a7feaed11d9109ae578eda3734dd Mon Sep 17 00:00:00 2001
From: Yves Rutschle <git1@rutschle.net>
Date: Sat, 20 Jul 2013 00:45:33 +0200
Subject: [PATCH] Fixed bug in sslh-select: if socket dropped while
 defered_data was present, sslh-select would crash.

---
 ChangeLog     |  4 ++++
 sslh-select.c | 13 +++++++------
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c7ddd03..fd31dcd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+vNEXT:
+	Fixed bug in sslh-select: if socket dropped while
+	defered_data was present, sslh-select would crash.
+
 v1.14: 21DEC2012
 	Corrected OpenVPN probe to support pre-shared secret
 	mode (OpenVPN port-sharing code is... wrong). Thanks
diff --git a/sslh-select.c b/sslh-select.c
index bdde468..c59c5bf 100644
--- a/sslh-select.c
+++ b/sslh-select.c
@@ -261,12 +261,13 @@ void main_loop(int listen_sockets[], int num_addr_listen)
                             tidy_connection(&cnx[i], &fds_r, &fds_w);
                             if (verbose)
                                 fprintf(stderr, "closed slot %d\n", i);
-                        }
-                        /* If no defered data is left, stop monitoring the fd 
-                         * for write, and restart monitoring the other one for reads*/
-                        if (!cnx[i].q[j].defered_data_size) {
-                            FD_CLR(cnx[i].q[j].fd, &fds_w);
-                            FD_SET(cnx[i].q[1-j].fd, &fds_r);
+                        } else {
+                            /* If no defered data is left, stop monitoring the fd 
+                             * for write, and restart monitoring the other one for reads*/
+                            if (!cnx[i].q[j].defered_data_size) {
+                                FD_CLR(cnx[i].q[j].fd, &fds_w);
+                                FD_SET(cnx[i].q[1-j].fd, &fds_r);
+                            }
                         }
                     }
                 }