ipaccess.c: Fix some resource leaks in error conditions.
* Close the socket when the bind is failing. * Close the socket when the listen is failing. * Close the socket then the bsc_register_fd is failing. * Return an error when the socket call is not returning a socket.
This commit is contained in:
parent
1fdbf40e8a
commit
4d2d95b35a
|
@ -598,6 +598,11 @@ static int make_sock(struct bsc_fd *bfd, u_int16_t port,
|
||||||
bfd->when = BSC_FD_READ;
|
bfd->when = BSC_FD_READ;
|
||||||
//bfd->data = line;
|
//bfd->data = line;
|
||||||
|
|
||||||
|
if (bfd->fd < 0) {
|
||||||
|
LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(port);
|
addr.sin_port = htons(port);
|
||||||
|
@ -609,18 +614,21 @@ static int make_sock(struct bsc_fd *bfd, u_int16_t port,
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n",
|
LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
close(bfd->fd);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = listen(bfd->fd, 1);
|
ret = listen(bfd->fd, 1);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
|
close(bfd->fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bsc_register_fd(bfd);
|
ret = bsc_register_fd(bfd);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("register_listen_fd");
|
perror("register_listen_fd");
|
||||||
|
close(bfd->fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -639,6 +647,11 @@ int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
|
||||||
bfd->data = line;
|
bfd->data = line;
|
||||||
bfd->priv_nr = 1;
|
bfd->priv_nr = 1;
|
||||||
|
|
||||||
|
if (bfd->fd < 0) {
|
||||||
|
LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
||||||
|
|
||||||
ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
|
ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
|
||||||
|
|
Loading…
Reference in New Issue