]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
style: use C99 designated initializers instead of memset() when possible
authorVincent Bernat <bernat@luffy.cx>
Sat, 22 Jun 2013 15:00:34 +0000 (17:00 +0200)
committerVincent Bernat <bernat@luffy.cx>
Sat, 22 Jun 2013 15:00:34 +0000 (17:00 +0200)
src/client/display.c
src/ctl.c
src/daemon/agent_priv.c
src/daemon/cdp.c
src/daemon/dmi-linux.c
src/daemon/edp.c
src/daemon/interfaces-linux.c
src/daemon/lldp.c
src/daemon/priv.c
src/daemon/sonmp.c
src/lib/atom-private.c

index 680eb06d4f83b87aeda6a9dea1069c11dd23a4c0..03f1b7da605a697407c0945ca2de50ebdfc900d0 100644 (file)
@@ -570,10 +570,9 @@ void
 display_stat(struct writer *w, const char *tag, const char *descr,
        long unsigned int cnt)
 {
-       char buf[20];
+       char buf[20] = {};
 
        tag_start(w, tag, descr);
-       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), "%lu", cnt );
        tag_attr(w, tag, "", buf);
        tag_end(w);
index b323c28050bd16ccb7f130e7151b5411cfeefa16..fd6d50d5772c3275ede081c0f2321cf6e3da2006 100644 (file)
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -125,7 +125,6 @@ ctl_msg_send_unserialized(uint8_t **output_buffer, size_t *output_len,
     enum hmsg_type type,
     void *t, struct marshal_info *mi)
 {
-       struct hmsg_header hdr;
        ssize_t len = 0, newlen;
        void *buffer = NULL;
 
@@ -156,9 +155,11 @@ ctl_msg_send_unserialized(uint8_t **output_buffer, size_t *output_len,
                }
                *output_buffer = new;
        }
-       memset(&hdr, 0, sizeof(struct hmsg_header));
-       hdr.type = type;
-       hdr.len  = len;
+
+       struct hmsg_header hdr = {
+               .type = type,
+               .len = len
+       };
        memcpy(*output_buffer + *output_len, &hdr, sizeof(struct hmsg_header));
        if (t)
                memcpy(*output_buffer + *output_len + sizeof(struct hmsg_header), buffer, len);
index 10f0e729dc295ce6833438f2e95333af02d8a03b..d74ab54c6cad8ebab024908a6cc83c7a6baa6a9d 100644 (file)
@@ -51,10 +51,9 @@ agent_priv_unix_recv(netsnmp_transport *t, void *buf, int size,
 
        if (t == NULL || t->sock < 0)
                goto recv_error;
-       to = (struct sockaddr *)malloc(sizeof(struct sockaddr_un));
+       to = (struct sockaddr *)calloc(1, sizeof(struct sockaddr_un));
        if (to == NULL)
                goto recv_error;
-       memset(to, 0, tolen);
        if (getsockname(t->sock, to, &tolen) != 0)
                goto recv_error;
        while (rc < 0) {
@@ -115,30 +114,27 @@ agent_priv_unix_accept(netsnmp_transport *t)
 static netsnmp_transport *
 agent_priv_unix_transport(const char *string, int len, int local)
 {
-       struct sockaddr_un addr;
+       struct sockaddr_un addr = {
+               .sun_family = AF_UNIX
+       };
        netsnmp_transport *t = NULL;
-       
+
        if (local) {
                log_warnx("snmp", "should not have been called for local transport");
                return NULL;
        }
        if (!string)
                return NULL;
-       if (len > 0 && len < (sizeof(addr.sun_path) - 1)) {
-               addr.sun_family = AF_UNIX;
-               memset(addr.sun_path, 0, sizeof(addr.sun_path));
-               strncpy(addr.sun_path, string, len);
-       } else {
+       if (len >= sizeof(addr.sun_path) ||
+           strlcpy(addr.sun_path, string, sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
                log_warnx("snmp", "path too long for Unix domain transport");
                return NULL;
        }
 
        if ((t = (netsnmp_transport *)
-               malloc(sizeof(netsnmp_transport))) == NULL)
+               calloc(1, sizeof(netsnmp_transport))) == NULL)
                return NULL;
 
-       memset(t, 0, sizeof(netsnmp_transport));
-
        t->domain = netsnmp_unix;
        t->domain_length =
            sizeof(netsnmp_unix) / sizeof(netsnmp_unix[0]);
index 671d8d5275a06f85c678465de7d73068cbba8c16..7dcdb12053bbfc4120ec8c6b18d52912c5478530 100644 (file)
@@ -60,9 +60,8 @@ cdp_send(struct lldpd *global,
 #endif
 
        length = hardware->h_mtu;
-       if ((packet = (u_int8_t*)malloc(length)) == NULL)
+       if ((packet = (u_int8_t*)calloc(1, length)) == NULL)
                return ENOMEM;
-       memset(packet, 0, length);
        pos = packet;
 
        /* Ethernet header */
index 9fe2801f0cd58779b65d0aac9b68d8be277d0f2b..6e1857ef68851bf48fc6f76a2a294d33ea50d1b9 100644 (file)
@@ -33,14 +33,13 @@ static char*
 dmi_get(char *file)
 {
        int dmi, s;
-       char buffer[100];
+       char buffer[100] = {};
 
        log_debug("localchassis", "DMI request for file %s", file);
        if ((dmi = priv_open(file)) < 0) {
                log_debug("localchassis", "cannot get DMI file %s", file);
                return NULL;
        }
-       memset(buffer, 0, sizeof(buffer));
        if ((s = read(dmi, buffer, sizeof(buffer))) == -1) {
                log_debug("localchassis", "cannot read DMI file %s", file);
                close(dmi);
index 71c0958e58444e78217cdbcb8b824a56e91c92d5..801093762a40858b84208f4ec2890e0823bcba4d 100644 (file)
@@ -56,9 +56,8 @@ edp_send(struct lldpd *global,
        while (state != 2) {
 #endif
                length = hardware->h_mtu;
-               if ((packet = (u_int8_t*)malloc(length)) == NULL)
+               if ((packet = (u_int8_t*)calloc(1, length)) == NULL)
                        return ENOMEM;
-               memset(packet, 0, length);
                pos = packet;
                v = 0;
 
index df8ec4631ad124fc7e4371acefc9af53690d713a..00124befda830068b61d06fee6cb6f9b7e95704b 100644 (file)
@@ -111,18 +111,18 @@ old_iflinux_is_bridge(struct lldpd *cfg,
 {
 #ifdef ENABLE_OLDIES
        int j;
-       int ifptindices[MAX_PORTS];
+       int ifptindices[MAX_PORTS] = {};
        unsigned long args2[4] = {
                BRCTL_GET_PORT_LIST,
                (unsigned long)ifptindices,
                MAX_PORTS,
                0
        };
-       struct ifreq ifr;
+       struct ifreq ifr = {
+               .ifr_data = (char*)&args2
+       };
 
        strncpy(ifr.ifr_name, iface->name, IFNAMSIZ);
-       memset(ifptindices, 0, sizeof(ifptindices));
-       ifr.ifr_data = (char *)&args2;
 
        if (ioctl(cfg->g_sock, SIOCDEVPRIVATE, &ifr) < 0)
                /* This can happen with a 64bit kernel and 32bit
@@ -193,8 +193,7 @@ iflinux_is_vlan(struct lldpd *cfg,
     struct interfaces_device_list *interfaces,
     struct interfaces_device *iface)
 {
-       struct vlan_ioctl_args ifv;
-       memset(&ifv, 0, sizeof(ifv));
+       struct vlan_ioctl_args ifv = {};
        ifv.cmd = GET_VLAN_REALDEV_NAME_CMD;
        strlcpy(ifv.device1, iface->name, sizeof(ifv.device1));
        if (ioctl(cfg->g_sock, SIOCGIFVLAN, &ifv) >= 0) {
@@ -230,10 +229,8 @@ iflinux_is_bond(struct lldpd *cfg,
     struct interfaces_device_list *interfaces,
     struct interfaces_device *master)
 {
-       struct ifreq ifr;
-       struct ifbond ifb;
-       memset(&ifr, 0, sizeof(ifr));
-       memset(&ifb, 0, sizeof(ifb));
+       struct ifreq ifr = {};
+       struct ifbond ifb = {};
        strlcpy(ifr.ifr_name, master->name, sizeof(ifr.ifr_name));
        ifr.ifr_data = (char *)&ifb;
        if (ioctl(cfg->g_sock, SIOCBONDINFOQUERY, &ifr) >= 0) {
@@ -640,8 +637,7 @@ iflinux_add_wireless(struct lldpd *cfg,
 {
        struct interfaces_device *iface;
        TAILQ_FOREACH(iface, interfaces, next) {
-               struct iwreq iwr;
-               memset(&iwr, 0, sizeof(struct iwreq));
+               struct iwreq iwr = {};
                strlcpy(iwr.ifr_name, iface->name, IFNAMSIZ);
                if (ioctl(cfg->g_sock, SIOCGIWNAME, &iwr) >= 0) {
                        log_debug("interfaces", "%s is wireless",
index fce84528b0c6cca7149eb89237a26ba990073d68..7d99b27cfabce7167d9802300083f7a8ee25faf1 100644 (file)
@@ -85,9 +85,8 @@ lldp_send(struct lldpd *global,
        port = &hardware->h_lport;
        chassis = port->p_chassis;
        length = hardware->h_mtu;
-       if ((packet = (u_int8_t*)malloc(length)) == NULL)
+       if ((packet = (u_int8_t*)calloc(1, length)) == NULL)
                return ENOMEM;
-       memset(packet, 0, length);
        pos = packet;
 
        /* Ethernet header */
index db1bda57340db518d71ec7762782be16ba22d6da..23cfbc4f7a96093de58de7d5fe1b32cea8249cf1 100644 (file)
@@ -325,13 +325,11 @@ asroot_open()
 static void
 asroot_ethtool()
 {
-       struct ifreq ifr;
-       struct ethtool_cmd ethc;
+       struct ifreq ifr = {};
+       struct ethtool_cmd ethc = {};
        int len, rc;
        char *ifname;
 
-       memset(&ifr, 0, sizeof(ifr));
-       memset(&ethc, 0, sizeof(ethc));
        must_read(remote, &len, sizeof(int));
        if ((ifname = (char*)malloc(len + 1)) == NULL)
                fatal("privsep", NULL);
index 665a160df35c646a3beb930018a1a8185846af81..444b4ef2362975419381fbb616c62b2f05d46dbf 100644 (file)
@@ -198,9 +198,8 @@ sonmp_send(struct lldpd *global,
 
        chassis = hardware->h_lport.p_chassis;
        length = hardware->h_mtu;
-       if ((packet = (u_int8_t*)malloc(length)) == NULL)
+       if ((packet = (u_int8_t*)calloc(1, length)) == NULL)
                return ENOMEM;
-       memset(packet, 0, length);
        pos = packet;
 
        /* Ethernet header */
index 8258b85a112552cf0496237f6c2131c4f68daaae..4325012e7ef50508455dcabe436e24c32815fad1 100644 (file)
@@ -373,12 +373,11 @@ _lldpctl_atom_set_str_config(lldpctl_atom_t *atom, lldpctl_key_t key,
 {
        struct _lldpctl_atom_config_t *c =
            (struct _lldpctl_atom_config_t *)atom;
-       struct lldpd_config config;
+       struct lldpd_config config = {};
        char *iface_pattern = NULL;
        char *description = NULL;
        int rc, len;
 
-       memset(&config, 0, sizeof(struct lldpd_config));
        len = strlen(value) + 1;
 
        switch (key) {
@@ -460,8 +459,7 @@ _lldpctl_atom_set_int_config(lldpctl_atom_t *atom, lldpctl_key_t key,
        int rc;
        struct _lldpctl_atom_config_t *c =
            (struct _lldpctl_atom_config_t *)atom;
-       struct lldpd_config config;
-       memset(&config, 0, sizeof(struct lldpd_config));
+       struct lldpd_config config = {};
 
        switch (key) {
        case lldpctl_k_config_paused:
@@ -749,7 +747,7 @@ _lldpctl_atom_set_atom_port(lldpctl_atom_t *atom, lldpctl_key_t key, lldpctl_ato
        struct _lldpctl_atom_port_t *p =
            (struct _lldpctl_atom_port_t *)atom;
        struct lldpd_hardware *hardware = p->hardware;
-       struct lldpd_port_set set;
+       struct lldpd_port_set set = {};
        int rc;
 
 #ifdef ENABLE_DOT3
@@ -767,8 +765,6 @@ _lldpctl_atom_set_atom_port(lldpctl_atom_t *atom, lldpctl_key_t key, lldpctl_ato
                return NULL;
        }
 
-       memset(&set, 0, sizeof(struct lldpd_port_set));
-
        switch (key) {
 #ifdef ENABLE_DOT3
        case lldpctl_k_port_dot3_power:
@@ -1951,7 +1947,7 @@ _lldpctl_atom_get_str_med_location(lldpctl_atom_t *atom, lldpctl_key_t key)
                return fixed_precision(atom, l, 9, 25, 0, " W", " E");
        case lldpctl_k_med_location_altitude:
                if (m->location->format != LLDP_MED_LOCFORMAT_COORD) break;
-               memset(&l, 0, sizeof(u_int64_t));
+               l = 0;
                memcpy(&l, m->location->data + 10, 5);
                l = (ntohll(l) & 0x3FFFFFFF000000ULL) >> 24;
                return fixed_precision(atom, l, 22, 8, 1, NULL, NULL);