Source-Changes-HG archive

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

[src/trunk]: src/sys/netipsec Use KASSERT for sanity checks of function argum...



details:   https://anonhg.NetBSD.org/src/rev/7f75558fe164
branches:  trunk
changeset: 353076:7f75558fe164
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Wed Apr 19 07:19:46 2017 +0000

description:
Use KASSERT for sanity checks of function arguments

diffstat:

 sys/netipsec/ipsec.c |   17 +--
 sys/netipsec/key.c   |  234 ++++++++++++++++++++++++--------------------------
 2 files changed, 120 insertions(+), 131 deletions(-)

diffs (truncated from 552 to 300 lines):

diff -r f4593d884d49 -r 7f75558fe164 sys/netipsec/ipsec.c
--- a/sys/netipsec/ipsec.c      Wed Apr 19 07:14:45 2017 +0000
+++ b/sys/netipsec/ipsec.c      Wed Apr 19 07:19:46 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ipsec.c,v 1.79 2017/04/19 07:14:45 ozaki-r Exp $       */
+/*     $NetBSD: ipsec.c,v 1.80 2017/04/19 07:19:46 ozaki-r Exp $       */
 /*     $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $       */
 /*     $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
 
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.79 2017/04/19 07:14:45 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.80 2017/04/19 07:19:46 ozaki-r Exp $");
 
 /*
  * IPsec controller part.
@@ -1172,9 +1172,7 @@
        struct udphdr uh;
        struct icmp6_hdr icmph;
 
-       /* sanity check */
-       if (m == NULL)
-               panic("%s: NULL pointer was passed", __func__);
+       KASSERT(m != NULL);
 
        if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
                printf("%s:\n", __func__);
@@ -1282,9 +1280,8 @@
 {
        struct inpcbpolicy *new;
 
-       /* sanity check. */
-       if (so == NULL || policy == NULL)
-               panic("%s: NULL pointer was passed", __func__);
+       KASSERT(so != NULL);
+       KASSERT(policy != NULL);
 
        new = malloc(sizeof(*new), M_SECA, M_NOWAIT|M_ZERO);
        if (new == NULL) {
@@ -1878,9 +1875,7 @@
        int error;
        int result;
 
-       /* sanity check */
-       if (m == NULL)
-               return 0;       /* XXX should be panic ? */
+       KASSERT(m != NULL);
 
        /* get SP for this packet.
         * When we are called from ip_forward(), we call
diff -r f4593d884d49 -r 7f75558fe164 sys/netipsec/key.c
--- a/sys/netipsec/key.c        Wed Apr 19 07:14:45 2017 +0000
+++ b/sys/netipsec/key.c        Wed Apr 19 07:19:46 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: key.c,v 1.111 2017/04/19 03:42:11 ozaki-r Exp $        */
+/*     $NetBSD: key.c,v 1.112 2017/04/19 07:19:46 ozaki-r Exp $        */
 /*     $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $        */
 /*     $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $   */
 
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.111 2017/04/19 03:42:11 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.112 2017/04/19 07:19:46 ozaki-r Exp $");
 
 /*
  * This code is referd to RFC 2367
@@ -956,9 +956,8 @@
                /* Which SA is the better ? */
 
                /* sanity check 2 */
-               if (candidate->lft_c == NULL || sav->lft_c == NULL)
-                       panic("key_do_allocsa_policy: "
-                           "lifetime_current is NULL");
+               KASSERT(candidate->lft_c != NULL);
+               KASSERT(sav->lft_c != NULL);
 
                /* What the best method is to compare ? */
                if (key_prefered_oldsa) {
@@ -1442,11 +1441,9 @@
 {
        struct secpolicy *newsp;
 
-       /* sanity check */
-       if (xpl0 == NULL)
-               panic("key_msg2sp: NULL pointer was passed");
-       if (len < sizeof(*xpl0))
-               panic("key_msg2sp: invalid length");
+       KASSERT(xpl0 != NULL);
+       KASSERT(len >= sizeof(*xpl0));
+
        if (len != PFKEY_EXTLEN(xpl0)) {
                ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
                *error = EINVAL;
@@ -1674,9 +1671,7 @@
        char *p;
        struct mbuf *m;
 
-       /* sanity check. */
-       if (sp == NULL)
-               panic("key_sp2msg: NULL pointer was passed");
+       KASSERT(sp != NULL);
 
        tlen = key_getspreqmsglen(sp);
 
@@ -1740,8 +1735,8 @@
        struct mbuf *result = NULL, *n;
        int len;
 
-       if (m == NULL || mhp == NULL)
-               panic("null pointer passed to key_gather");
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
 
        va_start(ap, nitem);
        for (i = 0; i < nitem; i++) {
@@ -1831,9 +1826,10 @@
        struct secpolicy *newsp;
        int error;
 
-       /* sanity check */
-       if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_spdadd: NULL pointer is passed");
+       KASSERT(so != NULL);
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
            mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
@@ -2080,9 +2076,10 @@
        struct secpolicyindex spidx;
        struct secpolicy *sp;
 
-       /* sanity check */
-       if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_spddelete: NULL pointer is passed");
+       KASSERT(so != NULL);
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
            mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
@@ -2178,9 +2175,10 @@
        u_int32_t id;
        struct secpolicy *sp;
 
-       /* sanity check */
-       if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_spddelete2: NULL pointer is passed");
+       KASSERT(so != NULL);
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
            mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
@@ -2277,9 +2275,10 @@
        struct secpolicy *sp;
        struct mbuf *n;
 
-       /* sanity check */
-       if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_spdget: NULL pointer is passed");
+       KASSERT(so != NULL);
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
            mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
@@ -2327,13 +2326,10 @@
        struct secspacq *newspacq;
        int error;
 
-       /* sanity check */
-       if (sp == NULL)
-               panic("key_spdacquire: NULL pointer is passed");
-       if (sp->req != NULL)
-               panic("key_spdacquire: called but there is request");
-       if (sp->policy != IPSEC_POLICY_IPSEC)
-               panic("key_spdacquire: policy mismathed. IPsec is expected");
+       KASSERT(sp != NULL);
+       KASSERTMSG(sp->req == NULL, "called but there is request");
+       KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
+           "policy mismathed. IPsec is expected");
 
        /* Get an entry to check whether sent message or not. */
        if ((newspacq = key_getspacq(&sp->spidx)) != NULL) {
@@ -2397,9 +2393,10 @@
        struct secpolicy *sp;
        u_int dir;
 
-       /* sanity check */
-       if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_spdflush: NULL pointer is passed");
+       KASSERT(so != NULL);
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
                return key_senderror(so, m, EINVAL);
@@ -2519,10 +2516,10 @@
        int ok, s;
        pid_t pid;
 
-       /* sanity check */
-       if (so == NULL || m0 == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_spddump: NULL pointer is passed");
-
+       KASSERT(so != NULL);
+       KASSERT(m0 != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        pid = mhp->msg->sadb_msg_pid;
        /*
@@ -2589,9 +2586,10 @@
        struct sadb_address *iaddr, *raddr;
        struct sadb_x_nat_t_frag *frag;
 
-       /* sanity check */
-       if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_nat_map: NULL pointer is passed.");
+       KASSERT(so != NULL);
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] == NULL ||
                mhp->ext[SADB_X_EXT_NAT_T_SPORT] == NULL ||
@@ -2743,9 +2741,7 @@
        /* XXX: Why do we lock ? */
        s = splsoftnet();       /*called from softclock()*/
 
-       /* sanity check */
-       if (sp == NULL)
-               panic("key_spdexpire: NULL pointer is passed");
+       KASSERT(sp != NULL);
 
        /* set msg header */
        m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
@@ -2877,9 +2873,7 @@
        int s;
        int zombie = 0;
 
-       /* sanity check */
-       if (sah == NULL)
-               panic("key_delsah: NULL pointer is passed");
+       KASSERT(sah != NULL);
 
        s = splsoftnet();       /*called from softclock()*/
 
@@ -2944,9 +2938,10 @@
        struct secasvar *newsav;
        const struct sadb_sa *xsa;
 
-       /* sanity check */
-       if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
-               panic("key_newsa: NULL pointer is passed");
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
+       KASSERT(sah != NULL);
 
        KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
        if (newsav == NULL) {
@@ -3176,9 +3171,9 @@
 {
        int error = 0;
 
-       /* sanity check */
-       if (m == NULL || mhp == NULL || mhp->msg == NULL)
-               panic("key_setsaval: NULL pointer is passed");
+       KASSERT(m != NULL);
+       KASSERT(mhp != NULL);
+       KASSERT(mhp->msg != NULL);
 
        /* initialization */
        sav->replay = NULL;
@@ -4101,9 +4096,7 @@
        int s;
 #endif
 
-       /* sanity check */
-       if (sa == NULL)
-               panic("key_ismyaddr: NULL pointer is passed");
+       KASSERT(sa != NULL);
 
        switch (sa->sa_family) {
 #ifdef INET
@@ -4917,9 +4910,10 @@
        u_int16_t reqid;
        int error;
 



Home | Main Index | Thread Index | Old Index