]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Fix all errors reported by clang static analyser.
authorRoy Marples <roy@marples.name>
Thu, 27 Feb 2014 20:00:39 +0000 (20:00 +0000)
committerRoy Marples <roy@marples.name>
Thu, 27 Feb 2014 20:00:39 +0000 (20:00 +0000)
auth.c
dhcp-common.c
dhcp.c
dhcp6.c
dhcpcd.c
if-options.c
ipv6nd.c

diff --git a/auth.c b/auth.c
index 553148bc6b06ad18a678093f6af8028c6e4a3a7c..1275750461c0e7f56207305c1db09c1de9bd9ef5 100644 (file)
--- a/auth.c
+++ b/auth.c
@@ -343,18 +343,20 @@ finish:
                        } else {
                                free(state->token);
                                state->token = NULL;
+                               return NULL;
                        }
-                       if (t->realm) {
+                       if (t->realm_len) {
                                state->token->realm = malloc(t->realm_len);
                                if (state->token->realm) {
                                        state->token->realm_len = t->realm_len;
                                        memcpy(state->token->realm, t->realm,
                                            t->realm_len);
-                           } else {
+                               } else {
                                        free(state->token->key);
                                        free(state->token);
                                        state->token = NULL;
-                           }
+                                       return NULL;
+                               }
                        } else {
                                state->token->realm = NULL;
                                state->token->realm_len = 0;
index 9dab167d63adaf3ccf0d9bcee90ec92bdd35461c..4a04360283ba472b23b144fe2d13ace8ba618391 100644 (file)
@@ -352,7 +352,6 @@ dhcp_optlen(const struct dhcp_opt *opt, size_t dl)
                return dl - (dl % ADDR6SZ);
        }
 
-       sz = 0;
        if (opt->type & (UINT32 | ADDRIPV4))
                sz = sizeof(uint32_t);
        else if (opt->type & UINT16)
diff --git a/dhcp.c b/dhcp.c
index 56547fe3c13da927c099d54af9b13983ce4bb50a..19e83901efe639713090b26145f3b710128179f1 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -151,7 +151,9 @@ get_option(struct dhcpcd_ctx *ctx,
                if (o == opt) {
                        if (op) {
                                if (!ctx->opt_buffer) {
-                                       ctx->opt_buffer = malloc(sizeof(*dhcp));
+                                       ctx->opt_buffer =
+                                           malloc(DHCP_OPTION_LEN +
+                                           BOOTFILE_LEN + SERVERNAME_LEN);
                                        if (ctx->opt_buffer == NULL)
                                                return NULL;
                                }
@@ -972,9 +974,8 @@ ssize_t
 write_lease(const struct interface *ifp, const struct dhcp_message *dhcp)
 {
        int fd;
-       ssize_t bytes = sizeof(*dhcp);
-       const uint8_t *p = dhcp->options;
-       const uint8_t *e = p + sizeof(dhcp->options);
+       ssize_t bytes;
+       const uint8_t *e, *p;
        uint8_t l;
        uint8_t o = 0;
        const struct dhcp_state *state = D_CSTATE(ifp);
@@ -993,6 +994,9 @@ write_lease(const struct interface *ifp, const struct dhcp_message *dhcp)
                return -1;
 
        /* Only write as much as we need */
+       p = dhcp->options;
+       e = p + sizeof(dhcp->options);
+       bytes = sizeof(*dhcp);
        while (p < e) {
                o = *p;
                if (o == DHO_END) {
diff --git a/dhcp6.c b/dhcp6.c
index 1db65f8f5567f2ff899fd4c790c6aab11c74016a..5031382877a899b142b6cb32738a8ad0f0e712ef 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -835,6 +835,8 @@ logsend:
 
        /* Set the outbound interface */
        cm = CMSG_FIRSTHDR(&ctx->sndhdr);
+       if (cm == NULL) /* unlikely */
+               return -1;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_PKTINFO;
        cm->cmsg_len = CMSG_LEN(sizeof(pi));
@@ -1416,7 +1418,6 @@ dhcp6_findpd(struct interface *ifp, const uint8_t *iaid,
                p += sizeof(u8);
                len = u8;
                memcpy(&prefix.s6_addr, p, sizeof(prefix.s6_addr));
-               p += sizeof(prefix.s6_addr);
 
                TAILQ_FOREACH(a, &state->addrs, next) {
                        if (IN6_ARE_ADDR_EQUAL(&a->prefix, &prefix))
@@ -2630,9 +2631,14 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason)
 {
        struct dhcp6_state *state;
        struct dhcpcd_ctx *ctx;
+       int options;
 
        eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
 
+       if (ifp->options)
+               options = ifp->options->options;
+       else
+               options = 0;
        /*
         * As the interface is going away from dhcpcd we need to
         * remove the delegated addresses, otherwise we lose track
@@ -2641,30 +2647,31 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason)
         * how we remember which interface delegated.
         *
         * XXX The below is no longer true due to the change of the
-        * default IAID, but do PPP links have stable ethernet addresses?
+        * default IAID, but do PPP links have stable ethernet
+        * addresses?
         *
         * To make it more interesting, on some OS's with PPP links
         * there is no guarantee the delegating interface will have
         * the same name or index so think very hard before changing
         * this.
         */
-       if (ifp->options &&
-           ifp->options->options & (DHCPCD_STOPPING | DHCPCD_RELEASE) &&
-           (ifp->options->options & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
+       if (options & (DHCPCD_STOPPING | DHCPCD_RELEASE) &&
+           (options &
+           (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
            (DHCPCD_EXITING | DHCPCD_PERSISTENT))
                dhcp6_delete_delegates(ifp);
 
        state = D6_STATE(ifp);
        if (state) {
                dhcp_auth_reset(&state->auth);
-               if (ifp->options->options & DHCPCD_RELEASE) {
+               if (options & DHCPCD_RELEASE) {
                        if (ifp->carrier != LINK_DOWN)
                                dhcp6_startrelease(ifp);
                        unlink(state->leasefile);
                }
                dhcp6_freedrop_addrs(ifp, drop, NULL);
                if (drop && state->new &&
-                   (ifp->options->options &
+                   (options &
                    (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
                    (DHCPCD_EXITING | DHCPCD_PERSISTENT))
                {
@@ -2681,7 +2688,7 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason)
        }
 
        /* If we don't have any more DHCP6 enabled interfaces,
-        * close the global socketo and release resources */
+        * close the global socket and release resources */
        ctx = ifp->ctx;
        if (ctx->ifaces) {
                TAILQ_FOREACH(ifp, ctx->ifaces, next) {
index 93a52ccc69bb2e220c06d6a58998116f91076f03..6350dd9ef3cc7f46ca2bf4fdb37746b6ea120ed5 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -146,7 +146,7 @@ free_globals(struct dhcpcd_ctx *ctx)
        }
        if (ctx->ifdc) {
                for (ctx->ifdc--; ctx->ifdc >= 0; ctx->ifdc--)
-                       free(ctx->ifdv[ctx->ifac]);
+                       free(ctx->ifdv[ctx->ifdc]);
                free(ctx->ifdv);
                ctx->ifdv = NULL;
        }
index 8f5c6bc2460dacdbfc32ace5e1649e5920575a61..19b1c07345838ac63c0dc743eb93bb015c764e13 100644 (file)
@@ -211,6 +211,7 @@ add_environ(struct if_options *ifo, const char *value, int uniq)
                                n = strdup(value);
                                if (n == NULL) {
                                        syslog(LOG_ERR, "%s: %m", __func__);
+                                       free(match);
                                        return NULL;
                                }
                                free(lst[i]);
@@ -218,10 +219,11 @@ add_environ(struct if_options *ifo, const char *value, int uniq)
                        } else {
                                /* Append a space and the value to it */
                                l = strlen(lst[i]);
-                               lv = strlen(p);
+                               lv = p ? strlen(p) : 0;
                                n = realloc(lst[i], l + lv + 2);
                                if (n == NULL) {
                                        syslog(LOG_ERR, "%s: %m", __func__);
+                                       free(match);
                                        return NULL;
                                }
                                lst[i] = n;
@@ -235,6 +237,7 @@ add_environ(struct if_options *ifo, const char *value, int uniq)
                i++;
        }
 
+       free(match);
        n = strdup(value);
        if (n == NULL) {
                syslog(LOG_ERR, "%s: %m", __func__);
@@ -243,12 +246,12 @@ add_environ(struct if_options *ifo, const char *value, int uniq)
        newlist = realloc(lst, sizeof(char *) * (i + 2));
        if (newlist == NULL) {
                syslog(LOG_ERR, "%s: %m", __func__);
+               free(n);
                return NULL;
        }
        newlist[i] = n;
        newlist[i + 1] = NULL;
        ifo->environ = newlist;
-       free(match);
        return newlist[i];
 }
 
@@ -431,16 +434,18 @@ splitv(int *argc, char **argv, const char *arg)
                nt = strdup(t);
                if (nt == NULL) {
                        syslog(LOG_ERR, "%s: %m", __func__);
-                       return NULL;
+                       free(o);
+                       return v;
                }
-               (*argc)++;
-               n = realloc(v, sizeof(char *) * ((*argc)));
+               n = realloc(v, sizeof(char *) * ((*argc) + 1));
                if (n == NULL) {
                        syslog(LOG_ERR, "%s: %m", __func__);
-                       return NULL;
+                       free(o);
+                       free(nt);
+                       return v;
                }
                v = n;
-               v[(*argc) - 1] = nt;
+               v[(*argc)++] = nt;
        }
        free(o);
        return v;
@@ -477,7 +482,7 @@ parse_addr(struct in_addr *addr, struct in_addr *net, const char *arg)
        }
        if (p != NULL)
                *--p = '/';
-       else if (net != NULL)
+       else if (net != NULL && addr != NULL)
                net->s_addr = ipv4_getnetmask(addr->s_addr);
        return 0;
 }
@@ -620,7 +625,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
 
        dop = NULL;
        dop_len = NULL;
-       i = 0;
+       //i = 0;
        switch(opt) {
        case 'f': /* FALLTHROUGH */
        case 'g': /* FALLTHROUGH */
@@ -1512,6 +1517,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                            sizeof(**dop) * ((*dop_len) + 1))) == NULL)
                        {
                                syslog(LOG_ERR, "%s: %m", __func__);
+                               free(np);
                                return -1;
                        }
                        *dop = ndop;
index e77632ad48d638b9cfe2f8c08fb5b2f488634843..6fe43c75d7bac4fea71fd4be680b8e5ff4ce6ff2 100644 (file)
--- a/ipv6nd.c
+++ b/ipv6nd.c
@@ -357,6 +357,8 @@ ipv6nd_sendrsprobe(void *arg)
 
        /* Set the outbound interface */
        cm = CMSG_FIRSTHDR(&ctx->sndhdr);
+       if (cm == NULL) /* unlikely */
+               return;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_PKTINFO;
        cm->cmsg_len = CMSG_LEN(sizeof(pi));
@@ -366,6 +368,8 @@ ipv6nd_sendrsprobe(void *arg)
 
        /* Hop limit */
        cm = CMSG_NXTHDR(&ctx->sndhdr, cm);
+       if (cm == NULL) /* unlikely */
+               return;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_HOPLIMIT;
        cm->cmsg_len = CMSG_LEN(sizeof(hoplimit));
@@ -750,9 +754,8 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp,
 
        len -= sizeof(struct nd_router_advert);
        p = ((uint8_t *)icp) + sizeof(struct nd_router_advert);
-       olen = 0;
        lifetime = ~0U;
-       for (olen = 0; len > 0; p += olen, len -= olen) {
+       for (; len > 0; p += olen, len -= olen) {
                if ((size_t)len < sizeof(struct nd_opt_hdr)) {
                        syslog(LOG_ERR, "%s: short option", ifp->name);
                        break;
@@ -1412,12 +1415,15 @@ ipv6nd_probeaddr(void *arg)
        //memcpy(&dst.sin6_addr, &ap->addr, sizeof(dst.sin6_addr));
        dst.sin6_scope_id = ap->iface->index;
 
-       sndhdr.msg_name = (caddr_t)&dst;
-       sndhdr.msg_iov[0].iov_base = ap->ns;
-       sndhdr.msg_iov[0].iov_len = ap->nslen;
+       ctx = ap->iface->ctx->ipv6;
+       ctx->sndhdr.msg_name = (caddr_t)&dst;
+       ctx->sndhdr.msg_iov[0].iov_base = ap->ns;
+       ctx->sndhdr.msg_iov[0].iov_len = ap->nslen;
 
        /* Set the outbound interface */
-       cm = CMSG_FIRSTHDR(&sndhdr);
+       cm = CMSG_FIRSTHDR(&ctx->sndhdr);
+       if (cm == NULL) /* unlikely */
+               return;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_PKTINFO;
        cm->cmsg_len = CMSG_LEN(sizeof(pi));
@@ -1427,6 +1433,8 @@ ipv6nd_probeaddr(void *arg)
 
        /* Hop limit */
        cm = CMSG_NXTHDR(&sndhdr, cm);
+       if (cm == NULL) /* unlikely */
+               return;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_HOPLIMIT;
        cm->cmsg_len = CMSG_LEN(sizeof(hoplimit));
@@ -1568,6 +1576,8 @@ ipv6nd_proberouter(void *arg)
 
        /* Set the outbound interface */
        cm = CMSG_FIRSTHDR(&ctx->sndhdr);
+       if (cm == NULL) /* unlikely */
+               return;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_PKTINFO;
        cm->cmsg_len = CMSG_LEN(sizeof(pi));
@@ -1577,6 +1587,8 @@ ipv6nd_proberouter(void *arg)
 
        /* Hop limit */
        cm = CMSG_NXTHDR(&ctx->sndhdr, cm);
+       if (cm == NULL) /* unlikely */
+               return;
        cm->cmsg_level = IPPROTO_IPV6;
        cm->cmsg_type = IPV6_HOPLIMIT;
        cm->cmsg_len = CMSG_LEN(sizeof(hoplimit));