From 4f56e3816b34ccf1c005ddb9f2ffca02fdb3eb6c Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 10 Oct 2024 16:57:18 -0600 Subject: [PATCH] encoding: opentelemetry: fix leaks on resource and scope handling Signed-off-by: Eduardo Silva --- src/cmt_encode_opentelemetry.c | 73 ++++++++++++++-------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/src/cmt_encode_opentelemetry.c b/src/cmt_encode_opentelemetry.c index 8378bc8..af7605f 100644 --- a/src/cmt_encode_opentelemetry.c +++ b/src/cmt_encode_opentelemetry.c @@ -33,8 +33,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics ** static void destroy_scope_metric_list( Opentelemetry__Proto__Metrics__V1__ScopeMetrics **metric_list); -struct cfl_kvlist *fetch_metadata_kvlist_key( - struct cfl_kvlist *kvlist, char *key) +struct cfl_kvlist *fetch_metadata_kvlist_key(struct cfl_kvlist *kvlist, char *key) { struct cfl_variant *entry_variant; struct cfl_kvlist *entry_kvlist; @@ -1015,16 +1014,6 @@ static Opentelemetry__Proto__Metrics__V1__ResourceMetrics ** return metric_list; } -static void destroy_scope_metrics( - Opentelemetry__Proto__Metrics__V1__ScopeMetrics *metric) -{ - if (metric != NULL) { - destroy_metric_list(metric->metrics); - - free(metric); - } -} - void destroy_instrumentation_scope(Opentelemetry__Proto__Common__V1__InstrumentationScope *scope) { if (scope->name != NULL) { @@ -1042,6 +1031,21 @@ void destroy_instrumentation_scope(Opentelemetry__Proto__Common__V1__Instrumenta free(scope); } +static void destroy_scope_metrics(Opentelemetry__Proto__Metrics__V1__ScopeMetrics *metric) +{ + if (metric != NULL) { + if (is_string_releaseable(metric->schema_url)) { + cfl_sds_destroy(metric->schema_url); + metric->schema_url = NULL; + } + if (metric->scope != NULL) { + destroy_instrumentation_scope(metric->scope); + } + destroy_metric_list(metric->metrics); + free(metric); + } +} + static Opentelemetry__Proto__Common__V1__InstrumentationScope * initialize_instrumentation_scope( struct cfl_kvlist *scope_root, @@ -1065,9 +1069,7 @@ static Opentelemetry__Proto__Common__V1__InstrumentationScope * return NULL; } - scope = \ - calloc(1, sizeof(Opentelemetry__Proto__Common__V1__InstrumentationScope)); - + scope = calloc(1, sizeof(Opentelemetry__Proto__Common__V1__InstrumentationScope)); if (scope == NULL) { *error_detection_flag = CMT_TRUE; @@ -1109,10 +1111,9 @@ static Opentelemetry__Proto__Common__V1__InstrumentationScope * return scope; } -static Opentelemetry__Proto__Metrics__V1__ScopeMetrics * - initialize_scope_metrics( - struct cfl_kvlist *scope_metrics_root, - size_t metric_element_count) +static Opentelemetry__Proto__Metrics__V1__ScopeMetrics *initialize_scope_metrics( + struct cfl_kvlist *scope_metrics_root, + size_t metric_element_count) { int error_detection_flag; Opentelemetry__Proto__Metrics__V1__ScopeMetrics *scope_metrics; @@ -1120,9 +1121,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics * metadata = fetch_metadata_kvlist_key(scope_metrics_root, "metadata"); - scope_metrics = \ - calloc(1, sizeof(Opentelemetry__Proto__Metrics__V1__ScopeMetrics)); - + scope_metrics = calloc(1, sizeof(Opentelemetry__Proto__Metrics__V1__ScopeMetrics)); if (scope_metrics == NULL) { return NULL; } @@ -1132,9 +1131,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics * error_detection_flag = CMT_FALSE; if (metric_element_count > 0) { - scope_metrics->metrics = \ - initialize_metric_list(metric_element_count); - + scope_metrics->metrics = initialize_metric_list(metric_element_count); if (scope_metrics->metrics == NULL) { error_detection_flag = CMT_TRUE; } @@ -1145,12 +1142,11 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics * if (!error_detection_flag && metadata != NULL) { scope_metrics->schema_url = fetch_metadata_string_key(metadata, "schema_url", &error_detection_flag); + } - if (error_detection_flag && - scope_metrics != NULL) { + if (error_detection_flag && scope_metrics != NULL) { destroy_scope_metrics(scope_metrics); - scope_metrics = NULL; } @@ -1195,9 +1191,7 @@ static void destroy_scope_metric_list( } } -static Opentelemetry__Proto__Metrics__V1__ScopeMetrics ** - initialize_scope_metrics_list( - size_t element_count) +static Opentelemetry__Proto__Metrics__V1__ScopeMetrics **initialize_scope_metrics_list(size_t element_count) { Opentelemetry__Proto__Metrics__V1__ScopeMetrics **metric_list; @@ -1207,8 +1201,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics ** return metric_list; } -static void destroy_attribute( - Opentelemetry__Proto__Common__V1__KeyValue *attribute) +static void destroy_attribute(Opentelemetry__Proto__Common__V1__KeyValue *attribute) { if (attribute != NULL) { if (attribute->value != NULL) { @@ -2078,10 +2071,8 @@ static void destroy_opentelemetry_context( } } -static Opentelemetry__Proto__Resource__V1__Resource * - initialize_resource( - struct cfl_kvlist *resource_root, - int *error_detection_flag) +static Opentelemetry__Proto__Resource__V1__Resource *initialize_resource(struct cfl_kvlist *resource_root, + int *error_detection_flag) { struct cfl_kvlist *attributes; struct cfl_kvlist *metadata; @@ -2096,17 +2087,13 @@ static Opentelemetry__Proto__Resource__V1__Resource * attributes = fetch_metadata_kvlist_key(resource_root, "attributes"); metadata = fetch_metadata_kvlist_key(resource_root, "metadata"); - if (cfl_kvlist_count(attributes) == 0 && - cfl_kvlist_count(metadata) == 0) { + if (cfl_kvlist_count(attributes) == 0 && cfl_kvlist_count(metadata) == 0) { return NULL; } - resource = \ - calloc(1, sizeof(Opentelemetry__Proto__Resource__V1__Resource)); - + resource = calloc(1, sizeof(Opentelemetry__Proto__Resource__V1__Resource)); if (resource == NULL) { *error_detection_flag = CMT_TRUE; - return NULL; }