]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
fix read of 0 bytes problem and make buffer null terminated before using strstr
authorSeven Du <dujinfang@gmail.com>
Tue, 27 Jan 2015 15:14:18 +0000 (23:14 +0800)
committerSeven Du <dujinfang@gmail.com>
Tue, 27 Jan 2015 15:18:52 +0000 (23:18 +0800)
on debian 7, ws_raw_read returns 0 when client side disconnect the socket,
no HUP is detected and it resulted in an infinite loop.

src/mod/endpoints/mod_verto/mod_verto.c

index 9c5272bad9ae5d9befd1b7ddf6e648681ae406e0..de5f2e4aa46447b0493e89b5606f69391a071cdb 100644 (file)
@@ -1713,14 +1713,28 @@ done:
                        if (pflags & SWITCH_POLL_READ) {
                                ssize_t bytes;
 
-                               bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, wsh->buflen - wsh->datalen, wsh->block);
+                               bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, wsh->buflen - wsh->datalen - 1, wsh->block);
 
                                if (bytes < 0) {
                                        die("BAD READ %" SWITCH_SIZE_T_FMT "\n", bytes);
                                        break;
                                }
 
+                               if (bytes == 0) {
+                                       bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, wsh->buflen - wsh->datalen - 1, wsh->block);
+
+                                       if (bytes < 0) {
+                                               die("BAD READ %" SWITCH_SIZE_T_FMT "\n", bytes);
+                                               break;
+                                       }
+
+                                       if (bytes == 0) { // socket broken ?
+                                               break;
+                                       }
+                               }
+
                                wsh->datalen += bytes;
+                               *(wsh->buffer + wsh->datalen) = '\0';
 
                                if (strstr(wsh->buffer, "\r\n\r\n") || strstr(wsh->buffer, "\n\n")) {
                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "socket %s is going to handle a new request\n", jsock->name);