From: Roy Marples Date: Thu, 27 Feb 2014 20:00:39 +0000 (+0000) Subject: Fix all errors reported by clang static analyser. X-Git-Tag: v6.3.1~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8fc52ced6f080d0bf25344e9b608fdd0f7ce4a93;p=thirdparty%2Fdhcpcd.git Fix all errors reported by clang static analyser. --- diff --git a/auth.c b/auth.c index 553148bc..12757504 100644 --- 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; diff --git a/dhcp-common.c b/dhcp-common.c index 9dab167d..4a043602 100644 --- a/dhcp-common.c +++ b/dhcp-common.c @@ -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 56547fe3..19e83901 100644 --- 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 1db65f8f..50313828 100644 --- 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) { diff --git a/dhcpcd.c b/dhcpcd.c index 93a52ccc..6350dd9e 100644 --- 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; } diff --git a/if-options.c b/if-options.c index 8f5c6bc2..19b1c073 100644 --- a/if-options.c +++ b/if-options.c @@ -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; diff --git a/ipv6nd.c b/ipv6nd.c index e77632ad..6fe43c75 100644 --- 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));