Also use flexible array member instead of array of length 1 in
struct lldpd_frame.

Signed-off-by: Ben Pfaff <b...@nicira.com>
---
 lib/lldp/lldp.c          | 33 +++++----------------------------
 lib/lldp/lldpd-structs.h |  4 ++--
 lib/lldp/lldpd.c         | 18 +++++-------------
 lib/ovs-lldp.c           |  7 +------
 4 files changed, 13 insertions(+), 49 deletions(-)

diff --git a/lib/lldp/lldp.c b/lib/lldp/lldp.c
index 76c7877..f620d73 100644
--- a/lib/lldp/lldp.c
+++ b/lib/lldp/lldp.c
@@ -341,17 +341,10 @@ lldp_decode(struct lldpd *cfg OVS_UNUSED, char *frame, 
int s,
 
     VLOG_DBG("receive LLDP PDU on %s", hardware->h_ifname);
 
-    if ((chassis = calloc(1, sizeof *chassis)) == NULL) {
-        VLOG_WARN("failed to allocate remote chassis");
-        return -1;
-    }
+    chassis = xzalloc(sizeof *chassis);
     list_init(&chassis->c_mgmt.m_entries);
 
-    if ((port = calloc(1, sizeof *port)) == NULL) {
-        VLOG_WARN("failed to allocate remote port");
-        free(chassis);
-        return -1;
-    }
+    port = xzalloc(sizeof *port);
     list_init(&port->p_isid_vlan_maps.m_entries);
 
     length = s;
@@ -411,12 +404,7 @@ lldp_decode(struct lldpd *cfg OVS_UNUSED, char *frame, int 
s,
                           hardware->h_ifname);
                 goto malformed;
             }
-            if ((b = (char *) calloc(1, tlv_size - 1)) == NULL) {
-                VLOG_WARN("unable to allocate memory for id tlv received "
-                          "on %s",
-                          hardware->h_ifname);
-                goto malformed;
-            }
+            b = xzalloc(tlv_size - 1);
             PEEK_BYTES(b, tlv_size - 1);
             if (tlv_type == LLDP_TLV_PORT_ID) {
                 port->p_id_subtype = tlv_subtype;
@@ -442,12 +430,7 @@ lldp_decode(struct lldpd *cfg OVS_UNUSED, char *frame, int 
s,
                 VLOG_DBG("empty tlv received on %s", hardware->h_ifname);
                 break;
             }
-            if ((b = (char *) calloc(1, tlv_size + 1)) == NULL) {
-                VLOG_WARN("unable to allocate memory for string tlv "
-                          "received on %s",
-                          hardware->h_ifname);
-                goto malformed;
-            }
+            b = xzalloc(tlv_size + 1);
             PEEK_BYTES(b, tlv_size);
             if (tlv_type == LLDP_TLV_PORT_DESCR) {
                 port->p_descr = b;
@@ -552,13 +535,7 @@ lldp_decode(struct lldpd *cfg OVS_UNUSED, char *frame, int 
s,
 
                     num_mappings /= 5; /* Each mapping is 5 Bytes */
                     for(; num_mappings > 0; num_mappings--) {
-                        isid_vlan_map = (struct lldpd_aa_isid_vlan_maps_tlv *)
-                            calloc(1, sizeof *isid_vlan_map);
-                        if (!isid_vlan_map) {
-                            VLOG_WARN("unable to allocate memory "
-                                      "for aa_isid_vlan_maps_tlv struct");
-                            goto malformed;
-                        }
+                        isid_vlan_map = xzalloc(sizeof *isid_vlan_map);
                         aa_status_vlan_word = PEEK_UINT16;
 
                         /* Status is first 4 most-significant bits. */
diff --git a/lib/lldp/lldpd-structs.h b/lib/lldp/lldpd-structs.h
index 98ebd52..727dc72 100644
--- a/lib/lldp/lldpd-structs.h
+++ b/lib/lldp/lldpd-structs.h
@@ -1,6 +1,6 @@
 /* -*- mode: c; c-file-style: "openbsd" -*- */
 /*
- * Copyright (c) 2008 Vincent Bernat <ber...@luffy.cx>
+ * Copyright (c) 2008, 2015 Vincent Bernat <ber...@luffy.cx>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -149,7 +149,7 @@ struct lldpd_config {
 
 struct lldpd_frame {
     int size;
-    unsigned char frame[1];
+    unsigned char frame[];
 };
 
 struct lldpd_hardware;
diff --git a/lib/lldp/lldpd.c b/lib/lldp/lldpd.c
index 9c8173e..a5a07b9 100644
--- a/lib/lldp/lldpd.c
+++ b/lib/lldp/lldpd.c
@@ -82,10 +82,7 @@ lldpd_alloc_hardware(struct lldpd *cfg, char *name, int 
index)
 
     VLOG_DBG("allocate a new local hardware interface (%s)", name);
 
-    if ((hw = (struct lldpd_hardware *) calloc(1, sizeof *hw)) == NULL) {
-        return NULL;
-    }
-
+    hw = xzalloc(sizeof *hw);
     hw->h_cfg = cfg;
     ovs_strlcpy(hw->h_ifname, name, sizeof hw->h_ifname);
     hw->h_ifindex = index;
@@ -112,11 +109,7 @@ lldpd_alloc_mgmt(int family, void *addrptr, size_t 
addrsize, u_int32_t iface)
         errno = EOVERFLOW;
         return NULL;
     }
-    mgmt = calloc(1, sizeof *mgmt);
-    if (mgmt == NULL) {
-        errno = ENOMEM;
-        return NULL;
-    }
+    mgmt = xzalloc(sizeof *mgmt);
     mgmt->m_family = family;
     memcpy(&mgmt->m_addr, addrptr, addrsize);
     mgmt->m_addrsize = addrsize;
@@ -392,10 +385,9 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
 
     /* Add port */
     port->p_lastchange = port->p_lastupdate = time(NULL);
-    if ((port->p_lastframe = malloc(s + sizeof(struct lldpd_frame))) != NULL) {
-        port->p_lastframe->size = s;
-        memcpy(port->p_lastframe->frame, frame, s);
-    }
+    port->p_lastframe = xmalloc(s + sizeof(struct lldpd_frame));
+    port->p_lastframe->size = s;
+    memcpy(port->p_lastframe->frame, frame, s);
     list_insert(&hw->h_rports.p_entries, &port->p_entries);
 
     port->p_chassis = chassis;
diff --git a/lib/ovs-lldp.c b/lib/ovs-lldp.c
index 5fbde7e..f2d966c 100644
--- a/lib/ovs-lldp.c
+++ b/lib/ovs-lldp.c
@@ -968,12 +968,7 @@ lldp_create_dummy(void)
     list_init(&lldp->lldpd->g_chassis.list);
     list_push_back(&lldp->lldpd->g_chassis.list, &lchassis->list);
 
-    if ((hw = lldpd_alloc_hardware(lldp->lldpd,
-                                   "dummy-hw",
-                                   0)) == NULL) {
-        VLOG_WARN("Unable to allocate space for dummy-hw");
-        out_of_memory();
-    }
+    hw = lldpd_alloc_hardware(lldp->lldpd, "dummy-hw", 0);
 
     ovs_refcount_init(&lldp->ref_cnt);
 #ifndef _WIN32
-- 
2.1.3

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to