Skip to content

Commit 558a5d3

Browse files
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 <[email protected]> Signed-off-by: Andrew Clayton <[email protected]> Signed-off-by: Alex Colomar <[email protected]>
1 parent 4924bd1 commit 558a5d3

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

docs/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ the prototype crashed.
122122
</para>
123123
</change>
124124

125+
<change type="bugfix">
126+
<para>
127+
mutex leak in the C API.
128+
</para>
129+
</change>
130+
125131
</changes>
126132

127133

src/nxt_unit.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,14 @@ nxt_unit_init(nxt_unit_init_t *init)
585585
static nxt_unit_impl_t *
586586
nxt_unit_create(nxt_unit_init_t *init)
587587
{
588-
int rc;
589-
nxt_unit_impl_t *lib;
590-
nxt_unit_callbacks_t *cb;
588+
int rc;
589+
nxt_unit_impl_t *lib;
590+
591+
if (nxt_slow_path(init->callbacks.request_handler == NULL)) {
592+
nxt_unit_alert(NULL, "request_handler is NULL");
593+
594+
return NULL;
595+
}
591596

592597
lib = nxt_unit_malloc(NULL,
593598
sizeof(nxt_unit_impl_t) + init->request_data_size);
@@ -630,15 +635,6 @@ nxt_unit_create(nxt_unit_init_t *init)
630635
goto fail;
631636
}
632637

633-
cb = &lib->callbacks;
634-
635-
if (cb->request_handler == NULL) {
636-
nxt_unit_alert(NULL, "request_handler is NULL");
637-
638-
pthread_mutex_destroy(&lib->mutex);
639-
goto fail;
640-
}
641-
642638
nxt_unit_mmaps_init(&lib->incoming);
643639
nxt_unit_mmaps_init(&lib->outgoing);
644640

0 commit comments

Comments
 (0)