diff options
author | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2006-11-28 15:54:53 +0000 |
---|---|---|
committer | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2006-11-28 15:54:53 +0000 |
commit | 62a7de2abde14b0d2e127191254a2db42f503aca (patch) | |
tree | 4f8f1ad180ce3a356888c3e1d99711d1f275ed04 | |
parent | 65d81a5d417e85cdb01954f3609af9083907cc7c (diff) |
OpenBSD fixes: snprintf/strlc{at,py}
-rw-r--r-- | lib/libxtrans/Xtrans.c | 38 | ||||
-rw-r--r-- | lib/libxtrans/Xtranssock.c | 21 | ||||
-rw-r--r-- | lib/libxtrans/Xtransutil.c | 42 |
3 files changed, 51 insertions, 50 deletions
diff --git a/lib/libxtrans/Xtrans.c b/lib/libxtrans/Xtrans.c index 00391a012..f92fc7b77 100644 --- a/lib/libxtrans/Xtrans.c +++ b/lib/libxtrans/Xtrans.c @@ -219,13 +219,15 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port) char *_protocol, *_host, *_port; char hostnamebuf[256]; int _host_len; + size_t len; PRMSG (3,"ParseAddress(%s)\n", address, 0, 0); /* Copy the string so it can be changed */ - tmpptr = mybuf = (char *) xalloc (strlen (address) + 1); - strcpy (mybuf, address); + len = strlen (address) + 1; + tmpptr = mybuf = (char *) xalloc (len); + strlcpy (mybuf, address, len); /* Parse the string to get each component */ @@ -364,8 +366,8 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port) * Now that we have all of the components, allocate new * string space for them. */ - - if ((*protocol = (char *) xalloc(strlen (_protocol) + 1)) == NULL) + len = strlen (_protocol) + 1; + if ((*protocol = (char *) xalloc(len)) == NULL) { /* Malloc failed */ *port = NULL; @@ -375,9 +377,10 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port) return 0; } else - strcpy (*protocol, _protocol); + strlcpy (*protocol, _protocol, len); - if ((*host = (char *) xalloc (strlen (_host) + 1)) == NULL) + len = strlen (_host) + 1; + if ((*host = (char *) xalloc (len)) == NULL) { /* Malloc failed */ *port = NULL; @@ -388,9 +391,10 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port) return 0; } else - strcpy (*host, _host); + strlcpy (*host, _host, len); - if ((*port = (char *) xalloc (strlen (_port) + 1)) == NULL) + len = strlen (_port) + 1; + if ((*port = (char *) xalloc (len)) == NULL) { /* Malloc failed */ *port = NULL; @@ -402,7 +406,7 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port) return 0; } else - strcpy (*port, _port); + strlcpy (*port, _port, len); xfree (tmpptr); @@ -522,6 +526,7 @@ TRANS(Reopen) (int type, int trans_id, int fd, char *port) Xtransport *thistrans = NULL; char *save_port; int i; + size_t len; PRMSG (2,"Reopen(%d,%d,%s)\n", trans_id, fd, port); @@ -541,15 +546,16 @@ TRANS(Reopen) (int type, int trans_id, int fd, char *port) return NULL; } - - if ((save_port = (char *) xalloc (strlen (port) + 1)) == NULL) + + len = strlen (port) + 1; + if ((save_port = (char *) xalloc (len)) == NULL) { PRMSG (1,"Reopen: Unable to malloc port string\n", 0, 0, 0); return NULL; } - strcpy (save_port, port); + strlcpy (save_port, port, len); /* Get a new XtransConnInfo object */ @@ -664,18 +670,20 @@ TRANS(GetReopenInfo) (XtransConnInfo ciptr, { int i; + size_t len; for (i = 0; i < NUMTRANS; i++) if (Xtransports[i].transport == ciptr->transptr) { *trans_id = Xtransports[i].transport_id; *fd = ciptr->fd; - - if ((*port = (char *) xalloc (strlen (ciptr->port) + 1)) == NULL) + + len = strlen (ciptr->port) + 1; + if ((*port = (char *) xalloc (len)) == NULL) return 0; else { - strcpy (*port, ciptr->port); + strlcpy (*port, ciptr->port, len); return 1; } } diff --git a/lib/libxtrans/Xtranssock.c b/lib/libxtrans/Xtranssock.c index 65d4beb89..aac43ddca 100644 --- a/lib/libxtrans/Xtranssock.c +++ b/lib/libxtrans/Xtranssock.c @@ -288,9 +288,6 @@ static int TRANS(SocketINETClose) (XtransConnInfo ciptr); #if defined(IPv6) && defined(AF_INET6) static const struct in6_addr local_in6addr_any = IN6ADDR_ANY_INIT; #pragma weak in6addr_any = local_in6addr_any -#ifndef __USLC__ -#pragma weak getaddrinfo -#endif static int haveIPv6 = 1; #endif @@ -469,9 +466,6 @@ TRANS(SocketOpen) (int i, int type) PRMSG (3,"SocketOpen(%d,%d)\n", i, type, 0); #if defined(IPv6) && defined(AF_INET6) - if (getaddrinfo == NULL) - haveIPv6 = 0; - if (!haveIPv6 && Sockettrans2devtab[i].family == AF_INET6) return NULL; #endif @@ -846,11 +840,11 @@ set_sun_path(const char *port, const char *upath, char *path) if (*port == '/') { /* a full pathname */ if (strlen(port) > maxlen) return -1; - sprintf(path, "%s", port); + snprintf(path, maxlen+1, "%s", port); } else { if (strlen(port) + strlen(upath) > maxlen) return -1; - sprintf(path, "%s%s", upath, port); + snprintf(path, maxlen+1, "%s%s", upath, port); } return 0; } @@ -972,7 +966,7 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int { /* fixup the server port address */ tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); - sprintf (portbuf,"%lu", tmpport); + snprintf (portbuf, sizeof(portbuf), "%lu", tmpport); port = portbuf; } #endif @@ -1024,7 +1018,7 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int } else { namelen = sizeof (struct sockaddr_in6); #ifdef SIN6_LEN - ((struct sockaddr_in6 *)&sockname)->sin6_len = sizeof(sockname); + ((struct sockaddr_in6 *)&sockname)->sin6_len = (u_int8_t)sizeof(sockname); #endif ((struct sockaddr_in6 *)&sockname)->sin6_family = AF_INET6; ((struct sockaddr_in6 *)&sockname)->sin6_port = htons(sport); @@ -1456,7 +1450,7 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) if (is_numeric (port)) { tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); - sprintf (portbuf, "%lu", tmpport); + snprintf (portbuf, sizeof(portbuf), "%lu", tmpport); port = portbuf; } #endif @@ -1813,11 +1807,6 @@ UnixHostReallyLocal (char *host) { char hostnamebuf[256]; -#if defined(IPv6) && defined(AF_INET6) - if (getaddrinfo == NULL) - haveIPv6 = 0; -#endif - TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf)); if (strcmp (hostnamebuf, host) == 0) diff --git a/lib/libxtrans/Xtransutil.c b/lib/libxtrans/Xtransutil.c index cc57193c3..cd090f70c 100644 --- a/lib/libxtrans/Xtransutil.c +++ b/lib/libxtrans/Xtransutil.c @@ -226,7 +226,7 @@ TRANS(ConvertAddress)(int *familyp, int *addrlenp, Xtransaddr **addrp) if (!*addrp) *addrp = (Xtransaddr *) xalloc (len + 1); if (*addrp) { - strcpy ((char *) *addrp, hostnamebuf); + strlcpy ((char *) *addrp, hostnamebuf, len + 1); *addrlenp = len; } else { *addrlenp = 0; @@ -259,6 +259,7 @@ TRANS(GetMyNetworkId) (XtransConnInfo ciptr) char hostnamebuf[256]; char *networkId = NULL; char *transName = ciptr->transptr->TransName; + size_t len; if (gethostname (hostnamebuf, sizeof (hostnamebuf)) < 0) { @@ -271,9 +272,10 @@ TRANS(GetMyNetworkId) (XtransConnInfo ciptr) case AF_UNIX: { struct sockaddr_un *saddr = (struct sockaddr_un *) addr; - networkId = (char *) xalloc (3 + strlen (transName) + - strlen (hostnamebuf) + strlen (saddr->sun_path)); - sprintf (networkId, "%s/%s:%s", transName, + len = 3 + strlen (transName) + + strlen (hostnamebuf) + strlen (saddr->sun_path); + networkId = (char *) xalloc (len); + snprintf (networkId, len, "%s/%s:%s", transName, hostnamebuf, saddr->sun_path); break; } @@ -301,9 +303,11 @@ TRANS(GetMyNetworkId) (XtransConnInfo ciptr) portnum = ntohs (saddr->sin_port); snprintf (portnumbuf, sizeof(portnumbuf), "%d", portnum); - networkId = (char *) xalloc (3 + strlen (transName) + - strlen (hostnamebuf) + strlen (portnumbuf)); - sprintf (networkId, "%s/%s:%s", transName, hostnamebuf, portnumbuf); + len = 3 + strlen (transName) + + strlen (hostnamebuf) + strlen (portnumbuf); + networkId = (char *) xalloc (len); + snprintf (networkId, len, "%s/%s:%s", transName, hostnamebuf, + portnumbuf); break; } #endif /* defined(TCPCONN) || defined(STREAMSCONN) */ @@ -312,10 +316,9 @@ TRANS(GetMyNetworkId) (XtransConnInfo ciptr) case AF_DECnet: { struct sockaddr_dn *saddr = (struct sockaddr_dn *) addr; - - networkId = (char *) xalloc ( - 13 + strlen (hostnamebuf) + saddr->sdn_objnamel); - sprintf (networkId, "dnet/%s::%s", + len = 13 + strlen (hostnamebuf) + saddr->sdn_objnamel; + networkId = (char *) xalloc (len); + snprintf (networkId, len, "dnet/%s::%s", hostnamebuf, saddr->sdn_objname); break; } @@ -365,6 +368,7 @@ TRANS(GetPeerNetworkId) (XtransConnInfo ciptr) char *hostname; char addrbuf[256]; const char *addr = NULL; + size_t len; switch (family) { @@ -448,9 +452,10 @@ TRANS(GetPeerNetworkId) (XtransConnInfo ciptr) if (np = getnodebyaddr(saddr->sdn_add.a_addr, saddr->sdn_add.a_len, AF_DECnet)) { - sprintf(addrbuf, "%s:", np->n_name); + snprintf(addrbuf, sizeof(addrbuf), "%s:", np->n_name); } else { - sprintf(addrbuf, "%s:", dnet_htoa(&saddr->sdn_add)); + snprintf(addrbuf, sizeof(addrbuf), "%s:", + dnet_htoa(&saddr->sdn_add)); } addr = addrbuf; break; @@ -461,13 +466,12 @@ TRANS(GetPeerNetworkId) (XtransConnInfo ciptr) return (NULL); } - - hostname = (char *) xalloc ( - strlen (ciptr->transptr->TransName) + strlen (addr) + 2); - strcpy (hostname, ciptr->transptr->TransName); - strcat (hostname, "/"); + len = strlen (ciptr->transptr->TransName) + strlen (addr) + 2; + hostname = (char *) xalloc (len); + strlcpy (hostname, ciptr->transptr->TransName, len); + strlcat (hostname, "/", len); if (addr) - strcat (hostname, addr); + strlcat (hostname, addr, len); return (hostname); } |