Refactor, close() may try to close -1, found by Coverity Scan

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-11-05 10:28:26 +01:00
parent 2e8dffe893
commit 0bf36c59a6

View File

@ -659,33 +659,41 @@ static void increase_rcvbuf(int fd)
static int create_unix_socket(const char *path)
{
struct sockaddr_un sunx;
struct sockaddr_un sun;
char line[MAXLINE + 1];
int fd;
int sd = -1;
if (path[0] == '\0')
return -1;
(void)unlink(path);
memset(&sunx, 0, sizeof(sunx));
sunx.sun_family = AF_UNIX;
strlcpy(sunx.sun_path, path, sizeof(sunx.sun_path));
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0 || bind(fd, (struct sockaddr *)&sunx, sizeof(sunx.sun_family) + strlen(sunx.sun_path)) < 0 ||
chmod(path, 0666) < 0) {
(void)snprintf(line, sizeof(line), "cannot create %s", path);
sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sd < 0)
goto err;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
if (bind(sd, (struct sockaddr *)&sun, sizeof(sun.sun_family) + strlen(sun.sun_path)))
goto err;
if (chmod(path, 0666) < 0)
goto err;
increase_rcvbuf(sd);
return sd;
err:
snprintf(line, sizeof(line), "cannot create %s", path);
logerror(line);
logit("cannot create %s (%d).\n", path, errno);
close(fd);
if (sd != -1)
close(sd);
return -1;
}
increase_rcvbuf(fd);
return fd;
}
static int *create_inet_sockets(void)
{
struct addrinfo hints, *res, *r;