From 4d2d95b35addba99a7927041554d50ed7452fd7c Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 19 Feb 2010 13:07:05 +0100 Subject: [PATCH] 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. --- openbsc/src/input/ipaccess.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/openbsc/src/input/ipaccess.c b/openbsc/src/input/ipaccess.c index 10a3d2283..73d798f87 100644 --- a/openbsc/src/input/ipaccess.c +++ b/openbsc/src/input/ipaccess.c @@ -598,6 +598,11 @@ static int make_sock(struct bsc_fd *bfd, u_int16_t port, bfd->when = BSC_FD_READ; //bfd->data = line; + if (bfd->fd < 0) { + LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n"); + return -EIO; + } + memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); @@ -609,18 +614,21 @@ static int make_sock(struct bsc_fd *bfd, u_int16_t port, if (ret < 0) { LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n", strerror(errno)); + close(bfd->fd); return -EIO; } ret = listen(bfd->fd, 1); if (ret < 0) { perror("listen"); + close(bfd->fd); return ret; } ret = bsc_register_fd(bfd); if (ret < 0) { perror("register_listen_fd"); + close(bfd->fd); return ret; } return 0; @@ -639,6 +647,11 @@ int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa) bfd->data = line; 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)); ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));