From 5b31955dfa56228d4e52eac09c0ab3f9acd30ef4 Mon Sep 17 00:00:00 2001 From: kang Date: Fri, 15 Sep 2017 08:41:53 +0900 Subject: [PATCH] examples:websocket fix strncpy bug --- apps/examples/websocket/websocket_main.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/examples/websocket/websocket_main.c b/apps/examples/websocket/websocket_main.c index 5b812e6..47893f0 100644 --- a/apps/examples/websocket/websocket_main.c +++ b/apps/examples/websocket/websocket_main.c @@ -134,7 +134,9 @@ ****************************************************************************/ #define WEBSOCKET_EXAMPLE_STACKSIZE (1024 * 10) - +#define WEBSOCKET_EXAMPLE_PORT_LEN 7 +#define WEBSOCKET_EXAMPLE_ADDR_LEN 19 +#define WEBSOCKET_EXAMPLE_PATH_LEN 31 /* * TLS debug configure (0 ~ 5) * @@ -166,9 +168,9 @@ struct options_s { int mode; int tls_mode; - char server_port[8]; - char server_ip[20]; - char path[32]; + char server_port[WEBSOCKET_EXAMPLE_PORT_LEN + 1]; + char server_ip[WEBSOCKET_EXAMPLE_ADDR_LEN + 1]; + char path[WEBSOCKET_EXAMPLE_PATH_LEN + 1]; int size; int num; }; @@ -747,9 +749,17 @@ int websocket_main(int argc, char *argv[]) goto error_with_input; } - strncpy(input->server_ip, argv[2], 20); - strncpy(input->server_port, argv[3], 8); - strncpy(input->path, argv[4], 32); + int addr_len = strlen(argv[2]); + int port_len = strlen(argv[3]); + int path_len = strlen(argv[4]); + + if (addr_len > WEBSOCKET_EXAMPLE_ADDR_LEN || port_len > WEBSOCKET_EXAMPLE_PORT_LEN + || path_len > WEBSOCKET_EXAMPLE_PATH_LEN) { + goto error_with_input; + } + strncpy(input->server_ip, argv[2], addr_len + 1); + strncpy(input->server_port, argv[3], port_len + 1); + strncpy(input->path, argv[4], path_len + 1); input->tls_mode = atoi(argv[5]); input->size = atoi(argv[6]); input->num = atoi(argv[7]); -- 2.7.4