summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAlex Colomar <a.colomar@f5.com>2022-09-10 18:00:27 +0200
committerAlejandro Colomar <alx@nginx.com>2022-10-14 14:43:04 +0200
commitf2213dbd1b51838ec6b59073d9d071a75def7858 (patch)
tree660015b7d47ea949b63f28afc120e9c09f040536
parentf8b892e1fa3d6398a21ce5e183911baa11a14d6a (diff)
downloadunit-f2213dbd1b51838ec6b59073d9d071a75def7858.tar.gz
unit-f2213dbd1b51838ec6b59073d9d071a75def7858.tar.bz2
Added missing error checking in the C API.
pthread_mutex_init(3) may fail for several reasons, and failing to check will cause Undefined Behavior when those errors happen. Add missing checks, and correctly deinitialize previously created stuff before exiting from the API. Signed-off-by: Alejandro Colomar <alx@nginx.com> Reviewed-by: Andrew Clayton <a.clayton@nginx.com> Reviewed-by: Zhidao HONG <z.hong@f5.com>
-rw-r--r--docs/changes.xml2
-rw-r--r--src/nxt_unit.c38
2 files changed, 29 insertions, 11 deletions
diff --git a/docs/changes.xml b/docs/changes.xml
index 91cf8fe3..5da00d88 100644
--- a/docs/changes.xml
+++ b/docs/changes.xml
@@ -177,7 +177,7 @@ the Ruby application process could crash on SIGINT.
<change type="bugfix">
<para>
-mutex leak in the C API.
+mutex leaks in the C API.
</para>
</change>
diff --git a/src/nxt_unit.c b/src/nxt_unit.c
index e5cb0b58..3932b2ab 100644
--- a/src/nxt_unit.c
+++ b/src/nxt_unit.c
@@ -116,7 +116,7 @@ static int nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd);
static void nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx,
nxt_unit_ctx_impl_t *ctx_impl);
-static void nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps);
+static int nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps);
nxt_inline void nxt_unit_process_use(nxt_unit_process_t *process);
nxt_inline void nxt_unit_process_release(nxt_unit_process_t *process);
static void nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps);
@@ -606,7 +606,7 @@ nxt_unit_create(nxt_unit_init_t *init)
if (nxt_slow_path(rc != 0)) {
nxt_unit_alert(NULL, "failed to initialize mutex (%d)", rc);
- goto fail;
+ goto out_unit_free;
}
lib->unit.data = init->data;
@@ -631,17 +631,35 @@ nxt_unit_create(nxt_unit_init_t *init)
rc = nxt_unit_ctx_init(lib, &lib->main_ctx, init->ctx_data);
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
- pthread_mutex_destroy(&lib->mutex);
- goto fail;
+ goto out_mutex_destroy;
+ }
+
+ rc = nxt_unit_mmaps_init(&lib->incoming);
+ if (nxt_slow_path(rc != 0)) {
+ nxt_unit_alert(NULL, "failed to initialize mutex (%d)", rc);
+
+ goto out_ctx_free;
}
- nxt_unit_mmaps_init(&lib->incoming);
- nxt_unit_mmaps_init(&lib->outgoing);
+ rc = nxt_unit_mmaps_init(&lib->outgoing);
+ if (nxt_slow_path(rc != 0)) {
+ nxt_unit_alert(NULL, "failed to initialize mutex (%d)", rc);
+
+ goto out_mmaps_destroy;
+ }
return lib;
-fail:
+out_mmaps_destroy:
+ nxt_unit_mmaps_destroy(&lib->incoming);
+
+out_ctx_free:
+ nxt_unit_ctx_free(&lib->main_ctx);
+
+out_mutex_destroy:
+ pthread_mutex_destroy(&lib->mutex);
+out_unit_free:
nxt_unit_free(NULL, lib);
return NULL;
@@ -4093,15 +4111,15 @@ nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx, nxt_unit_ctx_impl_t *ctx_impl)
}
-static void
+static int
nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps)
{
- pthread_mutex_init(&mmaps->mutex, NULL);
-
mmaps->size = 0;
mmaps->cap = 0;
mmaps->elts = NULL;
mmaps->allocated_chunks = 0;
+
+ return pthread_mutex_init(&mmaps->mutex, NULL);
}