From 922cf0612bb8ee3b7892ddbc205a9a661a13b9d7 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 22 Dec 2020 17:04:53 +0800 Subject: [PATCH] libc/netdb: Move hostbuffer out of the stack The length of hostbuffer come from Kconfig, it isn't safe to allocate this variable on the stack. Signed-off-by: Xiang Xiao Change-Id: I75213688153a1cba738544de2a51fa3247626dd3 --- libs/libc/netdb/lib_getaddrinfo.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libs/libc/netdb/lib_getaddrinfo.c b/libs/libc/netdb/lib_getaddrinfo.c index 501984eb1b5..d38cf356496 100644 --- a/libs/libc/netdb/lib_getaddrinfo.c +++ b/libs/libc/netdb/lib_getaddrinfo.c @@ -124,7 +124,7 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname, int flags = 0; int proto = 0; int socktype = 0; - char hostbuffer[CONFIG_NETDB_BUFSIZE]; + FAR char *hostbuffer; FAR struct hostent_s host; FAR struct ai_s *ai; FAR struct ai_s *prev_ai = NULL; @@ -280,12 +280,19 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname, #endif /* CONFIG_NET_LOOPBACK */ } + hostbuffer = lib_malloc(CONFIG_NETDB_BUFSIZE); + if (hostbuffer == NULL) + { + return EAI_MEMORY; + } + /* REVISIT: no check for AI_NUMERICHOST flag. */ gethostentbyname_r(hostname, &host, - hostbuffer, sizeof(hostbuffer), &ret); + hostbuffer, CONFIG_NETDB_BUFSIZE, &ret); if (ret != OK) { + lib_free(hostbuffer); return ret; } @@ -309,6 +316,7 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname, freeaddrinfo(*res); } + lib_free(hostbuffer); return EAI_MEMORY; } @@ -337,5 +345,6 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname, prev_ai = ai; } + lib_free(hostbuffer); return (*res != NULL) ? OK : EAI_FAMILY; }