diff --git a/lib/ctraces/CMakeLists.txt b/lib/ctraces/CMakeLists.txt index 08e1b6d29b7..12ff6d7c867 100644 --- a/lib/ctraces/CMakeLists.txt +++ b/lib/ctraces/CMakeLists.txt @@ -26,8 +26,8 @@ endif() # CTraces Version set(CTR_VERSION_MAJOR 0) -set(CTR_VERSION_MINOR 3) -set(CTR_VERSION_PATCH 1) +set(CTR_VERSION_MINOR 4) +set(CTR_VERSION_PATCH 0) set(CTR_VERSION_STR "${CTR_VERSION_MAJOR}.${CTR_VERSION_MINOR}.${CTR_VERSION_PATCH}") # Define __FILENAME__ consistently across Operating Systems diff --git a/lib/ctraces/examples/otlp-encoder/otlp-encoder.c b/lib/ctraces/examples/otlp-encoder/otlp-encoder.c index 00b984a40b9..2e178b3e938 100644 --- a/lib/ctraces/examples/otlp-encoder/otlp-encoder.c +++ b/lib/ctraces/examples/otlp-encoder/otlp-encoder.c @@ -107,7 +107,7 @@ int main() ctr_span_event_set_attribute_string(event, "syscall 3", "write()"); /* add a key/value pair list */ - kv = cfl_kvlist_create(1); + kv = cfl_kvlist_create(); cfl_kvlist_insert_string(kv, "language", "c"); ctr_span_set_attribute_kvlist(span_root, "my-list", kv); diff --git a/lib/ctraces/examples/simple-c-api.c b/lib/ctraces/examples/simple-c-api.c index aeaeff120a7..c9a9dfb9dec 100644 --- a/lib/ctraces/examples/simple-c-api.c +++ b/lib/ctraces/examples/simple-c-api.c @@ -102,7 +102,7 @@ int main() ctr_span_event_set_attribute_string(event, "syscall 3", "write()"); /* add a key/value pair list */ - kv = cfl_kvlist_create(1); + kv = cfl_kvlist_create(); cfl_kvlist_insert_string(kv, "language", "c"); ctr_span_set_attribute_kvlist(span_root, "my-list", kv); diff --git a/lib/ctraces/include/ctraces/ctr_variant_utils.h b/lib/ctraces/include/ctraces/ctr_variant_utils.h index c338be1b051..d8f25169ce6 100644 --- a/lib/ctraces/include/ctraces/ctr_variant_utils.h +++ b/lib/ctraces/include/ctraces/ctr_variant_utils.h @@ -22,6 +22,10 @@ #include +#define CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE 100 +#define CFL_VARIANT_UTILS_INITIAL_ARRAY_SIZE 100 +#define CFL_VARIANT_UTILS_SERIALIZED_ARRAY_SIZE_LIMIT 100000 + /* These are the only functions meant for general use, * the reason why the kvlist packing and unpacking * functions are exposed is the internal and external @@ -226,12 +230,25 @@ static inline int unpack_cfl_array(mpack_reader_t *reader, entry_count = mpack_tag_array_count(&tag); - internal_array = cfl_array_create(entry_count); + if (entry_count >= CFL_VARIANT_UTILS_SERIALIZED_ARRAY_SIZE_LIMIT) { + return -2; + } + + if (entry_count >= CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE) { + internal_array = cfl_array_create(CFL_VARIANT_UTILS_INITIAL_ARRAY_SIZE); + } + else { + internal_array = cfl_array_create(entry_count); + } if (internal_array == NULL) { return -3; } + if (entry_count >= CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE) { + cfl_array_resizable(internal_array, CFL_TRUE); + } + for (index = 0 ; index < entry_count ; index++) { result = unpack_cfl_variant(reader, &entry_value); diff --git a/lib/ctraces/src/ctr_attributes.c b/lib/ctraces/src/ctr_attributes.c index 44bb13853e2..67caebb88b7 100644 --- a/lib/ctraces/src/ctr_attributes.c +++ b/lib/ctraces/src/ctr_attributes.c @@ -29,7 +29,7 @@ struct ctrace_attributes *ctr_attributes_create() return NULL; } - attr->kv = cfl_kvlist_create(128); + attr->kv = cfl_kvlist_create(); if (!attr->kv) { free(attr); return NULL; diff --git a/lib/ctraces/src/ctr_decode_msgpack.c b/lib/ctraces/src/ctr_decode_msgpack.c index f1df5c32544..0711aaff492 100644 --- a/lib/ctraces/src/ctr_decode_msgpack.c +++ b/lib/ctraces/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/lib/ctraces/src/ctr_decode_opentelemetry.c b/lib/ctraces/src/ctr_decode_opentelemetry.c index 139ae2cd904..ba9f2eb03b1 100644 --- a/lib/ctraces/src/ctr_decode_opentelemetry.c +++ b/lib/ctraces/src/ctr_decode_opentelemetry.c @@ -571,7 +571,9 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr, ctr_span_kind_set(span, otel_span->kind); ctr_span_start_ts(ctr, span, otel_span->start_time_unix_nano); ctr_span_end_ts(ctr, span, otel_span->end_time_unix_nano); - ctr_span_set_status(span, otel_span->status->code, otel_span->status->message); + if (otel_span->status) { + ctr_span_set_status(span, otel_span->status->code, otel_span->status->message); + } ctr_span_set_attributes(span, otel_span->n_attributes, otel_span->attributes); ctr_span_set_events(span, otel_span->n_events, otel_span->events); ctr_span_set_dropped_attributes_count(span, otel_span->dropped_attributes_count); diff --git a/lib/ctraces/src/ctr_span.c b/lib/ctraces/src/ctr_span.c index d93935a7233..6e61f53ea03 100644 --- a/lib/ctraces/src/ctr_span.c +++ b/lib/ctraces/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,21 +351,21 @@ 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; } - ev->attr = ctr_attributes_create(128); + ev->attr = ctr_attributes_create(); ev->dropped_attr_count = 0; /* if no timestamp is given, use the current time */ diff --git a/lib/ctraces/tests/decoding.c b/lib/ctraces/tests/decoding.c index ab919b94a42..81076d090f6 100644 --- a/lib/ctraces/tests/decoding.c +++ b/lib/ctraces/tests/decoding.c @@ -581,7 +581,7 @@ void test_simple_to_msgpack_and_back() ctr_span_event_set_attribute_string(event, "syscall 3", "write()"); /* add a key/value pair list */ - kv = cfl_kvlist_create(1); + kv = cfl_kvlist_create(); TEST_ASSERT(kv != NULL); cfl_kvlist_insert_string(kv, "language", "c");