Skip to content

Commit

Permalink
avoid some strncpy usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lsalzman committed Mar 8, 2014
1 parent 5721b66 commit 73c9308
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 0 additions & 2 deletions include/enet/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
1 change: 0 additions & 1 deletion include/enet/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 15 additions & 4 deletions win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 73c9308

Please sign in to comment.