From 73c930881f7acabc9bea99eb698d0824cbe22c3e Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Sat, 8 Mar 2014 18:46:39 +0200 Subject: avoid some strncpy usage --- win32.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'win32.c') diff --git a/win32.c b/win32.c index c441841..04c7246 100644 --- a/win32.c +++ b/win32.c @@ -85,7 +85,13 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL char * addr = inet_ntoa (* (struct in_addr *) & address -> host); if (addr == NULL) return -1; - strncpy (name, addr, nameLength); + else + { + size_t addrLen = strlen(addr); + if (addrLen >= nameLength) + return -1; + memcpy (name, addr, addrLen + 1); + } return 0; } @@ -94,14 +100,19 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng { struct in_addr in; struct hostent * hostEntry; - + in.s_addr = address -> host; hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); if (hostEntry == NULL) return enet_address_get_host_ip (address, name, nameLength); - - strncpy (name, hostEntry -> h_name, nameLength); + else + { + size_t hostLen = strlen (hostEntry -> h_name); + if (hostLen >= nameLength) + return -1; + memcpy (name, hostEntry -> h_name, hostLen + 1); + } return 0; } -- cgit v1.2.3