 src/api/sockets.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/api/sockets.c b/src/api/sockets.c
index 15053b3..c987a2b 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -3020,7 +3020,8 @@ lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
     *namelen = saddr.sa.sa_len;
   }
 #else
-  *namelen = LWIP_MIN(*namelen, sizeof(saddr));
+  u8_t sa_len = IP_IS_V4_VAL(naddr) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
+  *namelen = LWIP_MIN(*namelen, sa_len);
 #endif
   MEMCPY(name, &saddr, *namelen);
 
