From 096b61ad14c90169f438e690d096e3fcf87e504e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juho=20V=C3=A4h=C3=A4-Herttua?= Date: Fri, 24 Aug 2018 12:39:27 +0300 Subject: [PATCH] Fix a crash when fairplay support is not enabled. --- src/lib/fairplay_dummy.c | 3 ++- src/lib/httpd.c | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib/fairplay_dummy.c b/src/lib/fairplay_dummy.c index 1c062bb..3b8f2b7 100644 --- a/src/lib/fairplay_dummy.c +++ b/src/lib/fairplay_dummy.c @@ -8,7 +8,8 @@ struct fairplay_s { fairplay_t * fairplay_init(logger_t *logger) { - return NULL; + /* NULL would mean failure so let's use any number */ + return (void *) 42; } int diff --git a/src/lib/httpd.c b/src/lib/httpd.c index 74e7541..835a509 100644 --- a/src/lib/httpd.c +++ b/src/lib/httpd.c @@ -97,9 +97,10 @@ httpd_destroy(httpd_t *httpd) } } -static void +static int httpd_add_connection(httpd_t *httpd, int fd, unsigned char *local, int local_len, unsigned char *remote, int remote_len) { + void *user_data; int i; for (i=0; imax_connections; i++) { @@ -110,15 +111,20 @@ httpd_add_connection(httpd_t *httpd, int fd, unsigned char *local, int local_len if (i == httpd->max_connections) { /* This code should never be reached, we do not select server_fds when full */ logger_log(httpd->logger, LOGGER_INFO, "Max connections reached"); - shutdown(fd, SHUT_RDWR); - closesocket(fd); - return; + return -1; + } + + user_data = httpd->callbacks.conn_init(httpd->callbacks.opaque, local, local_len, remote, remote_len); + if (!user_data) { + logger_log(httpd->logger, LOGGER_ERR, "Error initializing HTTP request handler"); + return -1; } httpd->open_connections++; httpd->connections[i].socket_fd = fd; httpd->connections[i].connected = 1; - httpd->connections[i].user_data = httpd->callbacks.conn_init(httpd->callbacks.opaque, local, local_len, remote, remote_len); + httpd->connections[i].user_data = user_data; + return 0; } static int @@ -142,6 +148,7 @@ httpd_accept_connection(httpd_t *httpd, int server_fd, int is_ipv6) local_saddrlen = sizeof(local_saddr); ret = getsockname(fd, (struct sockaddr *)&local_saddr, &local_saddrlen); if (ret == -1) { + shutdown(fd, SHUT_RDWR); closesocket(fd); return 0; } @@ -151,7 +158,12 @@ httpd_accept_connection(httpd_t *httpd, int server_fd, int is_ipv6) local = netutils_get_address(&local_saddr, &local_len); remote = netutils_get_address(&remote_saddr, &remote_len); - httpd_add_connection(httpd, fd, local, local_len, remote, remote_len); + ret = httpd_add_connection(httpd, fd, local, local_len, remote, remote_len); + if (ret == -1) { + shutdown(fd, SHUT_RDWR); + closesocket(fd); + return 0; + } return 1; }