diff options
| -rw-r--r-- | server.c | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -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, |
