From: Vincent Bernat Date: Sat, 29 Dec 2012 23:25:31 +0000 (+0100) Subject: global: fix some issues detected by cppcheck X-Git-Tag: 0.7.0~26 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=281a5cd416eef19fa2f279ab9e1e756a9be0ccc8;p=thirdparty%2Flldpd.git global: fix some issues detected by cppcheck This includes some memory leaks when decoding erroneous LLDP TLV. --- diff --git a/src/client/lldpctl.c b/src/client/lldpctl.c index db7c0a39..fc56a002 100644 --- a/src/client/lldpctl.c +++ b/src/client/lldpctl.c @@ -83,10 +83,10 @@ watchcb(lldpctl_conn_t *conn, lldpctl_atom_t *neighbor, void *data) { - int ch, i; + int i; struct cbargs *args = data; optind = 0; - while ((ch = getopt(args->argc, args->argv, LLDPCTL_ARGS)) != -1); + while (getopt(args->argc, args->argv, LLDPCTL_ARGS) != -1); if (optind < args->argc) { for (i = optind; i < args->argc; i++) if (strcmp(args->argv[i], diff --git a/src/client/text_writer.c b/src/client/text_writer.c index 017dca20..60adbb3a 100644 --- a/src/client/text_writer.c +++ b/src/client/text_writer.c @@ -109,6 +109,7 @@ struct writer * txt_init(FILE * fh) { result = malloc( sizeof( struct writer ) ); if ( ! result ) { fatalx("out of memory"); + free(priv); return NULL; } diff --git a/src/ctl.c b/src/ctl.c index eba8396d..3a88f0ec 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -125,7 +125,7 @@ ctl_msg_send_unserialized(uint8_t **output_buffer, size_t *output_len, void *t, struct marshal_info *mi) { struct hmsg_header hdr; - size_t len = 0, newlen; + ssize_t len = 0, newlen; void *buffer = NULL; log_debug("control", "send a message through control socket"); diff --git a/src/daemon/client.c b/src/daemon/client.c index 87059e62..dfe32ad5 100644 --- a/src/daemon/client.c +++ b/src/daemon/client.c @@ -144,7 +144,6 @@ client_handle_get_interface(struct lldpd *cfg, enum hmsg_type *type, free(name); if (output_len <= 0) { *type = NONE; - free(name); return 0; } return output_len; @@ -301,7 +300,7 @@ client_handle_client(struct lldpd *cfg, log_debug("rpc", "handle client request"); for (ch = client_handles; ch->handle != NULL; ch++) { if (ch->type == type) { - answer = NULL; len = 0; + answer = NULL; len = ch->handle(cfg, &type, buffer, n, &answer, subscribed); sent = send(out, type, answer, len); diff --git a/src/daemon/interfaces-bsd.c b/src/daemon/interfaces-bsd.c index 960488b0..d22153a2 100644 --- a/src/daemon/interfaces-bsd.c +++ b/src/daemon/interfaces-bsd.c @@ -644,7 +644,6 @@ ifbsd_eth_recv(struct lldpd *cfg, struct lldpd_hardware *hardware, int fd, char *buffer, size_t size) { - int n; struct bpf_buffer *bpfbuf = hardware->h_data; struct bpf_hdr *bh; log_debug("interfaces", "receive PDU from ethernet device %s", @@ -652,7 +651,7 @@ ifbsd_eth_recv(struct lldpd *cfg, /* We assume we have only receive one packet (unbuffered mode). Dunno if * this is correct. */ - if ((n = read(fd, bpfbuf->data, bpfbuf->len)) == -1) { + if (read(fd, bpfbuf->data, bpfbuf->len) == -1) { log_warn("interfaces", "error while receiving frame on %s", hardware->h_ifname); hardware->h_rx_discarded_cnt++; @@ -686,8 +685,8 @@ interfaces_update(struct lldpd *cfg) { struct lldpd_hardware *hardware; struct interfaces_device *iface; - struct interfaces_device_list *interfaces = NULL; - struct interfaces_address_list *addresses = NULL; + struct interfaces_device_list *interfaces; + struct interfaces_address_list *addresses; struct ifaddrs *ifaddrs = NULL, *ifaddr; interfaces = malloc(sizeof(struct interfaces_device_list)); diff --git a/src/daemon/interfaces-linux.c b/src/daemon/interfaces-linux.c index 8d3b710d..b96cd137 100644 --- a/src/daemon/interfaces-linux.c +++ b/src/daemon/interfaces-linux.c @@ -814,8 +814,8 @@ void interfaces_update(struct lldpd *cfg) { struct lldpd_hardware *hardware; - struct interfaces_device_list *interfaces = NULL; - struct interfaces_address_list *addresses = NULL; + struct interfaces_device_list *interfaces; + struct interfaces_address_list *addresses; interfaces = netlink_get_interfaces(); addresses = netlink_get_addresses(); if (interfaces == NULL || addresses == NULL) { diff --git a/src/daemon/lldp.c b/src/daemon/lldp.c index 39ce8bd2..b8d45979 100644 --- a/src/daemon/lldp.c +++ b/src/daemon/lldp.c @@ -671,6 +671,7 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, log_warn("lldp", "unable to alloc vlan name for " "tlv received on %s", hardware->h_ifname); + free(vlan); goto malformed; } PEEK_BYTES(vlan->v_name, vlan_len); @@ -725,6 +726,7 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, log_warn("lldp", "unable to alloc pid name for " "tlv received on %s", hardware->h_ifname); + free(pi); goto malformed; } PEEK_BYTES(pi->p_pi, pi->p_pi_len); diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 39f2d8b2..00ff4f7e 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -328,7 +328,7 @@ static void lldpd_decode(struct lldpd *cfg, char *frame, int s, struct lldpd_hardware *hardware) { - int i, result; + int i; struct lldpd_chassis *chassis, *ochassis = NULL; struct lldpd_port *port, *oport = NULL, *aport; int guess = LLDPD_MODE_LLDP; @@ -365,8 +365,8 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s, if (cfg->g_protocols[i].mode == guess) { log_debug("decode", "using decode function for %s protocol", cfg->g_protocols[i].name); - if ((result = cfg->g_protocols[i].decode(cfg, frame, - s, hardware, &chassis, &port)) == -1) { + if (cfg->g_protocols[i].decode(cfg, frame, + s, hardware, &chassis, &port) == -1) { log_debug("decode", "function for %s protocol did not decode this frame", cfg->g_protocols[i].name); return; diff --git a/src/daemon/netlink.c b/src/daemon/netlink.c index 595dc0ab..7558ebce 100644 --- a/src/daemon/netlink.c +++ b/src/daemon/netlink.c @@ -45,7 +45,7 @@ struct netlink_req { static int netlink_connect(int protocol) { - int s = 0; + int s; struct sockaddr_nl local = { .nl_family = AF_NETLINK, .nl_pid = getpid(), diff --git a/src/daemon/priv.c b/src/daemon/priv.c index 7753b94a..67612b86 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -494,12 +494,11 @@ static void priv_exit() { int status; - int rc; - if ((rc = waitpid(monitored, &status, WNOHANG)) == 0) { + if (waitpid(monitored, &status, WNOHANG) == 0) { log_debug("privsep", "killing child"); kill(monitored, SIGTERM); } - if ((rc = waitpid(monitored, &status, WNOHANG)) == -1) + if (waitpid(monitored, &status, WNOHANG) == -1) _exit(0); log_debug("privsep", "waiting for child %d to terminate", monitored); } diff --git a/src/marshal.c b/src/marshal.c index cde976a3..85087a5d 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -61,7 +61,7 @@ struct ref { TAILQ_HEAD(ref_l, ref); /* Serialize the given object. */ -size_t +ssize_t marshal_serialize_(struct marshal_info *mi, void *unserialized, void **input, int skip, void *_refs, int osize) { diff --git a/src/marshal.h b/src/marshal.h index eed7c9f2..22c432af 100644 --- a/src/marshal.h +++ b/src/marshal.h @@ -106,7 +106,7 @@ extern struct marshal_info marshal_info_ignore; MARSHAL_END /* Serialization */ -size_t marshal_serialize_(struct marshal_info *, void *, void **, int, void *, int); +ssize_t marshal_serialize_(struct marshal_info *, void *, void **, int, void *, int); #define marshal_serialize(type, o, output) marshal_serialize_(&MARSHAL_INFO(type), o, output, 0, NULL, 0) /* Unserialization */