res_http_websocket: Fix faulty read logic.
authorMark Michelson <mmichelson@digium.com>
Tue, 7 Mar 2017 19:38:17 +0000 (13:38 -0600)
committerMark Michelson <mmichelson@digium.com>
Tue, 7 Mar 2017 19:38:17 +0000 (13:38 -0600)
When doing some WebRTC testing, I found that the websocket would
disconnect whenever I attempted to place a call into Asterisk. After
looking into it, I pinpointed the problem to be due to the iostreams
change being merged in.

Under certain circumstances, a call to ast_iostream_read() can return a
negative value. However, in this circumstance, the websocket code was
treating this negative return as if it were a partial read from the
websocket. The expected length would get adjusted by this negative
value, resulting in the expected length being too large.

This patch simply adds an if check to be sure that we are only updating
the expected length of a read when the return from a read is positive.

ASTERISK-26842 #close
Reported by Mark Michelson

Change-Id: Ib4423239828a013d27d7bc477d317d2f02db61ab

res/res_http_websocket.c

index 8413823..799eb84 100644 (file)
@@ -529,10 +529,12 @@ static inline int ws_safe_read(struct ast_websocket *session, char *buf, int len
                                return -1;
                        }
                }
-               xlen = xlen - rlen;
-               rbuf = rbuf + rlen;
-               if (!xlen) {
-                       break;
+               if (rlen > 0) {
+                       xlen = xlen - rlen;
+                       rbuf = rbuf + rlen;
+                       if (!xlen) {
+                               break;
+                       }
                }
                if (ast_wait_for_input(ast_iostream_get_fd(session->stream), 1000) < 0) {
                        ast_log(LOG_ERROR, "ast_wait_for_input returned err: %s\n", strerror(errno));