Просмотр исходного кода

fix: websocket.send发送126字节以上会报错

https://gitee.com/openLuat/luatos-soc-idf5/issues/I6UTGM
Wendal Chen 2 лет назад
Родитель
Сommit
2a6bb8d0d3
1 измененных файлов с 12 добавлено и 3 удалено
  1. 12 3
      components/network/websocket/luat_websocket.c

+ 12 - 3
components/network/websocket/luat_websocket.c

@@ -325,8 +325,12 @@ static int websocket_connect(luat_websocket_ctrl_t *websocket_ctrl)
 
 int luat_websocket_send_frame(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_pkg_t *pkg)
 {
-	char *dst = luat_heap_malloc(pkg->plen + 6);
-	memset(dst, 0, pkg->plen + 6);
+	char *dst = luat_heap_malloc(pkg->plen + 8);
+	if (dst == NULL) {
+		LLOGE("out of memory when send_frame");
+		return -2;
+	}
+	memset(dst, 0, pkg->plen + 8);
 	size_t offset = 0;
 	size_t ret = 0;
 	// first byte, FIN and OPTCODE
@@ -341,9 +345,14 @@ int luat_websocket_send_frame(luat_websocket_ctrl_t *websocket_ctrl, luat_websoc
 	{
 		dst[1] = 126;
 		dst[2] = pkg->plen >> 8;
-		dst[4] = pkg->plen & 0xFF;
+		dst[3] = pkg->plen & 0xFF;
 		offset = 4;
 	}
+	else {
+		LLOGE("pkg too large %d", pkg->plen);
+		luat_heap_free(dst);
+		return -1;
+	}
 	dst[1] |= 1 << 7;
 
 	print_pkg("uplink", dst, pkg);