]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use a local variable to indicate if we need to close the fd we may have
authorRoy Marples <roy@marples.name>
Tue, 3 May 2016 15:06:22 +0000 (15:06 +0000)
committerRoy Marples <roy@marples.name>
Tue, 3 May 2016 15:06:22 +0000 (15:06 +0000)
opened when reading leases.

dhcp.c
dhcp6.c

diff --git a/dhcp.c b/dhcp.c
index f759948213bd080a16880a80203e5622e0205e0d..da99a80855fc179ea6b132ec1f2e774063af618d 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -45,6 +45,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1115,6 +1116,7 @@ static struct dhcp_message *
 read_lease(struct interface *ifp)
 {
        int fd;
+       bool fd_opened;
        struct dhcp_message *dhcp;
        struct dhcp_state *state = D_STATE(ifp);
        ssize_t bytes;
@@ -1122,10 +1124,13 @@ read_lease(struct interface *ifp)
        uint8_t type;
        size_t auth_len;
 
-       if (state->leasefile[0] == '\0')
+       if (state->leasefile[0] == '\0') {
                fd = fileno(stdin);
-       else
+               fd_opened = false;
+       } else {
                fd = open(state->leasefile, O_RDONLY);
+               fd_opened = true;
+       }
        if (fd == -1) {
                if (errno != ENOENT)
                        logger(ifp->ctx, LOG_ERR, "%s: open `%s': %m",
@@ -1144,7 +1149,7 @@ read_lease(struct interface *ifp)
                return NULL;
        }
        bytes = read(fd, dhcp, sizeof(*dhcp));
-       if (state->leasefile[0] != '\0')
+       if (fd_opened)
                close(fd);
        if (bytes < 0) {
                free(dhcp);
diff --git a/dhcp6.c b/dhcp6.c
index 2fc0257bda0455cc4cce772a8f9753622b8ee993..5f6201f87d332599e5c824df66460e2455b0d075 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -37,6 +37,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -2168,11 +2169,13 @@ dhcp6_readlease(struct interface *ifp, int validate)
        int retval;
        size_t newlen;
        void *newnew;
+       bool fd_opened;
 
        state = D6_STATE(ifp);
        if (state->leasefile[0] == '\0') {
                logger(ifp->ctx, LOG_DEBUG, "reading standard input");
                fd = fileno(stdin);
+               fd_opened = false;
        } else {
                logger(ifp->ctx, LOG_DEBUG, "%s: reading lease `%s'",
                    ifp->name, state->leasefile);
@@ -2181,12 +2184,13 @@ dhcp6_readlease(struct interface *ifp, int validate)
                        close(fd);
                        fd = -1;
                }
+               fd_opened = true;
        }
        if (fd == -1)
                return -1;
        state->new_len = 0;
        if ((state->new = malloc(BUFSIZ)) == NULL) {
-               if (state->leasefile[0] != '\0')
+               if (fd_opened)
                        close(fd);
                return -1;
        }
@@ -2212,7 +2216,7 @@ dhcp6_readlease(struct interface *ifp, int validate)
                state->new = newnew;
                state->new_len = newlen;
        }
-       if (state->leasefile[0] != '\0')
+       if (fd_opened)
                close(fd);
        if (retval == -1)
                goto ex;