From c7f8c3bdf48f2c0a65a1d25afd39ae7078fde32f Mon Sep 17 00:00:00 2001 From: Fernando Governatore Date: Thu, 13 Oct 2016 21:57:25 -0300 Subject: [PATCH] Websockets - Avoids trying to use an empty buffer Sometimes, in a SSL secure connection, the receiveCallback is called with an empty buffer and len == 0. At those times, the websocket client gets confused. At connection time, it may not receive the Upgrade answer and disconnect. At all other times, it would try to accumulate the payload until it exaust all memory and fails. --- app/websocket/websocketclient.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/websocket/websocketclient.c b/app/websocket/websocketclient.c index d9fa29ce7c..db7c4d3b29 100644 --- a/app/websocket/websocketclient.c +++ b/app/websocket/websocketclient.c @@ -257,6 +257,10 @@ static void ws_receiveCallback(void *arg, char *buf, unsigned short len) { os_timer_disarm(&ws->timeoutTimer); // reset ping check os_timer_arm(&ws->timeoutTimer, WS_PING_INTERVAL_MS, true); + if(len == 0){ + NODE_DBG("len == 0"); + return; + } char *b = buf; if (ws->frameBuffer != NULL) { // Append previous frameBuffer with new content @@ -497,6 +501,10 @@ static void ws_initReceiveCallback(void *arg, char *buf, unsigned short len) { struct espconn *conn = (struct espconn *) arg; ws_info *ws = (ws_info *) conn->reverse; + if (len == 0){ + NODE_DBG("len==0\n"); + return; + } // Check server is switch protocols if (strstr(buf, WS_HTTP_SWITCH_PROTOCOL_HEADER) == NULL) { NODE_DBG("Server is not switching protocols\n");