From 292997420c6fc385f31e568620476b0b71de97be Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Tue, 29 Dec 2015 11:17:05 -0800 Subject: [PATCH] libnetutils: Check socket() return value Add a check for socket() errors and make sure to preserve errno over the subsequent close() calls. Change-Id: If52d76cd3cb45044eaaf7fea9bfd4471dc66a078 --- libnetutils/ifc_utils.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c index 956ed30c3..e0a9f7f88 100644 --- a/libnetutils/ifc_utils.c +++ b/libnetutils/ifc_utils.c @@ -253,6 +253,7 @@ int ifc_act_on_address(int action, const char *name, const char *address, int prefixlen) { int ifindex, s, len, ret; struct sockaddr_storage ss; + int saved_errno; void *addr; size_t addrlen; struct { @@ -317,15 +318,21 @@ int ifc_act_on_address(int action, const char *name, const char *address, memcpy(RTA_DATA(rta), addr, addrlen); s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); - if (send(s, &req, req.n.nlmsg_len, 0) < 0) { - close(s); + if (s < 0) { return -errno; } + if (send(s, &req, req.n.nlmsg_len, 0) < 0) { + saved_errno = errno; + close(s); + return -saved_errno; + } + len = recv(s, buf, sizeof(buf), 0); + saved_errno = errno; close(s); if (len < 0) { - return -errno; + return -saved_errno; } // Parse the acknowledgement to find the return code.