From 73142add6aea22c2a727e5108bb9cdff78de7cf9 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Tue, 3 May 2016 10:14:14 +0000 Subject: [PATCH] Fix some issues reported by Converity. --- dhcp.c | 14 ++++++++++---- dhcp.h | 2 +- dhcp6.c | 21 ++++++++++++++------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/dhcp.c b/dhcp.c index e3d1de99..f7599482 100644 --- a/dhcp.c +++ b/dhcp.c @@ -198,7 +198,7 @@ get_option(struct dhcpcd_ctx *ctx, } l = *p++; - if (o == DHO_OPTIONSOVERLOADED) { + if (o == DHO_OPTSOVERLOADED) { /* Ensure we only get this option once by setting * the last bit as well as the value. * This is valid because only the first two bits @@ -1139,11 +1139,13 @@ read_lease(struct interface *ifp) ifp->name, state->leasefile); dhcp = calloc(1, sizeof(*dhcp)); if (dhcp == NULL) { - close(fd); + if (state->leasefile[0] != '\0') + close(fd); return NULL; } bytes = read(fd, dhcp, sizeof(*dhcp)); - close(fd); + if (state->leasefile[0] != '\0') + close(fd); if (bytes < 0) { free(dhcp); return NULL; @@ -1254,7 +1256,8 @@ dhcp_env(char **env, const char *prefix, const struct dhcp_message *dhcp, e = 0; ifo = ifp->options; - get_option_uint8(ifp->ctx, &overl, dhcp, DHO_OPTIONSOVERLOADED); + if (get_option_uint8(ifp->ctx, &overl, dhcp, DHO_OPTSOVERLOADED) == -1) + overl = 0; if (env == NULL) { if (dhcp->yiaddr || dhcp->ciaddr) @@ -2008,6 +2011,7 @@ dhcp_arp_conflicted(struct arp_state *astate, const struct arp_msg *amsg) arp_free(astate); eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); dhcpcd_startinterface(ifp); + return; } /* RFC 2131 3.1.5, Client-server interaction @@ -2039,6 +2043,7 @@ dhcp_arp_conflicted(struct arp_state *astate, const struct arp_msg *amsg) eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); eloop_timeout_add_sec(ifp->ctx->eloop, DHCP_RAND_MAX, dhcp_discover, ifp); + return; } /* Bound address */ @@ -2054,6 +2059,7 @@ dhcp_arp_conflicted(struct arp_state *astate, const struct arp_msg *amsg) arp_free(astate); dhcp_expire1(ifp); } + return; } } diff --git a/dhcp.h b/dhcp.h index f5f9e52f..a400d07d 100644 --- a/dhcp.h +++ b/dhcp.h @@ -95,7 +95,7 @@ enum DHO { DHO_VENDOR = 43, DHO_IPADDRESS = 50, DHO_LEASETIME = 51, - DHO_OPTIONSOVERLOADED = 52, + DHO_OPTSOVERLOADED = 52, DHO_MESSAGETYPE = 53, DHO_SERVERID = 54, DHO_PARAMETERREQUESTLIST = 55, diff --git a/dhcp6.c b/dhcp6.c index 80a12310..391d5c6b 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -2176,13 +2176,19 @@ dhcp6_readlease(struct interface *ifp, int validate) } else { logger(ifp->ctx, LOG_DEBUG, "%s: reading lease `%s'", ifp->name, state->leasefile); - fd = open(state->leasefile, O_RDONLY); + if (stat(state->leasefile, &st) == -1) + fd = -1; + else + fd = open(state->leasefile, O_RDONLY); } if (fd == -1) return -1; state->new_len = 0; - if ((state->new = malloc(BUFSIZ)) == NULL) + if ((state->new = malloc(BUFSIZ)) == NULL) { + if (state->leasefile[0] != '\0') + close(fd); return -1; + } retval = -1; /* DHCPv6 messages have no real maximum size. * As we could be reading from stdin, we loop like so. */ @@ -2205,11 +2211,13 @@ dhcp6_readlease(struct interface *ifp, int validate) state->new = newnew; state->new_len = newlen; } - close(fd); + if (state->leasefile[0] != '\0') + close(fd); if (retval == -1) goto ex; - if (ifp->ctx->options & DHCPCD_DUMPLEASE) + if (ifp->ctx->options & DHCPCD_DUMPLEASE || + state->leasefile[0] == '\0') return 0; /* If not validating IA's and if they have expired, @@ -2220,8 +2228,6 @@ dhcp6_readlease(struct interface *ifp, int validate) } retval = -1; - if (stat(state->leasefile, &st) == -1) - goto ex; clock_gettime(CLOCK_MONOTONIC, &acquired); if ((now = time(NULL)) == -1) goto ex; @@ -2282,7 +2288,8 @@ ex: free(state->new); state->new = NULL; state->new_len = 0; - if (!(ifp->ctx->options & DHCPCD_DUMPLEASE)) + if (!(ifp->ctx->options & DHCPCD_DUMPLEASE) && + state->leasefile[0] != '\0') unlink(state->leasefile); return retval; } -- 2.47.3