o Use snprintf over sprintf.

o Use strncpy correctly.
o Use enough buffer for line.

Inspired by or Obtained from: Similar changes in OpenBSD
This commit is contained in:
Warner Losh 1998-06-09 04:13:03 +00:00
parent c788c9b224
commit 9a1f6729af
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=36788
2 changed files with 17 additions and 13 deletions

View File

@ -36,7 +36,7 @@
static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
*/
static const char rcsid[] =
"$Id: inet.c,v 1.27 1998/05/15 20:19:15 wollman Exp $";
"$Id: inet.c,v 1.28 1998/05/19 16:00:55 pb Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -559,7 +559,7 @@ inetname(inp)
struct in_addr *inp;
{
register char *cp;
static char line[50];
static char line[MAXHOSTNAMELEN + 1];
struct hostent *hp;
struct netent *np;
@ -583,9 +583,10 @@ inetname(inp)
}
if (inp->s_addr == INADDR_ANY)
strcpy(line, "*");
else if (cp)
strcpy(line, cp);
else {
else if (cp) {
strncpy(line, cp, sizeof(line) - 1);
line[sizeof(line) - 1] = '\0';
} else {
inp->s_addr = ntohl(inp->s_addr);
#define C(x) ((x) & 0xff)
sprintf(line, "%lu.%lu.%lu.%lu", C(inp->s_addr >> 24),

View File

@ -36,7 +36,7 @@
static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95";
#endif
static const char rcsid[] =
"$Id: route.c,v 1.29 1998/04/19 18:18:25 phk Exp $";
"$Id: route.c,v 1.30 1998/04/22 06:54:31 phk Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -573,9 +573,10 @@ routename(in)
trimdomain(cp);
}
}
if (cp)
if (cp) {
strncpy(line, cp, sizeof(line) - 1);
else {
line[sizeof(line) - 1] = '\0';
} else {
#define C(x) ((x) & 0xff)
in = ntohl(in);
sprintf(line, "%lu.%lu.%lu.%lu",
@ -754,14 +755,16 @@ ipx_print(sa)
if (port) {
if (strcmp(host, "*") == 0)
host = "";
if (sp)
sprintf(cport, "%s%s", *host ? "." : "", sp->s_name);
else
sprintf(cport, "%s%x", *host ? "." : "", port);
if (sp)
snprintf(cport, sizeof(cport),
"%s%s", *host ? "." : "", sp->s_name);
else
snprintf(cport, sizeof(cport),
"%s%x", *host ? "." : "", port);
} else
*cport = 0;
sprintf(mybuf,"%s.%s%s", net, host, cport);
snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
return(mybuf);
}