Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/net eliminate sprintf, use snprintf. check strlcat...



details:   https://anonhg.NetBSD.org/src/rev/1292de12b70c
branches:  trunk
changeset: 535360:1292de12b70c
user:      itojun <itojun%NetBSD.org@localhost>
date:      Fri Aug 16 11:30:28 2002 +0000

description:
eliminate sprintf, use snprintf.  check strlcat error.

diffstat:

 lib/libc/net/gethnamaddr.c  |  19 +++++++++++--------
 lib/libc/net/getnetnamadr.c |  28 +++++++++++++++-------------
 2 files changed, 26 insertions(+), 21 deletions(-)

diffs (128 lines):

diff -r b5180fd15aa8 -r 1292de12b70c lib/libc/net/gethnamaddr.c
--- a/lib/libc/net/gethnamaddr.c        Fri Aug 16 11:22:13 2002 +0000
+++ b/lib/libc/net/gethnamaddr.c        Fri Aug 16 11:30:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gethnamaddr.c,v 1.48 2002/07/29 10:01:58 itojun Exp $  */
+/*     $NetBSD: gethnamaddr.c,v 1.49 2002/08/16 11:30:28 itojun Exp $  */
 
 /*
  * ++Copyright++ 1985, 1988, 1993
@@ -61,7 +61,7 @@
 static char sccsid[] = "@(#)gethostnamadr.c    8.1 (Berkeley) 6/4/93";
 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
 #else
-__RCSID("$NetBSD: gethnamaddr.c,v 1.48 2002/07/29 10:01:58 itojun Exp $");
+__RCSID("$NetBSD: gethnamaddr.c,v 1.49 2002/08/16 11:30:28 itojun Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -1164,7 +1164,7 @@
        void    *cb_data;
        va_list  ap;
 {
-       char qbuf[MAXDNAME + 1], *qp;
+       char qbuf[MAXDNAME + 1], *qp, *ep;
        int n;
        querybuf buf;
        struct hostent *hp;
@@ -1186,16 +1186,18 @@
 
        case AF_INET6:
                qp = qbuf;
+               ep = qbuf + sizeof(qbuf) - 1;
                for (n = IN6ADDRSZ - 1; n >= 0; n--) {
-                       advance = sprintf(qp, "%x.%x.", uaddr[n] & 0xf,
+                       advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
+                           uaddr[n] & 0xf,
                            ((unsigned int)uaddr[n] >> 4) & 0xf);
-                       if (advance > 0 &&
-                           qp + advance < qbuf + sizeof(qbuf) - 1)
+                       if (advance > 0 && qp + advance < ep)
                                qp += advance;
                        else
                                return NS_NOTFOUND;
                }
-               strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
+               if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf))
+                       return NS_NOTFOUND;
                break;
        default:
                abort();
@@ -1204,7 +1206,8 @@
        n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
        if (n < 0 && af == AF_INET6) {
                *qp = '\0';
-               strlcat(qbuf, "ip6.int", sizeof(qbuf));
+               if (strlcat(qbuf, "ip6.int", sizeof(qbuf)) >= sizeof(qbuf))
+                       return NS_NOTFOUND;
                n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
                    sizeof(buf));
        }
diff -r b5180fd15aa8 -r 1292de12b70c lib/libc/net/getnetnamadr.c
--- a/lib/libc/net/getnetnamadr.c       Fri Aug 16 11:22:13 2002 +0000
+++ b/lib/libc/net/getnetnamadr.c       Fri Aug 16 11:30:28 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getnetnamadr.c,v 1.22 2002/06/26 06:00:07 itojun Exp $ */
+/*     $NetBSD: getnetnamadr.c,v 1.23 2002/08/16 11:30:29 itojun Exp $ */
 
 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
  *     Dep. Matematica Universidade de Coimbra, Portugal, Europe
@@ -47,7 +47,7 @@
 static char sccsid_[] = "from getnetnamadr.c   1.4 (Coimbra) 93/06/03";
 static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
 #else
-__RCSID("$NetBSD: getnetnamadr.c,v 1.22 2002/06/26 06:00:07 itojun Exp $");
+__RCSID("$NetBSD: getnetnamadr.c,v 1.23 2002/08/16 11:30:29 itojun Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -297,18 +297,19 @@
        default:
                return NS_UNAVAIL;
        case 3:         /* Class A */
-               sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
+               snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
                break;
        case 2:         /* Class B */
-               sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
+               snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
+                   netbr[3], netbr[2]);
                break;
        case 1:         /* Class C */
-               sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
-                   netbr[1]);
+               snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
+                   netbr[3], netbr[2], netbr[1]);
                break;
        case 0:         /* Class D - E */
-               sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
-                   netbr[1], netbr[0]);
+               snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
+                   netbr[3], netbr[2], netbr[1], netbr[0]);
                break;
        }
        anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
@@ -499,17 +500,18 @@
        default:
                return NS_UNAVAIL;
        case 3:         /* Class A */
-               sprintf(qbuf, "%u", netbr[0]);
+               snprintf(qbuf, sizeof(qbuf), "%u", netbr[0]);
                break;
        case 2:         /* Class B */
-               sprintf(qbuf, "%u.%u", netbr[0], netbr[1]);
+               snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[0], netbr[1]);
                break;
        case 1:         /* Class C */
-               sprintf(qbuf, "%u.%u.%u", netbr[0], netbr[1], netbr[2]);
+               snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[0], netbr[1],
+                   netbr[2]);
                break;
        case 0:         /* Class D - E */
-               sprintf(qbuf, "%u.%u.%u.%u", netbr[0], netbr[1], netbr[2],
-                   netbr[3]);
+               snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
+                   netbr[2], netbr[3]);
                break;
        }
        r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),



Home | Main Index | Thread Index | Old Index