From b0cb07f7ed0b79551650ceafd5cd646ac8703081 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 22 Jun 2013 17:00:34 +0200 Subject: [PATCH] style: use C99 designated initializers instead of memset() when possible --- src/client/display.c | 3 +-- src/ctl.c | 9 +++++---- src/daemon/agent_priv.c | 20 ++++++++------------ src/daemon/cdp.c | 3 +-- src/daemon/dmi-linux.c | 3 +-- src/daemon/edp.c | 3 +-- src/daemon/interfaces-linux.c | 20 ++++++++------------ src/daemon/lldp.c | 3 +-- src/daemon/priv.c | 6 ++---- src/daemon/sonmp.c | 3 +-- src/lib/atom-private.c | 12 ++++-------- 11 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/client/display.c b/src/client/display.c index 680eb06d..03f1b7da 100644 --- a/src/client/display.c +++ b/src/client/display.c @@ -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); diff --git a/src/ctl.c b/src/ctl.c index b323c280..fd6d50d5 100644 --- 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); diff --git a/src/daemon/agent_priv.c b/src/daemon/agent_priv.c index 10f0e729..d74ab54c 100644 --- a/src/daemon/agent_priv.c +++ b/src/daemon/agent_priv.c @@ -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]); diff --git a/src/daemon/cdp.c b/src/daemon/cdp.c index 671d8d52..7dcdb120 100644 --- a/src/daemon/cdp.c +++ b/src/daemon/cdp.c @@ -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 */ diff --git a/src/daemon/dmi-linux.c b/src/daemon/dmi-linux.c index 9fe2801f..6e1857ef 100644 --- a/src/daemon/dmi-linux.c +++ b/src/daemon/dmi-linux.c @@ -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); diff --git a/src/daemon/edp.c b/src/daemon/edp.c index 71c0958e..80109376 100644 --- a/src/daemon/edp.c +++ b/src/daemon/edp.c @@ -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; diff --git a/src/daemon/interfaces-linux.c b/src/daemon/interfaces-linux.c index df8ec463..00124bef 100644 --- a/src/daemon/interfaces-linux.c +++ b/src/daemon/interfaces-linux.c @@ -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", diff --git a/src/daemon/lldp.c b/src/daemon/lldp.c index fce84528..7d99b27c 100644 --- a/src/daemon/lldp.c +++ b/src/daemon/lldp.c @@ -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 */ diff --git a/src/daemon/priv.c b/src/daemon/priv.c index db1bda57..23cfbc4f 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -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(ðc, 0, sizeof(ethc)); must_read(remote, &len, sizeof(int)); if ((ifname = (char*)malloc(len + 1)) == NULL) fatal("privsep", NULL); diff --git a/src/daemon/sonmp.c b/src/daemon/sonmp.c index 665a160d..444b4ef2 100644 --- a/src/daemon/sonmp.c +++ b/src/daemon/sonmp.c @@ -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 */ diff --git a/src/lib/atom-private.c b/src/lib/atom-private.c index 8258b85a..4325012e 100644 --- a/src/lib/atom-private.c +++ b/src/lib/atom-private.c @@ -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); -- 2.39.5