Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/ldpd - no cast for malloc



details:   https://anonhg.NetBSD.org/src/rev/f97677f1935f
branches:  trunk
changeset: 759426:f97677f1935f
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Dec 09 00:10:59 2010 +0000

description:
- no cast for malloc
- malloc + memset = calloc
- sizeof(type) -> sizeof(*var)
- small indents

diffstat:

 usr.sbin/ldpd/fsm.c            |   9 ++++-----
 usr.sbin/ldpd/label.c          |   7 +++----
 usr.sbin/ldpd/ldp_peer.c       |  20 ++++++++------------
 usr.sbin/ldpd/mpls_interface.c |  20 ++++++++++++++++----
 usr.sbin/ldpd/mpls_routes.c    |  25 ++++++++++---------------
 usr.sbin/ldpd/notifications.c  |   6 +++---
 usr.sbin/ldpd/socketops.c      |   5 ++---
 usr.sbin/ldpd/tlv_stack.c      |   8 ++++----
 8 files changed, 50 insertions(+), 50 deletions(-)

diffs (truncated from 329 to 300 lines):

diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/fsm.c
--- a/usr.sbin/ldpd/fsm.c       Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/fsm.c       Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fsm.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */
+/* $NetBSD: fsm.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
                if (hi->ldp_id.s_addr == pduid->ldp_id.s_addr)
                        break;
        if (hi == NULL) {
-               hi = (struct hello_info *)malloc(sizeof(struct hello_info));
+               hi = malloc(sizeof(*hi));
                if (!hi) {
                        fatalp("Cannot alloc a hello info");
                        return;
@@ -145,13 +145,12 @@
                        if (sa->sin_addr.s_addr << 24 >> 24 != 127)
                                adrcount++;
                }
-       t = (struct address_list_tlv *) malloc(sizeof(struct address_list_tlv)
-                                + (adrcount - 1) * sizeof(struct in_addr));
+       t = malloc(sizeof(*t) + (adrcount - 1) * sizeof(struct in_addr));
 
        if (!t) {
                fatalp("build_address_list_tlv: malloc problem\n");
                return NULL;
-               }
+       }
 
        t->type = htons(LDP_ADDRESS);
        t->length = htons(sizeof(struct address_list_tlv) - TLV_TYPE_LENGTH
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/label.c
--- a/usr.sbin/ldpd/label.c     Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/label.c     Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: label.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */
+/* $NetBSD: label.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -58,13 +58,12 @@
        struct label   *l;
        char    spreftmp[INET_ADDRSTRLEN];
 
-       l = (struct label *) malloc(sizeof(struct label));
-       memset(l, 0, sizeof(struct label));
+       l = calloc(1, sizeof(*l));
 
        if (!l) {
                fatalp("label_add: malloc problem\n");
                return NULL;
-               }
+       }
 
        assert(so_dest);
        assert(so_pref);
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/ldp_peer.c
--- a/usr.sbin/ldpd/ldp_peer.c  Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/ldp_peer.c  Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ldp_peer.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */
+/* $NetBSD: ldp_peer.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -87,14 +87,13 @@
        }
 
        /* Set the peer in CONNECTING/CONNECTED state */
-       p = (struct ldp_peer *) malloc(sizeof(struct ldp_peer));
+       p = calloc(1, sizeof(*p));
 
        if (!p) {
                fatalp("ldp_peer_new: malloc problem\n");
                return NULL;
-               }
+       }
 
-       memset(p, 0, sizeof(struct ldp_peer));
        SLIST_INSERT_HEAD(&ldp_peer_head, p, peers);
        memcpy(&p->address, a, sizeof(struct in_addr));
        memcpy(&p->ldp_id, ldp_id, sizeof(struct in_addr));
@@ -282,14 +281,12 @@
        if (check_ifaddr(p, a))
                return LDP_E_ALREADY_DONE;
 
-       lpa = (struct ldp_peer_address*)malloc(sizeof(struct ldp_peer_address));
+       lpa = calloc(1, sizeof(*lpa));
 
        if (!lpa) {
                fatalp("add_ifaddr: malloc problem\n");
                return LDP_E_MEMORY;
-               }
-
-       memset(lpa, 0, sizeof(struct ldp_peer_address));
+       }
 
        memcpy(&lpa->address, a, sizeof(struct in_addr));
 
@@ -355,8 +352,7 @@
 void 
 add_my_if_addrs(struct in_addr * a, int count)
 {
-       myaddresses = (struct in_addr *) malloc((count + 1) *
-           (sizeof(struct in_addr)));
+       myaddresses = calloc((count + 1), sizeof(*myaddresses));
 
        if (!myaddresses) {
                fatalp("add_my_if_addrs: malloc problem\n");
@@ -378,7 +374,7 @@
        if (ldp_peer_get_lm(p, a, prefix))
                return LDP_E_ALREADY_DONE;
 
-       lma = (struct label_mapping *) malloc(sizeof(struct label_mapping));
+       lma = malloc(sizeof(*lma));
 
        if (!lma) {
                fatalp("ldp_peer_add_mapping: malloc problem\n");
@@ -468,7 +464,7 @@
                debugp("Cannot match that prefix to the specified peer\n");
                return NULL;
        }
-       rv = (struct peer_map *) malloc(sizeof(struct peer_map));
+       rv = malloc(sizeof(*rv));
 
        if (!rv) {
                fatalp("ldp_test_mapping: malloc problem\n");
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/mpls_interface.c
--- a/usr.sbin/ldpd/mpls_interface.c    Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/mpls_interface.c    Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mpls_interface.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */
+/* $NetBSD: mpls_interface.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -164,7 +164,11 @@
 
        /* Add switching route */
        so_dest = make_mpls_union(lab->binding);
-       so_nexthop = (union sockunion *)malloc(sizeof(union sockunion));
+       so_nexthop = malloc(sizeof(*so_nexthop));
+       if (!so_nexthop) {
+               fatalp("Out of memory\n");
+               return LDP_E_MEMORY;
+       }
        memcpy(so_nexthop, so_gate, so_gate->sa.sa_len);
        so_tag = make_mpls_union(label);
        if (add_route(so_dest, NULL, so_nexthop, NULL, so_tag, FREESO, RTM_ADD) != LDP_E_OK)
@@ -183,11 +187,19 @@
                so_pref = from_cidr_to_union(len);
 
        /* Add tag to route */
-       so_nexthop = (union sockunion *)malloc(sizeof(union sockunion));
+       so_nexthop = malloc(sizeof(*so_nexthop));
+       if (!so_nexthop) {
+               fatalp("Out of memory\n");
+               return LDP_E_MEMORY;
+       }
        memcpy(so_nexthop, so_gate, so_gate->sa.sa_len);
        so_tag = make_mpls_union(label);
        if (so_oldifa != NULL) {
-               so_ifa = (union sockunion *)malloc(sizeof(union sockunion));
+               so_ifa = malloc(sizeof(*so_ifa));
+               if (!so_ifa) {
+                       fatalp("Out of memory\n");
+                       return LDP_E_MEMORY;
+               }
                memcpy(so_ifa, so_oldifa, so_oldifa->sa.sa_len);
        } else
                so_ifa = NULL;
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/mpls_routes.c
--- a/usr.sbin/ldpd/mpls_routes.c       Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/mpls_routes.c       Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mpls_routes.c,v 1.1 2010/12/08 07:20:15 kefren Exp $ */
+/* $NetBSD: mpls_routes.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -158,14 +158,12 @@
 {
        union sockunion *so_inet;
 
-       so_inet = (union sockunion *) malloc(sizeof(union sockunion));
+       so_inet = calloc(1, sizeof(*so_inet));
 
        if (!so_inet) {
                fatalp("make_inet_union: malloc problem\n");
                return NULL;
-               }
-
-       memset(so_inet, 0, sizeof(union sockunion));
+       }
 
        so_inet->sin.sin_len = sizeof(struct sockaddr_in);
        so_inet->sin.sin_family = AF_INET;
@@ -180,14 +178,12 @@
 {
        union sockunion *so_mpls;
 
-       so_mpls = (union sockunion *) malloc(sizeof(union sockunion));
+       so_mpls = calloc(1, sizeof(*so_mpls));
 
        if (!so_mpls) {
                fatalp("make_mpls_union: malloc problem\n");
                return NULL;
-               }
-
-       memset(so_mpls, 0, sizeof(union sockunion));
+       }
 
        so_mpls->smpls.smpls_len = sizeof(struct sockaddr_mpls);
        so_mpls->smpls.smpls_family = AF_MPLS;
@@ -218,15 +214,12 @@
        *m = (*m >> (32 - prefixlen) ) << (32 - prefixlen);
        *m = ntohl(*m);
 
-       u = (union sockunion *) malloc(sizeof(union sockunion));
+       u = calloc(1, sizeof(*u));
 
        if (!u) {
                fatalp("from_cidr_to_union: malloc problem\n");
                return NULL;
        }
-
-       memset (u, 0, sizeof(union sockunion));
-
        u->sin.sin_len = sizeof(struct sockaddr_in);
        u->sin.sin_family = AF_INET;
        u->sin.sin_addr.s_addr = *m;
@@ -848,8 +841,10 @@
                fatalp("route-sysctl-estimate: %s", strerror(errno));
                return LDP_E_ROUTE_ERROR;
        }
-       if ((buf = malloc(needed)) == 0)
-               return LDP_E_ROUTE_ERROR;
+       if ((buf = malloc(needed)) == NULL) {
+               fatalp("route-sysctl-estimate: %s", strerror(errno));
+               return LDP_E_MEMORY;
+       }
        if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
                free(buf);
                return LDP_E_ROUTE_ERROR;
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/notifications.c
--- a/usr.sbin/ldpd/notifications.c     Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/notifications.c     Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: notifications.c,v 1.1 2010/12/08 07:20:15 kefren Exp $ */
+/* $NetBSD: notifications.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -44,12 +44,12 @@
 build_notification(uint32_t msg, uint32_t n)
 {
        struct notification_tlv *t;
-       t = (struct notification_tlv *) malloc(sizeof(struct notification_tlv));
+       t = malloc(sizeof(*t));
 
        if (!t) {
                fatalp("build_notification: malloc problem\n");
                return NULL;
-               }
+       }
 
        t->type = htons(LDP_NOTIFICATION);
        t->length = htons(sizeof(struct notification_tlv) - TLV_TYPE_LENGTH);
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/socketops.c
--- a/usr.sbin/ldpd/socketops.c Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/socketops.c Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: socketops.c,v 1.1 2010/12/08 07:20:15 kefren Exp $ */
+/* $NetBSD: socketops.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -250,11 +250,10 @@
                        /* IPv4 Transport Address */                    \
                        sizeof(struct transport_address_tlv))
 
-       if ((v = malloc(HELLO_MSG_SIZE)) == NULL) {
+       if ((v = calloc(1, HELLO_MSG_SIZE)) == NULL) {
                fatalp("malloc problem in send_hello()\n");
                return;
        }
-       memset(v, 0, HELLO_MSG_SIZE);
 
        spdu = (struct ldp_pdu *)((char *)v);
        t = (struct hello_tlv *)(spdu + 1);
diff -r 4810d44ee695 -r f97677f1935f usr.sbin/ldpd/tlv_stack.c
--- a/usr.sbin/ldpd/tlv_stack.c Wed Dec 08 23:56:01 2010 +0000
+++ b/usr.sbin/ldpd/tlv_stack.c Thu Dec 09 00:10:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tlv_stack.c,v 1.1 2010/12/08 07:20:15 kefren Exp $ */
+/* $NetBSD: tlv_stack.c,v 1.2 2010/12/09 00:10:59 christos Exp $ */
 



Home | Main Index | Thread Index | Old Index