Răsfoiți Sursa

modules/websocket: Fix pkg memory leaks

- Fix pkg memory leaks in error cases
- Fix incorrect memory allocation size for ws connections
- Fix typo in websocket stats
(cherry picked from commit d0f88e19577d9b914922f83049075b7786f3d8df)
Hugh Waite 12 ani în urmă
părinte
comite
76319cf960
2 a modificat fișierele cu 13 adăugiri și 9 ștergeri
  1. 2 2
      modules/websocket/ws_conn.c
  2. 11 7
      modules/websocket/ws_frame.c

+ 2 - 2
modules/websocket/ws_conn.c

@@ -90,7 +90,7 @@ int wsconn_init(void)
 
 	wsconn_id_hash =
 		(ws_connection_t **) shm_malloc(TCP_ID_HASH_SIZE *
-						sizeof(ws_connection_t));
+						sizeof(ws_connection_t*));
 	if (wsconn_id_hash == NULL)
 	{
 		LM_ERR("allocating WebSocket hash-table\n");
@@ -364,7 +364,7 @@ static int add_node(struct mi_root *tree, ws_connection_t *wsc)
 
 		dst_proto = (con->rcv.proto == PROTO_WS) ? "ws" : "wss";
 		memset(dst_ip, 0, IP6_MAX_STR_SIZE + 1);
-		ip_addr2sbuf(&con->rcv.dst_ip, src_ip, IP6_MAX_STR_SIZE);
+		ip_addr2sbuf(&con->rcv.dst_ip, dst_ip, IP6_MAX_STR_SIZE);
 
 		pong = wsc->awaiting_pong ? "awaiting Pong, " : "";
 

+ 11 - 7
modules/websocket/ws_frame.c

@@ -219,6 +219,7 @@ static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
 	if ((con = tcpconn_get(frame->wsc->id, 0, 0, 0, 0)) == NULL)
 	{
 		LM_WARN("TCP/TLS connection get failed\n");
+		pkg_free(send_buf);
 		if (wsconn_rm(frame->wsc, WSCONN_EVENTROUTE_YES) < 0)
 			LM_ERR("removing WebSocket connection\n");
 		return -1;
@@ -230,6 +231,7 @@ static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
 		if (wsconn_rm(frame->wsc, WSCONN_EVENTROUTE_YES) < 0)
 		{
 			LM_ERR("removing WebSocket connection\n");
+			pkg_free(send_buf);
 			return -1;
 		}
 	}
@@ -240,6 +242,7 @@ static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
 		{
 			STATS_TX_DROPS;
 			LM_WARN("TCP disabled\n");
+			pkg_free(send_buf);
 			return -1;
 		}		
 	}
@@ -250,6 +253,7 @@ static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
 		{
 			STATS_TX_DROPS;
 			LM_WARN("TLS disabled\n");
+			pkg_free(send_buf);
 			return -1;
 		}		
 	}
@@ -291,15 +295,15 @@ static int close_connection(ws_connection_t *wsc, ws_close_type_t type,
 	char *data;
 	ws_frame_t frame;
 
-	data = pkg_malloc(sizeof(char) * (reason.len + 2));
-	if (data == NULL)
-	{
-		LM_ERR("allocating pkg memory\n");
-		return -1;
-	}
-
 	if (wsc->state == WS_S_OPEN)
 	{
+		data = pkg_malloc(sizeof(char) * (reason.len + 2));
+		if (data == NULL)
+		{
+			LM_ERR("allocating pkg memory\n");
+			return -1;
+		}
+
 		data[0] = (status & 0xff00) >> 8;
 		data[1] = (status & 0x00ff) >> 0;
 		memcpy(&data[2], reason.s, reason.len);