Fixed a mutex leak in the C API.

In nxt_unit_create() we could leak a mutex created in
nxt_unit_ctx_init().

This could happen if nxt_unit_ctx_init() succeeded but later on we
bailed out of nxt_unit_create(), we would destroy the mutex created in
nxt_unit_create() but not the one created in nxt_unit_ctx_init().

Reorder things so that we do the call to nxt_unit_create() after all the
other checks so if it fails we don't leak the mutex it created.

Co-developed-by: Andrew Clayton <a.clayton@f5.com>
Signed-off-by: Andrew Clayton <a.clayton@f5.com>
Signed-off-by: Alex Colomar <a.colomar@f5.com>
This commit is contained in:
Alex Colomar
2022-09-09 13:40:17 +01:00
committed by Alejandro Colomar
parent 4924bd185d
commit 558a5d3e89
2 changed files with 14 additions and 12 deletions

View File

@@ -122,6 +122,12 @@ the prototype crashed.
</para> </para>
</change> </change>
<change type="bugfix">
<para>
mutex leak in the C API.
</para>
</change>
</changes> </changes>

View File

@@ -587,7 +587,12 @@ nxt_unit_create(nxt_unit_init_t *init)
{ {
int rc; int rc;
nxt_unit_impl_t *lib; nxt_unit_impl_t *lib;
nxt_unit_callbacks_t *cb;
if (nxt_slow_path(init->callbacks.request_handler == NULL)) {
nxt_unit_alert(NULL, "request_handler is NULL");
return NULL;
}
lib = nxt_unit_malloc(NULL, lib = nxt_unit_malloc(NULL,
sizeof(nxt_unit_impl_t) + init->request_data_size); sizeof(nxt_unit_impl_t) + init->request_data_size);
@@ -630,15 +635,6 @@ nxt_unit_create(nxt_unit_init_t *init)
goto fail; goto fail;
} }
cb = &lib->callbacks;
if (cb->request_handler == NULL) {
nxt_unit_alert(NULL, "request_handler is NULL");
pthread_mutex_destroy(&lib->mutex);
goto fail;
}
nxt_unit_mmaps_init(&lib->incoming); nxt_unit_mmaps_init(&lib->incoming);
nxt_unit_mmaps_init(&lib->outgoing); nxt_unit_mmaps_init(&lib->outgoing);