diff --git a/include/enet/enet.h b/include/enet/enet.h index dd51e95..948f44e 100644 --- a/include/enet/enet.h +++ b/include/enet/enet.h @@ -517,7 +517,6 @@ ENET_API int enet_address_set_host (ENetAddress * address, const char * hostName @returns the null-terminated name of the host in hostName on success @retval 0 on success @retval < 0 on failure - @remarks if the name's length matches or exceeds nameLength, the result may not be null-terminated */ ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostName, size_t nameLength); @@ -528,7 +527,6 @@ ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostN @returns the null-terminated name of the host in hostName on success @retval 0 on success @retval < 0 on failure - @remarks if the name's length matches or exceeds nameLength, the result may not be null-terminated */ ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength); diff --git a/include/enet/win32.h b/include/enet/win32.h index d8968ee..e73ca9d 100644 --- a/include/enet/win32.h +++ b/include/enet/win32.h @@ -7,7 +7,6 @@ #ifdef _MSC_VER #ifdef ENET_BUILDING_LIB -#pragma warning (disable: 4996) // 'strncpy' was declared deprecated #pragma warning (disable: 4267) // size_t to int conversion #pragma warning (disable: 4244) // 64bit to 32bit int #pragma warning (disable: 4018) // signed/unsigned mismatch diff --git a/unix.c b/unix.c index e9a0602..5e7c731 100644 --- a/unix.c +++ b/unix.c @@ -138,7 +138,12 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL #else char * addr = inet_ntoa (* (struct in_addr *) & address -> host); if (addr != NULL) - strncpy (name, addr, nameLength); + { + size_t addrLen = strlen(addr); + if (addrLen >= nameLength) + return -1; + memcpy (name, addr, addrLen + 1); + } else #endif return -1; @@ -170,8 +175,13 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng 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; } 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; }