Skip to content

Commit 12a488a

Browse files
decode_msgpack: fix leaks
Signed-off-by: David Korczynski <[email protected]>
1 parent b0ac0de commit 12a488a

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/ctr_decode_msgpack.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_
124124
return CTR_DECODE_MSGPACK_VARIANT_DECODE_ERROR;
125125
}
126126

127+
if (context->scope_span->instrumentation_scope->attr != NULL) {
128+
ctr_attributes_destroy(context->scope_span->instrumentation_scope->attr);
129+
context->scope_span->instrumentation_scope->attr = NULL;
130+
}
131+
127132
context->scope_span->instrumentation_scope->attr = attributes;
128133
}
129134

@@ -132,6 +137,7 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_
132137

133138
static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_t index, void *ctx)
134139
{
140+
int result;
135141
struct ctrace_instrumentation_scope *instrumentation_scope;
136142
struct ctr_msgpack_decode_context *context = ctx;
137143
struct ctr_mpack_map_entry_callback_t callbacks[] = \
@@ -151,7 +157,12 @@ static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_
151157

152158
ctr_scope_span_set_instrumentation_scope(context->scope_span, instrumentation_scope);
153159

154-
return ctr_mpack_unpack_map(reader, callbacks, ctx);
160+
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
161+
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
162+
ctr_instrumentation_scope_destroy(context->scope_span->instrumentation_scope);
163+
context->scope_span->instrumentation_scope = NULL;
164+
}
165+
return result;
155166
}
156167

157168
/* Event callbacks */
@@ -541,6 +552,7 @@ static int unpack_span_status(mpack_reader_t *reader, size_t index, void *ctx)
541552

542553
static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx)
543554
{
555+
int result;
544556
struct ctr_msgpack_decode_context *context = ctx;
545557
struct ctr_mpack_map_entry_callback_t callbacks[] = \
546558
{
@@ -565,8 +577,14 @@ static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx)
565577
if (context->span == NULL) {
566578
return CTR_DECODE_MSGPACK_ALLOCATION_ERROR;
567579
}
580+
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
568581

569-
return ctr_mpack_unpack_map(reader, callbacks, ctx);
582+
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
583+
ctr_span_destroy(context->span);
584+
context->span = NULL;
585+
}
586+
587+
return result;
570588
}
571589

572590
/* Scope span callbacks */
@@ -591,6 +609,7 @@ static int unpack_scope_span_schema_url(mpack_reader_t *reader, size_t index, vo
591609

592610
static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx)
593611
{
612+
int result;
594613
struct ctr_msgpack_decode_context *context = ctx;
595614
struct ctr_mpack_map_entry_callback_t callbacks[] = \
596615
{
@@ -606,7 +625,12 @@ static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx)
606625
return CTR_DECODE_MSGPACK_ALLOCATION_ERROR;
607626
}
608627

609-
return ctr_mpack_unpack_map(reader, callbacks, ctx);
628+
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
629+
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
630+
ctr_scope_span_destroy(context->scope_span);
631+
context->scope_span = NULL;
632+
}
633+
return result;
610634
}
611635

612636
/* Resource span callbacks */

src/ctr_span.c

+18-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span
3434

3535
/* allocate a spanc context */
3636
span = calloc(1, sizeof(struct ctrace_span));
37-
if (!span) {
37+
if (span == NULL) {
3838
ctr_errno();
3939
return NULL;
4040
}
@@ -45,14 +45,14 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span
4545

4646
/* name */
4747
span->name = cfl_sds_create(name);
48-
if (!span->name) {
48+
if (span->name == NULL) {
4949
free(span);
5050
return NULL;
5151
}
5252

5353
/* attributes */
5454
span->attr = ctr_attributes_create();
55-
if (!span->attr) {
55+
if (span->attr == NULL) {
5656
free(span);
5757
return NULL;
5858
}
@@ -116,7 +116,9 @@ int ctr_span_set_span_id(struct ctrace_span *span, void *buf, size_t len)
116116
if (!buf || len <= 0) {
117117
return -1;
118118
}
119-
119+
if (span->span_id != NULL) {
120+
ctr_id_destroy(span->span_id);
121+
}
120122
span->span_id = ctr_id_create(buf, len);
121123
if (!span->span_id) {
122124
return -1;
@@ -294,26 +296,29 @@ void ctr_span_destroy(struct ctrace_span *span)
294296
struct ctrace_span_status *status;
295297
struct ctrace_link *link;
296298

297-
if (span->name) {
299+
if (span->name != NULL) {
298300
cfl_sds_destroy(span->name);
299301
}
300302

301-
if (span->trace_id) {
303+
if (span->trace_id != NULL) {
302304
ctr_id_destroy(span->trace_id);
303305
}
304306

305-
if (span->span_id) {
307+
if (span->span_id != NULL) {
306308
ctr_id_destroy(span->span_id);
307309
}
308310

309-
if (span->parent_span_id) {
311+
if (span->parent_span_id != NULL) {
310312
ctr_id_destroy(span->parent_span_id);
311313
}
312314

313315
/* attributes */
314-
if (span->attr) {
316+
if (span->attr != NULL) {
315317
ctr_attributes_destroy(span->attr);
316318
}
319+
if (span->trace_state != NULL) {
320+
cfl_sds_destroy(span->trace_state);
321+
}
317322

318323
/* events */
319324
cfl_list_foreach_safe(head, tmp, &span->events) {
@@ -329,7 +334,7 @@ void ctr_span_destroy(struct ctrace_span *span)
329334

330335
/* status */
331336
status = &span->status;
332-
if (status->message) {
337+
if (status->message != NULL) {
333338
cfl_sds_destroy(status->message);
334339
}
335340

@@ -346,17 +351,17 @@ struct ctrace_span_event *ctr_span_event_add_ts(struct ctrace_span *span, char *
346351
{
347352
struct ctrace_span_event *ev;
348353

349-
if (!name) {
354+
if (name == NULL) {
350355
return NULL;
351356
}
352357

353358
ev = calloc(1, sizeof(struct ctrace_span_event));
354-
if (!ev) {
359+
if (ev == NULL) {
355360
ctr_errno();
356361
return NULL;
357362
}
358363
ev->name = cfl_sds_create(name);
359-
if (!ev->name) {
364+
if (ev->name == NULL) {
360365
free(ev);
361366
return NULL;
362367
}

0 commit comments

Comments
 (0)