]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
global: fix some issues detected by cppcheck
authorVincent Bernat <bernat@luffy.cx>
Sat, 29 Dec 2012 23:25:31 +0000 (00:25 +0100)
committerVincent Bernat <bernat@luffy.cx>
Sat, 29 Dec 2012 23:25:31 +0000 (00:25 +0100)
This includes some memory leaks when decoding erroneous LLDP TLV.

12 files changed:
src/client/lldpctl.c
src/client/text_writer.c
src/ctl.c
src/daemon/client.c
src/daemon/interfaces-bsd.c
src/daemon/interfaces-linux.c
src/daemon/lldp.c
src/daemon/lldpd.c
src/daemon/netlink.c
src/daemon/priv.c
src/marshal.c
src/marshal.h

index db7c0a391aaf4a9bc6c7b7a3298df04b43b02b4a..fc56a002f9c2c26af359d3f94ce10cd1070e6734 100644 (file)
@@ -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],
index 017dca20cd4e241b748bf6029e2b623c65739702..60adbb3a5977bb678ece0427df801984b1e7e302 100644 (file)
@@ -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;
        }
 
index eba8396de2bf72013e0967e1b1b1400369e0facb..3a88f0ec0026825dc9f583bb0a2b07311fe2da93 100644 (file)
--- 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");
index 87059e62b1a6a2001c6ea2693f9733f768d21b88..dfe32ad5429effa8dcb3adf4aee49c7f591bdea9 100644 (file)
@@ -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);
index 960488b0bd92d791369ab2e2dbe978b66fc27c83..d22153a2a6e0a792ca390f58622a4ced1968347e 100644 (file)
@@ -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));
index 8d3b710d8af6feed4ca023b401b0a4dd3419f85b..b96cd1379dec3aae3cb9f83aa1417a1c622d8d90 100644 (file)
@@ -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) {
index 39ce8bd20dc605b0c276b55be3cee94f32a64786..b8d459791f6c38dec2bbb6dcbd1ab6b4e17524a2 100644 (file)
@@ -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);
index 39f2d8b23867b258be05e6664e9928e5e3d03607..00ff4f7eba557dfbb8e32946c4e43aecb149fd26 100644 (file)
@@ -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;
index 595dc0ab29082ee620e1834c425a32f4ca150e6b..7558ebce6e2b209374fc7d48f640fb403a239279 100644 (file)
@@ -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(),
index 7753b94a9c2e52b86a92b95e5a09b94a4ff1479a..67612b86a054f857809af2ccd200dc87cdaffe0c 100644 (file)
@@ -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);
 }
index cde976a36e5a35943047402f1f7504cf8b92b119..85087a5d9b884737b665b61b9cf3c34d2aa40e47 100644 (file)
@@ -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)
 {
index eed7c9f2ba03a6828642b5b30b947947ffa9d85c..22c432afd8f66569fee566e84f9b873187c1b199 100644 (file)
@@ -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 */