From 12a488a60079c91d398af8de0fb586a3be0b2a0e Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Thu, 28 Sep 2023 19:32:19 +0100 Subject: [PATCH] decode_msgpack: fix leaks Signed-off-by: David Korczynski --- src/ctr_decode_msgpack.c | 30 +++++++++++++++++++++++++++--- src/ctr_span.c | 31 ++++++++++++++++++------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/ctr_decode_msgpack.c b/src/ctr_decode_msgpack.c index f1df5c3..0711aaf 100644 --- a/src/ctr_decode_msgpack.c +++ b/src/ctr_decode_msgpack.c @@ -124,6 +124,11 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_ return CTR_DECODE_MSGPACK_VARIANT_DECODE_ERROR; } + if (context->scope_span->instrumentation_scope->attr != NULL) { + ctr_attributes_destroy(context->scope_span->instrumentation_scope->attr); + context->scope_span->instrumentation_scope->attr = NULL; + } + context->scope_span->instrumentation_scope->attr = attributes; } @@ -132,6 +137,7 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_ static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_t index, void *ctx) { + int result; struct ctrace_instrumentation_scope *instrumentation_scope; struct ctr_msgpack_decode_context *context = ctx; struct ctr_mpack_map_entry_callback_t callbacks[] = \ @@ -151,7 +157,12 @@ static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_ ctr_scope_span_set_instrumentation_scope(context->scope_span, instrumentation_scope); - return ctr_mpack_unpack_map(reader, callbacks, ctx); + result = ctr_mpack_unpack_map(reader, callbacks, ctx); + if (result != CTR_DECODE_MSGPACK_SUCCESS) { + ctr_instrumentation_scope_destroy(context->scope_span->instrumentation_scope); + context->scope_span->instrumentation_scope = NULL; + } + return result; } /* Event callbacks */ @@ -541,6 +552,7 @@ static int unpack_span_status(mpack_reader_t *reader, size_t index, void *ctx) static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx) { + int result; struct ctr_msgpack_decode_context *context = ctx; struct ctr_mpack_map_entry_callback_t callbacks[] = \ { @@ -565,8 +577,14 @@ static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx) if (context->span == NULL) { return CTR_DECODE_MSGPACK_ALLOCATION_ERROR; } + result = ctr_mpack_unpack_map(reader, callbacks, ctx); - return ctr_mpack_unpack_map(reader, callbacks, ctx); + if (result != CTR_DECODE_MSGPACK_SUCCESS) { + ctr_span_destroy(context->span); + context->span = NULL; + } + + return result; } /* Scope span callbacks */ @@ -591,6 +609,7 @@ static int unpack_scope_span_schema_url(mpack_reader_t *reader, size_t index, vo static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx) { + int result; struct ctr_msgpack_decode_context *context = ctx; struct ctr_mpack_map_entry_callback_t callbacks[] = \ { @@ -606,7 +625,12 @@ static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx) return CTR_DECODE_MSGPACK_ALLOCATION_ERROR; } - return ctr_mpack_unpack_map(reader, callbacks, ctx); + result = ctr_mpack_unpack_map(reader, callbacks, ctx); + if (result != CTR_DECODE_MSGPACK_SUCCESS) { + ctr_scope_span_destroy(context->scope_span); + context->scope_span = NULL; + } + return result; } /* Resource span callbacks */ diff --git a/src/ctr_span.c b/src/ctr_span.c index d93935a..9d2ef5b 100644 --- a/src/ctr_span.c +++ b/src/ctr_span.c @@ -34,7 +34,7 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span /* allocate a spanc context */ span = calloc(1, sizeof(struct ctrace_span)); - if (!span) { + if (span == NULL) { ctr_errno(); return NULL; } @@ -45,14 +45,14 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span /* name */ span->name = cfl_sds_create(name); - if (!span->name) { + if (span->name == NULL) { free(span); return NULL; } /* attributes */ span->attr = ctr_attributes_create(); - if (!span->attr) { + if (span->attr == NULL) { free(span); return NULL; } @@ -116,7 +116,9 @@ int ctr_span_set_span_id(struct ctrace_span *span, void *buf, size_t len) if (!buf || len <= 0) { return -1; } - + if (span->span_id != NULL) { + ctr_id_destroy(span->span_id); + } span->span_id = ctr_id_create(buf, len); if (!span->span_id) { return -1; @@ -294,26 +296,29 @@ void ctr_span_destroy(struct ctrace_span *span) struct ctrace_span_status *status; struct ctrace_link *link; - if (span->name) { + if (span->name != NULL) { cfl_sds_destroy(span->name); } - if (span->trace_id) { + if (span->trace_id != NULL) { ctr_id_destroy(span->trace_id); } - if (span->span_id) { + if (span->span_id != NULL) { ctr_id_destroy(span->span_id); } - if (span->parent_span_id) { + if (span->parent_span_id != NULL) { ctr_id_destroy(span->parent_span_id); } /* attributes */ - if (span->attr) { + if (span->attr != NULL) { ctr_attributes_destroy(span->attr); } + if (span->trace_state != NULL) { + cfl_sds_destroy(span->trace_state); + } /* events */ cfl_list_foreach_safe(head, tmp, &span->events) { @@ -329,7 +334,7 @@ void ctr_span_destroy(struct ctrace_span *span) /* status */ status = &span->status; - if (status->message) { + if (status->message != NULL) { cfl_sds_destroy(status->message); } @@ -346,17 +351,17 @@ struct ctrace_span_event *ctr_span_event_add_ts(struct ctrace_span *span, char * { struct ctrace_span_event *ev; - if (!name) { + if (name == NULL) { return NULL; } ev = calloc(1, sizeof(struct ctrace_span_event)); - if (!ev) { + if (ev == NULL) { ctr_errno(); return NULL; } ev->name = cfl_sds_create(name); - if (!ev->name) { + if (ev->name == NULL) { free(ev); return NULL; }