aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-08-25 14:32:27 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-08-25 14:32:27 +0200
commit199d801c835e95b54908b1a34963f65ba6ceee30 (patch)
tree60bdedd35198753ca12e715586d7137339ff8d59
parent43a39a1f2e5e6c7c520f03293fc57aa76fea8aaf (diff)
server.c: Fix descriptor leak on failed fcntl(2)
-rw-r--r--server.c22
1 files changed, 14 insertions, 8 deletions
diff --git a/server.c b/server.c
index 11e2af0..4c19439 100644
--- a/server.c
+++ b/server.c
@@ -92,13 +92,14 @@ static struct server_client *alloc_client(struct server *const s)
{
struct sockaddr_in addr;
socklen_t sz = sizeof addr;
+ struct server_client *c = NULL;
const int fd = accept(s->fd, (struct sockaddr *)&addr, &sz);
if (fd < 0)
{
fprintf(stderr, "%s: accept(2): %s\n",
__func__, strerror(errno));
- return NULL;
+ goto failure;
}
const int flags = fcntl(fd, F_GETFL);
@@ -107,21 +108,18 @@ static struct server_client *alloc_client(struct server *const s)
{
fprintf(stderr, "%s: fcntl(2) F_GETFL: %s\n",
__func__, strerror(errno));
- return NULL;
+ goto failure;
}
else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
{
fprintf(stderr, "%s: fcntl(2) F_SETFL: %s\n",
__func__, strerror(errno));
- return NULL;
+ goto failure;
}
-
- struct server_client *const c = malloc(sizeof *c);
-
- if (!c)
+ else if (!(c = malloc(sizeof *c)))
{
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
- return NULL;
+ goto failure;
}
*c = (const struct server_client)
@@ -141,6 +139,14 @@ static struct server_client *alloc_client(struct server *const s)
}
return c;
+
+failure:
+
+ if (fd >= 0 && close(fd))
+ fprintf(stderr, "%s: close(2): %s\n", __func__, strerror(errno));
+
+ free(c);
+ return NULL;
}
void server_client_write_pending(struct server_client *const c,