Skip to content

Commit

Permalink
decode_msgpack: fix leaks
Browse files Browse the repository at this point in the history
Signed-off-by: David Korczynski <[email protected]>
  • Loading branch information
DavidKorczynski authored Sep 28, 2023
1 parent b0ac0de commit 12a488a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
30 changes: 27 additions & 3 deletions src/ctr_decode_msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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[] = \
Expand All @@ -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 */
Expand Down Expand Up @@ -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[] = \
{
Expand All @@ -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 */
Expand All @@ -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[] = \
{
Expand All @@ -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 */
Expand Down
31 changes: 18 additions & 13 deletions src/ctr_span.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 12a488a

Please sign in to comment.