netutils/websocket: fixes wrong buffer size of inet_ntop
authorJunyeon LEE <junyeon2.lee@samsung.com>
Mon, 17 Apr 2017 04:28:08 +0000 (13:28 +0900)
committerHeesub Shin <heesub.shin@samsung.com>
Sat, 6 May 2017 10:50:05 +0000 (19:50 +0900)
This commit fixes wrong input size in inet_ntop and adds libc
dependency.

Change-Id: Ic9b567832b202209c1bb8aa9f5aef2c3e23cae4b
Signed-off-by: Junyeon LEE <junyeon2.lee@samsung.com>
apps/netutils/websocket/websocket.c

index 7d970f4..23ec1f0 100644 (file)
@@ -22,6 +22,8 @@
  *  Included Files
  ****************************************************************************/
 
+#include <tinyara/config.h>
+
 #include <fcntl.h>
 #include <errno.h>
 #include <netdb.h>
@@ -348,7 +350,7 @@ int connect_socket(websocket_t *client, const char *host, const char *port)
        socklen_t addrlen;
        struct sockaddr_in serveraddr;
 
-#ifdef CONFIG_NET_LOOPBACK
+#ifdef CONFIG_LIBC_NETDB
        struct hostent *he = NULL;
        char ip_str[INET6_ADDRSTRLEN];
 
@@ -358,9 +360,11 @@ int connect_socket(websocket_t *client, const char *host, const char *port)
                WEBSOCKET_DEBUG("failed to resolve hostname\n");
                return WEBSOCKET_CONNECT_ERROR;
        }
-       inet_ntop(he->h_addrtype, he->h_addr, ip_str, he->h_length);
+       if (inet_ntop(he->h_addrtype, he->h_addr, ip_str, sizeof(ip_str)) == NULL) {
+               WEBSOCKET_DEBUG("inet_ntop failed (errno=%d)\n", errno);
+               return WEBSOCKET_CONNECT_ERROR;
+       }
 #endif
-
        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (fd == -1) {
                WEBSOCKET_DEBUG("fail to open socket\n");
@@ -369,7 +373,7 @@ int connect_socket(websocket_t *client, const char *host, const char *port)
 
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_port = htons(atoi(port));
-#ifdef CONFIG_NET_LOOPBACK
+#ifdef CONFIG_LIBC_NETDB
        serveraddr.sin_addr.s_addr = inet_addr(ip_str);
 #else
        serveraddr.sin_addr.s_addr = inet_addr(host);