Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: fix leaks for map unpacking #40

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading