commit b7af711273c61ed7b87d2d87840bda427d3fb524
parent 792a413f7d9d1e52dc41c69024cad685cbd401da
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Mon, 24 Aug 2026 10:18:39 +0200
Fixed missing global lib deinit in case of memory allocation errors
Diffstat:
1 file changed, 48 insertions(+), 45 deletions(-)
diff --git a/src/mhd2/daemon_create.c b/src/mhd2/daemon_create.c
@@ -72,6 +72,9 @@ MHD_daemon_create (MHD_RequestCallback req_cb,
struct MHD_Daemon *d;
struct DaemonOptions *s;
+ if (NULL == req_cb)
+ return NULL;
+
if (!mhd_lib_init_global_if_needed ())
{
#ifndef NDEBUG
@@ -81,59 +84,59 @@ MHD_daemon_create (MHD_RequestCallback req_cb,
return NULL;
}
- if (NULL == req_cb)
- return NULL;
-
d = (struct MHD_Daemon *)mhd_calloc (1, sizeof(struct MHD_Daemon));
- if (NULL == d)
- return NULL;
-
- s = (struct DaemonOptions *)mhd_calloc (1, sizeof(struct DaemonOptions));
- if (NULL == s)
+ if (NULL != d)
{
- free (d);
- return NULL;
- }
- /* calloc() does not guarantee that floating point values and pointers
- are initialised to zero and NULL (respectfully). */
- /* Any floating point and pointer members must be initialised manually here */
+ s = (struct DaemonOptions *)mhd_calloc (1, sizeof(struct DaemonOptions));
+ if (NULL != s)
+ {
+ /* calloc() does not guarantee that floating point values and pointers
+ are initialised to zero and NULL (respectfully). */
+ /* Any floating point and pointer members must be initialised manually here */
#ifndef HAVE_NULL_PTR_ALL_ZEROS
- s->bind_sa.v_sa = NULL;
- s->tls_cert_key.v_mem_key = NULL;
- s->tls_cert_key.v_mem_cert = NULL;
- s->tls_cert_key.v_mem_pass = NULL;
- s->tls_client_ca = NULL;
- s->tls_psk_callback.v_psk_cb = NULL;
- s->tls_psk_callback.v_psk_cb_cls = NULL;
- s->accept_policy.v_apc = NULL;
- s->accept_policy.v_apc_cls = NULL;
- s->early_uri_logger.v_cb = NULL;
- s->early_uri_logger.v_cls = NULL;
- s->daemon_ready_callback.v_cb = NULL;
- s->daemon_ready_callback.v_cb_cls = NULL;
- s->notify_connection.v_ncc = NULL;
- s->notify_connection.v_cls = NULL;
- s->notify_stream.v_nsc = NULL;
- s->notify_stream.v_cls = NULL;
- s->random_entropy.v_buf = NULL;
- s->tls_cert_key.v_mem_cert = NULL;
- s->tls_cert_key.v_mem_key = NULL;
- s->tls_cert_key.v_mem_pass = NULL;
-
- /* d->log_params.v_log_cb = NULL; */ /* used directly */
+ s->bind_sa.v_sa = NULL;
+ s->tls_cert_key.v_mem_key = NULL;
+ s->tls_cert_key.v_mem_cert = NULL;
+ s->tls_cert_key.v_mem_pass = NULL;
+ s->tls_client_ca = NULL;
+ s->tls_psk_callback.v_psk_cb = NULL;
+ s->tls_psk_callback.v_psk_cb_cls = NULL;
+ s->accept_policy.v_apc = NULL;
+ s->accept_policy.v_apc_cls = NULL;
+ s->early_uri_logger.v_cb = NULL;
+ s->early_uri_logger.v_cls = NULL;
+ s->daemon_ready_callback.v_cb = NULL;
+ s->daemon_ready_callback.v_cb_cls = NULL;
+ s->notify_connection.v_ncc = NULL;
+ s->notify_connection.v_cls = NULL;
+ s->notify_stream.v_nsc = NULL;
+ s->notify_stream.v_cls = NULL;
+ s->random_entropy.v_buf = NULL;
+ s->tls_cert_key.v_mem_cert = NULL;
+ s->tls_cert_key.v_mem_key = NULL;
+ s->tls_cert_key.v_mem_pass = NULL;
+
+ /* d->log_params.v_log_cb = NULL; */ /* used directly */
#endif /* !HAVE_NULL_PTR_ALL_ZEROS */
- s->large_pool_size = SIZE_MAX; /* The impossible value */
+ s->large_pool_size = SIZE_MAX; /* The impossible value */
- s->listen_socket = MHD_INVALID_SOCKET;
- s->fd_number_limit = MHD_INVALID_SOCKET;
+ s->listen_socket = MHD_INVALID_SOCKET;
+ s->fd_number_limit = MHD_INVALID_SOCKET;
- d->log_params.v_log_cb = (MHD_LoggingCallback)mhd_logger_default;
- d->req_cfg.cb = req_cb;
- d->req_cfg.cb_cls = req_cb_cls;
- d->settings = s;
+ d->log_params.v_log_cb = (MHD_LoggingCallback)mhd_logger_default;
+ d->req_cfg.cb = req_cb;
+ d->req_cfg.cb_cls = req_cb_cls;
+ d->settings = s;
+
+ return d; /* Success exit point */
+ }
+ /* Below is a clean-up path */
+ free (d);
+ }
+ mhd_lib_deinit_global_if_needed ();
- return d;
+ return NULL; /* Failure exit point */
}